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: assembly dead code eliminator #3791

Merged
merged 4 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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: 2 additions & 2 deletions tests/unit/compiler/venom/test_duplicate_operands.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ def test_duplicate_operands():
bb.append_instruction("mul", sum_, op)
bb.append_instruction("stop")

asm = generate_assembly_experimental(ctx, optimize=OptimizationLevel.CODESIZE)
asm = generate_assembly_experimental(ctx, optimize=OptimizationLevel.GAS)

assert asm == ["PUSH1", 10, "DUP1", "DUP1", "DUP1", "ADD", "MUL", "STOP", "REVERT"]
assert asm == ["PUSH1", 10, "DUP1", "DUP1", "DUP1", "ADD", "MUL", "STOP"]
31 changes: 19 additions & 12 deletions vyper/ir/compile_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,18 +809,26 @@ def _prune_unreachable_code(assembly):
# unreachable
changed = False
i = 0
while i < len(assembly) - 2:
instr = assembly[i]
if isinstance(instr, list):
instr = assembly[i][-1]
while i < len(assembly) - 1:
if assembly[i] in _TERMINAL_OPS:
# find the next jumpdest or sublist
for j in range(i + 1, len(assembly)):
next_is_jumpdest = (
j < len(assembly) - 1
and is_symbol(assembly[j])
and assembly[j + 1] == "JUMPDEST"
)
next_is_list = isinstance(assembly[j], list)
if next_is_jumpdest or next_is_list:
break
else:
# fixup an off-by-one if we made it to the end of the assembly
# without finding an jumpdest or sublist
j = len(assembly)
changed = j > i + 1
del assembly[i + 1 : j]

if assembly[i] in _TERMINAL_OPS and not (
is_symbol(assembly[i + 1]) or isinstance(assembly[i + 1], list)
):
changed = True
del assembly[i + 1]
else:
i += 1
i += 1

return changed

Expand Down Expand Up @@ -1230,7 +1238,6 @@ def assembly_to_evm_with_symbol_map(assembly, pc_ofst=0, insert_compiler_metadat
if is_symbol_map_indicator(assembly[i + 1]):
# Don't increment pc as the symbol itself doesn't go into code
if item in symbol_map:
print(assembly)
raise CompilerPanic(f"duplicate jumpdest {item}")

symbol_map[item] = pc
Expand Down
Loading