Skip to content

perf(repsel): bound loop accumulators for i32 - #7973

Merged
proggeramlug merged 2 commits into
mainfrom
perf/7123-loop-accumulator
Aug 12, 2026
Merged

perf(repsel): bound loop accumulators for i32#7973
proggeramlug merged 2 commits into
mainfrom
perf/7123-loop-accumulator

Conversation

@proggeramlug

@proggeramlug proggeramlug commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Closes #7123

Summary

  • derive saturating trip-count upper bounds from proven constant-bounded for-loop induction variables, including inclusive/non-unit and nested loops
  • admit accumulators only when the literal entry magnitude plus every bounded write contribution fits signed i32
  • bound literal/const, remainder, non-negative bit-mask, and loop-bounded-local steps; conservatively reject unknown loops, writes, and expression forms
  • add the census liveness promotion and registered runtime-observable overflow witness

Validation

  • cargo test -p perry-codegen --no-default-features --lib collectors::loop_bounded_i32::tests (45 passed; used a temporary, restored test-only manifest setting to avoid the packaged Windows LLVM all-target linker gap)
  • cargo test -p perry-codegen --no-default-features --lib collectors::hir_facts::tests::bounded_modulo_accumulator_seeds_integer_provenance -- --exact (passed)
  • cargo check -p perry-codegen --tests
  • cargo clippy -p perry-codegen --tests (passed; existing warnings only)
  • cargo fmt -p perry-codegen -- --check
  • python -m py_compile scripts/compiler_output_harness/repsel_census.py
  • sabotage: suppressing the new result fails the bounded-promotion assertion; removing the final i32 range gate fails the 3-billion overflow refusal

No version bump.

Summary by CodeRabbit

  • New Features

    • Improved optimization of loop accumulators when their values can be safely proven to remain within 32-bit integer limits.
    • Supports bounded loop steps, nested loops, modulo operations, and bit-masked increments.
    • Preserves correct behavior for accumulators that may overflow.
  • Bug Fixes

    • Improved handling of bounded accumulator values while conservatively rejecting cases with unknown or unsafe limits.
  • Documentation

    • Added guidance covering supported bounded-loop and accumulator scenarios.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: b6f656e4-e7c2-4f34-93df-aef7b464cb53

📥 Commits

Reviewing files that changed from the base of the PR and between a40ae16 and 1345a36.

📒 Files selected for processing (11)
  • benchmarks/repsel_census/baseline.json
  • benchmarks/repsel_census/fixtures/fixture_loop_bounded_i32.ts
  • changelog.d/7973-repsel-loop-accumulator.md
  • crates/perry-codegen/src/collectors/hir_facts.rs
  • crates/perry-codegen/src/collectors/loop_bounded_i32.rs
  • crates/perry-codegen/src/collectors/loop_bounded_i32/tests.rs
  • crates/perry-codegen/src/expr/slot_rep.rs
  • crates/perry-codegen/src/stmt/let_stmt.rs
  • scripts/compiler_output_harness/repsel_census.py
  • test-files/test_gap_repsel_loop_bounded_i32.ts
  • test-parity/gc_repsel_corpus.txt

📝 Walkthrough

Walkthrough

The PR adds sound bounded-accumulator analysis for loop-local i32 values. It supports trip-count and step-magnitude proofs, integrates accepted locals into canonical i32 selection, and adds compiler, runtime, fixture, and census coverage.

Changes

Bounded accumulator promotion

Layer / File(s) Summary
Accumulator range analysis
crates/perry-codegen/src/collectors/loop_bounded_i32.rs, crates/perry-codegen/src/collectors/loop_bounded_i32/tests.rs
The collector derives saturating loop execution bounds and step magnitudes. It admits accumulators whose worst-case values fit i32 and rejects unknown, unsupported, or overflowing writes.
Representation eligibility integration
crates/perry-codegen/src/collectors/hir_facts.rs, crates/perry-codegen/src/expr/slot_rep.rs, crates/perry-codegen/src/stmt/let_stmt.rs
Accepted accumulator locals now contribute integer facts and canonical i32 eligibility. Documentation and denial expectations describe the expanded proof rules.
Fixture and census validation
benchmarks/repsel_census/fixtures/fixture_loop_bounded_i32.ts, test-files/test_gap_repsel_loop_bounded_i32.ts, test-parity/gc_repsel_corpus.txt, scripts/compiler_output_harness/repsel_census.py, benchmarks/repsel_census/baseline.json, changelog.d/7973-repsel-loop-accumulator.md
Coverage distinguishes bounded and overflowing accumulators. Runtime probes verify output behavior, and the expected canonical-i32 promotion count increases from three to four.

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

Sequence Diagram(s)

sequenceDiagram
  participant LoopAnalyzer as loop_bounded_i32 collector
  participant HIRFacts as collect_type_facts
  participant SlotRep as slot_rep
  participant Fixture as runtime fixture
  LoopAnalyzer->>HIRFacts: report bounded accumulator locals
  HIRFacts->>SlotRep: merge integer provenance
  SlotRep->>Fixture: select canonical i32 representation
  Fixture->>Fixture: compare bounded and overflowing results
Loading

Possibly related PRs

  • PerryTS/perry#6903: This PR extends its canonical-i32 representation selection with loop-bounded accumulator analysis.
  • PerryTS/perry#7122: This PR extends its loop-bounded i32 analysis from induction variables to accumulators.
  • PerryTS/perry#7132: Both PRs modify shared loop-bounded representation-selection logic and related fixtures.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the primary change: bounding loop accumulators for canonical i32 representation.
Description check ✅ Passed The description includes the summary, issue reference, implementation details, validation commands, sabotage evidence, and version note, although it omits some template headings.
Linked Issues check ✅ Passed The changes implement the linked issue's bounded-accumulator proof, conservative rejection, liveness fixture, runtime overflow witness, parity registration, and sabotage evidence.
Out of Scope Changes check ✅ Passed The modified code, tests, fixtures, census data, parity documentation, and changelog entry all support the linked accumulator-bounding objective.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/7123-loop-accumulator

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.

@proggeramlug
proggeramlug merged commit cbc9286 into main Aug 12, 2026
1 of 19 checks passed
@proggeramlug
proggeramlug deleted the perf/7123-loop-accumulator branch August 12, 2026 16:28
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.

repsel: bound a loop accumulator by trip count x step magnitude, so a bare accumulator can take canonical i32

1 participant