diff --git a/lib/Differentiator/ReverseModeVisitor.cpp b/lib/Differentiator/ReverseModeVisitor.cpp index 89e291c31..f405fea75 100644 --- a/lib/Differentiator/ReverseModeVisitor.cpp +++ b/lib/Differentiator/ReverseModeVisitor.cpp @@ -1124,8 +1124,14 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, /// This is a temporary measure to avoid the bug that arises from overwriting /// local variables on different loop passes. Expr* forwardCond = cond.getExpr(); + /// If there is a declaration in the condition, `cond` will be + /// a DeclRefExpr of the declared variable. There is no point in + /// inserting it since condVarRes.getExpr() represents an assignment with + /// that variable on the LHS. + /// e.g. for condition `int x = y`, + /// condVarRes.getExpr() will represent `x = y` if (condVarRes.getExpr() != nullptr && isa(condVarRes.getExpr())) { - forwardCond = BuildOp(BO_Comma, cond.getExpr(), cast(condVarRes.getExpr())); + forwardCond = cast(condVarRes.getExpr()); } Stmt* Forward = new (m_Context) ForStmt(m_Context, diff --git a/test/Gradient/Loops.C b/test/Gradient/Loops.C index 36068f2a4..a960a41c1 100644 --- a/test/Gradient/Loops.C +++ b/test/Gradient/Loops.C @@ -958,7 +958,7 @@ double fn13(double i, double j) { // CHECK-NEXT: double res = 0; // CHECK-NEXT: int counter = 3; // CHECK-NEXT: _t0 = 0; -// CHECK-NEXT: for (; k , clad::push(_t1, k) , k = counter; clad::push(_t2, counter) , (counter -= 1)) { +// CHECK-NEXT: for (; clad::push(_t1, k) , k = counter; clad::push(_t2, counter) , (counter -= 1)) { // CHECK-NEXT: _t0++; // CHECK-NEXT: clad::push(_t3, k); // CHECK-NEXT: k += i + 2 * clad::push(_t4, j);