From 437b086f6ce540d4fea90f649ffb561571b54858 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Tue, 7 May 2024 09:03:25 -0400 Subject: [PATCH 1/3] additionally test venom with -Ocodesize and -Ogas --- .github/workflows/test.yml | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5f02444af1..3162b7ae6f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -120,7 +120,20 @@ jobs: evm-version: shanghai evm-backend: revm experimental-codegen: true - # TODO: test experimental_codegen + -Ocodesize + + - python-version: ["3.11", "311"] + opt-mode: codesize + debug: false + evm-version: shanghai + evm-backend: revm + experimental-codegen: true + + - python-version: ["3.11", "311"] + opt-mode: none + debug: false + evm-version: shanghai + evm-backend: revm + experimental-codegen: true # run across other python versions. we don't really need to run all # modes across all python versions - one is enough From 5cadace3a7c2a5cfdd618f52fba478f269297d67 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Tue, 7 May 2024 09:18:44 -0400 Subject: [PATCH 2/3] fix jump map --- tests/unit/compiler/test_source_map.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/unit/compiler/test_source_map.py b/tests/unit/compiler/test_source_map.py index e4d5e69770..d99b546403 100644 --- a/tests/unit/compiler/test_source_map.py +++ b/tests/unit/compiler/test_source_map.py @@ -32,14 +32,19 @@ def foo(a: uint256) -> int128: """ -def test_jump_map(optimize): +def test_jump_map(optimize, experimental_codegen): source_map = compile_code(TEST_CODE, output_formats=["source_map"])["source_map"] pos_map = source_map["pc_pos_map"] jump_map = source_map["pc_jump_map"] expected_jumps = 1 if optimize == OptimizationLevel.NONE: - expected_jumps = 3 # some jumps get optimized out when optimizer is on + # some jumps which don't get optimized out when optimizer is off + # (slightly different behavior depending if venom pipeline is enabled): + if not experimental_codegen: + expected_jumps = 3 + else: + expected_jumps = 2 assert len([v for v in jump_map.values() if v == "o"]) == expected_jumps assert len([v for v in jump_map.values() if v == "i"]) == 2 From be467b35945306ca89dc4927d354b607b2a107fd Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Tue, 7 May 2024 15:17:21 -0400 Subject: [PATCH 3/3] fix exp eval in sccp --- vyper/ast/nodes.py | 1 + vyper/venom/passes/sccp/eval.py | 29 ++++++++++++++--------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/vyper/ast/nodes.py b/vyper/ast/nodes.py index ca101b61d5..b4042c75a7 100644 --- a/vyper/ast/nodes.py +++ b/vyper/ast/nodes.py @@ -1104,6 +1104,7 @@ def _op(self, left, right): # r > ln(2 ** 256) / ln(l) if right > math.log(decimal.Decimal(2**257)) / math.log(decimal.Decimal(left)): raise InvalidLiteral("Out of bounds", self) + return int(left**right) diff --git a/vyper/venom/passes/sccp/eval.py b/vyper/venom/passes/sccp/eval.py index 8acca039c0..cd4243e998 100644 --- a/vyper/venom/passes/sccp/eval.py +++ b/vyper/venom/passes/sccp/eval.py @@ -1,7 +1,14 @@ import operator from typing import Callable -from vyper.utils import SizeLimits, evm_div, evm_mod, signed_to_unsigned, unsigned_to_signed +from vyper.utils import ( + SizeLimits, + evm_div, + evm_mod, + evm_pow, + signed_to_unsigned, + unsigned_to_signed, +) from vyper.venom.basicblock import IROperand @@ -30,9 +37,11 @@ def wrapper(ops: list[IROperand]) -> int: def _wrap_binop(operation): def wrapper(ops: list[IROperand]) -> int: - first = ops[1].value - second = ops[0].value - return (int(operation(first, second))) & SizeLimits.MAX_UINT256 + first = _signed_to_unsigned(ops[1].value) + second = _signed_to_unsigned(ops[0].value) + ret = operation(first, second) + assert isinstance(ret, int) + return ret & SizeLimits.MAX_UINT256 return wrapper @@ -90,16 +99,6 @@ def _evm_not(ops: list[IROperand]) -> int: return SizeLimits.MAX_UINT256 ^ value -def _evm_exp(ops: list[IROperand]) -> int: - base = ops[1].value - exponent = ops[0].value - - if base == 0: - return 0 - - return pow(base, exponent, SizeLimits.CEILING_UINT256) - - ARITHMETIC_OPS: dict[str, Callable[[list[IROperand]], int]] = { "add": _wrap_binop(operator.add), "sub": _wrap_binop(operator.sub), @@ -108,7 +107,7 @@ def _evm_exp(ops: list[IROperand]) -> int: "sdiv": _wrap_signed_binop(evm_div), "mod": _wrap_binop(evm_mod), "smod": _wrap_signed_binop(evm_mod), - "exp": _evm_exp, + "exp": _wrap_binop(evm_pow), "eq": _wrap_binop(operator.eq), "ne": _wrap_binop(operator.ne), "lt": _wrap_binop(operator.lt),