Skip to content

Commit

Permalink
Revert "feat: more arithmetic optimizations (#2647)"
Browse files Browse the repository at this point in the history
This reverts commit efe1dbe.
  • Loading branch information
charles-cooper authored May 25, 2022
1 parent 42a372e commit 874bbcc
Show file tree
Hide file tree
Showing 25 changed files with 471 additions and 592 deletions.
4 changes: 3 additions & 1 deletion examples/auctions/blind_auction.vy
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ def reveal(_numBids: int128, _values: uint256[128], _fakes: bool[128], _secrets:

# Bid was not actually revealed
# Do not refund deposit
assert blindedBid == bidToCheck.blindedBid
if (blindedBid != bidToCheck.blindedBid):
assert 1 == 0
continue

# Add deposit to refund if bid was indeed revealed
refund += bidToCheck.deposit
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def test_ir_compile_fail(bad_ir, get_contract_from_ir, assert_compile_failed):

valid_list = [
["pass"],
["assert", ["slt", ["mload", 0], 300]],
["assert", ["sgt", ["mload", 0], -1]],
["assert", ["gt", 1, ["mload", 0]]],
["assert", ["ge", ["mload", 0], 0]],
["clamplt", ["mload", 0], 300],
["clampgt", ["mload", 0], -1],
["uclampgt", 1, ["mload", 0]],
["uclampge", ["mload", 0], 0],
]


Expand Down
21 changes: 21 additions & 0 deletions tests/compiler/LLL/test_optimize_lll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest

from vyper.codegen.ir_node import IRnode
from vyper.ir import optimizer

optimize_list = [
(["eq", 1, 0], ["iszero", 1]),
(["eq", 1, 2], ["eq", 1, 2]), # noop
(["if", ["eq", 1, 2], "pass"], ["if", ["iszero", ["xor", 1, 2]], "pass"]),
(["assert", ["eq", 1, 2]], ["assert", ["iszero", ["xor", 1, 2]]]),
(["mstore", 0, ["eq", 1, 2]], ["mstore", 0, ["eq", 1, 2]]), # noop
]


@pytest.mark.parametrize("ir", optimize_list)
def test_ir_compile_fail(ir):
optimized = optimizer.optimize(IRnode.from_list(ir[0]))
optimized.repr_show_gas = True
hand_optimized = IRnode.from_list(ir[1])
hand_optimized.repr_show_gas = True
assert optimized == hand_optimized
File renamed without changes.
File renamed without changes.
147 changes: 0 additions & 147 deletions tests/compiler/ir/test_optimize_ir.py

This file was deleted.

94 changes: 94 additions & 0 deletions tests/compiler/test_clamps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
def test_uclamplt(get_contract_from_ir, assert_compile_failed):
ir = ["uclamplt", 2, 1]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["uclamplt", 1, 1]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["uclamplt", 0, 1]
get_contract_from_ir(ir)


def test_uclample(get_contract_from_ir, assert_compile_failed):
ir = ["uclample", 2, 1]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["uclample", 1, 1]
get_contract_from_ir(ir)
ir = ["uclample", 0, 1]
get_contract_from_ir(ir)


def test_uclampgt(get_contract_from_ir, assert_compile_failed):
ir = ["uclampgt", 1, 2]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["uclampgt", 1, 1]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["uclampgt", 1, 0]
get_contract_from_ir(ir)


def test_uclampge(get_contract_from_ir, assert_compile_failed):
ir = ["uclampge", 1, 2]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["uclampge", 1, 1]
get_contract_from_ir(ir)
ir = ["uclampge", 1, 0]
get_contract_from_ir(ir)


def test_uclamplt_and_clamplt(get_contract_from_ir, assert_compile_failed):
ir = ["uclamplt", 2, 1]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["uclamplt", 1, 1]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["uclamplt", 0, 1]
get_contract_from_ir(ir)
ir = ["clamplt", 2, 1]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["clamplt", 1, 1]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["clamplt", 0, 1]
get_contract_from_ir(ir)


def test_uclample_clample(get_contract_from_ir, assert_compile_failed):
ir = ["uclample", 2, 1]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["uclample", 1, 1]
get_contract_from_ir(ir)
ir = ["uclample", 0, 1]
get_contract_from_ir(ir)
ir = ["clample", 2, 1]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["clample", 1, 1]
get_contract_from_ir(ir)
ir = ["clample", 0, 1]
get_contract_from_ir(ir)


def test_uclampgt_and_clampgt(get_contract_from_ir, assert_compile_failed):
ir = ["uclampgt", 1, 2]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["uclampgt", 1, 1]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["uclampgt", 1, 0]
get_contract_from_ir(ir)
ir = ["clampgt", 1, 2]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["clampgt", 1, 1]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["clampgt", 1, 0]
get_contract_from_ir(ir)


def test_uclampge_and_clampge(get_contract_from_ir, assert_compile_failed):
ir = ["uclampge", 1, 2]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["uclampge", 1, 1]
get_contract_from_ir(ir)
ir = ["uclampge", 1, 0]
get_contract_from_ir(ir)
ir = ["clampge", 1, 2]
assert_compile_failed(lambda: get_contract_from_ir(ir), Exception)
ir = ["clampge", 1, 1]
get_contract_from_ir(ir)
ir = ["clampge", 1, 0]
get_contract_from_ir(ir)
2 changes: 1 addition & 1 deletion tests/parser/features/test_assert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_assert_refund(w3, get_contract_with_gas_estimation, assert_tx_failed):
code = """
@external
def foo():
raise
assert 1 == 2
"""
c = get_contract_with_gas_estimation(code)
a0 = w3.eth.accounts[0]
Expand Down
2 changes: 1 addition & 1 deletion tests/parser/features/test_assert_unreachable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ def test_assure_refund(w3, get_contract):
code = """
@external
def foo():
assert msg.sender != msg.sender, UNREACHABLE
assert 1 == 2, UNREACHABLE
"""

c = get_contract(code)
Expand Down
3 changes: 2 additions & 1 deletion tests/parser/functions/test_raw_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ def test_multiple_levels2(assert_tx_failed, get_contract_with_gas_estimation):
inner_code = """
@external
def returnten() -> int128:
raise
assert False
return 10
"""

c = get_contract_with_gas_estimation(inner_code)
Expand Down
4 changes: 2 additions & 2 deletions tests/parser/syntax/test_unbalanced_return.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test() -> int128:
if True:
return 0
else:
assert msg.sender != msg.sender
assert False
""",
FunctionDeclarationException,
),
Expand Down Expand Up @@ -108,7 +108,7 @@ def test() -> int128:
if 1 == 1 :
return 1
else:
assert msg.sender != msg.sender
assert False
return 0
""",
"""
Expand Down
10 changes: 5 additions & 5 deletions vyper/builtin_functions/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
IRnode,
bytes_clamp,
bytes_data_ptr,
clamp,
clamp_basetype,
get_bytearray_length,
int_clamp,
Expand Down Expand Up @@ -120,14 +119,15 @@ def _clamp_numeric_convert(arg, arg_bounds, out_bounds, arg_is_signed):
if arg_lo < out_lo:
# if not arg_is_signed, arg_lo is 0, so this branch cannot be hit
assert arg_is_signed, "bad assumption in numeric convert"
arg = clamp("sge", arg, out_lo)
CLAMPGE = "clampge"
arg = [CLAMPGE, arg, out_lo]

if arg_hi > out_hi:
# out_hi must be smaller than MAX_UINT256, so clample makes sense.
# add an assertion, just in case this assumption ever changes.
assert out_hi < 2 ** 256 - 1, "bad assumption in numeric convert"
CLAMP_OP = "sle" if arg_is_signed else "le"
arg = clamp(CLAMP_OP, arg, out_hi)
CLAMPLE = "clample" if arg_is_signed else "uclample"
arg = [CLAMPLE, arg, out_hi]

return arg

Expand Down Expand Up @@ -194,7 +194,7 @@ def _int_to_int(arg, out_typ):

else:
# note: this also works for out_bits == 256.
arg = clamp("sge", arg, 0)
arg = IRnode.from_list(["clampge", arg, 0])

elif not arg_info.is_signed and out_info.is_signed:
# e.g. (uclample (uclampge arg 0) (2**127 - 1))
Expand Down
Loading

0 comments on commit 874bbcc

Please sign in to comment.