Skip to content

Commit

Permalink
test ge le improvement + signess fix
Browse files Browse the repository at this point in the history
  • Loading branch information
HodanPlodky committed Jan 14, 2025
1 parent 2472328 commit 16622be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
18 changes: 13 additions & 5 deletions tests/unit/compiler/venom/test_sccp_algebraic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
RemoveUnusedVariablesPass,
StoreElimination,
)
from vyper.venom.passes.sccp.eval import signed_to_unsigned

"""
Test abstract binop+unop optimizations in sccp and algebraic optimizations pass
Expand Down Expand Up @@ -457,11 +458,18 @@ def test_comparison_almost_always():
_sccp_algebraic_runner(pre, post)


@pytest.mark.parametrize("val", (100, 2, 3))
@pytest.mark.parametrize("val", (100, 2, 3, -100))
def test_comparison_ge_le(val):
# iszero(x < 100) => 99 <= x
# iszero(x > 100) => 101 >= x

up = val + 1
down = val - 1

val = signed_to_unsigned(val, 256)
up = signed_to_unsigned(up, 256)
down = signed_to_unsigned(down, 256)

pre = f"""
_global:
%par = param
Expand All @@ -478,10 +486,10 @@ def test_comparison_ge_le(val):
post = f"""
_global:
%par = param
%1 = lt {val - 1}, %par
%3 = gt {val + 1}, %par
%5 = slt {val - 1}, %par
%7 = sgt {val + 1}, %par
%1 = lt {down}, %par
%3 = gt {up}, %par
%5 = slt {down}, %par
%7 = sgt {up}, %par
return %1, %3, %5, %7
"""

Expand Down
3 changes: 2 additions & 1 deletion vyper/venom/passes/algebraic_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ def _rewrite_ge_le(self, inst: IRInstruction):

# this can happen for cases like `lt x 0` which get reduced in SCCP.
# don't handle them here, just return
if _wrap256(val, unsigned) != val:
# unsigned is true since we already converted it if needed
if _wrap256(val, unsigned=True) != val:
return

self.updater._update(inst, new_opcode, [IRLiteral(val), operands[1]])
Expand Down

0 comments on commit 16622be

Please sign in to comment.