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

RyuJIT: Optimize "X / POW2_CNS" via cmovns #41549

Open
EgorBo opened this issue Aug 29, 2020 · 0 comments
Open

RyuJIT: Optimize "X / POW2_CNS" via cmovns #41549

EgorBo opened this issue Aug 29, 2020 · 0 comments
Assignees
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

@EgorBo
Copy link
Member

EgorBo commented Aug 29, 2020

int Test(int x) => x / 4;

Current codegen:

       8BC1                 mov      eax, ecx
       C1F81F               sar      eax, 31
       83E003               and      eax, 3
       03C1                 add      eax, ecx
       C1F802               sar      eax, 2
       C3                   ret      
; Total bytes of code: 14

Expected codegen:

       8D4103               lea      eax, [rcx+3]
       85C9                 test     ecx, ecx
       0F49C1               cmovns   eax, ecx
       C1F802               sar      eax, 2
       C3                   ret      
; Total bytes of code: 12

This micro-peephole-optimization was added recently in LLVM (PR, see BuildSDIVPow2), see godbolt: https://godbolt.org/z/M153rj
My working (ugly) prototype for RyuJIT: EgorBo@9b1d149 (I believe it should be done the other way - I'd introduce a GT_SELECT/GT_CMOV operator so we can later use it for other cmov-based optimizations, e.g. remove branches)

Benchmark

[Benchmark]
public void Test()
{
    for (int i = 0; i < 10000; i++)
        Consume(i / 4 + i / 8 + i / 16 + i / 32);
}

[MethodImpl(MethodImplOptions.NoInlining)]
static void Consume(int x) { }
          | Method |     Mean |    Error |   StdDev |
          |------- |---------:|---------:|---------:|
   master |   Test | 19.14 us | 0.017 us | 0.013 us |
prototype |   Test | 17.02 us | 0.016 us | 0.013 us | ~11% faster

/cc @AntonLapounov

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

@EgorBo EgorBo added the tenet-performance Performance related issue label Aug 29, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI untriaged New issue has not been triaged by the area owner labels Aug 29, 2020
@BruceForstall BruceForstall added this to the 6.0.0 milestone Aug 31, 2020
@BruceForstall BruceForstall removed the untriaged New issue has not been triaged by the area owner label Aug 31, 2020
@BruceForstall BruceForstall added the JitUntriaged CLR JIT issues needing additional triage label Oct 28, 2020
@BruceForstall BruceForstall removed the JitUntriaged CLR JIT issues needing additional triage label Nov 10, 2020
@BruceForstall BruceForstall modified the milestones: 6.0.0, Future Nov 10, 2020
@TIHan TIHan self-assigned this Oct 31, 2022
@TIHan TIHan modified the milestones: Future, 8.0.0 Jan 17, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jan 27, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Feb 26, 2023
@TIHan TIHan 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 tenet-performance Performance related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants