Skip to content

Commit

Permalink
Merge branch 'master' into feat/dft_upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper authored Nov 12, 2024
2 parents 0725890 + fee16e6 commit a80c7bd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Build Status](https://github.com/vyperlang/vyper/workflows/Test/badge.svg)](https://github.com/vyperlang/vyper/actions/workflows/test.yml)
[![Documentation Status](https://readthedocs.org/projects/vyper/badge/?version=latest)](http://docs.vyperlang.org/en/latest/?badge=latest "ReadTheDocs")
[![Discord](https://img.shields.io/discord/969926564286459934.svg?label=%23vyper)](https://discord.gg/6tw7PTM7C2)
[![Telegram](https://img.shields.io/badge/Vyperholics🐍-Telegram-blue)](https://t.me/vyperlang)

[![PyPI](https://badge.fury.io/py/vyper.svg)](https://pypi.org/project/vyper "PyPI")
[![Docker](https://img.shields.io/docker/cloud/build/vyperlang/vyper)](https://hub.docker.com/r/vyperlang/vyper "DockerHub")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

extras_require["dev"] = extras_require["dev"] + extras_require["test"] + extras_require["lint"]

with open("README.md", "r") as f:
with open("README.md", "r", encoding="utf-8") as f:
long_description = f.read()


Expand Down
6 changes: 3 additions & 3 deletions tests/unit/compiler/venom/test_branch_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def test_simple_jump_case():
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("add", op3, p1)
br1.append_instruction("stop")
br2.append_instruction("add", op3, p1)
br2.append_instruction("add", op3, 10)
br2.append_instruction("stop")

term_inst = bb.instructions[-1]
Expand All @@ -47,6 +47,6 @@ def test_simple_jump_case():

# Test that the dfg is updated correctly
dfg = ac.request_analysis(DFGAnalysis)
assert dfg is old_dfg, "DFG should not be invalidated by BranchOptimizationPass"
assert dfg is not old_dfg, "DFG should be invalidated by BranchOptimizationPass"
assert term_inst in dfg.get_uses(op3), "jnz not using the new condition"
assert term_inst not in dfg.get_uses(jnz_input), "jnz still using the old condition"
30 changes: 22 additions & 8 deletions vyper/venom/passes/branch_optimization.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from vyper.venom.analysis import DFGAnalysis
from vyper.venom.analysis import CFGAnalysis, DFGAnalysis, LivenessAnalysis
from vyper.venom.basicblock import IRInstruction
from vyper.venom.passes.base_pass import IRPass


Expand All @@ -14,17 +15,30 @@ def _optimize_branches(self) -> None:
if term_inst.opcode != "jnz":
continue

prev_inst = self.dfg.get_producing_instruction(term_inst.operands[0])
if prev_inst.opcode == "iszero":
fst, snd = bb.cfg_out

fst_liveness = fst.instructions[0].liveness
snd_liveness = snd.instructions[0].liveness

cost_a, cost_b = len(fst_liveness), len(snd_liveness)

cond = term_inst.operands[0]
prev_inst = self.dfg.get_producing_instruction(cond)
if cost_a >= cost_b and 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
# and having to recompute it (which is expensive(er))
self.dfg.remove_use(prev_inst.output, term_inst)
self.dfg.add_use(new_cond, term_inst)
elif cost_a > cost_b:
new_cond = fn.get_next_variable()
inst = IRInstruction("iszero", [term_inst.operands[0]], output=new_cond)
bb.insert_instruction(inst, index=-1)
term_inst.operands = [new_cond, term_inst.operands[2], term_inst.operands[1]]

def run_pass(self):
self.liveness = self.analyses_cache.request_analysis(LivenessAnalysis)
self.cfg = self.analyses_cache.request_analysis(CFGAnalysis)
self.dfg = self.analyses_cache.request_analysis(DFGAnalysis)

self._optimize_branches()

self.analyses_cache.invalidate_analysis(LivenessAnalysis)
self.analyses_cache.invalidate_analysis(CFGAnalysis)

0 comments on commit a80c7bd

Please sign in to comment.