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

Optimize modification of immutable struct #104209

Open
PavelCibulka opened this issue Jun 30, 2024 · 2 comments
Open

Optimize modification of immutable struct #104209

PavelCibulka opened this issue Jun 30, 2024 · 2 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

@PavelCibulka
Copy link

When modifying immutable struct (by creating new one and coppying everything except some fields) generated code contains unnecessary instructions.

Examples (.Net 8)
Immutable struct code:

    public readonly struct StarData (float mass, float mDot, float logMsolLsol, float tSol) {
        public readonly float mass= mass;
        public readonly float mDot = mDot;
        public readonly float logMsolLsol = logMsolLsol;
        public readonly float tSol = tSol;

        public static StarData SetTsol(StarData s, float tSol){
            return new(
                s.mass,
                s.mDot,
                s.logMsolLsol,
                tSol);
        }
    }

generated code for SetTsol function (https://godbolt.org/ .NET 8.0)

StarData:SetTsol(StarData,float):StarData (FullOpts):
G_M19045_IG01:  ;; offset=0x0000
       sub      rsp, 40
       vzeroupper 
       vmovsd   qword ptr [rsp+0x18], xmm0
       vmovsd   qword ptr [rsp+0x20], xmm1
G_M19045_IG02:  ;; offset=0x0013
       vmovss   xmm0, dword ptr [rsp+0x18]
       vmovss   xmm1, dword ptr [rsp+0x1C]
       vmovss   xmm3, dword ptr [rsp+0x20]
       vmovss   dword ptr [rsp+0x08], xmm0
       vmovss   dword ptr [rsp+0x0C], xmm1
       vmovss   dword ptr [rsp+0x10], xmm3
       vmovss   dword ptr [rsp+0x14], xmm2
       vmovsd   xmm0, qword ptr [rsp+0x08]
       vmovsd   xmm1, qword ptr [rsp+0x10]
G_M19045_IG03:  ;; offset=0x0049
       add      rsp, 40
       ret      

There are three pairs (six) of unnecessary instructions that just cancel each other, in this example. Complier should generate same code as for mutable code. Example:

    public struct StarData (float mass, float mDot, float logMsolLsol, float tSol) {
        public float mass= mass;
        public float mDot = mDot;
        public float logMsolLsol = logMsolLsol;
        public float tSol = tSol;

        public static StarData SetTsol(StarData s, float tSol){
            s.tSol=tSol;
            return s;
        }
    }

generated code for SetTsol function (https://godbolt.org/ .NET 8.0)

StarData:SetTsol(StarData,float):StarData (FullOpts):
G_M19045_IG01:  ;; offset=0x0000
       sub      rsp, 24
       vzeroupper 
       vmovsd   qword ptr [rsp+0x08], xmm0
       vmovsd   qword ptr [rsp+0x10], xmm1
G_M19045_IG02:  ;; offset=0x0013
       vmovss   dword ptr [rsp+0x14], xmm2
       vmovsd   xmm0, qword ptr [rsp+0x08]
       vmovsd   xmm1, qword ptr [rsp+0x10]
G_M19045_IG03:  ;; offset=0x0025
       add      rsp, 24
       ret      
@PavelCibulka PavelCibulka added the tenet-performance Performance related issue label Jun 30, 2024
@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 Jun 30, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jun 30, 2024
Copy link
Contributor

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

@EgorBo EgorBo removed the untriaged New issue has not been triaged by the area owner label Jun 30, 2024
@EgorBo EgorBo added this to the Future milestone Jun 30, 2024
@jakobbotsch
Copy link
Member

The fundamental problem here is the same as in #11992, #89374, #91517, #93105, #96372. Related comment: #96372 (comment)

@jakobbotsch jakobbotsch self-assigned this Jul 1, 2024
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

No branches or pull requests

3 participants