-
-
Notifications
You must be signed in to change notification settings - Fork 836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat[venom]: optimize branching #4049
Merged
charles-cooper
merged 20 commits into
vyperlang:master
from
harkal:feat/optimize_branching
May 28, 2024
Merged
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7ca1a39
implement branch optimization pass
harkal ad77128
remove check
harkal 558c4d7
make pass handle a chain of `iszero`s
harkal 5133ac2
lint
harkal 3fea134
lint^2
harkal 36e0b76
remove unnecessary check
harkal 269b7bb
Revert iszero chain handling
harkal c58892c
add tests
harkal 21f5c39
fix pass order
harkal 86d2a4f
do not delete the instruction
harkal 9555eb7
update branch tests
harkal 9bf6865
cleanup imports
harkal 1f7a792
Merge branch 'master' into feat/optimize_branching
harkal b412aa7
improve clarity
harkal d73d490
Merge remote-tracking branch 'origin-vyper/master' into feat/optimize…
harkal 2b94cd2
implement `add_use()` and `remove_use()` in `DFGAnalysis`
harkal bb3c33c
Merge remote-tracking branch 'origin-vyper/master' into feat/optimize…
harkal 468275f
Merge remote-tracking branch 'origin-vyper/master' into feat/optimize…
harkal 124efd7
test if the dfg is properly updated in place
harkal c329834
update comment
harkal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from vyper.venom.analysis.analysis import IRAnalysesCache | ||
from vyper.venom.basicblock import IRBasicBlock, IRLabel | ||
from vyper.venom.context import IRContext | ||
from vyper.venom.passes.branch_optimization import BranchOptimizationPass | ||
from vyper.venom.passes.make_ssa import MakeSSA | ||
|
||
|
||
def test_simple_jump_case(): | ||
ctx = IRContext() | ||
fn = ctx.create_function("_global") | ||
|
||
bb = fn.get_basic_block() | ||
|
||
br1 = IRBasicBlock(IRLabel("then"), fn) | ||
fn.append_basic_block(br1) | ||
br2 = IRBasicBlock(IRLabel("else"), fn) | ||
fn.append_basic_block(br2) | ||
|
||
p1 = bb.append_instruction("param") | ||
op1 = bb.append_instruction("store", p1) | ||
op2 = bb.append_instruction("store", 64) | ||
op3 = bb.append_instruction("add", op1, op2) | ||
jnz_input = bb.append_instruction("iszero", op3) | ||
bb.append_instruction("jnz", jnz_input, br1.label, br2.label) | ||
|
||
br1.append_instruction("add", op3, 10) | ||
br1.append_instruction("stop") | ||
br2.append_instruction("add", op3, p1) | ||
br2.append_instruction("stop") | ||
|
||
ac = IRAnalysesCache(fn) | ||
MakeSSA(ac, fn).run_pass() | ||
BranchOptimizationPass(ac, fn).run_pass() | ||
|
||
assert bb.instructions[-1].opcode == "jnz" | ||
assert bb.instructions[-1].operands[0] == op3 | ||
assert bb.instructions[-1].operands[1] == br2.label | ||
assert bb.instructions[-1].operands[2] == br1.label |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from vyper.venom.analysis.dfg import DFGAnalysis | ||
from vyper.venom.passes.base_pass import IRPass | ||
|
||
|
||
class BranchOptimizationPass(IRPass): | ||
""" | ||
This pass optimizes branches inverting jnz instructions where appropriate | ||
""" | ||
|
||
def _optimize_branches(self) -> None: | ||
fn = self.function | ||
for bb in fn.get_basic_blocks(): | ||
term_inst = bb.instructions[-1] | ||
if term_inst.opcode != "jnz": | ||
continue | ||
|
||
prev_inst = self.dfg.get_producing_instruction(term_inst.operands[0]) | ||
harkal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if prev_inst.opcode == "iszero": | ||
new_cond = prev_inst.operands[0] | ||
term_inst.operands = [new_cond, term_inst.operands[2], term_inst.operands[1]] | ||
|
||
# Since the DFG update is simple we do in place to avoid invalidating the DFG | ||
harkal marked this conversation as resolved.
Show resolved
Hide resolved
charles-cooper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.dfg.remove_use(prev_inst.output, term_inst) | ||
self.dfg.add_use(new_cond, term_inst) | ||
|
||
def run_pass(self): | ||
self.dfg = self.analyses_cache.request_analysis(DFGAnalysis) | ||
|
||
self._optimize_branches() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: this could be out of order from recalculating dfg from scratch, maybe in the future we should use something like
set()
orOrderedSet()
for uses