forked from vyperlang/vyper
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[venom]: improve liveness computation (vyperlang#4330)
traversing in reverse topsort order improves stack scheduling slightly this commit also adds a topsort method to CFGAnalysis, and speeds it up by only checking the terminator instruction instead of iterating over all the instructions in every basic block. additional refactors: - move dfs order calculation from domtree to cfg analysis. - remove unnecessary calculation of domtree in sccp - remove redundant IRFunction.compute_reachability - change cfg_out order - refactor shared phi fixup code - remove useless `__eq__()` and `__hash__()` for IRBasicBlock
- Loading branch information
1 parent
fee16e6
commit 48cb39b
Showing
12 changed files
with
110 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,62 @@ | ||
from typing import Iterator | ||
|
||
from vyper.utils import OrderedSet | ||
from vyper.venom.analysis import IRAnalysis | ||
from vyper.venom.basicblock import CFG_ALTERING_INSTRUCTIONS | ||
from vyper.venom.basicblock import CFG_ALTERING_INSTRUCTIONS, IRBasicBlock | ||
|
||
|
||
class CFGAnalysis(IRAnalysis): | ||
""" | ||
Compute control flow graph information for each basic block in the function. | ||
""" | ||
|
||
_dfs: OrderedSet[IRBasicBlock] | ||
|
||
def analyze(self) -> None: | ||
fn = self.function | ||
self._dfs = OrderedSet() | ||
|
||
for bb in fn.get_basic_blocks(): | ||
bb.cfg_in = OrderedSet() | ||
bb.cfg_out = OrderedSet() | ||
bb.out_vars = OrderedSet() | ||
bb.is_reachable = False | ||
|
||
for bb in fn.get_basic_blocks(): | ||
assert len(bb.instructions) > 0, "Basic block should not be empty" | ||
last_inst = bb.instructions[-1] | ||
assert last_inst.is_bb_terminator, f"Last instruction should be a terminator {bb}" | ||
assert bb.is_terminated | ||
|
||
for inst in bb.instructions: | ||
if inst.opcode in CFG_ALTERING_INSTRUCTIONS: | ||
ops = inst.get_label_operands() | ||
for op in ops: | ||
fn.get_basic_block(op.value).add_cfg_in(bb) | ||
term = bb.instructions[-1] | ||
if term.opcode in CFG_ALTERING_INSTRUCTIONS: | ||
ops = term.get_label_operands() | ||
# order of cfg_out matters to performance! | ||
for op in reversed(list(ops)): | ||
next_bb = fn.get_basic_block(op.value) | ||
bb.add_cfg_out(next_bb) | ||
next_bb.add_cfg_in(bb) | ||
|
||
# Fill in the "out" set for each basic block | ||
for bb in fn.get_basic_blocks(): | ||
for in_bb in bb.cfg_in: | ||
in_bb.add_cfg_out(bb) | ||
self._compute_dfs_r(self.function.entry) | ||
|
||
def _compute_dfs_r(self, bb): | ||
if bb.is_reachable: | ||
return | ||
bb.is_reachable = True | ||
|
||
for out_bb in bb.cfg_out: | ||
self._compute_dfs_r(out_bb) | ||
|
||
self._dfs.add(bb) | ||
|
||
@property | ||
def dfs_walk(self) -> Iterator[IRBasicBlock]: | ||
return iter(self._dfs) | ||
|
||
def invalidate(self): | ||
from vyper.venom.analysis import DFGAnalysis, DominatorTreeAnalysis, LivenessAnalysis | ||
|
||
self.analyses_cache.invalidate_analysis(DominatorTreeAnalysis) | ||
self.analyses_cache.invalidate_analysis(LivenessAnalysis) | ||
|
||
self._dfs = None | ||
|
||
# be conservative - assume cfg invalidation invalidates dfg | ||
self.analyses_cache.invalidate_analysis(DFGAnalysis) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.