-
-
Notifications
You must be signed in to change notification settings - Fork 871
refactor[venom]: add new venom test machinery #4401
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
Merged
charles-cooper
merged 9 commits into
vyperlang:master
from
charles-cooper:refactor/venom-tests
Dec 17, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c7e4e2c
fix order of jnz operands
charles-cooper 59a7962
fix parser
charles-cooper b12f75e
refactor test_simplify_cfg
charles-cooper 146ecfd
make data section optional
charles-cooper ac9b0b2
small touchup
charles-cooper 5c1b0ad
reorder code
charles-cooper 12a9bde
add comments to grammar
charles-cooper 1d9b90b
remove dead comment
charles-cooper 84c922b
update phi order in text
charles-cooper 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 hidden or 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 hidden or 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,48 +1,37 @@ | ||
| from tests.venom_utils import assert_ctx_eq, parse_venom | ||
| from vyper.venom.analysis import IRAnalysesCache | ||
| from vyper.venom.basicblock import IRBasicBlock, IRLabel, IRLiteral | ||
| from vyper.venom.context import IRContext | ||
| from vyper.venom.passes import SCCP, SimplifyCFGPass | ||
|
|
||
|
|
||
| def test_phi_reduction_after_block_pruning(): | ||
| 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) | ||
|
|
||
| join = IRBasicBlock(IRLabel("join"), fn) | ||
| fn.append_basic_block(join) | ||
|
|
||
| true = IRLiteral(1) | ||
| bb.append_instruction("jnz", true, br1.label, br2.label) | ||
|
|
||
| op1 = br1.append_instruction("store", 1) | ||
| op2 = br2.append_instruction("store", 2) | ||
|
|
||
| br1.append_instruction("jmp", join.label) | ||
| br2.append_instruction("jmp", join.label) | ||
|
|
||
| join.append_instruction("phi", br1.label, op1, br2.label, op2) | ||
| join.append_instruction("stop") | ||
|
|
||
| ac = IRAnalysesCache(fn) | ||
| SCCP(ac, fn).run_pass() | ||
| SimplifyCFGPass(ac, fn).run_pass() | ||
|
|
||
| bbs = list(fn.get_basic_blocks()) | ||
|
|
||
| assert len(bbs) == 1 | ||
| final_bb = bbs[0] | ||
|
|
||
| inst0, inst1, inst2 = final_bb.instructions | ||
|
|
||
| assert inst0.opcode == "store" | ||
| assert inst0.operands == [IRLiteral(1)] | ||
| assert inst1.opcode == "store" | ||
| assert inst1.operands == [inst0.output] | ||
| assert inst2.opcode == "stop" | ||
| pre = """ | ||
| function _global { | ||
| _global: | ||
| jnz 1, @then, @else | ||
| then: | ||
| %1 = 1 | ||
| jmp @join | ||
| else: | ||
| %2 = 2 | ||
| jmp @join | ||
| join: | ||
| %3 = phi @then, %1, @else, %2 | ||
| stop | ||
| } | ||
| """ | ||
| post = """ | ||
| function _global { | ||
| _global: | ||
| %1 = 1 | ||
| %3 = %1 | ||
| stop | ||
| } | ||
| """ | ||
| ctx1 = parse_venom(pre) | ||
| for fn in ctx1.functions.values(): | ||
| ac = IRAnalysesCache(fn) | ||
| SCCP(ac, fn).run_pass() | ||
| SimplifyCFGPass(ac, fn).run_pass() | ||
|
|
||
| ctx2 = parse_venom(post) | ||
| assert_ctx_eq(ctx1, ctx2) |
This file contains hidden or 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,50 @@ | ||
| from vyper.venom.basicblock import IRBasicBlock, IRInstruction | ||
| from vyper.venom.context import IRContext | ||
| from vyper.venom.function import IRFunction | ||
| from vyper.venom.parser import parse_venom | ||
|
|
||
|
|
||
| def parse_from_basic_block(source: str, funcname="_global"): | ||
| """ | ||
| Parse an IRContext from a basic block | ||
| """ | ||
| source = f"function {funcname} {{\n{source}\n}}" | ||
| return parse_venom(source) | ||
|
|
||
|
|
||
| def instructions_eq(i1: IRInstruction, i2: IRInstruction) -> bool: | ||
| return i1.output == i2.output and i1.opcode == i2.opcode and i1.operands == i2.operands | ||
|
|
||
|
|
||
| def assert_bb_eq(bb1: IRBasicBlock, bb2: IRBasicBlock): | ||
| assert bb1.label.value == bb2.label.value | ||
| assert len(bb1.instructions) == len(bb2.instructions) | ||
| for i1, i2 in zip(bb1.instructions, bb2.instructions): | ||
| assert instructions_eq(i1, i2), f"[{i1}] != [{i2}]" | ||
|
|
||
|
|
||
| def assert_fn_eq(fn1: IRFunction, fn2: IRFunction): | ||
| assert fn1.name.value == fn2.name.value | ||
| assert len(fn1._basic_block_dict) == len(fn2._basic_block_dict) | ||
|
|
||
| for name1, bb1 in fn1._basic_block_dict.items(): | ||
| assert name1 in fn2._basic_block_dict | ||
| assert_bb_eq(bb1, fn2._basic_block_dict[name1]) | ||
|
|
||
| # check function entry is the same | ||
| assert fn1.entry.label == fn2.entry.label | ||
|
|
||
|
|
||
| def assert_ctx_eq(ctx1: IRContext, ctx2: IRContext): | ||
| assert ctx1.last_label == ctx2.last_label | ||
| assert len(ctx1.functions) == len(ctx2.functions) | ||
| for label1, fn1 in ctx1.functions.items(): | ||
| assert label1 in ctx2.functions | ||
| assert_fn_eq(fn1, ctx2.functions[label1]) | ||
|
|
||
| # check entry function is the same | ||
| assert next(iter(ctx1.functions.keys())) == next(iter(ctx2.functions.keys())) | ||
|
|
||
| assert len(ctx1.data_segment) == len(ctx2.data_segment) | ||
| for d1, d2 in zip(ctx1.data_segment, ctx2.data_segment): | ||
| assert instructions_eq(d1, d2), f"data: [{d1}] != [{d2}]" |
This file contains hidden or 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 hidden or 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.
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.
I now this is not necessarily part of the PR but there it should be
self.opcodeno? since the opcode contains space after original opcode so body of thisifis unreachable right now (I have noticed it when thinking about order of operands in jnz)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.
yea seems so. let's leave it out of scope, can be fixed in #4402 or #4403