Skip to content
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

Loop condition i != T.Length bounds check not eliminated #84697

Open
xtqqczze opened this issue Apr 12, 2023 · 6 comments
Open

Loop condition i != T.Length bounds check not eliminated #84697

xtqqczze opened this issue Apr 12, 2023 · 6 comments
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone

Comments

@xtqqczze
Copy link
Contributor

xtqqczze commented Apr 12, 2023

This is not a particularly common pattern, but it is a missed optimization all the same.

Consider:

    public static int Array_NE(byte[] src)
    {    
        int sum = 0;
        for (int i = 0; i != src.Length; i++)
            sum += src[i];
        return sum;
    }
    public static int Array_LT(byte[] src)
    {    
        int sum = 0;
        for (int i = 0; i < src.Length; i++)
            sum += src[i];
        return sum;
    }

A bounds check is eliminated for the second method but not the first.

Applies to T[], string, Span<T>, ReadOnlySpan<T>, etc.

https://csharp.godbolt.org/z/bKzrE9Wjq

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 12, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Apr 12, 2023
@ghost
Copy link

ghost commented Apr 12, 2023

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

Issue Details

null

Author: xtqqczze
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@xtqqczze xtqqczze changed the title Bounds check not eliminated for Loop condition i != T.Length bounds check not eliminated Apr 12, 2023
@EgorBo EgorBo added this to the Future milestone Apr 12, 2023
@EgorBo EgorBo removed the untriaged New issue has not been triaged by the area owner label Apr 12, 2023
@EgorBo
Copy link
Member

EgorBo commented Apr 12, 2023

cc @BruceForstall if this loop shape can be recognized as normal, monotonically increased one

@xtqqczze
Copy link
Contributor Author

xtqqczze commented Apr 12, 2023

Also missed in the backward iteration case, I think there there is an existing issue though.

    public static int Array_Backward_GE(Byte[] src)
    {    
        int sum = 0;
        for (int i = src.Length - 1; i >= 0; i--)
            sum += src[i];
        return sum;
    }

@hughbe
Copy link
Contributor

hughbe commented Apr 12, 2023

Also missed in the backward iteration case, I think there there is an existing issue though.

    public static int Array_GE(Byte[] src)
    {    
        int sum = 0;
        for (int i = src.Length - 1; i >= 0; i--)
            sum += src[i];
        return sum;
    }

This could be a common pattern in cases where people have copied things from c

@xtqqczze
Copy link
Contributor Author

Also missed in the backward iteration case, I think there there is an existing issue though.

Found the existing issue: #9505.

@xtqqczze
Copy link
Contributor Author

xtqqczze commented Apr 12, 2023

Backward iteration case for i != T.Length, very common in C:

    public static int Array_Backward_NE(Byte[] src)
    {    
        int sum = 0;
        int i = src.Length;
        while (i != 0)
        {
            sum += src[--i];
        }
        return sum;
    }

@BruceForstall BruceForstall self-assigned this Apr 14, 2023
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

No branches or pull requests

4 participants