Skip to content

Commit

Permalink
fix[codegen]: same symbol jumpdest merge (vyperlang#3982)
Browse files Browse the repository at this point in the history
Update the assembly optimizer to return `False` when trying to 
optimize two consecutive `JUMPDEST`s with the same symbol. 
It used to return `True` when no change was made (resulting in an
infinite loop)

+ unit tests
  • Loading branch information
harkal committed May 1, 2024
1 parent 2b5ce30 commit 2e6fbe7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 7 additions & 0 deletions tests/unit/compiler/asm/test_asm_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from vyper.compiler import compile_code
from vyper.compiler.phases import CompilerData
from vyper.compiler.settings import OptimizationLevel, Settings
from vyper.ir.compile_ir import _merge_jumpdests

codes = [
"""
Expand Down Expand Up @@ -123,3 +124,9 @@ def foo():

assert "unused1()" not in asm
assert "unused2()" not in asm


def test_merge_jumpdests():
asm = ["_sym_label_0", "JUMP", "PUSH0", "_sym_label_0", "JUMPDEST", "_sym_label_0", "JUMPDEST"]

assert _merge_jumpdests(asm) is False, "should not return True as no changes were made"
9 changes: 5 additions & 4 deletions vyper/ir/compile_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,10 +892,11 @@ def _merge_jumpdests(assembly):
# replace all instances of _sym_x with _sym_y
# (except for _sym_x JUMPDEST - don't want duplicate labels)
new_symbol = assembly[i + 2]
for j in range(len(assembly)):
if assembly[j] == current_symbol and i != j:
assembly[j] = new_symbol
changed = True
if new_symbol != current_symbol:
for j in range(len(assembly)):
if assembly[j] == current_symbol and i != j:
assembly[j] = new_symbol
changed = True
elif is_symbol(assembly[i + 2]) and assembly[i + 3] == "JUMP":
# _sym_x JUMPDEST _sym_y JUMP
# replace all instances of _sym_x with _sym_y
Expand Down

0 comments on commit 2e6fbe7

Please sign in to comment.