-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't mark non-varied constant params #1148
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,12 +3,13 @@ | |||||
|
||||||
#include "clang/AST/RecursiveASTVisitor.h" | ||||||
#include "llvm/ADT/SmallSet.h" | ||||||
#include <iterator> | ||||||
#include <set> | ||||||
#include "clad/Differentiator/DerivativeBuilder.h" | ||||||
#include "clad/Differentiator/DiffMode.h" | ||||||
#include "clad/Differentiator/DynamicGraph.h" | ||||||
#include "clad/Differentiator/ParseDiffArgsTypes.h" | ||||||
|
||||||
#include <iterator> | ||||||
#include <set> | ||||||
namespace clang { | ||||||
class CallExpr; | ||||||
class CompilerInstance; | ||||||
|
@@ -21,6 +22,7 @@ class Type; | |||||
} // namespace clang | ||||||
|
||||||
namespace clad { | ||||||
class DerivativeBuilder; | ||||||
|
||||||
/// A struct containing information about request to differentiate a function. | ||||||
struct DiffRequest { | ||||||
|
@@ -34,11 +36,13 @@ struct DiffRequest { | |||||
} m_TbrRunInfo; | ||||||
|
||||||
mutable struct ActivityRunInfo { | ||||||
std::set<const clang::VarDecl*> ToBeRecorded; | ||||||
std::set<const clang::VarDecl*> VariedDecls; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "clang::VarDecl" is directly included [misc-include-cleaner] include/clad/Differentiator/DiffPlanner.h:5: - #include <iterator>
+ #include <clang/AST/Decl.h>
+ #include <iterator> |
||||||
bool HasAnalysisRun = false; | ||||||
} m_ActivityRunInfo; | ||||||
|
||||||
public: | ||||||
const DerivativeBuilder* Builder = nullptr; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: member variable 'Builder' has public visibility [cppcoreguidelines-non-private-member-variables-in-classes] const DerivativeBuilder* Builder = nullptr;
^ |
||||||
// static std::set<const clang::VarDecl*> AllVariedDecls; | ||||||
/// Function to be differentiated. | ||||||
const clang::FunctionDecl* Function = nullptr; | ||||||
/// Name of the base function to be differentiated. Can be different from | ||||||
|
@@ -127,7 +131,8 @@ struct DiffRequest { | |||||
Mode == other.Mode && EnableTBRAnalysis == other.EnableTBRAnalysis && | ||||||
EnableVariedAnalysis == other.EnableVariedAnalysis && | ||||||
DVI == other.DVI && use_enzyme == other.use_enzyme && | ||||||
DeclarationOnly == other.DeclarationOnly; | ||||||
DeclarationOnly == other.DeclarationOnly && | ||||||
getVariedDecls() == other.getVariedDecls(); | ||||||
} | ||||||
|
||||||
const clang::FunctionDecl* operator->() const { return Function; } | ||||||
|
@@ -144,6 +149,16 @@ struct DiffRequest { | |||||
|
||||||
bool shouldBeRecorded(clang::Expr* E) const; | ||||||
bool shouldHaveAdjoint(const clang::VarDecl* VD) const; | ||||||
|
||||||
void setVariedDecls(std::set<const clang::VarDecl*> init) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: the parameter 'init' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]
Suggested change
|
||||||
for (auto* vd : init) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: 'auto *vd' can be declared as 'const auto *vd' [readability-qualified-auto]
Suggested change
|
||||||
this->m_ActivityRunInfo.VariedDecls.insert(vd); | ||||||
} | ||||||
std::set<const clang::VarDecl*> getVariedDecls() const { | ||||||
return this->m_ActivityRunInfo.VariedDecls; | ||||||
} | ||||||
DiffRequest() {} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use '= default' to define a trivial default constructor [modernize-use-equals-default]
Suggested change
|
||||||
DiffRequest(DerivativeBuilder& builder) : Builder(&builder) {} | ||||||
}; | ||||||
|
||||||
using DiffInterval = std::vector<clang::SourceRange>; | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,24 +121,56 @@ bool VariedAnalyzer::VisitCallExpr(CallExpr* CE) { | |
FunctionDecl* FD = CE->getDirectCallee(); | ||
bool noHiddenParam = (CE->getNumArgs() == FD->getNumParams()); | ||
if (noHiddenParam) { | ||
bool restoreMarking = m_Marking; | ||
bool restoreVaried = m_Varied; | ||
MutableArrayRef<ParmVarDecl*> FDparam = FD->parameters(); | ||
for (std::size_t i = 0, e = CE->getNumArgs(); i != e; ++i) { | ||
clang::Expr* par = CE->getArg(i); | ||
|
||
QualType parType = FDparam[i]->getType(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "clang::QualType" is directly included [misc-include-cleaner] lib/Differentiator/ActivityAnalyzer.cpp:1: + #include <clang/AST/Type.h> |
||
QualType innermostType = parType; | ||
while (innermostType->isPointerType()) | ||
innermostType = innermostType->getPointeeType(); | ||
|
||
m_Varied = false; | ||
m_Marking = false; | ||
TraverseStmt(par); | ||
m_VariedDecls.insert(FDparam[i]); | ||
if (m_Varied) | ||
m_VariedDecls.insert(FDparam[i]); | ||
else if ((parType->isReferenceType() || | ||
(utils::isArrayOrPointerType(parType) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "clad::utils::isArrayOrPointerType" is directly included [misc-include-cleaner] lib/Differentiator/ActivityAnalyzer.cpp:1: + #include <clad/Differentiator/CladUtils.h> |
||
!innermostType.isConstQualified()))) { | ||
m_Varied = true; | ||
m_Marking = true; | ||
TraverseStmt(par); | ||
m_VariedDecls.insert(FDparam[i]); | ||
} | ||
} | ||
m_Varied = restoreVaried; | ||
m_Marking = restoreMarking; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool VariedAnalyzer::VisitDeclStmt(DeclStmt* DS) { | ||
for (Decl* D : DS->decls()) { | ||
|
||
QualType VDTy = cast<VarDecl>(D)->getType(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "clang::cast" is directly included [misc-include-cleaner] QualType VDTy = cast<VarDecl>(D)->getType();
^ |
||
QualType innermost = VDTy; | ||
while (innermost->isPointerType()) | ||
innermost = innermost->getPointeeType(); | ||
if (VDTy->isArrayType() || | ||
(VDTy->isPointerType() && !innermost.isConstQualified())) { | ||
copyVarToCurBlock(cast<VarDecl>(D)); | ||
m_Varied = true; | ||
} | ||
ovdiiuv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (Expr* init = cast<VarDecl>(D)->getInit()) { | ||
m_Varied = false; | ||
TraverseStmt(init); | ||
m_Marking = true; | ||
QualType VDTy = cast<VarDecl>(D)->getType(); | ||
if (m_Varied || utils::isArrayOrPointerType(VDTy)) | ||
if (m_Varied) | ||
copyVarToCurBlock(cast<VarDecl>(D)); | ||
m_Marking = false; | ||
} | ||
|
@@ -147,12 +179,7 @@ bool VariedAnalyzer::VisitDeclStmt(DeclStmt* DS) { | |
} | ||
|
||
bool VariedAnalyzer::VisitUnaryOperator(UnaryOperator* UnOp) { | ||
const auto opCode = UnOp->getOpcode(); | ||
Expr* E = UnOp->getSubExpr(); | ||
if (opCode == UO_AddrOf || opCode == UO_Deref) { | ||
m_Varied = true; | ||
m_Marking = true; | ||
} | ||
TraverseStmt(E); | ||
m_Marking = false; | ||
return true; | ||
|
@@ -163,7 +190,7 @@ bool VariedAnalyzer::VisitDeclRefExpr(DeclRefExpr* DRE) { | |
if (!VD) | ||
return true; | ||
|
||
if (isVaried(VD)) | ||
if (isVaried(VD) || VD->getType()->isArrayType()) | ||
m_Varied = true; | ||
|
||
if (m_Varied && m_Marking) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: included header iterator is not used directly [misc-include-cleaner]