Skip to content

Commit

Permalink
Replace dominator-based backedge algorithm with simple DFS algorithm
Browse files Browse the repository at this point in the history
Note: dominator-based version is too expensive.
  • Loading branch information
sklam committed Aug 18, 2020
1 parent 5c94b91 commit 264665e
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions numba/core/controlflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,13 +572,41 @@ def _find_back_edges(self):
Find back edges. An edge (src, dest) is a back edge if and
only if *dest* dominates *src*.
"""

# Uses a simple DFS to find back-edges.
# The new algorithm is faster than the the previous dominator based
# algorithm.
back_edges = set()
for src, succs in self._succs.items():
back = self._doms[src] & succs
# In CPython bytecode, at most one back edge can flow from a
# given block.
assert len(back) <= 1
back_edges.update((src, dest) for dest in back)
# stack: keeps track of the traversal path
stack = []
# succs_state: keep track of unvisited successors of a node
succs_state = {}
entry_point = self.entry_point()

def push_state(node):
stack.append(node)
succs_state[node] = [dest for dest, _ in self.successors(node)]

push_state(entry_point)

while stack:
tos = stack[-1]
tos_succs = succs_state[tos]
# Are there successors not checked?
if tos_succs:
# Check the next successor
cur_node = tos_succs.pop()
# Is it in our traversal path?
if cur_node in stack:
# Yes, it's a backedge
back_edges.add((tos, cur_node))
else:
# Push
push_state(cur_node)
else:
# Checked all successors. Pop
stack.pop()

return back_edges

def _find_topo_order(self):
Expand Down

0 comments on commit 264665e

Please sign in to comment.