Skip to content

Fix for out of range rotate amount during lower - #131909

Draft
dhartglassMSFT wants to merge 7 commits into
dotnet:mainfrom
dhartglassMSFT:fix_for_ROR_roate_assert
Draft

Fix for out of range rotate amount during lower#131909
dhartglassMSFT wants to merge 7 commits into
dotnet:mainfrom
dhartglassMSFT:fix_for_ROR_roate_assert

Conversation

@dhartglassMSFT

Copy link
Copy Markdown
Contributor

Assert during arm64 lower that rotate amount on a rotate node (ROR, ROL) was an out of range integer

Change to instead add explicit masking onto rotate nodes in the jit (AND op (CNS_INT 63) for ex.), and to strip the mask during lower like is done already for shifts

Also change importercalls to allow non-constant rotate amounts onto the node. Gives a couple nice diffs where we can generate ROR now instead of a call.

There's a single bad diff caused by CSE deduplicating the explicit masks, which ends up defeating the lowering mask-stripping logic. This bug exists already for SHIFTS (not rotates) so this is an extant issue, I'll try and open a bug.

fixes #129298

Copilot AI review requested due to automatic review settings August 5, 2026 22:25
@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 Aug 5, 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.

Pull request overview

This PR updates the JIT’s handling of rotate counts to avoid out-of-range rotate amounts reaching lowering, including enabling import of variable rotate amounts for primitive intrinsics, and adds a regression test for the arm64 lowering assert scenario.

Changes:

  • Mask rotate counts explicitly in morphing so rotate nodes always have counts normalized to [0, bitsize-1].
  • Strip redundant AND(count, mask) for rotates during lowering on targets that implicitly mask rotate counts.
  • Refactor RotateLeft/RotateRight intrinsic importing into a shared helper and allow non-constant rotate amounts (with explicit masking), plus add a regression test.

Reviewed changes

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

Show a summary per file
File Description
src/coreclr/jit/morph.cpp Adds explicit masking of rotate counts when recognizing/morphing rotation patterns.
src/coreclr/jit/lower.cpp Adds TryRemoveShiftRotateMask and uses it to strip redundant rotate-count masks prior to lowering rotates on relevant targets.
src/coreclr/jit/lower.h Declares TryRemoveShiftRotateMask helper used by lowering.
src/coreclr/jit/importercalls.cpp Introduces impRotateHelper and uses it for RotateLeft/RotateRight, including masked variable rotate counts.
src/coreclr/jit/compiler.h Adds the impRotateHelper declaration.
src/tests/JIT/Regression/JitBlue/Runtime_129298/Runtime_129298.cs Adds regression test ensuring JIT compiles the dead rotate block without asserting.
src/tests/JIT/Regression/Regression_ro_2.csproj Includes the new regression test source file in the project.

Comment thread src/coreclr/jit/morph.cpp
Comment on lines +12537 to +12539
// Explicitly mask the rotate amount to the range [0, bitsize-1]. Otherwise, a later
// tranform can stick an out of range constant here and trip up lowering. If the
// target's rotate or shift instructions mask their operand implicitly, those targets
Copilot AI review requested due to automatic review settings August 5, 2026 22:32

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 7 out of 7 changed files in this pull request and generated no new comments.

Suppressed comments (1)

src/coreclr/jit/lower.cpp:8875

  • The LowerShift doc comment still refers specifically to xarch, but this helper is used for multiple targets (see the TARGET_XARCH || TARGET_ARM64 || TARGET_LOONGARCH64 || TARGET_RISCV64 call sites). Updating the comment would avoid misleading future readers about why the mask removal is safe here.
// Notes:
//    Remove unnecessary shift count masking, xarch shift instructions
//    mask the shift count to 5 bits (or 6 bits for 64 bit operations).
//

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.

[arm64] JIT assert at lowerarmarch.cpp:339 — rotateAmount can be negative after %

2 participants