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

Intrinsify typeof(T).IsPrimitive #96226

Merged
merged 12 commits into from
Jan 2, 2024
Merged

Intrinsify typeof(T).IsPrimitive #96226

merged 12 commits into from
Jan 2, 2024

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Dec 20, 2023

Closes #95929

bool Test<T>() => typeof(T).IsPrimitive;

Codegen diff for e.g. Test<int> and Test<string>:

; Assembly listing for method P:Test[int]():ubyte (FullOpts)
-      sub      rsp, 40
-      mov      rcx, 0x27E800054B0      ; 'System.Int32'
-      call     System.RuntimeTypeHandle:GetCorElementType(System.RuntimeType):ubyte
-      mov      ecx, 1
-      shlx     eax, ecx, eax
-      test     eax, 0x3003FFC
-      setne    al
-      movzx    rax, al
-      add      rsp, 40
+      mov      eax, 1
       ret      

; Assembly listing for method P:Test[System.__Canon]():ubyte (FullOpts)
-      sub      rsp, 40
-      mov      qword ptr [rsp+0x20], rcx
-      mov      rcx, qword ptr [rcx+0x38]
-      mov      rcx, qword ptr [rcx]
-      call     CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE
-      mov      rcx, rax
-      call     System.RuntimeTypeHandle:GetCorElementType(System.RuntimeType):ubyte
-      mov      ecx, 1
-      shlx     eax, ecx, eax
-      test     eax, 0x3003FFC
-      setne    al
-      movzx    rax, al
-      add      rsp, 40
+      xor      eax, eax
       ret

Should allow us to avoid listing all primitives by hands for perf reasons (e.g. here)

will add tests.

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

ghost commented Dec 20, 2023

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

Issue Details

Seems to be straightforward unless I am missing something (if my CorInfoType -> CorElementType mapping is correct, otherwise it will need a new JIT-EE API).

Closes #95929

bool Test() => typeof(int).IsPrimitive;
; Method P:Test():ubyte:this (FullOpts)
-      sub      rsp, 40
-      mov      rcx, 0x17E001054B0      ; 'System.Int32'
-      call     System.RuntimeTypeHandle:GetCorElementType(System.RuntimeType):ubyte
-      mov      ecx, 1
-      shlx     eax, ecx, eax
-      test     eax, 0x3003FFC
-      setne    al
-      movzx    rax, al
-      add      rsp, 40
+      mov      eax, 1
       ret      
; Total bytes of code: 45

Some other APIs are now constant-folded too, e.g.:

typeof(T).IsFunctionPointer
typeof(T).HasElementType
typeof(T).IsArray
typeof(T).IsSZArray
typeof(T).IsPointer
typeof(T).IsByRef

will add tests.

Author: EgorBo
Assignees: EgorBo
Labels:

area-CodeGen-coreclr

Milestone: -

@EgorBo
Copy link
Member Author

EgorBo commented Dec 20, 2023

@MihuBot

@EgorBo EgorBo changed the title Fold typeof(T).IsPrimitive and others via GetCorElementType intrinsification Intrinsify typeof(T).IsPrimitive Dec 21, 2023
@EgorBo
Copy link
Member Author

EgorBo commented Dec 21, 2023

@MihuBot

@ShreyasJejurkar
Copy link
Contributor

I love this kind of improvements. ❤🙌

@EgorBo EgorBo marked this pull request as ready for review December 21, 2023 13:38
@jkotas
Copy link
Member

jkotas commented Dec 21, 2023

Any interesting diffs? Any places in this repo to switch over to this intrinsic?

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
@EgorBo
Copy link
Member Author

EgorBo commented Dec 21, 2023

Any interesting diffs? Any places in this repo to switch over to this intrinsic?

No diffs in BCL yet but it was requested a few times in different threads so I decided to add it since it's not a lot of efforts after all. The only place in the BCL I know is(https://github.com/dotnet/runtime/blob/main/src/libraries/System.Collections.Immutable/src/System/Collections/Frozen/Constants.cs#L46-L58)

@EgorBo
Copy link
Member Author

EgorBo commented Jan 2, 2024

@jkotas does it look good to your otherwise?

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@EgorBo EgorBo merged commit f9fea59 into dotnet:main Jan 2, 2024
@EgorBo EgorBo deleted the fold-isprimitive branch January 2, 2024 21:14
@github-actions github-actions bot locked and limited conversation to collaborators Feb 2, 2024
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.

Make Type.IsPrimitive a JIT intrinsic (and JIT time constant)
4 participants