Skip to content

feat[venom]: fold var-vs-var comparisons and tighten range-based evaluator folding#4827

Open
harkal wants to merge 10 commits intovyperlang:masterfrom
harkal:feat/venom/var_to_var
Open

feat[venom]: fold var-vs-var comparisons and tighten range-based evaluator folding#4827
harkal wants to merge 10 commits intovyperlang:masterfrom
harkal:feat/venom/var_to_var

Conversation

@harkal
Copy link
Copy Markdown
Collaborator

@harkal harkal commented Jan 31, 2026

What I did

I extended Venom algebraic optimization to fold comparisons when both operands are variables, not just literals.

  • Implemented var-vs-var folding for lt, gt, slt, and sgt using variable range analysis.
  • Added boundary-safe logic for unsigned comparisons so we do not fold when ranges span the signed/unsigned boundary.
  • Improved range evaluators to treat variable operands with constant ranges as constants in:
    byte, signextend, mod, div, shl, shr, sar, sdiv, and smod.
  • Added broad test coverage for:
    disjoint ranges, overlapping ranges (no-fold cases), boundary-touching cases,
    signed wraparound edge cases, unsigned negative-vs-nonnegative behavior,
    and assert elimination after comparison folding.
  • Updated byte evaluator tests to reflect constant-range variable index folding.

How I did it

How to verify it

Commit message

- add var-vs-var comparison folding in `AlgebraicOptimizationPass` (`lt`/`gt`/`slt`/`sgt`)
- use range analysis to fold always-true/always-false comparisons
- fix unsigned sign-boundary handling to avoid incorrect folds on spanning ranges
- propagate constant ranges through evaluator variable operands
  (`byte`, `signextend`, `mod`/`div`, `shl`/`shr`/`sar`, `sdiv`/`smod`)
- add unit tests for disjoint/overlap/boundary/wraparound cases and assert-elimination flow
- update byte evaluator tests for constant-range variable index behavior

Description for the changelog

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

@harkal harkal changed the title feat:[venom] var to var feat[venom]: var to var Jan 31, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 31, 2026

Codecov Report

❌ Patch coverage is 81.25000% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.88%. Comparing base (4449393) to head (0072674).
⚠️ Report is 16 commits behind head on master.

Files with missing lines Patch % Lines
vyper/venom/passes/algebraic_optimization.py 72.72% 8 Missing and 7 partials ⚠️
vyper/venom/analysis/variable_range/evaluators.py 92.68% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4827      +/-   ##
==========================================
- Coverage   92.93%   92.88%   -0.05%     
==========================================
  Files         157      157              
  Lines       21943    22039      +96     
  Branches     3927     3965      +38     
==========================================
+ Hits        20392    20472      +80     
- Misses       1026     1032       +6     
- Partials      525      535      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

vyperteam-bot and others added 4 commits January 31, 2026 09:48
- Fix _range_spans_sign_boundary to detect ranges that extend from
  non-negative into high unsigned territory (lo >= 0 and hi > MAX_INT256)
- Add symmetric lo bound check for signed comparisons (lo >= MIN_INT256)
- Add tests for boundary spanning cases and negative-vs-nonnegative
  unsigned comparisons
Range evaluators like _eval_mod, _eval_div, _eval_shr, etc. previously
only worked when the divisor/shift amount was a direct IRLiteral.
This meant that patterns like:
  %div = 10
  %result = mod %x, %div
would produce TOP ranges instead of [0, 9].

Now evaluators first try to get a literal value, and if that fails,
check if the operand has a constant range that can be used instead.
This enables more cases where var-to-var comparison folding can trigger.

Updated test_eval_byte.py to reflect improved constant propagation.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 9, 2026

📊 Bytecode Size Changes (venom)

No changes detected.

Full bytecode sizes

Contract legacy-O2 legacy-Os -O2 -O3 -Os
curvefi/amm/stableswap/implementation/implementation_v_700.vy 24962 23769 19717 19506 17921
curvefi/legacy/CurveStableSwapMetaNG.vy 24952 23578 19332 19188 17597
curvefi/legacy/CurveStableSwapNG.vy 24473 23298 19180 18954 17401
curvefi/amm/stableswap/meta_implementation/meta_implementation_v_700.vy 23610 22805 18607 18271 17257
yearnfi/VaultV3.vy 19972 19063 16650 16452 14042
curvefi/amm/tricryptoswap/implementation/implementation_v_200.vy 20590 19825 16457 16202 15278
curvefi/amm/twocryptoswap/implementation/implementation_v_210.vy 18090 17350 14664 14414 13452
curvefi/legacy/CurveCryptoSwap2.vy 18947 18382 14468 14173 13532
yearnfi/VaultV2.vy 16676 15763 13303 13241 11954
curvefi/amm/stableswap/factory/factory_v_100.vy 14558 13978 12923 12366 11423
curvefi/gauge/child_gauge/implementation/implementation_v_110.vy 12338 11561 10014 9881 8983
curvefi/gauge/child_gauge/implementation/implementation_v_100.vy 12017 11249 9733 9600 8712
curvefi/amm/stableswap/views/views_v_120.vy 12784 12368 9596 9328 8750
curvefi/amm/tricryptoswap/math/math_v_200.vy 11055 10992 9226 8734 7964
curvefi/legacy/CurveCryptoMathOptimized3.vy 11054 10991 9225 8733 7964
curvefi/gauge/child_gauge/implementation/implementation_v_020.vy 10665 9947 8688 8512 7711
curvefi/registries/metaregistry/metaregistry_v_110.vy 7590 6732 6325 5854 5343
curvefi/amm/tricryptoswap/views/views_v_200.vy 7821 7776 6172 6031 5937
curvefi/helpers/stable_swap_meta_zap/stable_swap_meta_zap_v_100.vy 7302 7067 5920 5776 5454
curvefi/helpers/router/router_v_110.vy 6717 6717 5816 5476 5444
curvefi/amm/twocryptoswap/views/views_v_200.vy 6991 6946 5698 5570 5463
curvefi/registries/metaregistry/registry_handlers/stableswap/handler_v_110.vy 6633 6259 5661 5279 5216
curvefi/amm/twocryptoswap/factory/factory_v_200.vy 5540 5252 5265 4623 4219
curvefi/amm/twocryptoswap/math/math_v_210.vy 6666 6666 5221 5221 4647
curvefi/amm/tricryptoswap/factory/factory_v_200.vy 5246 5021 4829 4323 4081
curvefi/gauge/child_gauge/factory/factory_v_201.vy 4844 4547 3980 3962 3525
yearnfi/VaultFactory.vy 3765 3617 3909 2919 2928
curvefi/registries/metaregistry/registry_handlers/tricryptoswap/handler_v_110.vy 4241 3939 3577 3300 3203
curvefi/registries/metaregistry/registry_handlers/twocryptoswap/handler_v_110.vy 4186 3884 3464 3199 3065
curvefi/gauge/child_gauge/factory/factory_v_100.vy 4183 3914 3354 3336 2963
curvefi/registries/address_provider/address_provider_v_201.vy 2973 2782 2652 2645 2365
curvefi/helpers/rate_provider/rate_provider_v_101.vy 3260 3260 2467 2445 2288
curvefi/amm/stableswap/math/math_v_100.vy 3067 3046 2442 2441 2298
curvefi/helpers/rate_provider/rate_provider_v_100.vy 2847 2841 2352 2344 2091
curvefi/helpers/deposit_and_stake_zap/deposit_and_stake_zap_v_100.vy 2322 2316 2036 2007 1799
curvefi/governance/relayer/taiko/relayer_v_001.vy 2068 2064 1795 1788 1669
curvefi/governance/relayer/polygon_cdk/relayer_v_101.vy 1556 1523 1460 1453 1323
curvefi/governance/relayer/arb_orbit/relayer_v_101.vy 1266 1262 1172 1158 1093
curvefi/governance/relayer/op_stack/relayer_v_101.vy 1186 1182 1109 1098 1030
curvefi/governance/relayer/not_rollup/relayer_v_100.vy 1168 1153 1103 1098 1012
curvefi/governance/vault/vault_v_100.vy 964 941 889 889 820
curvefi/governance/agent/agent_v_100.vy 541 541 517 517 463
curvefi/governance/agent/agent_v_101.vy 541 541 517 517 463
curvefi/governance/relayer/relayer_v_100.vy 496 496 465 460 465

@harkal harkal changed the title feat[venom]: var to var feat[venom]: fold var-vs-var comparisons and tighten range-based evaluator folding Feb 9, 2026
@harkal
Copy link
Copy Markdown
Collaborator Author

harkal commented Feb 9, 2026

Review session cmc/feat/venom/var_to_var

@harkal harkal marked this pull request as ready for review February 9, 2026 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants