Skip to content

feat[venom]: mark loads as non-volatile#4388

Merged
charles-cooper merged 53 commits intovyperlang:masterfrom
charles-cooper:feat/improve-volatile
Apr 28, 2025
Merged

feat[venom]: mark loads as non-volatile#4388
charles-cooper merged 53 commits intovyperlang:masterfrom
charles-cooper:feat/improve-volatile

Conversation

@charles-cooper
Copy link
Copy Markdown
Member

@charles-cooper charles-cooper commented Dec 4, 2024

What I did

How I did it

How to verify it

Commit message

this commit marks load instructions (`mload`, `sload`,
etc) as non-volatile, allowing them to be removed in the
`remove_unused_variables` pass.

this also adds a reachability analysis. this is used to detect which
basic blocks with `msize` can be reached from a memory modifying
instruction

Description for the changelog

Cute Animal Picture

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

this commit marks load instructions (`mload`, `sload`, etc) as
non-volatile, allowing them to be removed in the
`remove_unused_variables` pass.
@codecov
Copy link
Copy Markdown

codecov bot commented Dec 4, 2024

Codecov Report

❌ Patch coverage is 95.45455% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.50%. Comparing base (29dd745) to head (68b5541).
⚠️ Report is 63 commits behind head on master.

Files with missing lines Patch % Lines
vyper/venom/passes/remove_unused_variables.py 94.73% 1 Missing and 1 partial ⚠️
vyper/venom/analysis/reachable.py 95.23% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4388   +/-   ##
=======================================
  Coverage   92.49%   92.50%           
=======================================
  Files         127      128    +1     
  Lines       18371    18432   +61     
  Branches     3180     3191   +11     
=======================================
+ Hits        16992    17050   +58     
- Misses        939      941    +2     
- Partials      440      441    +1     

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

@charles-cooper
Copy link
Copy Markdown
Member Author

@HodanPlodky pointed out offline -- there could be an msize instruction that this optimization interferes with

for idx, inst in enumerate(bb.instructions):
self.instruction_index[inst] = idx
if inst.opcode == "msize" and bb not in self.reads_msize:
self.reads_msize[bb] = idx
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should not this store last msize in basic block instead of first msize. This could be the problem in case which is showed in PR charles-cooper#53

@charles-cooper
Copy link
Copy Markdown
Member Author

i investigated refactoring (or at least trimming down) VOLATILE_INSTRUCTIONS and replacing it with essentially checking if the instruction is a terminator instruction or has write effects. however, it was not that clean since we still need to special-case MSIZE.

another approach which simplifies the code here would be to have a special volatile instruction like mtouch which has an msize write effect, and then have the frontend issue it specifically when it needs to fence msize. i think this approach is OK - i think the frontend only depends on the "memory reservation" capability of mload (actually iload) in one place.

# see py-evm extend_memory: after_size = ceil32(start_position + size)
if immutables_len > 0:
deploy_code.append(["iload", max(0, immutables_len - 32)])
deploy_code.append(["itouch", max(0, immutables_len - 32)])
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

let's update the comment above to reflect the new instruction

"DEBUGGER": (None, 0, 0, 0),
"ILOAD": (None, 1, 1, 6),
"ISTORE": (None, 2, 0, 6),
"ITOUCH": (None, 1, 0, 6),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

shouldn't the cost be higher than iload given it also pops the value from the stack?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

hm, that's true

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

(btw i think issues with gas estimates are at best a UX issue since we don't use them for anything besides providing gas estimates in the ABI and IR printouts)

@charles-cooper charles-cooper force-pushed the feat/improve-volatile branch from 8f49bf7 to 476507c Compare April 9, 2025 08:52
@charles-cooper charles-cooper requested a review from harkal April 28, 2025 10:49
Co-authored-by: Harry Kalogirou <[email protected]>
@charles-cooper charles-cooper merged commit 3b0ee78 into vyperlang:master Apr 28, 2025
162 checks passed
@charles-cooper charles-cooper deleted the feat/improve-volatile branch April 28, 2025 12:53
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.

5 participants