-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Make Type.IsGenericType and GetGenericTypeDefinition() JIT intrinsics (and JIT time constants) #96898
Comments
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch Issue DetailsOverviewThis is one more codegen improvement we could leverage for CsWinRT, related to #95929. One scenario we have (here) is to check whether a given type Codegenusing SharpLab.Runtime;
[JitGeneric(typeof(System.Collections.Generic.KeyValuePair<int, int>))]
[JitGeneric(typeof(int))]
static bool IsKVP<T>()
{
return typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(System.Collections.Generic.KeyValuePair<,>);
} Current codegen on .NET 8 x64 (sharplab): Program.<<Main>$>g__IsKVP|0_0[[System.Collections.Generic.KeyValuePair`2[[System.Int32, System.Private.CoreLib],[System.Int32, System.Private.CoreLib]], System.Private.CoreLib]]()
L0000: sub rsp, 0x28
L0004: mov rcx, 0x23e784e3260
L000e: call qword ptr [0x7ffb1060a518]
L0014: test eax, eax
L0016: je short L0040
L0018: mov rcx, 0x23e784e3260
L0022: call qword ptr [0x7ffb1060a568]
L0028: mov rcx, 0x23e783e2728
L0032: cmp rax, rcx
L0035: sete al
L0038: movzx eax, al
L003b: add rsp, 0x28
L003f: ret
L0040: xor eax, eax
L0042: add rsp, 0x28
L0046: ret
Program.<<Main>$>g__IsKVP|0_0[[System.Int32, System.Private.CoreLib]]()
; pretty much the same as above Expected codegen: Program.<<Main>$>g__IsKVP|0_0[[System.Collections.Generic.KeyValuePair`2[[System.Int32, System.Private.CoreLib],[System.Int32, System.Private.CoreLib]], System.Private.CoreLib]]()
L0000: mov eax, 1
L0005: ret
Program.<<Main>$>g__IsKVP|0_0[[System.Int32, System.Private.CoreLib]]()
L0000: xor eax, eax
L0002: ret cc. @jkoritzinsky @MichalStrehovsky @EgorBo
|
Sharing some notes from Discord:
|
I agree there's not many hits, but this would be beneficial for CsWinRT. We're really trying to minimize the binary size in minimal applications and WinRT components, to make the adoption of C# easier to justify in place of C++ (and binary size is one of the main problems you immediately hit when doing so). We have lots of heavily generic code especially in our marshallers, and they all end up being instantiated over |
There are more hits with GitHub Search: |
It would also help with F#'s |
Can we also intrinsify |
Thank you for volunteering! 😄 |
Overview
This is one more codegen improvement we could leverage for CsWinRT, related to #95929. One scenario we have (here) is to check whether a given type
T
is someKeyValuePair<,>
instantiation. This needs to be marshalled in a specific way to match the expectations of WinRT. Right now however, the only way we have to check for this case is to useIsGenericType
andGetGenericTypeDefinition()
, which are not JIT intrinsics. This makes the linker not see this special case, and therefore not correctly trim the code away (or just specialize it on the other hand). This is both marginally slower, but most importantly leaves some codegen size reduction opportunities on the table. We're seeing a bunch ofKeyValuePair<__Canon, __Canon>
instantiations in sizoscope even in sample WinRT components that never useKeyValuePair<,>
at all anywhere. It would be nice if this could just be properly inlined as a constant instead.Codegen
Current codegen on .NET 8 x64 (sharplab):
Expected codegen:
cc. @jkoritzinsky @MichalStrehovsky @EgorBo
The text was updated successfully, but these errors were encountered: