Skip to content

Commit

Permalink
Make const loop variables global and drop const.
Browse files Browse the repository at this point in the history
Fixes #667.
  • Loading branch information
PetroZarytskyi committed Dec 11, 2023
1 parent ec5ea41 commit 87d1c89
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/Differentiator/ReverseModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2692,9 +2692,15 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
// assignments. This is a temporary measure to avoid the bug that arises
// from overwriting local variables on different loop passes.
if (isInsideLoop) {
if (VD->getType()->isBuiltinType() &&
!VD->getType().isConstQualified()) {
if (VD->getType()->isBuiltinType()) {
auto* decl = VDDiff.getDecl();
/// The same variable will be assigned with new values every
/// loop iteration so the const qualifier must be dropped.
if (decl->getType().isConstQualified()) {
QualType nonConstType =
getNonConstType(decl->getType(), m_Context, m_Sema);
decl->setType(nonConstType);

Check warning on line 2702 in lib/Differentiator/ReverseModeVisitor.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Differentiator/ReverseModeVisitor.cpp#L2701-L2702

Added lines #L2701 - L2702 were not covered by tests
}
if (decl->getInit()) {
auto* declRef = BuildDeclRef(decl);
auto pushPop =
Expand Down Expand Up @@ -2744,8 +2750,7 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
/// overwriting local variables on different loop passes.
if (isInsideLoop) {
if (auto* VD = dyn_cast<VarDecl>(decls[0])) {
if (VD->getType()->isBuiltinType() &&
!VD->getType().isConstQualified()) {
if (VD->getType()->isBuiltinType()) {
addToBlock(DSClone, m_Globals);
Stmt* initAssignments = MakeCompoundStmt(inits);
initAssignments = unwrapIfSingleStmt(initAssignments);
Expand Down

0 comments on commit 87d1c89

Please sign in to comment.