Skip to content

Commit

Permalink
Use the innermost lvalue instead of a reference when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
PetroZarytskyi committed Jul 22, 2024
1 parent 222f034 commit c0ee0fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
11 changes: 9 additions & 2 deletions lib/Differentiator/ReverseModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2335,8 +2335,15 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,

if (L->HasSideEffects(m_Context)) {
Expr* E = Ldiff.getExpr();
auto* storeE = GlobalStoreAndRef(BuildOp(UO_AddrOf, E));
Ldiff.updateStmt(BuildOp(UO_Deref, storeE));
llvm::SmallVector<Expr*, 4> returnExprs;
utils::GetInnermostReturnExpr(E, returnExprs);
if (returnExprs.size() == 1) {
addToCurrentBlock(E, direction::forward);
Ldiff.updateStmt(returnExprs[0]);
} else {
auto* storeE = GlobalStoreAndRef(BuildOp(UO_AddrOf, E));
Ldiff.updateStmt(BuildOp(UO_Deref, storeE));
}
}

Stmts Lblock = EndBlockWithoutCreatingCS(direction::reverse);
Expand Down
24 changes: 11 additions & 13 deletions test/Gradient/Assignments.C
Original file line number Diff line number Diff line change
Expand Up @@ -414,20 +414,19 @@ double f9(double x, double y) {
//CHECK: void f9_grad(double x, double y, double *_d_x, double *_d_y) {
//CHECK-NEXT: double _d_t = 0;
//CHECK-NEXT: double _t0;
//CHECK-NEXT: double *_t1;
//CHECK-NEXT: double _t2;
//CHECK-NEXT: double _t1;
//CHECK-NEXT: double t = x;
//CHECK-NEXT: _t0 = t;
//CHECK-NEXT: _t1 = &(t *= x);
//CHECK-NEXT: _t2 = *_t1;
//CHECK-NEXT: *_t1 *= y;
//CHECK-NEXT: (t *= x);
//CHECK-NEXT: _t1 = t;
//CHECK-NEXT: t *= y;
//CHECK-NEXT: _d_t += 1;
//CHECK-NEXT: {
//CHECK-NEXT: *_t1 = _t2;
//CHECK-NEXT: t = _t1;
//CHECK-NEXT: double _r_d1 = _d_t;
//CHECK-NEXT: _d_t = 0;
//CHECK-NEXT: _d_t += _r_d1 * y;
//CHECK-NEXT: *_d_y += *_t1 * _r_d1;
//CHECK-NEXT: *_d_y += t * _r_d1;
//CHECK-NEXT: t = _t0;
//CHECK-NEXT: double _r_d0 = _d_t;
//CHECK-NEXT: _d_t = 0;
Expand Down Expand Up @@ -474,16 +473,15 @@ double f11(double x, double y) {
//CHECK: void f11_grad(double x, double y, double *_d_x, double *_d_y) {
//CHECK-NEXT: double _d_t = 0;
//CHECK-NEXT: double _t0;
//CHECK-NEXT: double *_t1;
//CHECK-NEXT: double _t2;
//CHECK-NEXT: double _t1;
//CHECK-NEXT: double t = x;
//CHECK-NEXT: _t0 = t;
//CHECK-NEXT: _t1 = &(t = x);
//CHECK-NEXT: _t2 = *_t1;
//CHECK-NEXT: *_t1 = y;
//CHECK-NEXT: (t = x);
//CHECK-NEXT: _t1 = t;
//CHECK-NEXT: t = y;
//CHECK-NEXT: _d_t += 1;
//CHECK-NEXT: {
//CHECK-NEXT: *_t1 = _t2;
//CHECK-NEXT: t = _t1;
//CHECK-NEXT: double _r_d1 = _d_t;
//CHECK-NEXT: _d_t = 0;
//CHECK-NEXT: *_d_y += _r_d1;
Expand Down

0 comments on commit c0ee0fb

Please sign in to comment.