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

Duplicated loop body #52586

Closed
iSazonov opened this issue May 11, 2021 · 5 comments
Closed

Duplicated loop body #52586

iSazonov opened this issue May 11, 2021 · 5 comments
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue
Milestone

Comments

@iSazonov
Copy link
Contributor

iSazonov commented May 11, 2021

Follow pattern results in a duplicated loop body:

        for (int i = 0; i < arr.Length; i++)
        {
            for (int j = i; j < arr.Length; j++)
            {
                var b = arr[j];
                Console.WriteLine(b);
            }
        }

See https://sharplab.io/#v2:EYLgtghglgdgNAFxBAzmAPgAQEwEYCwAUJgMwAEOZAwmQN5FmMXmYAsZAsgBSwJkwBKOgyaiAbhABOZKdIC8/AKYB3MrwDaMALoBuEaMYAzAPbSeMPlDIKADDrVkAPDMmSAdABlFMAOYIAFvZQANTBAvoG9IQGMWQmZrxkAFbWavYpzrKe3n6ByaHh0bGiUcXFAPTlEtIQqbLqULoRZUyVmLgAnFwQAnpFLeJSZMB1rupJTf0DjO1dwL3NsQC+i4wrU+tLQA

category:cq
theme:loop-opt
skill-level:expert
cost:medium
impact:small

@iSazonov iSazonov added the tenet-performance Performance related issue label May 11, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label May 11, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@EgorBo
Copy link
Member

EgorBo commented May 11, 2021

The nested loop doesn't know that i is never negative (assertprop?) so it can't elide bound checks and is cloned by LoopCloning so at least the cloned loop will be able to iterate without them.

@EgorBo EgorBo added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 11, 2021
@iSazonov
Copy link
Contributor Author

The nested loop doesn't know

I guess you mean "CodeGen doesn't know". From this I conclude this comes from the fact CodeGen generates the code based only on static analysis. But at runtime we can check "j = i is less arr.Length" before the cycle.

@AndyAyersMS
Copy link
Member

cc @BruceForstall

@BruceForstall
Copy link
Member

BruceForstall commented May 13, 2021

#8558 Covers this somewhat: loop cloning kicks in for the inner loop, eliminates the "fast path" bounds check. But later on, assertion prop gets rid of the array null check it inserted, and range prop gets rid of the "slow path" bounds check, so the loops are the same. Assertion prop doesn't get rid of the extra "i < 0" check that gets inserted (which it should). If that was eliminated, we're left with #5820.

@BruceForstall BruceForstall added this to the 6.0.0 milestone May 13, 2021
@BruceForstall BruceForstall removed the untriaged New issue has not been triaged by the area owner label May 13, 2021
@BruceForstall BruceForstall modified the milestones: 6.0.0, Future May 17, 2021
@ghost ghost locked as resolved and limited conversation to collaborators May 23, 2023
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 tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests

4 participants