-
-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: initcode codesize regression (#3450)
this commit fixes a regression in c202c4e. the commit message states that we rely on the dead code eliminator to prune unused internal functions in the initcode, but the dead code eliminator does not prune dead code in all cases, including nested internal functions and loops. this commit reintroduces the reachability analysis in `vyper/codegen/module.py` as a stopgap until the dead code eliminator is more robust.
- Loading branch information
1 parent
71c8e55
commit 510125e
Showing
4 changed files
with
100 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,102 @@ | ||
from vyper.compiler.phases import CompilerData | ||
import pytest | ||
|
||
from vyper.compiler.phases import CompilerData | ||
|
||
def test_dead_code_eliminator(): | ||
code = """ | ||
codes = [ | ||
""" | ||
s: uint256 | ||
@internal | ||
def foo(): | ||
def ctor_only(): | ||
self.s = 1 | ||
@internal | ||
def qux(): | ||
def runtime_only(): | ||
self.s = 2 | ||
@external | ||
def bar(): | ||
self.runtime_only() | ||
@external | ||
def __init__(): | ||
self.ctor_only() | ||
""", | ||
# code with nested function in it | ||
""" | ||
s: uint256 | ||
@internal | ||
def runtime_only(): | ||
self.s = 1 | ||
@internal | ||
def foo(): | ||
self.runtime_only() | ||
@internal | ||
def ctor_only(): | ||
self.s += 1 | ||
@external | ||
def bar(): | ||
self.foo() | ||
@external | ||
def __init__(): | ||
self.qux() | ||
self.ctor_only() | ||
""", | ||
# code with loop in it, these are harder for dead code eliminator | ||
""" | ||
s: uint256 | ||
@internal | ||
def ctor_only(): | ||
self.s = 1 | ||
@internal | ||
def runtime_only(): | ||
for i in range(10): | ||
self.s += 1 | ||
@external | ||
def bar(): | ||
self.runtime_only() | ||
@external | ||
def __init__(): | ||
self.ctor_only() | ||
""", | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("code", codes) | ||
def test_dead_code_eliminator(code): | ||
c = CompilerData(code, no_optimize=True) | ||
initcode_asm = [i for i in c.assembly if not isinstance(i, list)] | ||
runtime_asm = c.assembly_runtime | ||
|
||
foo_label = "_sym_internal_foo___" | ||
qux_label = "_sym_internal_qux___" | ||
ctor_only_label = "_sym_internal_ctor_only___" | ||
runtime_only_label = "_sym_internal_runtime_only___" | ||
|
||
# qux reachable from unoptimized initcode, foo not reachable. | ||
assert ctor_only_label + "_deploy" in initcode_asm | ||
assert runtime_only_label + "_deploy" not in initcode_asm | ||
|
||
# all the labels should be in all the unoptimized asms | ||
for s in (foo_label, qux_label): | ||
assert s + "_deploy" in initcode_asm | ||
# all labels should be in unoptimized runtime asm | ||
for s in (ctor_only_label, runtime_only_label): | ||
assert s + "_runtime" in runtime_asm | ||
|
||
c = CompilerData(code, no_optimize=False) | ||
initcode_asm = [i for i in c.assembly if not isinstance(i, list)] | ||
runtime_asm = c.assembly_runtime | ||
|
||
# qux should not be in runtime code | ||
# ctor only label should not be in runtime code | ||
for instr in runtime_asm: | ||
if isinstance(instr, str): | ||
assert not instr.startswith(qux_label), instr | ||
assert not instr.startswith(ctor_only_label), instr | ||
|
||
# foo should not be in initcode asm | ||
# runtime only label should not be in initcode asm | ||
for instr in initcode_asm: | ||
if isinstance(instr, str): | ||
assert not instr.startswith(foo_label), instr | ||
assert not instr.startswith(runtime_only_label), instr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters