From e46cbef77e73a089589de5db371b74a2b5dab64c Mon Sep 17 00:00:00 2001 From: "petro.zarytskyi" Date: Thu, 8 Feb 2024 11:27:27 +0200 Subject: [PATCH] Remove dead code and add comments. --- lib/Differentiator/ReverseModeVisitor.cpp | 28 ++++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/Differentiator/ReverseModeVisitor.cpp b/lib/Differentiator/ReverseModeVisitor.cpp index 8a8e66c48..8593e6a95 100644 --- a/lib/Differentiator/ReverseModeVisitor.cpp +++ b/lib/Differentiator/ReverseModeVisitor.cpp @@ -2524,11 +2524,15 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, VarDeclDiff ReverseModeVisitor::DifferentiateVarDecl(const VarDecl* VD) { StmtDiff initDiff; Expr* VDDerivedInit = nullptr; - // reverse_mode_forward_pass does not have a reverse pass so declarations - // don't have to be moved to the function global scope. + // Local declarations are promoted to the function global scope. This + // procedure is done to make declarations visible in the reverse sweep. + // The reverse_mode_forward_pass mode does not have a reverse pass so + // declarations don't have to be moved to the function global scope. bool promoteToFnScope = !getCurrentScope()->isFunctionScope() && m_Mode != DiffMode::reverse_mode_forward_pass; auto VDDerivedType = ComputeAdjointType(VD->getType()); auto VDCloneType = CloneType(VD->getType()); + // If the cloned declaration is moved to the function global scope, + // change its type for the corresponding adjoint type. if (promoteToFnScope) VDCloneType = VDDerivedType; bool isDerivativeOfRefType = VD->getType()->isReferenceType(); @@ -2542,6 +2546,10 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, VDDerived = BuildGlobalVarDecl(VDDerivedType, "_d_" + VD->getNameAsString(), VDDerivedInit, false, nullptr, clang::VarDecl::InitializationStyle::CallInit); + // If an array-type declaration is promoted to function global, + // its type is changed for clad::array. In that case we should + // initialize it with its size the same way the derived variable + // is initialized. if (promoteToFnScope) initDiff = VDDerivedInit; } else { @@ -2667,6 +2675,13 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, derivedVDE = BuildOp(UnaryOperatorKind::UO_Deref, derivedVDE); } + // If a ref-type declaration is promoted to function global scope, + // it's replaced with a pointer and should be initialized with the + // address of the cloned init. e.g. + // double& ref = x; + // -> + // double* ref; + // ref = &x; if (isDerivativeOfRefType && promoteToFnScope) VDClone = BuildGlobalVarDecl(VDCloneType, VD->getNameAsString(), BuildOp(UnaryOperatorKind::UO_AddrOf, @@ -2782,15 +2797,6 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, // ... if (promoteToFnScope) { auto* decl = VDDiff.getDecl(); - // The same variable will be assigned with new values every - // loop iteration so the const qualifier must be dropped. - // (look at the next comment for more details). - QualType DeclTy = decl->getType(); - if (DeclTy.isConstQualified()) { - QualType nonConstType = - getNonConstType(decl->getType(), m_Context, m_Sema); - decl->setType(nonConstType); - } if (VD->getInit()) { auto* declRef = BuildDeclRef(decl); auto* assignment = BuildOp(BO_Assign, declRef, decl->getInit());