Skip to content

JIT: don't re-read multi-reg struct arg addresses per register - #138153

Merged
AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:fix-134474-multireg-arg-addr
Nov 24, 2026
Merged

AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:fix-134474-multireg-arg-addr

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

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. 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).

; before (linux-x64, MinOpts)
       mov      rax, gword ptr [rax+0x08]      ; o.I (1st read)
       mov      rdi, qword ptr [rax+0x08]      ; .P.A
       mov      rax, gword ptr [rbp-0x08]
       mov      rax, gword ptr [rax+0x08]      ; o.I (2nd read)
       mov      rsi, qword ptr [rax+0x10]      ; .P.B

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 a volatile. ldobj struct arg lost its volatile marking when it was split into per-register loads.

Validation:

  • Torn reads: the issue's repro (20M iterations), A/B on a linux-x64 Checked runtime with only libclrjit.so differing. Base: torn 136k–424k times per run. Fix: Done on every run.
  • Volatile: a volatile field in the address (public volatile Inner I;) is read twice by the base JIT even in FullOpts, and once with the fix. A volatile. ldobj struct arg now keeps its volatile flag on both per-register loads.
  • Build: build.cmd clr.alljits+clr.spmi -c Checked at this PR's head: succeeded, 0 warnings, 0 errors.
  • SuperPMI replay at this PR's head: superpmi.py replay -filter realworld smoke_tests, cross-targeting linux-x64 and windows-arm64: clean.
  • Full SuperPMI asmdiffs (on the previous base, d7a6547f2a0; the rebase changed no JIT code these diffs depend on):
    • windows-x64, all 12 collections: -921 bytes in 642 of 2.86M contexts, no replay failures. Windows x64 passes these structs by reference, so these diffs come from the flag change.
    • linux-x64 cross-OS, all 12 collections: -126,138 bytes in 2,092 of 3.14M contexts, no JIT failures. Two contexts were missing replay data with the diff JIT (one is a NativeAOT DynamicInvoke stub that hit IsIntrinsicType). MinOpts +11.5 KB (the new address temp). FullOpts -137.7 KB, dominated by coreclr_tests that pass Decimal elements 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 NativeAOT DynamicInvoke stubs.
    • windows-arm64, realworld + smoke_tests only: -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.

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
@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 Nov 23, 2026
@azure-pipelines

Copy link
Copy Markdown
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.

@AndyAyersMS
AndyAyersMS requested review from EgorBo and a balanced review from Copilot November 23, 2026 20:24
@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 stopped reviewing on behalf of AndyAyersMS due to an error November 23, 2026 20:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread src/coreclr/jit/morph.cpp Resolved
Comment thread src/coreclr/jit/morph.cpp Resolved
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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The disassembly check requires call, rejecting valid Linux x64 tail jumps and potentially failing even when the address is correctly loaded only once.

1 open issue
2 resolved since last review

Review effort: Balanced

@AndyAyersMS
AndyAyersMS merged commit 769684a into dotnet:main Nov 24, 2026
126 of 128 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 25, 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) multi-reg struct arg load re-reads the heap address per register (torn struct values)

3 participants