Skip to content

Use SSA-based GetRange for ArrLen in OptimizeRangeCheck - #121683

Merged
EgorBo merged 3 commits into
dotnet:mainfrom
EgorBo:slice-pattern-bce
Nov 18, 2025
Merged

EgorBo merged 3 commits into
dotnet:mainfrom
EgorBo:slice-pattern-bce

Conversation

@EgorBo

@EgorBo EgorBo commented Nov 16, 2025 •

Copy link
Copy Markdown
Member

Closes #121682

Removes the bounds checks in ^

Diffs

@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 16, 2025
@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 enhances the range check optimization by adding a fallback mechanism to determine array length using SSA-based analysis when assertion-based lookups fail. This enables removal of bounds checks in more scenarios, particularly for patterns involving index-from-end (^) operations.

Key Changes

  • Adds SSA-based range analysis as a fallback when assertion-based array length lookup fails
  • Uses the existing GetRangeWorker method to compute array length ranges when faster methods don't find a constant length

@hez2010

hez2010 commented Nov 16, 2025

Copy link
Copy Markdown
Contributor

Can you check does this also handle patterns like:

var buffer = new T[rows.Length];
var count = 0;
for (var i = 0; i < rows.Length; i++)
{
    if (predicate(rows[i])) buffer[count++] = rows[i]; // current there's bound check for buffer[count++]
}

@EgorBo

EgorBo commented Nov 17, 2025

Copy link
Copy Markdown
Member Author

Can you check does this also handle patterns like:

var buffer = new T[rows.Length];
var count = 0;
for (var i = 0; i < rows.Length; i++)
{
    if (predicate(rows[i])) buffer[count++] = rows[i]; // current there's bound check for buffer[count++]
}

it seems to be a different pattern, feel free to file an issue!

@AndyAyersMS

Copy link
Copy Markdown
Member

it seems to be a different pattern, feel free to file an issue!

Seems like that case is already covered by #116238

@EgorBo
EgorBo merged commit 17b2266 into dotnet:main Nov 18, 2025
@EgorBo
EgorBo deleted the slice-pattern-bce branch November 18, 2025 08:08
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 18, 2025
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 reduce-unsafe

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bounds checks] Conditional Slice pattern

4 participants