Skip to content

JIT: reject variable-count XOR rotation recognition - #133867

Merged
EgorBo merged 2 commits into
mainfrom
copilot/fix-xor-based-rotation-recognition
Sep 24, 2026
Merged

EgorBo merged 2 commits into
mainfrom
copilot/fix-xor-based-rotation-recognition

Conversation

Copilot AI commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

When the shift count is a multiple of the operand width, both shifts yield x: XOR must return zero, whereas a rotation returns x.

// Must return zero when count is 0 or 32.
static int ShiftXor(int x, int count) =>
    (x << (count & 31)) ^ (x >>> ((32 - count) & 31));
  • Recognition: Reject variable-count XOR patterns while preserving OR and constant-count XOR rotations.
  • Regression coverage: Extend existing rotation tests for both directions and widths, including zero, width multiples, negative and boundary counts, and constant XOR rotations.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
16 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@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 Sep 14, 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.

Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
@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.

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.

🟢 Approval recommended

The fix and regression coverage address the reported JIT miscompile.

Pull request overview

Fixes a RyuJIT miscompile by rejecting variable-count XOR patterns as rotations.

Changes:

  • Restricts XOR rotation recognition to valid constant-count patterns.
  • Adds regression coverage for widths, directions, boundaries, and constant rotations.
File summaries
File Description
src/coreclr/jit/morph.cpp Prevents invalid variable-count XOR-to-rotation transformations.
src/tests/JIT/CodeGenBringUpTests/Rotate.cs Adds regression tests for XOR rotation edge cases.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

@EgorBo

EgorBo commented Sep 21, 2026

Copy link
Copy Markdown
Member

PTAL @dhartglassMSFT

@tannergooding tannergooding left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. That being said, the rotation recognition logic here is "very complex" for something where ideally most users would just use int.RotateLeft or similar.

We also support "basically any pattern" that resembles a rotation, rather than pushing users towards a single standard pattern.

I think it'd be worth simplifying this in .NET 12 and telling users to fix their code or use the dedicated helpers. We really don't need this to handle every scenario possible or be this complex.

@EgorBo
EgorBo enabled auto-merge (squash) September 24, 2026 10:51
@EgorBo
EgorBo merged commit a3b1efa into main Sep 24, 2026
138 of 141 checks passed
@EgorBo
EgorBo deleted the copilot/fix-xor-based-rotation-recognition branch September 24, 2026 10:51
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 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) XOR-based rotation recognition produces a wrong result when the shift count is a multiple of the operand width

4 participants