From 87d1c89359fb80d1465b2cd22f7b3f8a822f3a83 Mon Sep 17 00:00:00 2001 From: "petro.zarytskyi" Date: Tue, 12 Dec 2023 00:30:20 +0200 Subject: [PATCH] Make const loop variables global and drop const. Fixes #667. --- lib/Differentiator/ReverseModeVisitor.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/Differentiator/ReverseModeVisitor.cpp b/lib/Differentiator/ReverseModeVisitor.cpp index f2443e4ee..0b3d53d0f 100644 --- a/lib/Differentiator/ReverseModeVisitor.cpp +++ b/lib/Differentiator/ReverseModeVisitor.cpp @@ -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); + } if (decl->getInit()) { auto* declRef = BuildDeclRef(decl); auto pushPop = @@ -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(decls[0])) { - if (VD->getType()->isBuiltinType() && - !VD->getType().isConstQualified()) { + if (VD->getType()->isBuiltinType()) { addToBlock(DSClone, m_Globals); Stmt* initAssignments = MakeCompoundStmt(inits); initAssignments = unwrapIfSingleStmt(initAssignments);