Skip to content

JIT: preserve SIMD operands across side effects - #137860

Merged
AndyAyersMS merged 3 commits into
dotnet:mainfrom
AndyAyersMS:fix-134893
Nov 19, 2026
Merged

AndyAyersMS merged 3 commits into
dotnet:mainfrom
AndyAyersMS:fix-134893

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Two hardware intrinsic expansions could reread an operand after a later operand's side effects modified it. Vector128.WithElement reread the index for its range check after evaluating the value operand, and the AVX2 fallback for Vector256<byte>.Shuffle reread the source vector after evaluating the indices.

Spill the WithElement index to a temp when the value operand has persistent side effects, and spill the shuffle source to a temp when it cannot be reordered with the indices.

Adds a regression test.

Validation:

  • Checked runtime and JIT rebuilt together. Runtime_134893 passes with default ISAs, DOTNET_EnableAVX512=0, and DOTNET_EnableHWIntrinsic=0.
  • SuperPMI asmdiffs (windows x64) on three smaller collections: no diffs across 114,613 contexts. On a recording made with AVX-512 disabled, there were two expected diffs: WithElement stayed at 73 bytes, and Shuffle shrank from 119 to 111 bytes.

Resolves #134893

Note

This PR description and the change were generated with GitHub Copilot.

Two hardware intrinsic expansions could reread an operand after a later
operand's side effects modified it. `Vector128.WithElement` reread the
index for its range check after evaluating the value operand, and the
AVX2 fallback for `Vector256<byte>.Shuffle` reread the source vector
after evaluating the indices.

Spill the `WithElement` index to a temp when the value operand has
persistent side effects, and spill the shuffle source to a temp when it
cannot be reordered with the indices.

Adds a regression test.

Resolves dotnet#134893

Co-authored-by: Copilot <[email protected]>
@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 18, 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.

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

@AndyAyersMS
AndyAyersMS requested review from tannergooding and a balanced review from Copilot November 18, 2026 19:52
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@tannergooding ptal
fyi @dotnet/jit-contrib

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 AVX2 fallback still duplicates the source read for Vector256 and Vector256, allowing index side effects to change the source before its second read.

1 open issue
What changed in this PR

This PR fixes two JIT intrinsic expansions that could reread operands after later arguments modified them through side effects.

Changes:

  • Preserve the WithElement index before evaluating a value argument with persistent side effects.
  • Preserve the source vector in the AVX2 byte-shuffle fallback when operand reordering is unsafe.
  • Add regression coverage using Interlocked.Exchange and overlapping fields.
File Description
src/​tests/​JIT/​Regression_ro_2/​Runtime_134893.cs Adds regression cases for index and shuffle-source preservation.
src/​coreclr/​jit/​gentree.cpp Adds temporary storage to preserve operands across side effects.

Review effort: Balanced

Comment thread src/coreclr/jit/gentree.cpp Resolved
The AVX2 fallback for 256-bit shuffles is shared by byte and short
elements and reads the source vector twice, so the short case could
also observe index side effects. Extend the temp to that case and add a
16-bit regression case.

Co-authored-by: Copilot <[email protected]>

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 WithElement regression lacks exception-assertion coverage for an initially invalid index changed to zero, the reported case that bypassed bounds checking and allowed an out-of-bounds stack write.

1 open issue
1 resolved since last review

Review effort: Balanced

Also verify an initially out-of-range index still throws when the value
argument changes the local index to zero.

Co-authored-by: Copilot <[email protected]>
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@tannergooding I tweaked the test, can you re-approve?

@AndyAyersMS

Copy link
Copy Markdown
Member Author

/ba-g osx build machine stopped communicating

@AndyAyersMS
AndyAyersMS merged commit f4ee640 into dotnet:main Nov 19, 2026
125 of 128 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 23, 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) Vector128.WithElement range-checks a re-read of the index local (spurious AOORE / out-of-bounds stack write)

3 participants