Skip to content

Remove redundant bounds checks around arr[arr.Length - cns] pattern #116105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 30, 2025

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented May 29, 2025

Closes #116088

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 May 29, 2025
Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@EgorBo EgorBo force-pushed the drop-more-bounds-checks branch from 7c46126 to b417da0 Compare May 29, 2025 18:49
Copy link
Contributor

@Copilot 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 removes redundant bounds checks around the arr[arr.Length - cns] pattern by tightening the comparison operator and directly eliminating unnecessary checks when certain patterns are detected.

  • In rangecheck.cpp the check is converted from a >= to a > comparison to correctly reflect that arr.Length must be strictly greater than the constant.
  • In assertionprop.cpp a new lambda (dropBoundsCheck) is introduced to centralize and simplify the removal of redundant checks based on different index patterns.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/coreclr/jit/rangecheck.cpp Adjusts the bounds check operator and inline comment to ensure strict inequality.
src/coreclr/jit/assertionprop.cpp Refactors redundant bounds check removal using an inline lambda to enable early returns.

if (arrBndsChk != stmt->GetRootNode())
{
// Defer the removal.
arrBndsChk->gtFlags |= GTF_CHK_INDEX_INBND;
Copy link
Member Author

Choose a reason for hiding this comment

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

this is the pre-existing logic, I didn't change it, just moved up.

We can drop the bounds checks node unconditionally in fact, I might look into it later

@EgorBo
Copy link
Member Author

EgorBo commented May 30, 2025

PTAL @jakobbotsch @dotnet/jit-contrib

@EgorBo EgorBo requested a review from jakobbotsch May 30, 2025 12:39
int indexCns = comp->vnStore->GetConstantInt32(indexVN);
if (indexCns >= 0)
{
cmpOper = GT_GE;
cmpOper = GT_GT;
Copy link
Member Author

Choose a reason for hiding this comment

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

A small pre-existing issue. E.g. arr[1] means that arr is at least 2 elements long, not 1.

@EgorBo
Copy link
Member Author

EgorBo commented May 30, 2025

/ba-g "NativeAOT timeout"

@EgorBo EgorBo merged commit 9c7434b into dotnet:main May 30, 2025
106 of 110 checks passed
@EgorBo EgorBo deleted the drop-more-bounds-checks branch May 30, 2025 14:39
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.

Span index access not eliminating bounds checks depending on access order
2 participants