From 1728bee6050fe10a501e33c096e91294d012ab4d Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Tue, 24 Dec 2024 09:08:17 -0500 Subject: [PATCH] test that parsed venom can go to bytecode --- tests/functional/venom/test_venom_repr.py | 39 +++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/tests/functional/venom/test_venom_repr.py b/tests/functional/venom/test_venom_repr.py index c25ce381d8..85f71d055a 100644 --- a/tests/functional/venom/test_venom_repr.py +++ b/tests/functional/venom/test_venom_repr.py @@ -1,9 +1,12 @@ import glob +import textwrap import pytest from tests.venom_utils import assert_ctx_eq, parse_venom from vyper.compiler import compile_code +from vyper.compiler.phases import generate_bytecode +from vyper.venom import generate_assembly_experimental, run_passes_on from vyper.venom.context import IRContext """ @@ -16,15 +19,47 @@ def get_example_vy_filenames(): @pytest.mark.parametrize("vy_filename", get_example_vy_filenames()) -def test_round_trip(vy_filename, optimize, request): +def test_round_trip_example(vy_filename, optimize): + """ + Check all examples round trip + """ path = f"examples/{vy_filename}" with open(path) as f: vyper_source = f.read() - out = compile_code(vyper_source, output_formats=["bb_runtime"]) + _round_trip_helper(vyper_source, optimize) + +vyper_sources = [""" + @external + def _loop() -> uint256: + res: uint256 = 9 + for i: uint256 in range(res, bound=10): + res = res + i + return res + """] + +@pytest.mark.parametrize("vyper_source", vyper_sources) +def test_round_trip_source(vyper_source, optimize): + vyper_source = textwrap.dedent(vyper_source) + _round_trip_helper(vyper_source, optimize) + + +def _round_trip_helper(vyper_source, optimize): + out = compile_code(vyper_source, output_formats=["bb_runtime", "bytecode_runtime"]) bb_runtime = out["bb_runtime"] venom_code = IRContext.__repr__(bb_runtime) ctx = parse_venom(venom_code) assert_ctx_eq(bb_runtime, ctx) + + # check it's valid to run venom passes+analyses + run_passes_on(ctx, optimize) + + asm = generate_assembly_experimental(ctx) + bytecode = generate_bytecode(asm, compiler_metadata=None) + bytecode = f"0x{bytecode.hex()}" + + # TODO investigate: bytecodes should be equal (even without + # `run_passes_on`) but not for some reason + # assert bytecode == out["bytecode_runtime"]