Skip to content

Commit 6039eef

Browse files
authored
[OPT] Remove recursion from redundancy_elimination (KhronosGroup#6141)
Remove unnecessary recursion from redundancy elimination. This should avoid potenitail stack overflows for function with deep dominator trees. Fixes KhronosGroup#6104
1 parent feba06f commit 6039eef

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

source/opt/redundancy_elimination.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,29 @@ Pass::Status RedundancyEliminationPass::Process() {
3333
DominatorTree& dom_tree =
3434
context()->GetDominatorAnalysis(&func)->GetDomTree();
3535

36-
// Keeps track of all ids that contain a given value number. We keep
37-
// track of multiple values because they could have the same value, but
38-
// different decorations.
39-
std::map<uint32_t, uint32_t> value_to_ids;
40-
41-
if (EliminateRedundanciesFrom(dom_tree.GetRoot(), vnTable, value_to_ids)) {
36+
if (EliminateRedundanciesFrom(dom_tree.GetRoot(), vnTable)) {
4237
modified = true;
4338
}
4439
}
4540
return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
4641
}
4742

4843
bool RedundancyEliminationPass::EliminateRedundanciesFrom(
49-
DominatorTreeNode* bb, const ValueNumberTable& vnTable,
50-
std::map<uint32_t, uint32_t> value_to_ids) {
51-
bool modified = EliminateRedundanciesInBB(bb->bb_, vnTable, &value_to_ids);
52-
53-
for (auto dominated_bb : bb->children_) {
54-
modified |= EliminateRedundanciesFrom(dominated_bb, vnTable, value_to_ids);
44+
DominatorTreeNode* bb, const ValueNumberTable& vnTable) {
45+
struct State {
46+
DominatorTreeNode* node;
47+
std::map<uint32_t, uint32_t> value_to_id_map;
48+
};
49+
std::vector<State> todo;
50+
todo.push_back({bb, std::map<uint32_t, uint32_t>()});
51+
bool modified = false;
52+
for (size_t next_node = 0; next_node < todo.size(); next_node++) {
53+
modified |= EliminateRedundanciesInBB(todo[next_node].node->bb_, vnTable,
54+
&todo[next_node].value_to_id_map);
55+
for (DominatorTreeNode* child : todo[next_node].node->children_) {
56+
todo.push_back({child, todo[next_node].value_to_id_map});
57+
}
5558
}
56-
5759
return modified;
5860
}
5961
} // namespace opt

source/opt/redundancy_elimination.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ class RedundancyEliminationPass : public LocalRedundancyEliminationPass {
4646
//
4747
// Returns true if at least one instruction is deleted.
4848
bool EliminateRedundanciesFrom(DominatorTreeNode* bb,
49-
const ValueNumberTable& vnTable,
50-
std::map<uint32_t, uint32_t> value_to_ids);
49+
const ValueNumberTable& vnTable);
5150
};
5251

5352
} // namespace opt

0 commit comments

Comments
 (0)