Skip to content

feat[venom]: add loop analysis and LICM pass#4901

Open
frederikgramkortegaard wants to merge 8 commits into
vyperlang:masterfrom
frederikgramkortegaard:feat/venom/loop-analysis
Open

feat[venom]: add loop analysis and LICM pass#4901
frederikgramkortegaard wants to merge 8 commits into
vyperlang:masterfrom
frederikgramkortegaard:feat/venom/loop-analysis

Conversation

@frederikgramkortegaard

@frederikgramkortegaard frederikgramkortegaard commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

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:

  1. No write effects (except MSIZE)
  2. No read/write conflicts with loop (e.g. sload is safe if no sstore in loop)
  3. Not a phi or terminator
  4. All operands defined outside the loop or by other invariants
  5. Its block dominates all loop exits (unless 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

pytest tests/unit/compiler/venom/test_loop_analysis.py tests/unit/compiler/venom/test_licm.py -v

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

Copy link
Copy Markdown

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: 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".

Comment thread vyper/venom/passes/licm.py Outdated
Comment thread vyper/venom/passes/licm.py Outdated
Comment thread vyper/venom/analysis/loop.py
@codecov

codecov Bot commented Mar 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.26752% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.72%. Comparing base (e57e6ac) to head (01f34e5).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
vyper/venom/passes/licm.py 94.11% 2 Missing and 3 partials ⚠️
vyper/venom/analysis/loop.py 94.28% 2 Missing and 2 partials ⚠️
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.
📢 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.

@frederikgramkortegaard

Copy link
Copy Markdown
Contributor Author

@codex review

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

Copy link
Copy Markdown

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: 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".

Comment thread vyper/venom/passes/licm.py
Comment thread vyper/venom/analysis/loop.py Outdated
@frederikgramkortegaard frederikgramkortegaard marked this pull request as draft March 31, 2026 15:04
@frederikgramkortegaard frederikgramkortegaard marked this pull request as ready for review March 31, 2026 15:54

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

Copy link
Copy Markdown

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: 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".

Comment thread vyper/venom/passes/licm.py Outdated
Frederik Gram Kortegaard added 7 commits May 13, 2026 02:02
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.
@charles-cooper charles-cooper requested a review from harkal May 21, 2026 09:12
@charles-cooper

Copy link
Copy Markdown
Member

@frederikgramkortegaard for this repo please do not force push branches under review as it invalidates prior reviews

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.

3 participants