diff --git a/include/clad/Differentiator/ReverseModeVisitor.h b/include/clad/Differentiator/ReverseModeVisitor.h index 11673bb63..356375601 100644 --- a/include/clad/Differentiator/ReverseModeVisitor.h +++ b/include/clad/Differentiator/ReverseModeVisitor.h @@ -220,6 +220,12 @@ namespace clad { /// This is the central point for checkpointing. bool ShouldRecompute(const clang::Expr* E); + bool isTBR(clang::SourceLocation loc) { + if (!m_DiffReq.EnableTBRAnalysis||!loc.isValid()) + return true; + return m_ToBeRecorded.find(loc) != m_ToBeRecorded.end(); + } + /// Builds a variable declaration and stores it in the function /// global scope. /// @@ -249,8 +255,7 @@ namespace clad { clang::Expr* GlobalStoreAndRef(clang::Expr* E, llvm::StringRef prefix = "_t", bool force = false); - StmtDiff StoreAndRestore(clang::Expr* E, llvm::StringRef prefix = "_t", - bool force = false); + StmtDiff StoreAndRestore(clang::Expr* E, llvm::StringRef prefix = "_t", clang::SourceLocation loc = noLoc); //// A type returned by DelayedGlobalStoreAndRef /// .Result is a reference to the created (yet uninitialized) global diff --git a/lib/Differentiator/ReverseModeVisitor.cpp b/lib/Differentiator/ReverseModeVisitor.cpp index 6394ee9dd..888191786 100644 --- a/lib/Differentiator/ReverseModeVisitor.cpp +++ b/lib/Differentiator/ReverseModeVisitor.cpp @@ -1962,7 +1962,7 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, Expr* diff_dx = diff.getExpr_dx(); if (isPointerOp) addToCurrentBlock(BuildOp(opCode, diff_dx), direction::forward); - if (UsefulToStoreGlobal(diff.getRevSweepAsExpr())) { + if (isTBR(E->getBeginLoc())) { auto op = opCode == UO_PostInc ? UO_PostDec : UO_PostInc; addToCurrentBlock(BuildOp(op, Clone(diff.getRevSweepAsExpr())), direction::reverse); @@ -1979,7 +1979,7 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, Expr* diff_dx = diff.getExpr_dx(); if (isPointerOp) addToCurrentBlock(BuildOp(opCode, diff_dx), direction::forward); - if (UsefulToStoreGlobal(diff.getRevSweepAsExpr())) { + if (isTBR(E->getBeginLoc())) { auto op = opCode == UO_PreInc ? UO_PreDec : UO_PreInc; addToCurrentBlock(BuildOp(op, Clone(diff.getRevSweepAsExpr())), direction::reverse); @@ -2255,15 +2255,6 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, beginBlock(direction::reverse); Ldiff = Visit(L, dfdx()); auto* Lblock = endBlock(direction::reverse); - llvm::SmallVector ExprsToStore; - utils::GetInnermostReturnExpr(Ldiff.getExpr(), ExprsToStore); - - // We need to store values of derivative pointer variables in forward pass - // and restore them in reverse pass. - if (isPointerOp) { - Expr* Edx = Ldiff.getExpr_dx(); - ExprsToStore.push_back(Edx); - } if (L->HasSideEffects(m_Context)) { Expr* E = Ldiff.getExpr(); @@ -2288,8 +2279,16 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, Lblock_begin = std::next(Lblock_begin); } - for (auto& E : ExprsToStore) { - auto pushPop = StoreAndRestore(E); + // Store the value of the LHS of the assignment in the forward pass + // and restore it in the reverse pass + StmtDiff pushPop = StoreAndRestore(LCloned, /*prefix=*/"_t", /*loc=*/L->getBeginLoc()); + addToCurrentBlock(pushPop.getExpr(), direction::forward); + addToCurrentBlock(pushPop.getExpr_dx(), direction::reverse); + + // We need to store values of derivative pointer variables in forward pass + // and restore them in reverse pass. + if (isPointerOp) { + pushPop = StoreAndRestore(Ldiff.getExpr_dx()); addToCurrentBlock(pushPop.getExpr(), direction::forward); addToCurrentBlock(pushPop.getExpr_dx(), direction::reverse); } @@ -2643,8 +2642,7 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, initDiff.getForwSweepExpr_dx())); addToCurrentBlock(assignDerivativeE); if (isInsideLoop) { - StmtDiff pushPop = - StoreAndRestore(derivedVDE, /*prefix=*/"_t", /*force=*/true); + StmtDiff pushPop = StoreAndRestore(derivedVDE); addToCurrentBlock(pushPop.getExpr(), direction::forward); m_LoopBlock.back().push_back(pushPop.getExpr_dx()); } @@ -2826,8 +2824,7 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, auto* declRef = BuildDeclRef(decl); auto* assignment = BuildOp(BO_Assign, declRef, decl->getInit()); if (isInsideLoop) { - auto pushPop = - StoreAndRestore(declRef, /*prefix=*/"_t", /*force=*/true); + auto pushPop = StoreAndRestore(declRef); if (pushPop.getExpr() != declRef) addToCurrentBlock(pushPop.getExpr_dx(), direction::reverse); assignment = BuildOp(BO_Comma, pushPop.getExpr(), assignment); @@ -3002,18 +2999,8 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, // current system that should have been decided by the parent expression. // FIXME: Here will be the entry point of the advanced activity analysis. if (isa(B) || isa(B) || - isa(B)) { - // If TBR analysis is off, assume E is useful to store. - if (!m_DiffReq.EnableTBRAnalysis) - return true; - // FIXME: currently, we allow all pointer operations to be stored. - // This is not correct, but we need to implement a more advanced analysis - // to determine which pointer operations are useful to store. - if (E->getType()->isPointerType()) - return true; - auto found = m_ToBeRecorded.find(B->getBeginLoc()); - return found != m_ToBeRecorded.end(); - } + isa(B)) + return true; // FIXME: Attach checkpointing. if (isa(B)) @@ -3080,10 +3067,10 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, StmtDiff ReverseModeVisitor::StoreAndRestore(clang::Expr* E, llvm::StringRef prefix, - bool force) { + SourceLocation loc) { auto Type = getNonConstType(E->getType(), m_Context, m_Sema); - if (!force && !UsefulToStoreGlobal(E)) + if (!isTBR(loc)) return {}; if (isInsideLoop) { diff --git a/lib/Differentiator/TBRAnalyzer.cpp b/lib/Differentiator/TBRAnalyzer.cpp index 72fbb463c..9fb6033f6 100644 --- a/lib/Differentiator/TBRAnalyzer.cpp +++ b/lib/Differentiator/TBRAnalyzer.cpp @@ -295,17 +295,7 @@ void TBRAnalyzer::addVar(const clang::VarDecl* VD, bool forceNonRefType) { } void TBRAnalyzer::markLocation(const clang::Expr* E) { - VarData* data = getExprVarData(E); - if (!data || findReq(*data)) { - // FIXME: If any of the data's child nodes are required to store then data - // itself is stored. We might add an option to store separate fields. - // FIXME: Sometimes one location might correspond to multiple stores. For - // example, in ``(x*=y)=u`` x's location will first be marked as required to - // be stored (when passing *= operator) but then marked as not required to - // be stored (when passing = operator). Current method of marking locations - // does not allow to differentiate between these two. - m_TBRLocs.insert(E->getBeginLoc()); - } + m_TBRLocs.insert(E->getBeginLoc()); } void TBRAnalyzer::setIsRequired(const clang::Expr* E, bool isReq) { @@ -701,17 +691,27 @@ bool TBRAnalyzer::VisitBinaryOperator(BinaryOperator* BinOp) { TraverseStmt(R); resetMode(); } + // FIXME: For now, pointers are not analysed correctly with TBR analysis + // so we always consider them useful to store. + if (L->getType()->isPointerType()) { + markLocation(L); + return true; + } llvm::SmallVector ExprsToStore; utils::GetInnermostReturnExpr(L, ExprsToStore); + bool hasToBeSetReq = false; for (const auto* innerExpr : ExprsToStore) { - // Mark corresponding SourceLocation as required/not required to be - // stored for all expressions that could be used changed. - markLocation(innerExpr); + // If at least one of ExprsToStore has to be stored, + // mark L as useful to store. + if (VarData* data = getExprVarData(innerExpr)) + hasToBeSetReq = hasToBeSetReq || findReq(*data); // Set them to not required to store because the values were changed. // (if some value was not changed, this could only happen if it was // already not required to store). setIsRequired(innerExpr, /*isReq=*/false); } + if (hasToBeSetReq) + markLocation(L); } else if (opCode == BO_Comma) { setMode(0); TraverseStmt(L); @@ -731,15 +731,25 @@ bool TBRAnalyzer::VisitUnaryOperator(clang::UnaryOperator* UnOp) { TraverseStmt(E); if (opCode == UO_PostInc || opCode == UO_PostDec || opCode == UO_PreInc || opCode == UO_PreDec) { + // FIXME: For now, pointers are not analysed correctly with TBR analysis + // so we always consider them useful to store. + if (E->getType()->isPointerType()) { + markLocation(E); + return true; + } // FIXME: this doesn't support all the possible references // Mark corresponding SourceLocation as required/not required to be // stored for all expressions that could be used in this operation. llvm::SmallVector ExprsToStore; utils::GetInnermostReturnExpr(E, ExprsToStore); for (const auto* innerExpr : ExprsToStore) { - // Mark corresponding SourceLocation as required/not required to be - // stored for all expressions that could be changed. - markLocation(innerExpr); + // If at least one of ExprsToStore has to be stored, + // mark L as useful to store. + if (VarData* data = getExprVarData(innerExpr)) + if (findReq(*data)) { + markLocation(E); + break; + } } } // FIXME: Ideally, `__real` and `__imag` operators should be treated as member diff --git a/test/Gradient/Assignments.C b/test/Gradient/Assignments.C index 4aba01a28..20e111ccd 100644 --- a/test/Gradient/Assignments.C +++ b/test/Gradient/Assignments.C @@ -418,11 +418,11 @@ double f9(double x, double y) { //CHECK-NEXT: double t = x; //CHECK-NEXT: _t0 = t; //CHECK-NEXT: double &_t1 = (t *= x); -//CHECK-NEXT: _t2 = t; +//CHECK-NEXT: _t2 = _t1; //CHECK-NEXT: _t1 *= y; //CHECK-NEXT: _d_t += 1; //CHECK-NEXT: { -//CHECK-NEXT: t = _t2; +//CHECK-NEXT: _t1 = _t2; //CHECK-NEXT: double _r_d1 = _d_t; //CHECK-NEXT: _d_t -= _r_d1; //CHECK-NEXT: _d_t += _r_d1 * y; @@ -477,11 +477,11 @@ double f11(double x, double y) { //CHECK-NEXT: double t = x; //CHECK-NEXT: _t0 = t; //CHECK-NEXT: double &_t1 = (t = x); -//CHECK-NEXT: _t2 = t; +//CHECK-NEXT: _t2 = _t1; //CHECK-NEXT: _t1 = y; //CHECK-NEXT: _d_t += 1; //CHECK-NEXT: { -//CHECK-NEXT: t = _t2; +//CHECK-NEXT: _t1 = _t2; //CHECK-NEXT: double _r_d1 = _d_t; //CHECK-NEXT: _d_t -= _r_d1; //CHECK-NEXT: *_d_y += _r_d1; @@ -505,7 +505,6 @@ double f12(double x, double y) { //CHECK-NEXT: double _t0; //CHECK-NEXT: double _t1; //CHECK-NEXT: double _t3; -//CHECK-NEXT: double _t4; //CHECK-NEXT: double t; //CHECK-NEXT: _cond0 = x > y; //CHECK-NEXT: if (_cond0) @@ -513,13 +512,11 @@ double f12(double x, double y) { //CHECK-NEXT: else //CHECK-NEXT: _t1 = t; //CHECK-NEXT: double &_t2 = (_cond0 ? (t = x) : (t = y)); -//CHECK-NEXT: _t3 = t; -//CHECK-NEXT: _t4 = t; +//CHECK-NEXT: _t3 = _t2; //CHECK-NEXT: _t2 *= y; //CHECK-NEXT: _d_t += 1; //CHECK-NEXT: { -//CHECK-NEXT: t = _t3; -//CHECK-NEXT: t = _t4; +//CHECK-NEXT: _t2 = _t3; //CHECK-NEXT: double _r_d2 = (_cond0 ? _d_t : _d_t); //CHECK-NEXT: (_cond0 ? _d_t : _d_t) -= _r_d2; //CHECK-NEXT: (_cond0 ? _d_t : _d_t) += _r_d2 * y;