Skip to content

Preserve stelem.ref exception order during helper demotion - #134661

Closed
EgorBo with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-stelem-ref-exception-order
Closed

EgorBo with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-stelem-ref-exception-order

Conversation

Copilot AI commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Demoting a stelem.ref helper to STOREIND can evaluate the bounds check before the value expression, throwing IndexOutOfRangeException instead of NullReferenceException.

  • JIT: Use gtPrepareOperandsForReordering at demotion to preserve array → index → value → bounds-check order. Importer-only spills are insufficient because forward substitution can undo them.
  • Regression coverage: Add cases for null receivers, empty arrays, invalid indices, successful stores, and competing index/value exceptions.

@azure-pipelines

azure-pipelines Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Sep 25, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Copilot AI changed the title [WIP] Fix wrong exception order for stelem.ref Preserve stelem.ref exception order during helper demotion Sep 25, 2026
Copilot AI requested a review from EgorBo September 25, 2026 16:31
@EgorBo
EgorBo marked this pull request as ready for review September 25, 2026 20:47
@EgorBo
EgorBo requested a review from adamperlin September 25, 2026 20:47
@EgorBo

EgorBo commented Sep 25, 2026

Copy link
Copy Markdown
Member

PTAL @adamperlin @dotnet/jit-contrib, almost no diffs

@adamperlin

Copy link
Copy Markdown
Contributor

@EgorBo the fix here seems reasonable, but it looks like the agent's regression test is failing.

@adamperlin

Copy link
Copy Markdown
Contributor

@copilot Can you diagnose the failure and propose a fix?

Copilot AI commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Can you diagnose the failure and propose a fix?

The failure is in browser-Wasm ReadyToRun, Checked, no tiering (test results). All five rows fail: invalid indices throw IndexOutOfRangeException, but the valid (1,0) case throws nothing. So this is a missing null check, not an incorrect test expectation.

I confirmed the underlying IR problem with a fresh Linux x64 Debug build and JitDump:

  • fgMorphArgs morphs c.F and records c != null.
  • Helper demotion calls fgMorphTree(store), revisiting that same load. The dump shows node [000009] consuming its own non-null assertion and becoming GTF_IND_NONFAULTING.
  • Wasm's genCodeForIndir omits its explicit null check for that flag. Native x64 still faults on the actual load, hiding the problem.

Proposed fix: immediately before fgMorphTree(store), when optLocalAssertionProp is enabled, use BitVecOps::Assign(apTraits, apLocal, apLocalPostorder). That retains surviving prior-statement facts but discards assertions generated by the operands being remorphed. Keep the operand-ordering changes and all existing test expectations. The double-morph call already existed before this PR; the new coverage exposes this additional defect.

Validation: baseline and regression-runner builds succeeded; all five focused native cases passed, with the faulty IR flag confirmed in the dump. This is a proposal only—no source changes made, and I have not built or run the proposed correction on Wasm.

@EgorBo

EgorBo commented Oct 1, 2026

Copy link
Copy Markdown
Member

@copilot the new test still fails on R2R-CG2 browser wasm Checked no_tiered_comp

Copilot AI commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

@copilot the new test still fails on R2R-CG2 browser wasm Checked no_tiered_comp

The assertion-state correction from the previous reply was only proposed; it is still absent from the branch. I confirmed the same Wasm failure, but hit a build blocker before editing: the unchanged clr+libs+host baseline fails at crossgen-corelib.proj:124 because dotnet-pgo merge receives no MIBC inputs. No source changes were made.

May I proceed with targeted JIT builds and Wasm validation despite that baseline failure?

Copilot AI requested a review from EgorBo October 1, 2026 13:28
@EgorBo

EgorBo commented Oct 5, 2026

Copy link
Copy Markdown
Member

@copilot fix Runtime_133860 test failure, CI is red.

@adamperlin

Copy link
Copy Markdown
Contributor

@EgorBo it looks like the session failed due to the new model permissions. Maybe you can switch models and re-run?

@EgorBo

EgorBo commented Oct 5, 2026

Copy link
Copy Markdown
Member

@copilot fix Runtime_133860 test failure, CI is red.

@EgorBo

EgorBo commented Oct 5, 2026

Copy link
Copy Markdown
Member

I'll file myself 🙂

@EgorBo EgorBo closed this Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: (bug) Wrong exception order for stelem.ref: the array range check runs before the value expression

3 participants