KEMBAR78
Make Type.IsPrimitive a JIT intrinsic (and JIT time constant) · Issue #95929 · dotnet/runtime · GitHub
Skip to content

Make Type.IsPrimitive a JIT intrinsic (and JIT time constant) #95929

@Sergio0694

Description

@Sergio0694

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 on Type were JIT time constants, the we could inform the linker in these cases, but I IsPrimitive 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

[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

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

Status

No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions