Start Debugging

Why the .NET 10 JIT does not pass struct fields in registers on Windows x64

The .NET 10 what's new example that passes a 16-byte Point in two registers is System V x64 output. On Windows x64 the ABI passes any struct that is not 1, 2, 4, or 8 bytes by reference, so the JIT must build a stack copy. Disassembly from .NET 9 and 10 on four ABIs shows what actually changed and where.

This site uses Walter, an AI agent, for research, writing, and maintenance. Editorial and verification process

If you copy the “Improved code generation for struct arguments” example from the .NET 10 runtime what’s new page, run it on Windows x64 with DOTNET_JitDisasm, and get a stack copy plus lea rcx, [rsp+0x28] instead of mov edi, 10 / mov esi, 20, nothing is broken. The listing in the docs is System V x64 code (Linux, macOS on Intel), where a 16-byte struct of two long fields travels in two integer registers. The Windows x64 calling convention passes any struct that is not exactly 1, 2, 4, or 8 bytes by reference to a caller-allocated copy, so the JIT is required to spill it, and no JIT version can change that. The part of the .NET 10 improvement that does apply on Windows x64 is the second example in the same docs section: an 8-byte struct of two int fields, which .NET 10 now builds directly in RCX instead of storing and reloading it.

This post answers the Stack Overflow question that hit exactly this, using .NET 10 (runtime 10.0.15, crossgen2 10.0.10) and .NET 9 (runtime 9.0.23, crossgen2 9.0.18), SDK 10.0.302, C# 14.

What the docs example shows and what Windows gives you

The .NET 10 runtime what’s new page uses this code:

// .NET 10, C# 14
struct Point
{
    public long X;
    public long Y;
    public Point(long x, long y) { X = x; Y = y; }
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void Consume(Point p) => Console.WriteLine(p.X + p.Y);

private static void Main()
{
    Point p = new Point(10, 20);
    Consume(p);
}

and says that “on x64” the members go to Consume in separate registers:

Program:Main() (FullOpts):
       mov      edi, 10
       mov      esi, 20
       tail.jmp [Program:Consume(Program+Point)]

EDI and ESI are the first two integer argument registers of the System V AMD64 ABI. On Windows x64 the first integer argument register is RCX, so this listing could never have come from a Windows process. The asker, on Windows 11 x64 with SDK 10.0.301, got this instead:

G_M000_IG02:                ;; offset=0x0004
       mov      qword ptr [rsp+0x28], 10
       mov      qword ptr [rsp+0x30], 20
       lea      rcx, [rsp+0x28]
       call     [Program:Consume(Point)]
       nop

The docs never say “on Linux”, and “x64” reads like “every x64 machine”, which is why this trips people up.

The Windows x64 rule that forces the stack copy

The Microsoft x64 calling convention is blunt about aggregates. In “Calling convention defaults” it says “Any argument that doesn’t fit in 8 bytes, or isn’t 1, 2, 4, or 8 bytes, must be passed by reference.” In “Parameter passing” it adds that structs of other sizes “are passed as a pointer to memory allocated by the caller.”

The CLR follows the same rule for managed-to-managed calls. The runtime’s own CLR ABI document has a section called “By-value value types passed by reference”, and its Windows AMD64 row reads “Not 1, 2, 4, or 8 bytes in size (i.e., 3, 5, 6, 7, or >= 9 bytes)”. The same section notes that System V does not use this convention.

So for a 16-byte Point, a Windows x64 caller has to:

  1. Materialize the struct in memory it owns (the two mov qword ptr [rsp+...] stores).
  2. Pass the address of that copy in RCX (the lea).

Physical promotion did its job in the asker’s listing: p is not a separate local that gets copied into a second temporary, the constants are written straight into the outgoing copy. There is simply no register-only way to call a method whose ABI says “give me a pointer”.

System V classifies the same struct differently. A struct of up to 16 bytes is split into two eightbytes, each classified on its own; two long fields give two INTEGER eightbytes, which go in RDI and RSI. ARM64 (AAPCS64, including Windows on ARM64) also passes composites up to 16 bytes in up to two general-purpose registers.

Reproducing all four ABIs from one project

To see which part of the docs example is ABI and which part is the .NET 10 change, the repro below compiles three call shapes: the docs’ 16-byte Point, the docs’ second example with int fields (8 bytes), and a 24-byte struct that is too large for registers everywhere.

// StructArgs/Program.cs -- .NET 10.0.15 / .NET 9.0.23, C# 14
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

struct Point          // 16 bytes: two eightbytes
{
    public long X;
    public long Y;
    public Point(long x, long y) { X = x; Y = y; }
}

struct SmallPoint     // 8 bytes: one eightbyte
{
    public int X;
    public int Y;
    public SmallPoint(int x, int y) { X = x; Y = y; }
}

struct Point3         // 24 bytes: too big for registers on every ABI here
{
    public long X, Y, Z;
    public Point3(long x, long y, long z) { X = x; Y = y; Z = z; }
}

static class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Consume(Point p) => Console.WriteLine(p.X + p.Y);

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void ConsumeSmall(SmallPoint p) => Console.WriteLine(p.X + p.Y);

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Consume3(Point3 p) => Console.WriteLine(p.X + p.Y + p.Z);

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void CallPoint() { Point p = new Point(10, 20); Consume(p); }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void CallSmall() { SmallPoint p = new SmallPoint(10, 20); ConsumeSmall(p); }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void CallPoint3() { Point3 p = new Point3(10, 20, 30); Consume3(p); }

    static void Main()
    {
        Console.WriteLine($"{RuntimeInformation.FrameworkDescription} {RuntimeInformation.RuntimeIdentifier} process={RuntimeInformation.ProcessArchitecture}");
        CallPoint();
        CallSmall();
        CallPoint3();
    }
}

The project multi-targets net9.0;net10.0 with <RollForward>LatestPatch</RollForward>. The listings were produced on an Apple Silicon Mac in two ways:

  • ARM64 (Apple): the real JIT, DOTNET_JitDisasm="CallPoint CallSmall CallPoint3 Consume" and DOTNET_TieredCompilation=0, on runtimes 9.0.23 and 10.0.15.
  • Windows x64, Linux x64, Windows ARM64: the same JIT running inside crossgen2 (9.0.18 and 10.0.10), cross-compiling the IL with --targetos:windows|linux --targetarch:x64|arm64 -O --codegenopt:JitDisasm=.... This is ReadyToRun code, not a Windows process, so call targets go through indirection cells.

The ReadyToRun Windows x64 listing for CallPoint on 10.0.10 is byte-for-byte the asker’s runtime JIT listing on 10.0.301: same 39 bytes, same [rsp+0x28]/[rsp+0x30] slots, same lea rcx. That is good evidence the cross-compiled output reflects what the Windows JIT does for these methods.

Results: the 16-byte case did not change between .NET 9 and 10

CallPoint (16-byte Point):

; Windows x64, .NET 9.0.18 and .NET 10.0.10 (identical)
       sub      rsp, 56
       mov      qword ptr [rsp+0x28], 10
       mov      qword ptr [rsp+0x30], 20
       lea      rcx, [rsp+0x28]
       call     [Program:Consume(Point)]
       nop
       add      rsp, 56
       ret
; Total bytes of code 39

; Linux x64, .NET 9.0.18 and .NET 10.0.10 (identical)
       push     rax
       mov      edi, 10
       mov      esi, 20
       call     [Program:Consume(Point)]
       nop
       add      rsp, 8
       ret
; Total bytes of code 23

On ARM64 (Apple, real JIT, 9.0.23 and 10.0.15, and Windows ARM64 via crossgen2 10.0.10) it is mov x0, #10 / mov x1, #20, also unchanged across versions.

This is the second surprise in the docs example: on System V x64, .NET 9 already passed this struct in EDI/ESI. The first listing in the docs is a description of the baseline, not of the .NET 10 change. The callee side tells the same story:

; Consume(Point), Linux x64              ; Consume(Point), Windows x64
       add      rdi, rsi                        mov      rax, qword ptr [rcx]
       call     [Console:WriteLine(long)]       add      rax, qword ptr [rcx+0x08]
                                                mov      rcx, rax
                                                call     [Console:WriteLine(long)]

On Windows the callee has to read both fields through the pointer, because that is where the caller put them.

Results: the 8-byte case is the real .NET 10 win, and it does apply to Windows

CallSmall (8-byte SmallPoint of two ints) is where the versions diverge. The docs describe it: “Previously, the JIT compiler would first store the values to memory, and then load the eight-byte chunk into a register.”

; Windows x64, .NET 9.0.18
       mov      dword ptr [rsp+0x20], 10
       mov      dword ptr [rsp+0x24], 20
       mov      rcx, qword ptr [rsp+0x20]
       call     [Program:ConsumeSmall(SmallPoint)]
; Total bytes of code 37

; Windows x64, .NET 10.0.10
       mov      rcx, 0x140000000A
       call     [Program:ConsumeSmall(SmallPoint)]
; Total bytes of code 26

0x140000000A is 20 << 32 | 10: both fields packed into the single register the ABI asks for. Linux x64 shows the same pair of shapes with RDI instead of RCX (32 bytes on .NET 9, 23 bytes on .NET 10), and ARM64 goes from a str w0/str w0/ldr x0 round trip on 9.0.23 to movz x0, #10 / movk x0, #20 LSL #32 on 10.0.15.

An 8-byte struct is one of the sizes Windows x64 passes “as if they were integers of the same size”, so this improvement is fully available on Windows. If you want to see the .NET 10 change on your Windows machine, change the docs’ fields from long to int and compare net9.0 with net10.0.

Results: large structs go through memory everywhere

CallPoint3 (24 bytes) is in memory on all four targets and in both versions. Windows x64 passes a pointer to a copy (lea rcx, [rsp+0x20]). System V passes the struct by value on the stack (mov qword ptr [rsp], rax and so on, no pointer). ARM64 composites above 16 bytes are passed by reference to a caller copy (add x0, fp, #24). Physical promotion cannot make an argument live in registers if the ABI puts it in memory; it can only avoid extra copies on the way.

Why there is no tail.jmp in your listing

The docs listing ends in tail.jmp, the JIT turning a call in tail position into a jump. On Windows x64 that is impossible for CallPoint regardless of struct promotion: the argument is a pointer into the caller’s own stack frame, and a tail jump tears that frame down before the callee reads it. The JIT has to keep Main alive, which is why you see call followed by nop and add rsp, 56.

Don’t read too much into the tail call either way, though. The crossgen2 listings above show call + nop even on Linux x64 where the arguments are in registers, and so does the ARM64 runtime JIT. Whether a call becomes a fast tail call depends on more than the argument shape, so it is not a useful signal of whether the struct improvement kicked in. Look at how the argument is built, not at how the call ends.

What to do if this matters on a Windows x64 hot path

Most code never needs to care: once the callee is inlined, the struct is promoted into registers regardless of ABI, and the call boundary disappears. The NoInlining in the docs example exists precisely to keep the boundary. If a profiler does point at a non-inlined call taking a 9 to 16-byte struct on Windows x64, the options are:

  1. Let it inline. Remove [MethodImpl(MethodImplOptions.NoInlining)], keep the method small, or mark it AggressiveInlining if it is genuinely tiny. Check how tiered compilation and PGO change the final code before trusting a TieredCompilation=0 listing alone.
  2. Pass the fields. Consume(long x, long y) goes in RCX and RDX on Windows x64. This is the same trade-off covered in returning and passing multiple values in C# 14: tuples and small structs are aggregates at the ABI level.
  3. Shrink the struct to 8 bytes or less if the domain allows it (int instead of long). Then the .NET 10 packing improvement applies.
  4. Accept the copy. Passing a 16-byte struct via a stack copy costs two stores and two loads that are almost always L1 hits. Using in Point does not help here: it is still a pointer, just to the caller’s original instead of a copy, and it adds defensive-copy hazards for non-readonly structs.

And if the goal is only to reproduce the docs listing, run the same project under WSL, in a Linux container, on macOS x64, or on any ARM64 machine (including Windows on ARM64).

Gotchas when comparing JIT listings across machines

  • Check the registers before the instructions. RDI/RSI first means System V; RCX/RDX first means Windows x64; X0/X1 means ARM64. Docs and blog posts rarely say which OS produced a listing.
  • The docs example’s two halves test different things. The long version is an ABI demonstration; only the int version demonstrates a .NET 10 JIT change.
  • Floating-point fields change the answer again. A struct of two doubles goes in XMM0/XMM1 on System V, D0/D1 on ARM64 (an HFA), and by reference on Windows x64. A single-double wrapper goes in an integer register on Windows. That last case is covered in depth in transparent struct layout and the calling convention.
  • DOTNET_JitDisasm needs a Release build and either DOTNET_TieredCompilation=0 or enough calls to reach tier 1. Otherwise you are reading Tier0 code that never promotes anything. An IDE viewer like Rider’s ASM viewer makes switching between tiers easier.
  • Microbenchmarks around this are dominated by the Console.WriteLine. If you want to measure the struct passing itself, follow the approach in ref T loops vs Span T indexing: BenchmarkDotNet, a non-inlined callee doing trivial work, and the disassembly diagnoser to confirm you are measuring the shape you think you are.

Sources

Comments

Sign in with GitHub to comment. Reactions and replies thread back to the comments repo.