-
Notifications
You must be signed in to change notification settings - Fork 200
Store adjoint decls in m_Variables and rebuild references on read. NFC #1896
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
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 |
|---|---|---|
|
|
@@ -4,6 +4,9 @@ | |
| #include "clad/Differentiator/CladUtils.h" | ||
| #include "clad/Differentiator/DerivativeBuilder.h" | ||
|
|
||
| #include "clang/AST/Decl.h" | ||
| #include "clang/AST/OperationKinds.h" | ||
|
|
||
| #include "llvm/Support/SaveAndRestore.h" | ||
|
|
||
| using namespace clang; | ||
|
|
@@ -89,17 +92,17 @@ DerivativeAndOverload JacobianModeVisitor::Derive() { | |
| continue; | ||
| auto derivedPVDName = "_d_vector_" + std::string(PVDII->getName()); | ||
| IdentifierInfo* derivedPVDII = CreateUniqueIdentifier(derivedPVDName); | ||
| Expr* derivedExpr = nullptr; | ||
| VarDecl* adjointDecl = nullptr; | ||
|
Contributor
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] VarDecl* adjointDecl = nullptr;
^ |
||
| AdjointInfo::WrapKind wrap = AdjointInfo::Plain; | ||
| if (utils::isArrayOrPointerType(PVD->getType())) { | ||
| ParmVarDecl* derivedPVD = | ||
| utils::BuildParmVarDecl(m_Sema, m_Derivative, derivedPVDII, | ||
| utils::GetParameterDerivativeType( | ||
| m_Sema, m_DiffReq.Mode, PVD->getType()), | ||
| PVD->getStorageClass()); | ||
| derivedParams.push_back(derivedPVD); | ||
| derivedExpr = | ||
| BuildOp(UO_Deref, BuildDeclRef(derivedPVD), PVD->getBeginLoc()); | ||
| derivedExpr = utils::BuildParenExpr(m_Sema, derivedExpr); | ||
| adjointDecl = derivedPVD; | ||
| wrap = AdjointInfo::ParenDeref; | ||
| Expr* getSize = BuildCallExprToMemFn(BuildDeclRef(derivedPVD), | ||
| /*MemberFunctionName=*/"rows", {}); | ||
| llvm::StringRef PVDName = PVD->getName(); | ||
|
|
@@ -117,8 +120,8 @@ DerivativeAndOverload JacobianModeVisitor::Derive() { | |
| m_Sema, m_DiffReq.Mode, PVD->getType()), | ||
| PVD->getStorageClass()); | ||
| derivedParams.push_back(derivedPVD); | ||
| derivedExpr = | ||
| BuildOp(UO_Deref, BuildDeclRef(derivedPVD), PVD->getBeginLoc()); | ||
| adjointDecl = derivedPVD; | ||
| wrap = AdjointInfo::Deref; | ||
| nonArrayIndVarCount += 1; | ||
| } else { | ||
| VarDecl* derivedPVD = | ||
|
|
@@ -127,10 +130,10 @@ DerivativeAndOverload JacobianModeVisitor::Derive() { | |
| ->getPointeeType(), | ||
| derivedPVDII); | ||
| adjointDecls.push_back(BuildDeclStmt(derivedPVD)); | ||
| derivedExpr = BuildDeclRef(derivedPVD); | ||
| adjointDecl = derivedPVD; | ||
| nonArrayIndVarCount += 1; | ||
| } | ||
| m_Variables[newPVD] = derivedExpr; | ||
| m_Variables[newPVD] = {adjointDecl, wrap}; | ||
| } | ||
|
|
||
| params.insert(params.end(), derivedParams.begin(), derivedParams.end()); | ||
|
|
@@ -178,7 +181,6 @@ DerivativeAndOverload JacobianModeVisitor::Derive() { | |
| bool is_array = | ||
| utils::isArrayOrPointerType(m_DiffReq->getParamDecl(i)->getType()); | ||
| ParmVarDecl* param = params[i]; | ||
| Expr* paramDiff = m_Variables[param]->IgnoreParens(); | ||
| QualType dParamType = clad::utils::GetValueType(param->getType()); | ||
| // Desugaring the type is necessary to pass it to other templates | ||
| dParamType = dParamType.getDesugaredType(m_Context); | ||
|
|
@@ -198,9 +200,11 @@ DerivativeAndOverload JacobianModeVisitor::Derive() { | |
| nonArrayIndVarCountExpr); | ||
|
|
||
| if (is_array) { | ||
| Expr* base = cast<UnaryOperator>(paramDiff)->getSubExpr(); | ||
| // The adjoint is `(*_d_p)`; the array whose size we need is the bare | ||
| // `_d_p` reference (m_Variables stores its decl). | ||
| Expr* base = BuildDeclRef(m_Variables[param].Decl); | ||
| // Get size of the array. | ||
| Expr* getSize = BuildCallExprToMemFn(Clone(base), | ||
| Expr* getSize = BuildCallExprToMemFn(base, | ||
| /*MemberFunctionName=*/"rows", {}); | ||
| // Create an identity matrix for the parameter, | ||
| // with number of rows equal to the size of the array, | ||
|
|
@@ -246,12 +250,14 @@ DerivativeAndOverload JacobianModeVisitor::Derive() { | |
| // -> clad::array<double> _d_vector_z = {0, 1}; | ||
| if (utils::isArrayOrPointerType(param->getType()) || | ||
| param->getType()->isReferenceType()) { | ||
| // The store target is `*_d_p`; strip the parens the ParenDeref adjoint | ||
| // carries for element access elsewhere. | ||
| Expr* paramAssignment = | ||
| BuildOp(BO_Assign, Clone(paramDiff), dVectorParam); | ||
| BuildOp(BO_Assign, buildAdjoint(m_Variables[param])->IgnoreParens(), | ||
|
Contributor
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::BO_Assign" is directly included [misc-include-cleaner] BuildOp(BO_Assign, buildAdjoint(m_Variables[param])->IgnoreParens(),
^ |
||
| dVectorParam); | ||
| addToCurrentBlock(paramAssignment); | ||
| } else { | ||
| auto* paramDecl = cast<VarDecl>(cast<DeclRefExpr>(paramDiff)->getDecl()); | ||
| SetDeclInit(paramDecl, dVectorParam); | ||
| SetDeclInit(m_Variables[param].Decl, dVectorParam); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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: member variable 'm_Variables' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]
std::unordered_map<const clang::ValueDecl*, AdjointInfo> m_Variables; ^