Repository navigation
JIT: don't re-read multi-reg struct arg addresses per register - #138153
Merged
AndyAyersMS merged 2 commits intoNov 24, 2026
Merged
Conversation
When a struct argument passed in multiple registers is loaded from memory, fgTryMorphStructArg creates one load per register and clones the address for each. If computing that address reads memory, as in `Use(o.I.P)`, each clone re-reads it, so the registers can come from different objects and the callee sees a torn struct. This happens in Tier0/MinOpts code, and in optimized code when the address read is volatile. Evaluate such addresses once into a temp. Also give the per-register loads the volatile, unaligned, initclass and nonfaulting flags of the original load, as lowering already does when it splits struct args. Fixes dotnet#134474 Co-authored-by: Copilot <[email protected]> Copilot-Session: 277eb00d-5f36-4200-9de5-e06228a1f721
|
Azure Pipelines: Successfully started running 5 pipeline(s). 10 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
Copilot stopped reviewing on behalf of
AndyAyersMS due to an error
November 23, 2026 20:24
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Replacement loads copy the nonfaulting flag without preserving ordering, allowing argument scheduling to move them ahead of the null check they depend on.
2 open issues
What changed in this PR
This PR fixes CoreCLR JIT struct-argument decomposition so separate register loads reuse one address evaluation rather than potentially reading from different objects.
Changes:
- Save addresses that read mutable memory into a temporary when multiple argument slots need them.
- Evaluate the temporary before the generated slot loads.
- Preserve copyable indirection flags, including volatile and unaligned semantics, on replacement loads.
| File | Description |
|---|---|
src/coreclr/jit/morph.cpp |
Updates struct-argument decomposition to reuse mutable addresses and preserve memory-access flags. |
Review effort: Balanced
Review feedback. Non-null assertion prop marks a load nonfaulting and ordered so it can't move above the null check that made it safe. When copying the nonfaulting flag to the per-register loads, also keep them ordered, as morphblock's copyOrderingFlag does. Add a disasm-check regression test verifying that `o.I` is read once when passing `o.I.P` in registers, for MinOpts and for a volatile field under FullOpts. Co-authored-by: Copilot <[email protected]> Copilot-Session: 277eb00d-5f36-4200-9de5-e06228a1f721
This was referenced Nov 24, 2026
EgorBo
approved these changes
Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



When a struct argument passed in multiple registers is loaded from memory,
fgTryMorphStructArgcreates one load per register and clones the address for each. If computing that address reads memory, as inUse(o.I.P), each clone re-reads it. The registers can then come from different objects, so the callee sees a torn struct. This happens in Tier0/MinOpts code, and in optimized code when the address read is volatile (CSE won't merge volatile loads). It affects targets that pass structs in multiple registers (SysV x64, arm64).Fix: if the address contains a non-invariant indirection or reads an address-exposed local, evaluate it once into a temp and build the per-register loads from that temp. Lowering's split-arg path already does the equivalent.
Also give the per-register loads the original load's
GTF_IND_COPYABLE_FLAGS(volatile, unaligned, initclass, nonfaulting), as lowering does when it splits struct args. Previously avolatile. ldobjstruct arg lost its volatile marking when it was split into per-register loads.Validation:
libclrjit.sodiffering. Base: torn 136k–424k times per run. Fix:Doneon every run.public volatile Inner I;) is read twice by the base JIT even in FullOpts, and once with the fix. Avolatile. ldobjstruct arg now keeps its volatile flag on both per-register loads.build.cmd clr.alljits+clr.spmi -c Checkedat this PR's head: succeeded, 0 warnings, 0 errors.superpmi.py replay -filter realworld smoke_tests, cross-targeting linux-x64 and windows-arm64: clean.d7a6547f2a0; the rebase changed no JIT code these diffs depend on):DynamicInvokestub that hitIsIntrinsicType). MinOpts +11.5 KB (the new address temp). FullOpts -137.7 KB, dominated by coreclr_tests that passDecimalelements of multi-dimensional arrays: their bounds-checked address computations are no longer duplicated per register. Excluding those methods, FullOpts is about +4.2 KB, mostly NativeAOTDynamicInvokestubs.realworld+smoke_testsonly: -4 bytes (43 contexts) and +4,300 bytes (252 contexts), no replay failures.No regression test is included yet; the repro needs a racing thread to show the torn values.
Fixes #134474
Note
This PR description and the change were generated with GitHub Copilot.