Skip to content

Optimize generated code for Fixed statement #4872

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

Closed
Anderman opened this issue Dec 29, 2015 · 2 comments
Closed

Optimize generated code for Fixed statement #4872

Anderman opened this issue Dec 29, 2015 · 2 comments
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI enhancement Product code improvement that does NOT require public API changes/additions optimization tenet-performance Performance related issue

Comments

@Anderman
Copy link

An extra statement is needed to use register vars

I have two functions

        private static unsafe int UnsafeSlow(byte[] src, int srcOffset, byte[] dst, int dstOffset)
        {
            var x = 0;
            fixed (byte* pSrc = &src[srcOffset])
            {
                x = *(pSrc + 0);
                x += *(pSrc + 1);
                return x;
            }
        }

With extra assignment

        private static unsafe int UnsafeFast(byte[] src, int srcOffset, byte[] dst, int dstOffset)
        {
            var x = 0;
            fixed (byte* pSrc1 = &src[srcOffset])
            {
                byte* pSrc = pSrc1;
                x = *(pSrc + 0);
                x += *(pSrc + 1);
                return x;
            }
        }

Code generated slow (line 9 and 11 do nothing)

            fixed (byte* pSrc = &src[srcOffset])
1  sub         rsp,28h  
2  xor         eax,eax  
3  mov         qword ptr [rsp+20h],rax  
4  cmp         edx,dword ptr [rcx+8]  
5  jae         000007FF622854D5  
6  movsxd      rax,edx  
7  lea         rax,[rcx+rax+10h]  
8  mov         qword ptr [rsp+20h],rax  
            {
                x = *(pSrc + 0);
9  mov         rax,qword ptr [rsp+20h]  
10  movzx       eax,byte ptr [rax]  
11  mov         rdx,qword ptr [rsp+20h]  
12  movzx       edx,byte ptr [rdx+1]  
13  add         eax,edx  
14  add         rsp,28h  
15  ret  

Fast version ( Line 9 does nothing)

1  sub         rsp,28h  
2  xor         eax,eax  
3  mov         qword ptr [rsp+20h],rax  
4  cmp         edx,dword ptr [rcx+8]  
5  jae         000007FF62285520  
6  movsxd      rax,edx  
7  lea         rax,[rcx+rax+10h]  
8  mov         qword ptr [rsp+20h],rax  
            {
                byte* pSrc = pSrc1;
9  mov         rax,qword ptr [rsp+20h]  
                x = *(pSrc + 0);
10  movzx       edx,byte ptr [rax]  
11  movzx       eax,byte ptr [rax+1]  
12  add         eax,edx  
13  add         rsp,28h  
14  ret  

category:cq
theme:pinning
skill-level:intermediate
cost:small

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 30, 2020
@msftgits msftgits added this to the Future milestone Jan 30, 2020
@BruceForstall BruceForstall added the JitUntriaged CLR JIT issues needing additional triage label Oct 28, 2020
@deeprobin
Copy link
Contributor

I can't reproduce this.
Sharplab

@teo-tsirpanis
Copy link
Contributor

Closing, thanks for the heads-up @deeprobin. This issue seems to have been fixed since at least .NET Core 3.1.

@teo-tsirpanis teo-tsirpanis removed this from the Future milestone Aug 16, 2022
@teo-tsirpanis teo-tsirpanis removed the JitUntriaged CLR JIT issues needing additional triage label Aug 16, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 16, 2022
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 enhancement Product code improvement that does NOT require public API changes/additions optimization tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests

5 participants