Skip to content

Comments

feat[venom]: better mem elision#4851

Open
HodanPlodky wants to merge 11 commits intovyperlang:masterfrom
HodanPlodky:feat/venom/better-mem-elision
Open

feat[venom]: better mem elision#4851
HodanPlodky wants to merge 11 commits intovyperlang:masterfrom
HodanPlodky:feat/venom/better-mem-elision

Conversation

@HodanPlodky
Copy link
Collaborator

What I did

Improved memory copy elision to achieve detection of full copies between different allocations. This can at the end cause total elimination of that allocation if it is only used temporarily.

Example:

before

main:
        %dst = alloca 1, 64
        %ret_buf = alloca 2, 64
        invoke @fn, %ret_buf
        mcopy %dst, %ret_buf, 64
        %res = mload %dst
        sink %res

after

main:
        ; this alloca is later removed by
        ; RemoveUnusedVariablesPass
        %dst = alloca 1, 64
        %ret_buf = alloca 2, 64
        invoke @fn, %ret_buf
        nop
        %res = mload %ret_buf
        sink %res

How I did it

How to verify it

Commit message

Improvements for MemoryCopyElisionPass to allow detection and elimination of full copies of allocation

Description for the changelog

Cute Animal Picture

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

@HodanPlodky HodanPlodky changed the title Feat/venom/better mem elision feat[venom]: better mem elision Feb 18, 2026
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c13bc52c04

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +296 to +297
if read_loc.alloca.alloca_size != read_loc.size:
return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Require full destination coverage before translation

The translation logic only checks that the source allocation is fully copied (read_loc.alloca.alloca_size == read_loc.size) before mapping write_loc.alloca to the source. If the destination allocation is larger than the copied size, later reads from the uncopied portion of destination are still rewritten to the source by _try_update_from_translates, which can return incorrect data. A full-allocation translation should only be created when the destination is fully covered as well (or guarded by range checks on rewritten reads).

Useful? React with 👍 / 👎.

Comment on lines 134 to 135
if Effects.MEMORY in inst.get_read_effects():
self._try_update_from_translates(inst)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Invalidate translations before rewriting reads after volatile writes

This pass rewrites every memory-read instruction using total_translation before opcode dispatch, but the _volatile_memory(inst) path in the same loop does not clear total_translation. As a result, a volatile writer such as invoke/call can clobber translated allocations, and a later read in the same block may still be redirected through stale mapping, producing wrong values. Translation state should be invalidated on volatile memory writes just like copy state.

Useful? React with 👍 / 👎.

@github-actions
Copy link

github-actions bot commented Feb 18, 2026

📊 Bytecode Size Changes (venom)

Contract legacy-O2 legacy-Os -O2 -O3 -Os
curvefi/amm/stableswap/implementation/implementation_v_700.vy 24962 23769 19789 19566 (🟢-12) 17993
curvefi/legacy/CurveStableSwapMetaNG.vy 24952 23578 19424 19251 (🟢-29) 17689
curvefi/legacy/CurveStableSwapNG.vy 24473 23298 19252 19009 (🟢-17) 17473
curvefi/amm/tricryptoswap/implementation/implementation_v_200.vy 20590 19825 16493 16237 (🟢-1) 15314
curvefi/amm/twocryptoswap/implementation/implementation_v_210.vy 18090 17350 14692 14417 (🟢-25) 13480
curvefi/amm/tricryptoswap/math/math_v_200.vy 11055 10992 9226 8726 (🟢-8) 7964
curvefi/legacy/CurveCryptoMathOptimized3.vy 11054 10991 9225 8725 (🟢-8) 7964

Full bytecode sizes

Contract legacy-O2 legacy-Os -O2 -O3 -Os
curvefi/amm/stableswap/implementation/implementation_v_700.vy 24962 23769 19789 19566 17993
curvefi/legacy/CurveStableSwapMetaNG.vy 24952 23578 19424 19251 17689
curvefi/legacy/CurveStableSwapNG.vy 24473 23298 19252 19009 17473
curvefi/amm/stableswap/meta_implementation/meta_implementation_v_700.vy 23610 22805 18702 18366 17352
yearnfi/VaultV3.vy 19972 19063 16662 16464 14054
curvefi/amm/tricryptoswap/implementation/implementation_v_200.vy 20590 19825 16493 16237 15314
curvefi/amm/twocryptoswap/implementation/implementation_v_210.vy 18090 17350 14692 14417 13480
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 12927 12370 11427
curvefi/gauge/child_gauge/implementation/implementation_v_110.vy 12338 11561 10016 9883 8985
curvefi/gauge/child_gauge/implementation/implementation_v_100.vy 12017 11249 9735 9602 8714
curvefi/amm/stableswap/views/views_v_120.vy 12784 12368 9596 9328 8750
curvefi/amm/tricryptoswap/math/math_v_200.vy 11055 10992 9226 8726 7964
curvefi/legacy/CurveCryptoMathOptimized3.vy 11054 10991 9225 8725 7964
curvefi/gauge/child_gauge/implementation/implementation_v_020.vy 10665 9947 8690 8514 7713
curvefi/registries/metaregistry/metaregistry_v_110.vy 7590 6732 6329 5858 5347
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 5818 5478 5446
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 5267 4625 4221
curvefi/amm/twocryptoswap/math/math_v_210.vy 6666 6666 5221 5221 4647
curvefi/amm/tricryptoswap/factory/factory_v_200.vy 5246 5021 4831 4325 4083
curvefi/gauge/child_gauge/factory/factory_v_201.vy 4844 4547 3995 3977 3540
yearnfi/VaultFactory.vy 3765 3617 3917 2927 2936
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 2469 2447 2290
curvefi/amm/stableswap/math/math_v_100.vy 3067 3046 2442 2441 2298
curvefi/helpers/rate_provider/rate_provider_v_100.vy 2847 2841 2354 2346 2093
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 893 893 824
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

@codecov
Copy link

codecov bot commented Feb 18, 2026

Codecov Report

❌ Patch coverage is 86.02941% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.91%. Comparing base (a714053) to head (ac07cd6).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
vyper/venom/passes/memory_copy_elision.py 86.06% 8 Missing and 9 partials ⚠️
vyper/venom/analysis/base_ptr_analysis.py 85.71% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4851      +/-   ##
==========================================
- Coverage   92.93%   92.91%   -0.03%     
==========================================
  Files         157      157              
  Lines       21961    22090     +129     
  Branches     3931     3968      +37     
==========================================
+ Hits        20410    20524     +114     
- Misses       1026     1033       +7     
- Partials      525      533       +8     

☔ 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.

from vyper.venom.basicblock import IRBasicBlock, IRInstruction, IRVariable, IRLiteral
from vyper.venom.effects import Effects, to_addr_space
from vyper.venom.memory_location import MemoryLocation
from vyper.venom.memory_location import MemoryLocation, Allocation, get_memory_read_op, update_read_location, update_write_location

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'get_memory_read_op' is not used.
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.

1 participant