Skip to content

Commit

Permalink
Remove dead code and add comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
PetroZarytskyi committed Feb 8, 2024
1 parent 0add291 commit e46cbef
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions lib/Differentiator/ReverseModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit e46cbef

Please sign in to comment.