Skip to content

Commit

Permalink
Do not change predecessors in VarsData merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
PetroZarytskyi committed Nov 21, 2023
1 parent 2c37889 commit 9c43d4f
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions lib/Differentiator/TBRAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,19 +396,19 @@ void TBRAnalyzer::VisitCFGBlock(const CFGBlock& block) {
/// a loop condition and the loop body has already been visited 2 times.
/// This means we should not visit the loop body anymore.
if (notLastPass) {
/// Add the successor to the queue.
m_CFGQueue.insert(succ->getBlockID());

/// This part is necessary for loops. For other cases, this is not
/// supposed to do anything.
if (succ->getBlockID() < block.getBlockID()) {
/// If there is another loop condition present inside a loop,
/// We have to set it's loop pass counter to 0 (it might be 3
/// from the previous outer loop pass).
m_BlockPassCounter[succ->getBlockID()] = 0;
/// Remove VarsData left after the previous pass.
varsData->clear();
}
/// Add the successor to the queue.
m_CFGQueue.insert(succ->getBlockID());

/// This part is necessary for loops. For other cases, this is not
/// supposed to do anything.
if (succ->getBlockID() < block.getBlockID()) {
/// If there is another loop condition present inside a loop,
/// We have to set it's loop pass counter to 0 (it might be 3
/// from the previous outer loop pass).
m_BlockPassCounter[succ->getBlockID()] = 0;
/// Remove VarsData left after the previous pass.
varsData->clear();
}
}

/// If the successor's previous block is not this one,
Expand Down Expand Up @@ -495,7 +495,6 @@ void TBRAnalyzer::merge(VarsData* targetData, VarsData* mergeData) {
auto* LCA = findLowestCommonAncestor(targetData, mergeData);
auto collectedMergeData =
collectDataFromPredecessors(mergeData, /*limit=*/LCA);
auto collectedTargetData = collectDataFromPredecessors(targetData, /*limit=*/LCA);

/// For every variable in 'collectedMergeData', search it in targetData
/// and all its predecessors (if found in a predecessor, make a copy to
Expand Down Expand Up @@ -526,19 +525,43 @@ void TBRAnalyzer::merge(VarsData* targetData, VarsData* mergeData) {
(*targetData)[pair.first] = copy(*pair.second);
}

/// For every variable in collectedTargetData, search it inside
/// For every variable in collected targetData predecessors,
/// search it inside collectedMergeData. If it's not found,
/// that means it was not used anywhere between LCA and mergeData.
/// To correctly merge, we have to take it from LCA's
/// predecessors and merge it to targetData.
/// If targetData is LCA, LCA will come after targetData->prev and
/// collectDataFromPredecessors will not reach the limit.
if (targetData != LCA) {
for (auto& pair : collectDataFromPredecessors(targetData->prev, /*limit=*/LCA)) {
auto elemSearch = collectedMergeData.find(pair.first);
if (elemSearch == collectedMergeData.end()) {
auto* branch = LCA;
while (branch) {
auto it = branch->find(pair.first);
if (it != branch->end()) {
(*targetData)[pair.first] = copy(*pair.second);
merge((*targetData)[pair.first], it->second);
break;
}
branch = branch->prev;
}
}
}
}
/// For every variable in targetData, search it inside
/// collectedMergeData. If it's not found, that means it
/// was not used anywhere between LCA and mergeData.
/// To correctly merge, we have to take it from LCA's
/// predecessors and merge it to targetData.
for (auto& pair : collectedTargetData) {
for (auto& pair : *targetData) {
auto elemSearch = collectedMergeData.find(pair.first);
if (elemSearch == collectedMergeData.end()) {
auto* branch = LCA;
while (branch) {
auto it = branch->find(pair.first);
if (it != branch->end()) {
merge(*pair.second, it->second);
merge(pair.second, it->second);
break;
}
branch = branch->prev;
Expand Down

0 comments on commit 9c43d4f

Please sign in to comment.