Skip to content

Commit

Permalink
Remove unnecessary DeclRefExpr from for loop condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
PetroZarytskyi committed Nov 24, 2023
1 parent 3a1dad0 commit 628f71e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/Differentiator/ReverseModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Expr>(condVarRes.getExpr())) {
forwardCond = BuildOp(BO_Comma, cond.getExpr(), cast<Expr>(condVarRes.getExpr()));
forwardCond = cast<Expr>(condVarRes.getExpr());
}

Stmt* Forward = new (m_Context) ForStmt(m_Context,
Expand Down
2 changes: 1 addition & 1 deletion test/Gradient/Loops.C
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 628f71e

Please sign in to comment.