Skip to content

Commit

Permalink
Use a reference to store the LHS of assignments with LHS with side ef…
Browse files Browse the repository at this point in the history
…fects
  • Loading branch information
PetroZarytskyi committed Jul 12, 2024
1 parent ef668c7 commit f455cc6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 59 deletions.
9 changes: 7 additions & 2 deletions include/clad/Differentiator/ReverseModeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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
Expand Down
49 changes: 18 additions & 31 deletions lib/Differentiator/ReverseModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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<Expr*, 4> 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();
Expand All @@ -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);
}
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<DeclRefExpr>(B) || isa<ArraySubscriptExpr>(B) ||
isa<MemberExpr>(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<MemberExpr>(B))
return true;

// FIXME: Attach checkpointing.
if (isa<CallExpr>(B))
Expand Down Expand Up @@ -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) {
Expand Down
44 changes: 27 additions & 17 deletions lib/Differentiator/TBRAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Expr*, 4> 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);
Expand All @@ -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<Expr*, 4> 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
Expand Down
15 changes: 6 additions & 9 deletions test/Gradient/Assignments.C
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -505,21 +505,18 @@ 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)
//CHECK-NEXT: _t0 = t;
//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;
Expand Down

0 comments on commit f455cc6

Please sign in to comment.