feat[venom]: add loop analysis and LICM pass#4901
feat[venom]: add loop analysis and LICM pass#4901frederikgramkortegaard wants to merge 8 commits into
Conversation
2d5645f to
b68dca6
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2d5645f6f2
ℹ️ 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".
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4901 +/- ##
==========================================
+ Coverage 92.71% 92.72% +0.01%
==========================================
Files 187 189 +2
Lines 27626 27783 +157
Branches 4776 4815 +39
==========================================
+ Hits 25614 25763 +149
- Misses 1363 1366 +3
- Partials 649 654 +5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
8187aa9 to
2bf1956
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2bf195627c
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7b9c9208f6
ℹ️ 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".
Add infrastructure for loop detection and Loop Invariant Code Motion (LICM) optimization to the Venom IR. Loop analysis (`vyper/venom/analysis/loop.py`) identifies natural loops by finding back edges in the CFG (edges where the target dominates the source) and computing the loop body via backward reachability. Provides utilities for querying exit nodes, preheaders, and whether variables are defined within a loop. LICM (`vyper/venom/passes/licm.py`) hoists loop-invariant instructions to a preheader block, reducing redundant computation. An instruction is hoistable if: (1) it has no side effects, (2) all operands are defined outside the loop or by other invariants, and (3) its block dominates all loop exits to avoid speculative execution. The dominance check (3) is conservative - it prevents hoisting from blocks that don't dominate exits, avoiding extra work when loops run zero iterations. An `allow_speculative` flag relaxes this for cases where minimal extra work is acceptable. Future work could integrate range analysis to prove loops execute at least once, enabling more aggressive hoisting. Preheader insertion handles loops with multiple outside predecessors by creating a new block that aggregates them, properly updating phi nodes in both the new preheader and the loop header.
- Guard against zero outside predecessors in preheader creation - Add effects-based hoistability checks (read/write conflicts) - Skip unreachable blocks in loop back-edge detection - Add tests for sload hoisting with/without sstore
Don't create preheaders in LICM - skip loops without valid preheaders instead. This avoids CFG modification complexity that caused malformed phi operands when processing multiple loops with stale cached analyses. The approach now matches PR vyperlang#4819's simpler design: loops must already have a valid preheader (single outside predecessor whose only successor is the loop header) for LICM to apply.
Use has_outputs and get_outputs() instead of output property to avoid assertion error on instructions like nop that have no outputs.
MSIZE effect was removed in b7db6bb (sunset msize instruction). The memtop instruction now only has a MEMORY read effect, so the write effects check no longer needs to mask out MSIZE.
59a6daf to
99f3487
Compare
|
@frederikgramkortegaard for this repo please do not force push branches under review as it invalidates prior reviews |
What I did
Add loop analysis and LICM (Loop Invariant Code Motion) to Venom IR.
How I did it
Loop analysis (
vyper/venom/analysis/loop.py): finds natural loops by detecting back edges in the CFG (where target dominates source), then computes loop bodies via backward reachability. Exposes helpers for exit nodes, preheaders, and checking if a variable is defined inside a loop. Skips unreachable blocks to avoid dominance lookup errors.LICM (
vyper/venom/passes/licm.py): hoists loop-invariant instructions to a preheader block. An instruction is hoistable if:allow_speculative=True)Loops without a valid preheader (single outside predecessor whose only successor is the loop header) are skipped. This avoids CFG modification complexity - most Vyper-generated loops already have natural preheaders.
Invalidates CFG, DFG, Liveness, and Loop analyses after mutations.
Integration: Added to O2, O3, Os optimization levels (runs after MakeSSA).
How to verify it