Skip to content

Optimize new DateTime(const, const, const)#64612

Closed
EgorBo wants to merge 4 commits into
dotnet:mainfrom
EgorBo:const-date
Closed

Optimize new DateTime(const, const, const)#64612
EgorBo wants to merge 4 commits into
dotnet:mainfrom
EgorBo:const-date

Conversation

@EgorBo

@EgorBo EgorBo commented Feb 1, 2022

Copy link
Copy Markdown
Member

This PR optimizes new DateTime(const, const, const) case, e.g.:

[Benchmark]
public DateTime ConstDate() => new (2022, 12, 10);

Currently emits:

; Method Test:ConstDate():System.DateTime:this
G_M10442_IG01:              ;; offset=0000H
       4883EC28             sub      rsp, 40
G_M10442_IG02:              ;; offset=0004H
       48B9104AD1CEF87F0000 mov      rcx, 0x7FF8CED14A10
       BAF9000000           mov      edx, 249
       E808639F5F           call     CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE
       48B8201280B98B020000 mov      rax, 0x28BB9801220
       488B00               mov      rax, gword ptr [rax]
       8B5008               mov      edx, dword ptr [rax+8]
       83FA0C               cmp      edx, 12
       762B                 jbe      SHORT G_M10442_IG05
       8B5040               mov      edx, dword ptr [rax+64]
       8B403C               mov      eax, dword ptr [rax+60]
       2BD0                 sub      edx, eax
       83FA0A               cmp      edx, 10
       7218                 jb       SHORT G_M10442_IG04
       0574430B00           add      eax, 0xB4374
       48BA00C0692AC9000000 mov      rdx, 0xC92A69C000
       480FAFC2             imul     rax, rdx
G_M10442_IG03:              ;; offset=004DH
       4883C428             add      rsp, 40
       C3                   ret      
G_M10442_IG04:              ;; offset=0052H
       E8B183FFFF           call     System.ThrowHelper:ThrowArgumentOutOfRange_BadYearMonthDay()
       CC                   int3     
G_M10442_IG05:              ;; offset=0058H
       E803AD685F           call     CORINFO_HELP_RNGCHKFAIL
       CC                   int3     
; Total bytes of code: 94

New codegen:

; Method Test:ConstDate():System.DateTime:this
G_M10442_IG01:              ;; offset=0000H
G_M10442_IG02:              ;; offset=0000H
       48B80040044BBAE2D908 mov      rax, 0x8D9E2BA4B044000
G_M10442_IG03:              ;; offset=000AH
       C3                   ret      
; Total bytes of code: 11

Benchmarks

Method Toolchain y d m Mean
ConstDate \Core_Root_base\corerun.exe ? ? ? 0.4368 ns
ConstDate \Core_Root\corerun.exe ? ? ? 0.0266 ns
ConstMonth \Core_Root_base\corerun.exe 2022 1 ? 1.2839 ns
ConstMonth \Core_Root\corerun.exe 2022 1 ? 0.7184 ns
VarDate \Core_Root_base\corerun.exe 2022 1 2 1.6864 ns
VarDate \Core_Root\corerun.exe 2022 1 2 1.6519 ns

Feel free to reject it 🙂, eventually we should be able to get constant case codegen even with the current code, for that we need:

  1. Wait till C# gets ReadOnlySpan<any primitive> for RVA data
  2. Make s_daysToMonth365 and s_daysToMonth366 ReadOnlySpan<uint>
  3. Implement RVA[const index] folding in JIT - unfortunately it's a bit tricky but should be doable
  4. Forward Substitution (probably a bit more advanced than what Andy's currently doing)

@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Feb 1, 2022
@ghost ghost assigned EgorBo Feb 1, 2022
@ghost

ghost commented Feb 1, 2022

Copy link
Copy Markdown

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

Issue Details

This PR optimizes new DateTime(const, const, const) case, e.g.:

[Benchmark]
public DateTime ConstDate() => new (2022, 12, 10);

Currently emits:

; Method Test:ConstDate():System.DateTime:this
G_M10442_IG01:              ;; offset=0000H
       4883EC28             sub      rsp, 40
G_M10442_IG02:              ;; offset=0004H
       48B9104AD1CEF87F0000 mov      rcx, 0x7FF8CED14A10
       BAF9000000           mov      edx, 249
       E808639F5F           call     CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE
       48B8201280B98B020000 mov      rax, 0x28BB9801220
       488B00               mov      rax, gword ptr [rax]
       8B5008               mov      edx, dword ptr [rax+8]
       83FA0C               cmp      edx, 12
       762B                 jbe      SHORT G_M10442_IG05
       8B5040               mov      edx, dword ptr [rax+64]
       8B403C               mov      eax, dword ptr [rax+60]
       2BD0                 sub      edx, eax
       83FA0A               cmp      edx, 10
       7218                 jb       SHORT G_M10442_IG04
       0574430B00           add      eax, 0xB4374
       48BA00C0692AC9000000 mov      rdx, 0xC92A69C000
       480FAFC2             imul     rax, rdx
G_M10442_IG03:              ;; offset=004DH
       4883C428             add      rsp, 40
       C3                   ret      
G_M10442_IG04:              ;; offset=0052H
       E8B183FFFF           call     System.ThrowHelper:ThrowArgumentOutOfRange_BadYearMonthDay()
       CC                   int3     
G_M10442_IG05:              ;; offset=0058H
       E803AD685F           call     CORINFO_HELP_RNGCHKFAIL
       CC                   int3     
; Total bytes of code: 94

New codegen:

; Method Test:ConstDate():System.DateTime:this
G_M10442_IG01:              ;; offset=0000H
G_M10442_IG02:              ;; offset=0000H
       48B80040044BBAE2D908 mov      rax, 0x8D9E2BA4B044000
G_M10442_IG03:              ;; offset=000AH
       C3                   ret      
; Total bytes of code: 11

Benchmarks

Method Toolchain y d m Mean
ConstDate \Core_Root_base\corerun.exe ? ? ? 0.4368 ns
ConstDate \Core_Root\corerun.exe ? ? ? 0.0266 ns
ConstMonth \Core_Root_base\corerun.exe 2022 1 ? 1.2839 ns
ConstMonth \Core_Root\corerun.exe 2022 1 ? 0.7184 ns
VarDate \Core_Root_base\corerun.exe 2022 1 2 1.6864 ns
VarDate \Core_Root\corerun.exe 2022 1 2 1.6519 ns

Feel free to reject it 🙂, eventually we should be able to get constant case codegen even with the current code, for that we need:

  1. Wait till C# gets ReadOnlySpan<any primitive> for RVA data
  2. Make s_daysToMonth365 and s_daysToMonth366 ReadOnlySpan<uint>
  3. Implement RVA[const index] folding in JIT - unfortunately it's a bit tricky but should be doable
  4. Forward Substitution (probably a bit more advanced than what Andy's currently doing)
Author: EgorBo
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@EgorBo

EgorBo commented Feb 1, 2022

Copy link
Copy Markdown
Member Author

Closing as it's unlikely the hack worth it or people use this pattern on hot paths, but it should be a good challenge for jit to do everything under the hood.

Another note that my single line change in morph.cpp seems to trigger some diffs: Total bytes of delta: -1496 (-0.00 % of base)

@EgorBo EgorBo closed this Feb 1, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 3, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant