Skip to content

jit: give the exact segmentation a total sort key and an explicit owner - #1280

Merged
youknowone merged 1 commit into
mainfrom
residual
Aug 16, 2026
Merged

jit: give the exact segmentation a total sort key and an explicit owner#1280
youknowone merged 1 commit into
mainfrom
residual

Conversation

@youknowone

@youknowone youknowone commented Aug 16, 2026

Copy link
Copy Markdown
Owner

Follows up the CodeRabbit review on #1268, which flagged the exact-segmentation
dedup and was merged unaddressed.

What the review got right, and what it got wrong

Three code-level facts held:

  • py_exact_by_jit_pc.sort_unstable_by_key(|&(off, _)| off) keys on the offset
    alone, so equal offsets are left in an unspecified order.
  • dedup_by_key retains the first entry of each run.
  • The comment above them claimed the later-emitted run wins, "matching the
    floor tier's later-py-wins tie break".

So the code implemented neither its own stated rule nor the floor tier's. The
floor tier cannot express "later-emitted" at all: its source is a py-indexed
first-offset table with no run order in it, so py magnitude is its only tie.
Those are two different rules, and the comment conflated them.

Two parts of the review's claim did not hold, and are worth recording so the
finding is not re-opened at the wrong severity:

  • "can still" — duplicate offsets are unreachable. A run start always
    addresses a non--live- Insn::Op; every one of those reaches start_instr
    and emits at least one byte (the dispatch_op fallthrough is a panic!).
    remove_repeated_live's remap is many-to-one only over the -live- positions
    it folds, and the three splice passes only insert and shift monotonically.
    Measured to agree: PYRE_VSTACK_EXACT_AUDIT over the bench corpus reports
    runs_recorded == translated for 1075/1075 jitcodes across 442 programs
    the dedup never dropped an entry.
  • "nondeterministically" — there is no randomness. sort_unstable_by_key
    has no randomness source, and the input is already non-decreasing in the
    offset, so the sort is a no-op on the production input. Had a duplicate
    existed, the winner would have been deterministically wrong, not flaky.

The consumer is sensitive — exact_py_pc_for_jitcode_pc feeds
vstack_step_py_pc, which is the walk mirror's coordinate — so the whole
defence rests on the duplicate never existing, and nothing asserted that.

The change

  • Carry the emission index into the sort key and collapse last-wins, so the code
    implements the rule its comment states, under any input.
  • debug_assert! that the offsets are distinct, turning a future pass that
    deletes an ordinary op (or an encoder arm that emits nothing) into a loud
    failure here rather than a silent wrong coordinate in the mirror.
  • Correct two comments that describe behaviour the code does not have:
    filter_liveness_in_place's claim that the -live- merge collapses run
    positions onto each other, and the tie-break claim.
  • Give abort_permanent_py_pc_by_jit_pc the same total sort key. Its markers
    each emit an opcode byte so its dedup is a no-op too, but the partial key had
    the same shape.

Verification

  • Collision sweep after the change: 1075 jitcodes / 442 programs, 0
    collisions
    — the explicit collapse never fires, so the published table is
    unchanged.
  • debug_assert exercised: debug build over 40 synth benches, 0 firings.
  • cargo test --all --no-default-features --features dynasm --no-fail-fast:
    rc=0, 138 test binaries.
  • pyre/check.py: dynasm 436/436, cranelift 436/436. The one wasm red is
    short_circuit_value_kept_stack at ratio 4.6x > 4x, which is pre-existing on
    this host and unrelated to this change.

authored by Claude

Summary by CodeRabbit

  • Bug Fixes
    • Improved deterministic ordering for abort points and exact program-counter mappings.
    • Corrected handling of duplicate offsets so the later mapping takes precedence.
    • Added validation to ensure distinct run-start offsets and preserve increasing position order.

`py_exact_by_jit_pc` sorted on the jitcode offset alone and collapsed with
`dedup_by_key`.  `sort_unstable_by_key` leaves entries with equal keys in an
unspecified order, and `dedup_by_key` retains the FIRST of each run, so neither
is what the comment above them claimed: that the later-emitted run owns a shared
offset.  The floor tier's rule is not that one either — its source is a
py-indexed first-offset table carrying no run order, so py magnitude is the only
tie it can express.

Carry the emission index into the sort key and collapse last-wins, which is the
stated rule.  Assert the offsets are distinct: they are for every one of the
1075 jitcodes the bench corpus compiles, because a run start always addresses a
non-`-live-` op and every one of those emits at least one byte.

Correct two comments that describe behaviour the code does not have:
`filter_liveness_in_place`'s claim that the `-live-` merge collapses run
positions onto each other, and the tie-break claim above.

Give `abort_permanent_py_pc_by_jit_pc` the same total sort key.

Assisted-by: Claude
@coderabbitai

coderabbitai Bot commented Aug 16, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 72d80f58-d2d5-4d84-a0af-e95d0c6bc2dc

📥 Commits

Reviewing files that changed from the base of the PR and between affdab0 and ec9732e.

📒 Files selected for processing (1)
  • pyre/pyre-jit/src/jit/codewriter.rs

Included review availability: Your plan includes up to 2 reviews per rolling hour; 0 remain after this review.


Walkthrough

The codewriter now documents remapping order, sorts abort-PC markers deterministically, and preserves deterministic ownership when exact PC runs share offsets.

Changes

JIT PC mapping

Layer / File(s) Summary
Remapping ordering and ownership
pyre/pyre-jit/src/jit/codewriter.rs
The remapping invariant documents strict ordering for non--live- positions. Abort-PC markers sort by JIT offset and Python PC. Exact PC-run mapping retains emission indices, checks distinct run-start offsets, and preserves the later owner for duplicate offsets.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to ec973

The change makes segmentation ownership deterministic and adds invariant checks without changing the published table; no actionable merge-blocking risk remains after normal checks and review.

Possibly related PRs

Suggested reviewers: lifthrasiir

Poem

A rabbit checks each offset line,
And keeps the later owner’s sign.
PC markers sort in place,
With steady runs and ordered grace.
“Hop!” says the map, deterministic and fine.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main changes to exact segmentation sorting and ownership.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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 residual

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.

@github-actions

Copy link
Copy Markdown

🤖 Codex parity review

Static analysis of this diff vs the local RPython/PyPy sources (commit ec9732e).
Updated: 2026-08-16T17:53:59.337Z

Files in the reviewed diff
pyre/pyre-jit/src/jit/codewriter.rs

1. Regressions to PyPy parity introduced by this patch

None.

2. Other mismatches introduced by this patch

None.

3. Pre-existing mismatches (already present before this patch)

None.

4. Structural adaptations

  • pyre/pyre-jit/src/jit/codewriter.rs:14823 ↔ rpython/jit/codewriter/liveness.py:82 — Pyre’s pyre-only JitCode-byte-offset → CPython-bytecode-PC ownership table carries an emission index to make same-offset handling deterministic. RPython’s remove_repeated_live only rebuilds SSA instructions and has no corresponding Python-PC inverse metadata. The new ordering/collapse preserves the later emitted run if such a collision occurs; ordinary non--live- run starts remain injective through the liveness remap.

@youknowone
youknowone merged commit c45b04f into main Aug 16, 2026
15 of 18 checks passed
@youknowone
youknowone deleted the residual branch August 16, 2026 22:35
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.

1 participant