Skip to content
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

fix[venom]: fix duplicate allocas #4321

Merged
merged 25 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions vyper/venom/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def chain_basic_blocks(self) -> None:
for fn in self.functions.values():
fn.chain_basic_blocks()

def float_allocas(self) -> None:
for fn in self.functions.values():
fn.float_allocas()

def append_data(self, opcode: str, args: list[IROperand]) -> None:
"""
Append data
Expand Down
19 changes: 19 additions & 0 deletions vyper/venom/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,25 @@ def chain_basic_blocks(self) -> None:
else:
bb.append_instruction("exit")

def float_allocas(self):
entry_bb = self.entry
assert entry_bb.is_terminated
tmp = entry_bb.instructions.pop()

for bb in self.get_basic_blocks():
if bb is entry_bb:
continue

# "fast" way to strip allocas from each basic block
def is_alloca(inst):
return inst.opcode in ("alloca", "palloca")

bb.instructions.sort(key=is_alloca)
while len(bb.instructions) > 0 and is_alloca(bb.instructions[-1]):
entry_bb.insert_instruction(bb.instructions.pop())

entry_bb.instructions.append(tmp)

def copy(self):
new = IRFunction(self.name)
new._basic_block_dict = self._basic_block_dict.copy()
Expand Down
12 changes: 5 additions & 7 deletions vyper/venom/ir_node_to_venom.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@

ctx.chain_basic_blocks()

# errors in make_ssa whether this is commented or not
#ctx.float_allocas()

return ctx


Expand Down Expand Up @@ -269,12 +272,14 @@
# Internal definition
var_list = ir.args[0].args[1]
does_return_data = IRnode.from_list(["return_buffer"]) in var_list.args
saved_global_symbols = _global_symbols
_global_symbols = {}
symbols = {}
new_fn = _handle_internal_func(fn, ir, does_return_data, symbols)
for ir_node in ir.args[1:]:
ret = _convert_ir_bb(new_fn, ir_node, symbols)

_global_symbols = saved_global_symbols
return ret
elif is_external:
ret = _convert_ir_bb(fn, ir.args[0], symbols)
Expand All @@ -297,8 +302,6 @@
cont_ret = _convert_ir_bb(fn, cond, symbols)
cond_block = fn.get_basic_block()

saved_global_symbols = _global_symbols.copy()

then_block = IRBasicBlock(ctx.get_next_label("then"), fn)
else_block = IRBasicBlock(ctx.get_next_label("else"), fn)

Expand All @@ -313,7 +316,6 @@

# convert "else"
cond_symbols = symbols.copy()
_global_symbols = saved_global_symbols.copy()
fn.append_basic_block(else_block)
else_ret_val = None
if len(ir.args) == 3:
Expand Down Expand Up @@ -342,8 +344,6 @@
if not then_block_finish.is_terminated:
then_block_finish.append_instruction("jmp", exit_bb.label)

_global_symbols = saved_global_symbols

return if_ret

elif ir.value == "with":
Expand Down Expand Up @@ -468,10 +468,8 @@
global _break_target, _continue_target, _global_symbols
old_targets = _break_target, _continue_target
_break_target, _continue_target = exit_block, incr_block
saved_global_symbols = _global_symbols.copy()
_convert_ir_bb(fn, body, symbols.copy())
_break_target, _continue_target = old_targets
_global_symbols = saved_global_symbols

sym = ir.args[0]
start, end, _ = _convert_ir_bb_list(fn, ir.args[1:4], symbols)
Expand Down
Loading