-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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.IsPrimitive a JIT intrinsic (and JIT time constant) #95929
Comments
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch Issue DetailsOverviewWe're currently working on AOT support for CsWinRT, and as part of that we're also looking into ways to reduce the binary size footprint, which is currently very large even for very simple applications. One issue we've noticed in particular is the static constructor in the It would be beneficial to use Codegen[JitGeneric(typeof(int))]
static bool Test<T>() => typeof(T).IsPrimitive; Current codegen on .NET 8 x64 (sharplab): Program.<<Main>$>g__Test|0_0[[System.Int32, System.Private.CoreLib]]()
L0000: sub rsp, 0x28
L0004: mov rcx, 0x158e6ad3660
L000e: call 0x00007ffb5ea7e7a0
L0013: mov ecx, 1
L0018: shlx eax, ecx, eax
L001d: test eax, 0x3003ffc
L0022: setne al
L0025: movzx eax, al
L0028: add rsp, 0x28
L002c: ret Expected codegen on .NET 8 x64: Program.<<Main>$>g__Test|0_0[[System.Int32, System.Private.CoreLib]]()
L0000: mov eax, 1
L0005: ret cc. @jkoritzinsky @MichalStrehovsky @EgorBo
|
Where is the IsPrimitive check in CsWinRT that you expect this optimization to help with? |
We're not using it yet, but @jkoritzinsky suggested we could add a branch checking that here so that all primitive value types would be covered, and wouldn't cause the linker to preserve and generate code for everything because it can't statically resolve what the result of that helper type logic is doing. We're basically looking for a way of statically being able to tell if an unconstrained |
Do you mean IL linker? IL linker does not analyze instantiated generics. If you mean AOT compiler, you can replace the IsPrimitive check with inlined check for all primitive types |
Wouldn't an intrinsic like this: public class Assembly
{
public bool IsBlittable(Type type);
} make more sense for this? It being on Assembly would let it respect |
Overview
We're currently working on AOT support for CsWinRT, and as part of that we're also looking into ways to reduce the binary size footprint, which is currently very large even for very simple applications. One issue we've noticed in particular is the static constructor in the
Marshaler<T>
type which is quite large for value types (we've started doing some work in that area here, but there's plenty more to do). This is because the linker is not able to properly handle all branches, so it just preserves everything, which then ends up compiling a whole bunch of useless code, as can be seen when opening the MSTAT files with sizoscope.It would be beneficial to use
Type.IsPrimitive
as a JIT constant, as we could use that to at least skip the (currently reflection based) path checking for ABI types, which is just completely breaking the flow analysis of the linker. We could then at least avoid that generic size explosion for all primitive types. In general, the more properties onType
were JIT time constants, the we could inform the linker in these cases, but IIsPrimitive
seems like a good place to start, since there's plenty of (commonly used) value types there, and we already know none of them will ever have an ABI type anyway.Codegen
Current codegen on .NET 8 x64 (sharplab):
Expected codegen on .NET 8 x64:
cc. @jkoritzinsky @MichalStrehovsky @EgorBo
The text was updated successfully, but these errors were encountered: