Skip to content

Fix for 127747 - #128962

Merged
dhartglassMSFT merged 4 commits into
dotnet:mainfrom
dhartglassMSFT:127747
Jun 17, 2026
Merged

dhartglassMSFT merged 4 commits into
dotnet:mainfrom
dhartglassMSFT:127747

Conversation

@dhartglassMSFT

@dhartglassMSFT dhartglassMSFT commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

fgOptimizeHWIntrinsic does the following transform (note V09 used before it is defined in the 'After')

Before
\--*  HWINTRINSIC simd16 16 byte Add $441
   +--*  HWINTRINSIC simd16 16 byte Negate $480
   |  \--*  HWINTRINSIC simd16 16 byte ShiftLogicalSaturate $440
   |     +--*  COMMA     simd16 $400
   |     |  +--*  STORE_LCL_VAR simd16 V09 cse1         d:1 $VN.Void
   |     |  |  \--*  CNS_VEC   simd16<0x01010101, 0x01010101, 0x01010101, 0x01010101> $400
   |     |  \--*  LCL_VAR   simd16 V09 cse1         u:1 $400
   |     \--*  CNS_VEC   simd16<0x00000000, 0x00000000, 0x00000000, 0x00000000> $401
   \--*  LCL_VAR   simd16 V09 cse1         u:1 $400
After:
\--*  HWINTRINSIC simd16 16 byte Subtract $441
   +--*  LCL_VAR   simd16 V09 cse1         u:1 $400
   \--*  HWINTRINSIC simd16 16 byte ShiftLogicalSaturate $440
      +--*  COMMA     simd16 $400
      |  +--*  STORE_LCL_VAR simd16 V09 cse1         d:1 $VN.Void
      |  |  \--*  CNS_VEC   simd16<0x01010101, 0x01010101, 0x01010101, 0x01010101> $400
      |  \--*  LCL_VAR   simd16 V09 cse1         u:1 $400
      \--*  CNS_VEC   simd16<0x00000000, 0x00000000, 0x00000000, 0x00000000> $401

fgOptimizeAddition already checks "gtCanSwapOrder" for the transform (ADD(NEG(a), b) => SUB(b, a)) - that check was missing from fgOptimizeHWIntrinsic for an identical transform

fixes #127747
"Assertion failed Write to unaliased local overlaps outstanding read during 'Rationalize IR'"

Copilot AI review requested due to automatic review settings June 3, 2026 21:04
@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 Jun 3, 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 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.

Pull request overview

This PR updates CoreCLR JIT morphing (fgOptimizeHWIntrinsic) to avoid performing a HWIntrinsic ADD optimization that would swap operand evaluation order when such swapping is not safe.

Changes:

  • Add a gtCanSwapOrder(op1, op2) guard before rewriting (-v1) + v2 into v2 - v1 for SIMD HWIntrinsics.
  • Bail out of that optimization when swapping could change observable behavior (e.g., due to side effects / ordering constraints).

Comment thread src/coreclr/jit/morph.cpp Resolved

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot stopped reviewing on behalf of dhartglassMSFT due to an error June 4, 2026 19:00
@dhartglassMSFT dhartglassMSFT changed the title [DRAFT] Fix for 127747 Fix for 127747 Jun 4, 2026
@dhartglassMSFT

Copy link
Copy Markdown
Contributor Author

An earlier run showed green in all the spmi asmdiff tests

Copilot AI review requested due to automatic review settings June 8, 2026 05: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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@dhartglassMSFT
dhartglassMSFT merged commit aca48fc into dotnet:main Jun 17, 2026
142 of 144 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 18, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
fgOptimizeHWIntrinsic does the following transform (note `V09` used
before it is defined in the 'After')
```
Before
\--*  HWINTRINSIC simd16 16 byte Add $441
   +--*  HWINTRINSIC simd16 16 byte Negate $480
   |  \--*  HWINTRINSIC simd16 16 byte ShiftLogicalSaturate $440
   |     +--*  COMMA     simd16 $400
   |     |  +--*  STORE_LCL_VAR simd16 V09 cse1         d:1 $VN.Void
   |     |  |  \--*  CNS_VEC   simd16<0x01010101, 0x01010101, 0x01010101, 0x01010101> $400
   |     |  \--*  LCL_VAR   simd16 V09 cse1         u:1 $400
   |     \--*  CNS_VEC   simd16<0x00000000, 0x00000000, 0x00000000, 0x00000000> $401
   \--*  LCL_VAR   simd16 V09 cse1         u:1 $400
After:
\--*  HWINTRINSIC simd16 16 byte Subtract $441
   +--*  LCL_VAR   simd16 V09 cse1         u:1 $400
   \--*  HWINTRINSIC simd16 16 byte ShiftLogicalSaturate $440
      +--*  COMMA     simd16 $400
      |  +--*  STORE_LCL_VAR simd16 V09 cse1         d:1 $VN.Void
      |  |  \--*  CNS_VEC   simd16<0x01010101, 0x01010101, 0x01010101, 0x01010101> $400
      |  \--*  LCL_VAR   simd16 V09 cse1         u:1 $400
      \--*  CNS_VEC   simd16<0x00000000, 0x00000000, 0x00000000, 0x00000000> $401
```

`fgOptimizeAddition` already checks "gtCanSwapOrder" for the transform
(`ADD(NEG(a), b) => SUB(b, a)`) - that check was missing from
`fgOptimizeHWIntrinsic` for an identical transform

fixes #127747 
"Assertion failed Write to unaliased local overlaps outstanding read
during 'Rationalize IR'"
dhartglassMSFT pushed a commit that referenced this pull request Jul 17, 2026
Adds a JIT regression test for the `'"'"'!"Write to unaliased local
overlaps outstanding read"'"'"'` assert (lir.cpp) reported in #124749,
found by Fuzzlyn.

The bug was already fixed by #128962. `fgOptimizeHWIntrinsic` rewrites
`Add(Neg(X), Y)` to `Sub(Y, X)`, reversing operand evaluation order (for
SIMD, negation is `Subtract(0, X)`). When CSE had planted a `COMMA(STORE
cse_temp, use)` def inside `X` and left a bare `LCL_VAR cse_temp` use as
`Y`, the swap scheduled the use before its def, tripping the
use-before-def assert during Rationalization. #128962 guards the
transform with `gtCanSwapOrder(op1, op2)`, which returns `false` here
because `op1` carries `GTF_ASG` (the store) while `op2` is
non-invariant.

This just adds coverage; there is no product change.

----------

Verification:
- Reproduced the exact assert in `Program:M0()` via SPMI replay at the
collection'"'"'s compatible commit; backporting only the
`gtCanSwapOrder` guard made it disappear.
- On current `main`, removing the guard and rebuilding `clrjit.dll`
reproduced the assert through this repro; restoring it produced a clean
run.

I could not execute-verify locally as this machine lacks AVX512, so the
test no-ops here, but the SPMI collection is this exact repro and
validates it on AVX512 legs.

Closes #124749

> [!NOTE]
> This PR description was drafted by Copilot.

Co-authored-by: Copilot App <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 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: Assertion failed Write to unaliased local overlaps outstanding read during 'Rationalize IR'

3 participants