forked from vyperlang/vyper
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[venom]: add function inliner (vyperlang#4478)
This commit adds a function inliner to the Venom backend, including: - A basic heuristic to determine when inlining is beneficial (placeholder for future sophisticated analysis) - new machinery for global (inter-function) passes over Venom IR to perform inlining - A new function call graph analysis to support inlining - Removal of the variable equivalence analysis (now provided directly by DFG analysis) misc/refactor: - add new `all2` utility function - add dfs preorder traversal to CFG - add ensure_well_formed util to IRBasicBlock - machinery for IRParameter (may be useful in the future) - add `copy()` machinery for venom data structures (may be useful in the future) --------- Co-authored-by: Charles Cooper <[email protected]>
- Loading branch information
1 parent
cd31867
commit a466dc8
Showing
30 changed files
with
712 additions
and
126 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
tests/functional/codegen/calling_convention/test_inlineable_functions.py
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Test functionality of internal functions which may be inlined | ||
""" | ||
# note for refactor: this may be able to be merged with | ||
# calling_convention/test_internal_call.py | ||
|
||
|
||
def test_call_in_call(get_contract): | ||
code = """ | ||
@internal | ||
def _foo(a: uint256,) -> uint256: | ||
return 1 + a | ||
@internal | ||
def _foo2() -> uint256: | ||
return 4 | ||
@external | ||
def foo() -> uint256: | ||
return self._foo(self._foo2()) | ||
""" | ||
|
||
c = get_contract(code) | ||
assert c.foo() == 5 | ||
|
||
|
||
def test_call_in_call_with_raise(get_contract, tx_failed): | ||
code = """ | ||
@internal | ||
def sum(a: uint256) -> uint256: | ||
if a > 1: | ||
return a + 1 | ||
raise | ||
@internal | ||
def middle(a: uint256) -> uint256: | ||
return self.sum(a) | ||
@external | ||
def test(a: uint256) -> uint256: | ||
return self.middle(a) | ||
""" | ||
|
||
c = get_contract(code) | ||
|
||
assert c.test(2) == 3 | ||
|
||
with tx_failed(): | ||
c.test(0) | ||
|
||
|
||
def test_inliner_with_unused_param(get_contract): | ||
code = """ | ||
data: public(uint256) | ||
@internal | ||
def _foo(start: uint256, length: uint256): | ||
self.data = start | ||
@external | ||
def foo(x: uint256, y: uint256): | ||
self._foo(x, y) | ||
""" | ||
|
||
c = get_contract(code) | ||
c.foo(1, 2) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import itertools | ||
|
||
from tests.venom_utils import parse_from_basic_block | ||
from vyper.venom.analysis import DFGAnalysis, IRAnalysesCache | ||
from vyper.venom.basicblock import IRVariable | ||
|
||
|
||
def test_variable_equivalence_dfg_order(): | ||
a_code = """ | ||
main: | ||
%1 = 1 | ||
%2 = %1 | ||
%3 = %2 | ||
""" | ||
# technically invalid code, but variable equivalence should handle | ||
# it either way | ||
b_code = """ | ||
main: | ||
%3 = %2 | ||
%2 = %1 | ||
%1 = 1 | ||
""" | ||
fn1 = parse_from_basic_block(a_code).entry_function | ||
fn2 = parse_from_basic_block(b_code).entry_function | ||
|
||
dfg1 = IRAnalysesCache(fn1).request_analysis(DFGAnalysis) | ||
dfg2 = IRAnalysesCache(fn2).request_analysis(DFGAnalysis) | ||
|
||
vars_ = map(IRVariable, ("%1", "%2", "%3")) | ||
for var1, var2 in itertools.combinations(vars_, 2): | ||
assert dfg1.are_equivalent(var1, var2) | ||
assert dfg2.are_equivalent(var1, var2) |
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
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
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
Oops, something went wrong.