Skip to content

Commit

Permalink
Revert "remove store expanstion"
Browse files Browse the repository at this point in the history
This reverts commit a99ccf6.
  • Loading branch information
harkal committed Oct 2, 2024
1 parent a99ccf6 commit d8d747b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions vyper/venom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from typing import Optional

from vyper.venom.passes.store_expansion import StoreExpansionPass
from vyper.codegen.ir_node import IRnode
from vyper.compiler.settings import OptimizationLevel
from vyper.venom.analysis.analysis import IRAnalysesCache
Expand Down Expand Up @@ -56,6 +57,7 @@ def _run_passes(fn: IRFunction, optimize: OptimizationLevel) -> None:
BranchOptimizationPass(ac, fn).run_pass()
ExtractLiteralsPass(ac, fn).run_pass()
RemoveUnusedVariablesPass(ac, fn).run_pass()
StoreExpansionPass(ac, fn).run_pass()
DFTPass(ac, fn).run_pass()


Expand Down
46 changes: 46 additions & 0 deletions vyper/venom/passes/store_expansion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from vyper.venom.analysis.dfg import DFGAnalysis
from vyper.venom.analysis.liveness import LivenessAnalysis
from vyper.venom.basicblock import IRInstruction
from vyper.venom.passes.base_pass import IRPass


class StoreExpansionPass(IRPass):
"""
This pass expands variables to their uses though `store` instructions,
reducing pressure on the stack scheduler
"""

def run_pass(self):
dfg = self.analyses_cache.request_analysis(DFGAnalysis)

for bb in self.function.get_basic_blocks():
for idx, inst in enumerate(bb.instructions):
if inst.output is None:
continue

# print("ENTER", inst)
self._process_inst(dfg, inst, idx)

self.analyses_cache.invalidate_analysis(LivenessAnalysis)
self.analyses_cache.invalidate_analysis(DFGAnalysis)

def _process_inst(self, dfg, inst, idx):
"""
Process store instruction. If the variable is only used by a load instruction,
forward the variable to the load instruction.
"""
var = inst.output
uses = dfg.get_uses(var)

insertion_idx = idx + 1

for use_inst in uses:
if use_inst.parent != inst.parent:
continue # improves codesize

for i, operand in enumerate(use_inst.operands):
if operand == var:
new_var = self.function.get_next_variable()
new_inst = IRInstruction("store", [var], new_var)
inst.parent.insert_instruction(new_inst, insertion_idx)
use_inst.operands[i] = new_var

0 comments on commit d8d747b

Please sign in to comment.