Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Nov 10, 2024
1 parent 31c9440 commit 745deef
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
57 changes: 56 additions & 1 deletion tests/unit/compiler/venom/test_literals_codesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from vyper.venom.passes import ReduceLiteralsCodesize


@pytest.mark.parametrize("orig_value", [0xFF << 248, 2**256 - 1])
@pytest.mark.parametrize("orig_value", [0xFFFF << 240, 2**256 - 1])
def test_ff_inversion(orig_value):
ctx = IRContext()
fn = ctx.create_function("_global")
Expand All @@ -20,3 +20,58 @@ def test_ff_inversion(orig_value):

assert bb.instructions[0].opcode == "not"
assert evm_not(bb.instructions[0].operands[0].value) == orig_value


should_not_invert = [1, 0xFE << 248 | (2**248 - 1)] # 0xfeff...ff


@pytest.mark.parametrize("orig_value", should_not_invert)
def test_no_inversion(orig_value):
ctx = IRContext()
fn = ctx.create_function("_global")
bb = fn.get_basic_block()

bb.append_instruction("store", IRLiteral(orig_value))
bb.append_instruction("stop")
ac = IRAnalysesCache(fn)
ReduceLiteralsCodesize(ac, fn).run_pass()

assert bb.instructions[0].opcode == "store"
assert bb.instructions[0].operands[0].value == orig_value


should_shl = [0x01_000000] # saves 3 bytes


@pytest.mark.parametrize("orig_value", should_shl)
def test_shl(orig_value):
ctx = IRContext()
fn = ctx.create_function("_global")
bb = fn.get_basic_block()

bb.append_instruction("store", IRLiteral(orig_value))
bb.append_instruction("stop")
ac = IRAnalysesCache(fn)
ReduceLiteralsCodesize(ac, fn).run_pass()

assert bb.instructions[0].opcode == "shl"
op0, op1 = bb.instructions[0].operands
assert op0.value << op1.value == orig_value


should_not_shl = [0x01_00] # only saves 2 bytes


@pytest.mark.parametrize("orig_value", should_not_shl)
def test_no_shl(orig_value):
ctx = IRContext()
fn = ctx.create_function("_global")
bb = fn.get_basic_block()

bb.append_instruction("store", IRLiteral(orig_value))
bb.append_instruction("stop")
ac = IRAnalysesCache(fn)
ReduceLiteralsCodesize(ac, fn).run_pass()

assert bb.instructions[0].opcode == "store"
assert bb.instructions[0].operands[0].value == orig_value
2 changes: 1 addition & 1 deletion vyper/venom/passes/literals_codesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _process_bb(self, bb):
val = op.value % (2**256)

# transform things like 0xffff...01 to (not 0xfe)
if len(hex(val)) // 2 - len(hex(evm_not(val))) // 2 > 0:
if len(hex(val)) // 2 - len(hex(evm_not(val))) // 2 > 1: # not is 1 byte
inst.opcode = "not"
op.value = evm_not(val)
continue
Expand Down

0 comments on commit 745deef

Please sign in to comment.