Skip to content

perf(interp): fold loop branch legs into native back-edges#160

Merged
siyul-park merged 7 commits into
mainfrom
perf/jit-loop-leg-fold
Jul 18, 2026
Merged

perf(interp): fold loop branch legs into native back-edges#160
siyul-park merged 7 commits into
mainfrom
perf/jit-loop-leg-fold

Conversation

@siyul-park

@siyul-park siyul-park commented Jul 18, 2026

Copy link
Copy Markdown
Owner

Record native loop fallback exits as branches so loop anchors recompile
through the side-exit machinery, fold partial legs whose cut lands on the
plan's own header into the committing-flush native back-edge, and share
identical scheduled continuations so folded legs that cycle through a loop
nest converge instead of exhausting the continuation limit. Handle the
trapNone return a folded RETURN or completed leg produces in the loop
wrapper: tear the frame down like call() for a function loop and mark the
module frame exhausted like start() for top-level code.

Sieve(256) drops from 4.19us to 1.61us (default) and 4.17us to 1.57us
(eager jit) on M4 Pro interleaved A/B; scan-loop native entries fall from
~55 to ~1 per run. All other benchmarks stay at parity.

Closes #155

Summary by CodeRabbit

  • Performance
    • Reduced native loop re-entry overhead and unnecessary exits, improving loop execution efficiency.
    • Refreshed Sieve(256) benchmark figures based on recent re-measurements.
  • Bug Fixes
    • Fixed loop completion and native execution teardown/return handling, including fallback exits.
    • Corrected trace-plan branching/cut behavior to avoid incorrect folding and spurious blocks.
  • Tests
    • Added ARM64 coverage for loop-leg folding and updated trace-plan scenarios for loop-header cuts and related edge cases.
  • Documentation
    • Updated JIT internals docs to reflect revised loop, branch folding, and continuation scheduling semantics.

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@siyul-park, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 7 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 513a1cc3-f865-44cf-8af6-094f3abdc928

📥 Commits

Reviewing files that changed from the base of the PR and between 7bf65ad and 46a2ed2.

📒 Files selected for processing (6)
  • interp/interp.go
  • interp/interp_test.go
  • interp/jit.go
  • interp/jit_arm64.go
  • interp/jit_arm64_test.go
  • interp/jit_plan.go
📝 Walkthrough

Walkthrough

Changes

Native loop-leg folding

Layer / File(s) Summary
Loop plan back-edge folding
interp/jit_plan.go, interp/jit_plan_test.go, docs/jit-internals.md
Loop-header cuts and rejoining legs now wire to native back-edges, while unrelated cuts retain fallback exits and explicit branches no longer create spurious blocks.
Continuation snapshot reuse
interp/jit.go, interp/jit_arm64.go, docs/jit-internals.md
ARM64 lowering records scheduled work and reuses continuation labels only for matching block, tail, value snapshots, and frame shapes.
Loop completion and exit handling
interp/interp.go, AGENTS.md, docs/jit-internals.md
Native loop completion handles top-level exhaustion and function-return teardown separately, and fallback exits are recorded before threaded resumption.
Behavioral validation and measurements
interp/jit_arm64_test.go, interp/jit_plan_test.go, docs/benchmarks.md
Tests compare folded loop, return, and completion legs with threaded execution and validate plan wiring; Sieve benchmark results are updated with July 18, 2026 measurements.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant NativeLoop
  participant tracePlan
  participant arm64Lowerer
  participant Interpreter
  NativeLoop->>tracePlan: process loop leg and header cut
  tracePlan->>arm64Lowerer: emit native back-edge or fallback edge
  arm64Lowerer->>NativeLoop: reuse matching continuation or schedule work
  NativeLoop->>Interpreter: trapNone or trapFallback
  Interpreter->>Interpreter: exhaust module or tear down frame
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description omits the required template headings and sections, so it doesn't match the repository format. Add the Changes Made, Related Issues, and Additional Information sections, and summarize the implementation and issue reference.
Docstring Coverage ⚠️ Warning Docstring coverage is 37.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title concisely describes the main change: folding loop branch legs into native back-edges.
Linked Issues check ✅ Passed The code updates match #155 by folding loop-kind legs, wiring header cuts correctly, and reusing continuations to avoid limit exhaustion.
Out of Scope Changes check ✅ Passed The docs, tests, and interpreter/JIT changes all support the loop-leg folding objective, with no clear unrelated changes.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/jit-loop-leg-fold

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@interp/jit_arm64_test.go`:
- Around line 2671-2680: The native-entry assertion in the test currently
aggregates entries from both the scan and fill loops, so it does not verify
scan-loop behavior. Update the metrics iteration around profile.Metrics() to
filter entries using the scan loop’s identifying func/ip/kind labels, then apply
the existing count assertions only to that scoped metric.

In `@interp/jit_plan.go`:
- Around line 626-634: Restrict the shortcut in the trace split logic around the
hot-edge check to the plan’s own depth-0 loop cut by applying the same
entryLoop, depth, and plan-header conditions used near Lines 564-568. Ensure
matching branch/cut pairs at depth > 0 follow fallback instead of folding onto
the native loop back-edge, and add a regression case covering that inlined-frame
scenario.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 13c9e2c3-1ff9-4b5f-aa48-19bc79f98f2e

📥 Commits

Reviewing files that changed from the base of the PR and between b4719c5 and 08a6c74.

📒 Files selected for processing (9)
  • AGENTS.md
  • docs/benchmarks.md
  • docs/jit-internals.md
  • interp/interp.go
  • interp/jit.go
  • interp/jit_arm64.go
  • interp/jit_arm64_test.go
  • interp/jit_plan.go
  • interp/jit_plan_test.go

Comment thread interp/jit_arm64_test.go Outdated
Comment thread interp/jit_plan.go
@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 36.00000% with 32 lines in your changes missing coverage. Please review.
✅ Project coverage is 29.74%. Comparing base (b4719c5) to head (29521b2).

Files with missing lines Patch % Lines
interp/interp.go 0.00% 20 Missing ⚠️
interp/jit.go 0.00% 9 Missing ⚠️
interp/jit_plan.go 85.71% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #160      +/-   ##
==========================================
+ Coverage   29.70%   29.74%   +0.03%     
==========================================
  Files          86       86              
  Lines       60357    60386      +29     
==========================================
+ Hits        17930    17962      +32     
  Misses      41156    41156              
+ Partials     1271     1268       -3     

☔ View full report in Codecov by Harness.
📢 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.

@siyul-park
siyul-park merged commit 8c413ae into main Jul 18, 2026
5 checks passed
@siyul-park
siyul-park deleted the perf/jit-loop-leg-fold branch July 18, 2026 14:07
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.

Fold loop-kind branch legs so in-loop branches stop exiting native code

1 participant