dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.63k stars 4.57k forks source link

JIT: Unnecessary bounds checks for MD array accesses #103321

Open jakobbotsch opened 1 month ago

jakobbotsch commented 1 month ago

For code like:

void Foo(int[,] foo)
{
    for (int i = foo.GetLowerBound(0); i < foo.GetUpperBound(0); i++)
    {
        int count = i * 100;

        for (int j = foo.GetLowerBound(1); j < foo.GetUpperBound(1); j++)
        {
            foo[i, j] = count++;
        }
    }
}

we generate unnecessary bounds checks on the accesses within the loop. This pattern is also what Roslyn produces for foreach over MD arrays, so we should be able to optimize it better.

Reported by @richlander

category:cq theme:md-arrays skill-level:expert cost:large

dotnet-policy-service[bot] commented 1 month ago

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