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

Unnecessary shr emitted for repeated multiplication #74020

Open
danmoseley opened this issue Aug 16, 2022 · 5 comments
Open

Unnecessary shr emitted for repeated multiplication #74020

danmoseley opened this issue Aug 16, 2022 · 5 comments
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@danmoseley
Copy link
Member

danmoseley commented Aug 16, 2022

Noted in discussion here about the recent DateTime perf optimization, that we could not rely on the JIT to safely collapse multiplication. In this example below, there's an extra shr. Same on x86.

sharplab

class Program
{
    uint f(uint u2) {
        return u2 / 2939745 / 4;
    }

    uint g(uint u2) {
        return u2 / 11758980;
    }
}
Program.f(UInt32)
    L0000: mov eax, edx
    L0002: imul rax, 0x5b4fffcb
    L0009: shr rax, 0x34
    L000d: shr eax, 2
    L0010: ret

Program.g(UInt32)
    L0000: mov eax, edx
    L0002: imul rax, 0x5b4fffcb
    L0009: shr rax, 0x36
    L000d: ret

cc @cassioneri

category:cq
theme:basic-cq
skill-level:beginner
cost:small
impact:small

@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 Aug 16, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 16, 2022
@ghost
Copy link

ghost commented Aug 16, 2022

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

Issue Details

Noted in discussion here about the recent DateTime perf optimization, that we could not rely on the JIT to safely collapse multiplication. In this example below, there's an extra shr. Same on x86.

sharplab

class Program
{
    uint f(uint u2) {
        return u2 / 2939745 / 4;
    }

    uint g(uint u2) {
        return u2 / 11758980;
    }
}
Program.f(UInt32)
    L0000: mov eax, edx
    L0002: imul rax, 0x5b4fffcb
    L0009: shr rax, 0x34
    L000d: shr eax, 2
    L0010: ret

Program.g(UInt32)
    L0000: mov eax, edx
    L0002: imul rax, 0x5b4fffcb
    L0009: shr rax, 0x36
    L000d: ret

cc @cassioneri

Author: danmoseley
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@danmoseley
Copy link
Member Author

Can repeated shr (or shl) be safely collapsed as a peephole optimization? Or are there subtleties/sideeffects that cannot be done in that way.

@danmoseley
Copy link
Member Author

Adding the Godbolt link from @cassioneri for gcc/clang: https://godbolt.org/z/T7M8Pqq1e .. MSVC seems to miss the same thing.

@EgorBo
Copy link
Member

EgorBo commented Aug 16, 2022

Looks like a good first issue for jit first-time contributors 🙂

@EgorBo EgorBo added this to the 8.0.0 milestone Aug 16, 2022
@EgorBo EgorBo added good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors and removed untriaged New issue has not been triaged by the area owner labels Aug 16, 2022
@SkiFoD
Copy link
Contributor

SkiFoD commented Aug 19, 2022

Hey, I would like to try to work with this issue.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 24, 2022
SkiFoD added a commit to SkiFoD/runtime that referenced this issue Oct 12, 2022
SkiFoD added a commit to SkiFoD/runtime that referenced this issue Oct 12, 2022
SkiFoD added a commit to SkiFoD/runtime that referenced this issue Oct 12, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Mar 27, 2023
@JulieLeeMSFT JulieLeeMSFT modified the milestones: 8.0.0, Future Jun 9, 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 good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants