-
-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix[venom]: fix invalid
phi
s after SCCP (#4181)
This commit reduces `phi` nodes which, after SCCP, refer to a basic block which is unreachable. This could happen when the SCCP would replace a `jnz` by a `jmp` instruction, resulting in a `phi` instruction which refers to a non-existent block, or a `phi` instruction in a basic block which only has one predecessor. This commit reduces such `phi` instructions into `store` instructions. --------- Co-authored-by: Harry Kalogirou <[email protected]> Co-authored-by: Charles Cooper <[email protected]>
- Loading branch information
1 parent
d60d31f
commit e2f6001
Showing
4 changed files
with
127 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from vyper.venom.analysis.analysis import IRAnalysesCache | ||
from vyper.venom.basicblock import IRBasicBlock, IRLabel, IRLiteral | ||
from vyper.venom.context import IRContext | ||
from vyper.venom.passes.sccp import SCCP | ||
from vyper.venom.passes.simplify_cfg import 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" |
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