Skip to content

JIT: restore type-based upper bound for RSZ in range analysis#128146

Open
EgorBo wants to merge 2 commits into
dotnet:mainfrom
EgorBo:fix-rsz-bce
Open

JIT: restore type-based upper bound for RSZ in range analysis#128146
EgorBo wants to merge 2 commits into
dotnet:mainfrom
EgorBo:fix-rsz-bce

Conversation

@EgorBo
Copy link
Copy Markdown
Member

@EgorBo EgorBo commented May 13, 2026

fix a regression in range check elimination found by @saucecontrol for:

public static int M(int i)
{
    ReadOnlySpan<int> vals = [0, 1];
    return vals[i >>> 31]; // RNGCHK was no longer eliminated
}

Copilot AI review requested due to automatic review settings May 13, 2026 13:47
@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 May 13, 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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 adjusts CoreCLR JIT range analysis for logical right shifts (GT_RSZ) to recover tighter, type-based upper bounds so bounds-check elimination opportunities aren’t lost when the shift count is known but the shifted value’s range isn’t a helpful constant.

Changes:

  • Extends RangeOps::ShiftRight to accept an op1BitWidth and uses it (for logical shifts) to compute a type-based upper bound UMAX(op1Type) >> minShift and to force a non-negative lower bound when shifting by at least 1.
  • Passes the shift operand’s bit width from RangeCheck::ComputeRangeForBinOp when analyzing GT_RSZ.
  • Adds JIT tests asserting bounds checks are elided for i >>> 31 indexing into a 2-element span and i >>> 28 indexing into a 16-element span.

Reviewed changes

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

File Description
src/tests/JIT/opt/RangeChecks/ElidedBoundsChecks.cs Adds regression coverage for bounds-check elimination on logical right shift–derived indices.
src/coreclr/jit/rangecheck.h Enhances RangeOps::ShiftRight with an optional operand bit-width parameter and type-based bound derivation for logical shifts.
src/coreclr/jit/rangecheck.cpp Supplies operand bit width to ShiftRight for GT_RSZ range computation.

Comment thread src/coreclr/jit/rangecheck.cpp Outdated
@EgorBo EgorBo force-pushed the fix-rsz-bce branch 2 times, most recently from 791b33d to 81ffe2a Compare May 13, 2026 14:12
PR dotnet#122263 dropped the type-based RSZ fallback that derives an upper bound

from op1's bit-width when r1's range isn't a useful constant. As a result,

patterns like `vals[i >>> 31]` against a length-2 span no longer had their

bounds check eliminated.

For RSZ by N >= 1 with r1 not proven non-negative, override with the sound

[0, UINT_MAX >> N] bound. (RSZ here is always 32-bit since GetRangeWorker

bails out on TYP_LONG and CIL shifts have no small-typed operands.)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 14, 2026 11:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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

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.

3 participants