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

Enable Dynamic PGO for IsIntanceOfAny and ChkCastAny #86791

Closed
wants to merge 2 commits into from

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented May 26, 2023

public class Class1 {}
public class Class2 : Class1 {}

[Benchmark]
public bool Test() => GenericCast<Class1>(new Class2());

[MethodImpl(MethodImplOptions.NoInlining)]
public bool GenericCast<T>(object o) => o is T;
Method Toolchain Mean Ratio Code Size
Test \Core_Root_PR\corerun.exe 2.292 ns 1.00 112 B
Test \Core_Root_base\corerun.exe 3.489 ns 1.54 89 B
; Assembly listing for method Program:GenericCast[System.__Canon](System.Object):bool:this
       sub      rsp, 40
       mov      qword ptr [rsp+20H], rdx
+      mov      rax, r8
+      test     rax, rax
+      je       SHORT G_M24407_IG05
+      mov      rcx, 0x7FFBE47CAF78  ;; Class2
+      cmp      qword ptr [rax], rcx
+      je       SHORT G_M24407_IG05
       mov      rcx, qword ptr [rdx+38H]
       mov      rcx, qword ptr [rcx]
       mov      rdx, r8
       call     [CORINFO_HELP_ISINSTANCEOFANY]
+G_M24407_IG05:  
       test     rax, rax
       setne    al
       movzx    rax, al
       add      rsp, 40
       ret      
-; Total bytes of code 39
+; Total bytes of code 62

(T)o is also improved because in #86728 we made it to use T as a fast path which won't help here (T is Class1 while the actual object is Class2).

@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 May 26, 2023
@ghost ghost assigned EgorBo May 26, 2023
@ghost
Copy link

ghost commented May 26, 2023

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

Issue Details
public class Class1 {}
public class Class2 : Class1 {}

[Benchmark]
public bool Test() => GenericCast<Class1>(new Class2());

[MethodImpl(MethodImplOptions.NoInlining)]
public bool GenericCast<T>(object o) => o is T;
Method Toolchain Mean Ratio Code Size
Test \Core_Root_PR\corerun.exe 2.292 ns 1.00 112 B
Test \Core_Root_base\corerun.exe 3.489 ns 1.54 89 B
; Assembly listing for method Program:GenericCast[System.__Canon](System.Object):bool:this
       sub      rsp, 40
       mov      qword ptr [rsp+20H], rdx
+      mov      rax, r8
+      test     rax, rax
+      je       SHORT G_M24407_IG05
+      mov      rcx, 0x7FFBE47CAF78  ;; Class2
+      cmp      qword ptr [rax], rcx
+      je       SHORT G_M24407_IG05
       mov      rcx, qword ptr [rdx+38H]
       mov      rcx, qword ptr [rcx]
       mov      rdx, r8
       call     [CORINFO_HELP_ISINSTANCEOFANY]
+G_M24407_IG05:  
       test     rax, rax
       setne    al
       movzx    rax, al
       add      rsp, 40
       ret      
-; Total bytes of code 39
+; Total bytes of code 62

(T)o is also improved because in #86728 we made it to use T as a fast path which won't help here (T is Class1 while the actual object is Class2).

Author: EgorBo
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@EgorBo
Copy link
Member Author

EgorBo commented May 26, 2023

I seem to see stable improvements in TechEmpower with DOTNET_JitProfileCasts=1 🤔, e.g. +2-3% in Plaintext-MVC (default vs this PR + DOTNET_JitProfileCasts=1)

image

(12 runs in total)

@EgorBo
Copy link
Member Author

EgorBo commented May 26, 2023

/azp run runtime-coreclr pgostress

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo EgorBo closed this May 26, 2023
@EgorBo
Copy link
Member Author

EgorBo commented May 26, 2023

Actually, it's not legal in the current shape I guess.

 bool GenericCast<T>(object o) => o is T;

if we bake o is Class2 here and then use this shared method for completely differen type we might get a false-positive case.

@EgorBo EgorBo reopened this May 26, 2023
@EgorBo EgorBo closed this May 26, 2023
@dotnet dotnet locked as resolved and limited conversation to collaborators Jun 25, 2023
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.

None yet

1 participant