Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fold "new T[size].Length" into "size" #114845

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 1 commit into from
Apr 22, 2025
Merged

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Apr 20, 2025

Closes #114744

static void Test(int count)
{
    int[] arrA = new int[count];
    float[] arrB = new float[count];
    for (int i = 0; i < count; i++)
    {
        arrA[i] = 0;     // bounds check
        arrB[i] = 0.0f;  // bounds check
    }
}

Codegen diff: https://www.diffchecker.com/7iCHeDLt/

Seems like this transformation produces nice SPMI diffs.

@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 20, 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 marked this pull request as ready for review April 20, 2025 17:02
@Copilot Copilot AI review requested due to automatic review settings April 20, 2025 17:02
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 implements an optimization to fold the expression new T[size].Length into the original size argument, reducing unnecessary bounds checks and streamlining array length calculations.

  • Introduces a new folding case for ARR_LENGTH with new array expressions.
  • Handles different type cases (TYP_LONG and TYP_INT) to extract the original size.

@EgorBo
Copy link
Member Author

EgorBo commented Apr 20, 2025

PTAL @AndyAyersMS @jakobbotsch @dotnet/jit-contrib simple opt with diffs.

@EgorBo EgorBo requested a review from AndyAyersMS April 20, 2025 17:04
@neon-sunset

This comment was marked as resolved.

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.

JIT: Redundant bounds check for when loopUpperBound==arrayLength
3 participants