Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add optimization mode to vyper compiler #3493

Merged
merged 21 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add some tests for new pragma directives
  • Loading branch information
charles-cooper committed Jul 10, 2023
commit 214274d0f8a5d90e9c646a8f1e9529f8297e3fd8
77 changes: 76 additions & 1 deletion tests/ast/test_pre_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from vyper.ast.pre_parser import validate_version_pragma
from vyper.ast.pre_parser import pre_parse, validate_version_pragma
from vyper.compiler.settings import OptimizationLevel, Settings
from vyper.exceptions import VersionException

SRC_LINE = (1, 0) # Dummy source line
Expand Down Expand Up @@ -106,3 +107,77 @@ def test_prerelease_invalid_version_pragma(file_version, mock_version):
mock_version(PRERELEASE_COMPILER_VERSION)
with pytest.raises(VersionException):
validate_version_pragma(file_version, (SRC_LINE))


pragma_examples = [
(
"""
""",
Settings(),
),
(
"""
#pragma optimize codesize
""",
Settings(optimize=OptimizationLevel.CODESIZE),
),
(
"""
#pragma optimize none
""",
Settings(optimize=OptimizationLevel.NONE),
),
(
"""
#pragma optimize gas
""",
Settings(optimize=OptimizationLevel.GAS),
),
(
"""
#pragma version 0.3.10
""",
Settings(compiler_version="0.3.10"),
),
(
"""
#pragma evm-version shanghai
""",
Settings(evm_version="shanghai"),
),
(
"""
#pragma optimize codesize
#pragma evm-version shanghai
""",
Settings(evm_version="shanghai", optimize=OptimizationLevel.GAS),
),
(
"""
#pragma version 0.3.10
#pragma evm-version shanghai
""",
Settings(evm_version="shanghai", compiler_version="0.3.10"),
),
(
"""
#pragma version 0.3.10
#pragma optimize gas
""",
Settings(compiler_version="0.3.10", optimize=OptimizationLevel.GAS),
),
(
"""
#pragma version 0.3.10
#pragma evm-version shanghai
#pragma optimize gas
""",
Settings(compiler_version="0.3.10", optimize=OptimizationLevel.GAS, evm_version="shanghai"),
),
]


@pytest.mark.parametrize("code, expected_pragmas", pragma_examples)
def parse_pragmas(code, expected_pragmas):
pragmas, _, _ = pre_parse(code)
assert pragmas == expected_pragmas
61 changes: 57 additions & 4 deletions tests/compiler/test_pre_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from pytest import raises
import pytest

from vyper.exceptions import SyntaxException
from vyper.compiler import compile_code
from vyper.compiler.settings import OptimizationLevel, Settings
from vyper.exceptions import StructureException, SyntaxException


def test_semicolon_prohibited(get_contract):
Expand All @@ -10,7 +12,7 @@ def test() -> int128:
return a + b
"""

with raises(SyntaxException):
with pytest.raises(SyntaxException):
get_contract(code)


Expand Down Expand Up @@ -70,6 +72,57 @@ def test():
assert get_contract(code)


def test_version_pragma2(get_contract):
# new, `#pragma` way of doing things
from vyper import __version__

installed_version = ".".join(__version__.split(".")[:3])

code = f"""
#pragma version {installed_version}

@external
def test():
pass
"""
assert get_contract(code)


def test_evm_version_check(assert_compile_failed):
code = """
#pragma evm-version berlin
"""
assert compile_code(code, settings=Settings(evm_version=None)) is not None
assert compile_code(code, settings=Settings(evm_version="berlin")) is not None
# should fail if compile options indicate different evm version
# from source pragma
with pytest.raises(StructureException):
compile_code(code, settings=Settings(evm_version="shanghai"))


def test_optimization_mode_check():
code = """
#pragma optimize codesize
"""
assert compile_code(code, settings=Settings(optimize=None))
# should fail if compile options indicate different optimization mode
# from source pragma
with pytest.raises(StructureException):
compile_code(code, settings=Settings(optimize=OptimizationLevel.GAS))
with pytest.raises(StructureException):
compile_code(code, settings=Settings(optimize=OptimizationLevel.NONE))


def test_optimization_mode_check_none():
code = """
#pragma optimize none
"""
assert compile_code(code, settings=Settings(optimize=None))
# "none" conflicts with "gas"
with pytest.raises(StructureException):
compile_code(code, settings=Settings(optimize=OptimizationLevel.GAS))


def test_version_empty_version(assert_compile_failed, get_contract):
code = """
#@version
Expand Down Expand Up @@ -110,5 +163,5 @@ def foo():
convert(
"""

with raises(SyntaxException):
with pytest.raises(SyntaxException):
get_contract(code)