Skip to content

Commit 46e5272

Browse files
Sink TBR::copyVarToCurBlock in TBR::setIsRequired
1 parent 735154e commit 46e5272

2 files changed

Lines changed: 28 additions & 27 deletions

File tree

lib/Differentiator/TBRAnalyzer.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -260,22 +260,6 @@ void TBRAnalyzer::overlay(const clang::Expr* E) {
260260
}
261261
// NOLINTEND(cppcoreguidelines-pro-type-union-access)
262262

263-
void TBRAnalyzer::copyVarToCurBlock(const clang::VarDecl* VD) {
264-
// Visit all predecessors one by one until the variable VD is found.
265-
auto& curBranch = getCurBlockVarsData();
266-
auto* pred = curBranch.m_Prev;
267-
while (pred) {
268-
auto it = pred->find(VD);
269-
if (it != pred->end()) {
270-
curBranch[VD] = copy(it->second);
271-
return;
272-
}
273-
pred = pred->m_Prev;
274-
}
275-
// If this variable was not found in predecessors, add it.
276-
addVar(VD);
277-
}
278-
279263
void TBRAnalyzer::addVar(const clang::VarDecl* VD, bool forceNonRefType) {
280264
auto& curBranch = getCurBlockVarsData();
281265

@@ -296,6 +280,34 @@ void TBRAnalyzer::markLocation(const clang::Expr* E) {
296280
}
297281

298282
void TBRAnalyzer::setIsRequired(const clang::Expr* E, bool isReq) {
283+
// FIXME: generalize to other exprs
284+
if (const auto* DRE = dyn_cast<DeclRefExpr>(E)) {
285+
// Since TBRAnalyzer is a RecursiveASTVisitor,
286+
// it automatically visits all sub-stmts including
287+
// decl refs of functions.
288+
if (!isa<VarDecl>(DRE->getDecl()))
289+
return;
290+
const auto* VD = cast<VarDecl>(DRE->getDecl());
291+
auto& curBranch = getCurBlockVarsData();
292+
if (curBranch.find(VD) == curBranch.end()) {
293+
// Visit all predecessors one by one until the variable VD is found.
294+
auto* pred = curBranch.m_Prev;
295+
bool found = false;
296+
while (pred) {
297+
auto it = pred->find(VD);
298+
if (it != pred->end()) {
299+
curBranch[VD] = copy(it->second);
300+
found = true;
301+
break;
302+
}
303+
pred = pred->m_Prev;
304+
}
305+
// If this variable was not found in predecessors, add it.
306+
if (!found)
307+
addVar(VD);
308+
}
309+
}
310+
299311
if (!isReq ||
300312
(m_ModeStack.back() == (Mode::kMarkingMode | Mode::kNonLinearMode))) {
301313
VarData* data = getExprVarData(E);
@@ -571,14 +583,7 @@ void TBRAnalyzer::merge(VarsData* targetData, VarsData* mergeData) {
571583
}
572584

573585
bool TBRAnalyzer::VisitDeclRefExpr(DeclRefExpr* DRE) {
574-
if (const auto* VD = dyn_cast<VarDecl>(DRE->getDecl())) {
575-
auto& curBranch = getCurBlockVarsData();
576-
if (curBranch.find(VD) == curBranch.end())
577-
copyVarToCurBlock(VD);
578-
}
579-
580586
setIsRequired(DRE);
581-
582587
return true;
583588
}
584589

lib/Differentiator/TBRAnalyzer.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,6 @@ class TBRAnalyzer : public clang::RecursiveASTVisitor<TBRAnalyzer> {
250250
//// Setters
251251
/// Creates VarData for a new VarDecl*.
252252
void addVar(const clang::VarDecl* VD, bool forceNonRefType = false);
253-
/// Makes a copy of the VarData corresponding to VD
254-
/// to the current block from the lowest predecessor
255-
/// where VD is present.
256-
void copyVarToCurBlock(const clang::VarDecl* VD);
257253
/// Marks the SourceLocation of E if it is required to store.
258254
/// E could be DeclRefExpr*, ArraySubscriptExpr* or MemberExpr*.
259255
void markLocation(const clang::Expr* E);

0 commit comments

Comments
 (0)