Skip to content

Remove bounds checks for Phi indices - #121640

Merged
EgorBo merged 4 commits into
dotnet:mainfrom
EgorBo:remove-phi-bound-checks
Nov 16, 2025
Merged

EgorBo merged 4 commits into
dotnet:mainfrom
EgorBo:remove-phi-bound-checks

Conversation

@EgorBo

@EgorBo EgorBo commented Nov 14, 2025 •

Copy link
Copy Markdown
Member

Closes #121617

In global assertion prop we have an optimization that tries to optimize bound checks for arr[cns] if we already have ArrBnds assertion for the same array with another constant index that is greater than this one, e.g.:

arr[2] = 0; // creates an assertion that arr is at least 3 elements.
arr[1] = 0; // bound check is optimized away

This PR extends this logic by handling Phi indices.

Should remove bounds checks for cases like:

arr[cond1 ? 10 : 12] = 0; // means arr has at least 11 elements
arr[8] = 0; // redundant bounds check

or even:

arr[cond1 ? 10 : 12] = 0;
arr[cond2 ? 1 : 2] = 0;

Some nice diffs (although, mostly because I removed unnecessary TypeOfVN checks)

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

@EgorBo
EgorBo marked this pull request as ready for review November 14, 2025 21:37
Copilot AI review requested due to automatic review settings November 14, 2025 21:37

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 extends bounds check elimination to handle Phi indices (conditional expressions like ternary operators). The optimization identifies when an array has been accessed with a set of potential indices and eliminates redundant bounds checks for subsequent accesses that are guaranteed to be within bounds. For example, after arr[cond ? 10 : 5], the compiler knows the array has at least 6 elements, allowing it to eliminate the bounds check for arr[2].

Key Changes

  • Replaced simple constant comparison logic with phi-aware analysis
  • Introduced tryGetMaxOrMinConst lambda to find minimum/maximum constants across phi predecessors
  • Removed unnecessary type checks that were previously required

Comment thread src/coreclr/jit/assertionprop.cpp Resolved
Comment thread src/coreclr/jit/assertionprop.cpp Resolved
@EgorBo

EgorBo commented Nov 14, 2025 •

Copy link
Copy Markdown
Member Author

PTAL @AndyAyersMS @dotnet/jit-contrib

@EgorBo

EgorBo commented Nov 14, 2025

Copy link
Copy Markdown
Member Author

ah. oops, copilot has noticed a bug in my logic (tests didn't find anything), let me re-run the CI diffs, they should be smaller now 🙁

Comment thread src/coreclr/jit/assertionprop.cpp Resolved
Comment thread src/coreclr/jit/assertionprop.cpp Resolved
@EgorBo
EgorBo enabled auto-merge (squash) November 16, 2025 05:08
@EgorBo
EgorBo merged commit 0b9d2cc into dotnet:main Nov 16, 2025
@EgorBo
EgorBo deleted the remove-phi-bound-checks branch November 16, 2025 05:08
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 16, 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.

ArrBnds assertion with Phi index doesn't remove bounds checks.

4 participants