Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Nov 20, 2024
1 parent e8bbdb7 commit 0478878
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
10 changes: 7 additions & 3 deletions vyper/venom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ def _run_passes(fn: IRFunction, optimize: OptimizationLevel) -> None:

FunctionInlinerPass(ac, fn).run_pass()

RemoveUnusedVariablesPass(ac, fn).run_pass()
StoreElimination(ac, fn).run_pass()

SimplifyCFGPass(ac, fn).run_pass()
MakeSSA(ac, fn).run_pass()

Mem2Var(ac, fn).run_pass()
MakeSSA(ac, fn).run_pass()

# function inliner can insert bad variables, remove them before sccp

RemoveUnusedVariablesPass(ac, fn).run_pass()

SCCP(ac, fn).run_pass()
StoreElimination(ac, fn).run_pass()
SimplifyCFGPass(ac, fn).run_pass()
Expand All @@ -76,6 +79,7 @@ def _run_passes(fn: IRFunction, optimize: OptimizationLevel) -> None:
def generate_ir(ir: IRnode, optimize: OptimizationLevel) -> IRContext:
# Convert "old" IR to "new" IR
ctx = ir_node_to_venom(ir)

for fn in ctx.functions.values():
_run_passes(fn, optimize)

Expand Down
35 changes: 23 additions & 12 deletions vyper/venom/passes/function_inliner.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def _build_alloca_map(self):
ret = {}
for bb in self.function.get_basic_blocks():
for inst in bb.instructions:
if inst.opcode.startswith("alloca"):
ret[tuple(inst.operands)] = inst
if inst.opcode == "alloca":
ret[inst.operands[0]] = inst
return ret

@property
Expand Down Expand Up @@ -62,13 +62,12 @@ def _handle_invoke(self, invoke_inst, invoke_idx):

next_bb = IRBasicBlock(ctx.get_next_label(), fn)

label_map = defaultdict(ctx.get_next_label)
label_map = defaultdict(lambda: ctx.get_next_label(f"inline {target_function.name}"))

# make copies of every bb and inline them into the code
for bb in bbs:
new_label = label_map[bb.label]
new_bb = IRBasicBlock(new_label, fn)

if bb is target_function.entry:
target_bb = new_bb

Expand All @@ -87,18 +86,28 @@ def _handle_invoke(self, invoke_inst, invoke_idx):
inst.opcode = "jmp"
inst.operands = [next_bb.label]
inst.output = None
if inst.opcode.startswith("palloca"):
alloca_id = tuple(inst.operands)
if inst.opcode == "palloca":
alloca_id = inst.operands[0]
inst.opcode = "store"
inst.operands = [self._alloca_map[alloca_id].output]
if inst.opcode == "param":
inst.opcode = "store"
op = invoke_inst.operands[-i - 1]
inst.operands = [invoke_inst.operands[-i - 1]]

new_var = fn.get_next_variable()
var_map[inst.output] = new_var
inst.output = new_var
inst.operands = [invoke_inst.operands[-i-1]]
if inst.opcode == "alloca":
alloca_id = inst.operands[0]
#assert alloca_id not in self._alloca_map, (alloca_id, inst, fn, target_function)
if alloca_id in self._alloca_map:
inst.opcode = "store"
#inst.operands = self._alloca_map[alloca_id].

if inst.output is not None:
if inst.output in var_map:
# this can happen because we are not in SSA yet.
inst.output = var_map[inst.output]
else:
new_var = fn.get_next_variable()
var_map[inst.output] = new_var
inst.output = new_var

fn.append_basic_block(new_bb)
self.worklist.append(new_bb)
Expand All @@ -116,4 +125,6 @@ def _handle_invoke(self, invoke_inst, invoke_idx):
invoke_inst.opcode = "jmp"
invoke_inst.operands = [target_bb.label]
invoke_inst.output = None

self._alloca_map = self._build_alloca_map()
return True
4 changes: 3 additions & 1 deletion vyper/venom/passes/mem2var.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def _process_alloca_var(self, dfg: DFGAnalysis, var: IRVariable):
uses = dfg.get_uses(var)
if all([inst.opcode == "mload" for inst in uses]):
return
if all([inst.opcode in ["mstore", "mload", "return"] for inst in uses]):
elif all([inst.opcode == "mstore" for inst in uses]):
return
elif all([inst.opcode in ["mstore", "mload", "return"] for inst in uses]):
var_name = self._mk_varname(var.name)
for inst in uses:
if inst.opcode == "mstore":
Expand Down
2 changes: 1 addition & 1 deletion vyper/venom/passes/sccp/sccp.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def finalize(ret):
if eval_result is LatticeEnum.BOTTOM:
return finalize(LatticeEnum.BOTTOM)

assert isinstance(eval_result, IROperand)
assert isinstance(eval_result, IROperand), (op, eval_result)
ops.append(eval_result)

# If we haven't found BOTTOM yet, evaluate the operation
Expand Down

0 comments on commit 0478878

Please sign in to comment.