Skip to content

JIT: Keep volatile loads when folding full-range relational comparisons - #135724

Merged
EgorBo merged 3 commits into
dotnet:mainfrom
EgorBo:fix-jit-134477
Oct 16, 2026
Merged

EgorBo merged 3 commits into
dotnet:mainfrom
EgorBo:fix-jit-134477

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 13, 2026

Copy link
Copy Markdown
Member

fgOptimizeRelationalComparisonWithFullRangeConst replaces "volatileByte >= 0" with a constant if the tree has no GTF_SIDE_EFFECT. A non-faulting volatile load only has GTF_ORDER_SIDEEFF, so it was discarded together with its acquire semantics, allowing a later load to be hoisted out of a spin loop. Check GTF_OBS_EFFECT instead.

Fixes #134477

fgOptimizeRelationalComparisonWithFullRangeConst replaces "volatileByte >= 0" with a constant if the tree has no GTF_SIDE_EFFECT. A non-faulting volatile load only has GTF_ORDER_SIDEEFF, so it was discarded together with its acquire semantics, allowing a later load to be hoisted out of a spin loop. Check GTF_OBS_EFFECT instead.

Fixes dotnet#134477

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 88502efb-96e2-4483-b698-59f5003a2e2c
@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 Oct 13, 2026
@azure-pipelines

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.

@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 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 regression test must be gated on multithreading support.

1 open finding
What changed in this PR

Fixes JIT folding so volatile-load ordering semantics are preserved.

Changes:

  • Checks GTF_OBS_EFFECT before folding full-range comparisons.
  • Adds a threaded regression test.
File Description
src/​coreclr/​jit/​morph.cpp Preserves ordering side effects during comparison folding.
src/​tests/​JIT/​Regression_ro_2/​Runtime_134477.cs Tests volatile-load acquire semantics.

🧠 Review effort: Balanced

EgorBo and others added 2 commits October 13, 2026 23:46
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 9894cfda-4521-43f5-b406-aeb318def64e
@EgorBo

EgorBo commented Oct 14, 2026

Copy link
Copy Markdown
Member Author

PTAL @dotnet/jit-contrib, trivial bug-fix

@EgorBo
EgorBo requested a review from a team October 14, 2026 02:04
Comment thread src/coreclr/jit/morph.cpp Resolved
@EgorBo

EgorBo commented Oct 16, 2026

Copy link
Copy Markdown
Member Author

/ba-g infra

@EgorBo
EgorBo merged commit 8af938a into dotnet:main Oct 16, 2026
130 of 135 checks passed
@EgorBo
EgorBo deleted the fix-jit-134477 branch October 16, 2026 23:53
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 19, 2026
@MichalStrehovsky

Copy link
Copy Markdown
Member

@EgorBo this test is failing on native AOT, the disassembly of the Test method is:

IN0001: 000000 lea      rax, [(reloc 0x420670)]
IN0002: 000007 mov      eax, dword ptr [rax+0x04]
IN0003: 00000A align    [0 bytes for IG03]

G_M19990_IG03:        ; offs=0x00000A, size=0x0004, bbWeight=8, PerfScore 10.00, gcrefRegs=0000 {}, byrefRegs=0000 {}, loop=IG03, BB02 [0001], byref, isz

IN0004: 00000A test     eax, eax
IN0005: 00000C je       SHORT G_M19990_IG03

G_M19990_IG04:        ; offs=0x00000E, size=0x0001, bbWeight=1, PerfScore 1.00, gcrefRegs=0000 {}, byrefRegs=0000 {}, BB04 [0003], byref, epilog, nogc

IN0006: 00000E ret

Full jitdump:
jitdump.txt

Clanker says:

The JitDump identifies a different path from the already-fixed morphing bug. During import of volatile.ldsfld s_state, Native AOT reports “static readonly” and replaces the load with constant 0. It can infer that value because the test never writes s_state. The importer then folds 0 >= 0 to true, so the volatile read is gone before the morph.cpp fix can protect it.

With that read missing, loop hoisting moves the non-volatile s_data load into the preheader, and CSE keeps its initial value in rax. The worker tests that same value forever, explaining the Join(30000) failure. The fix belongs at static-field import: even when Native AOT knows a field’s value, a volatile.-prefixed load must not be replaced with a constant.

EgorBo added a commit that referenced this pull request Oct 20, 2026
…6149)

Fixes the NativeAOT regression reported in [this
comment](#135724 (comment))
on #135724.

NativeAOT can infer that a never-written static field is readonly.
Folding a volatile read of that field to a constant drops its acquire
semantics, allowing subsequent loads to be hoisted out of a spin loop.
Skip this folding for volatile accesses.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 4330ad35-fbb0-419e-b0b1-bfdb463e3ece
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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) volatile load dropped when byteField >= 0 is folded to true (spin loop hangs)

6 participants