Skip to content

JIT: invariant this-field loads are no longer hoisted out of loops #134352

Open

Description

@bbartels

Description

In .NET 9, RyuJIT hoists loads of loop-invariant instance fields (this.Field) out of a counted loop: it creates a pre-header block, loads the fields there once, and the loop body works on registers. Starting with .NET 10 this no longer happens. No pre-header is created and every field the loop body uses is re-read from this on every iteration. .NET 11 RC1 behaves like .NET 10.

The pattern is as ordinary as it gets — a method summing over a pointer or array stored in a field:

public double SumPlain()
{
    double sum = 0;
    for (var j = 0; j < N; j++)
        sum += V[j];        // .NET 10+: `V` is loaded from `this` on every iteration
    return sum;
}

Both pointer-typed fields (V above) and scalar fields (a double K used inside the loop) are affected. The loop bound N is the only field that stays in a register, and only because the loop guard (test ecx, ecx / jle) had already loaded it — that is a CSE with the guard, not a hoist.

Lambdas hit this as well: a captured local is a field of the compiler-generated closure object, so a hot loop inside a Parallel.For body re-reads every captured pointer and value from the closure on each iteration. That is how I noticed it — the Benchmarks Game spectral-norm C# program has an unchanged inner loop but got ~7% slower going from .NET 9 to .NET 10.

Configuration

  • Runtimes compared: Microsoft.NETCore.App 9.0.17 (hoists), 10.0.9 (does not), 11.0.0-rc.1.26425.128 (does not).
  • Reproduces with the normal JIT — both DOTNET_TieredCompilation=0 (FullOpts) and Tier1 after tier-up with TieredPGO — and with NativeAOT (ILCompiler 10.0.9 and 11.0.0-rc.1).
  • Ubuntu 24.04.4 LTS, x64 (Emitting BLENDED_CODE for generic X64 + VEX on Unix), AMD EPYC 73F3 (Zen 3). The difference is visible directly in DOTNET_JitDisasm output, so the hardware is not relevant to the codegen part.

Regression?

Yes. .NET 9.0.17 produces a pre-header containing the hoisted loads. .NET 10.0.9 and 11.0.0-rc.1 produce no pre-header and keep the loads inside the loop. I have not bisected further than major versions.

Data

Minimal repro

Console project with <AllowUnsafeBlocks>true</AllowUnsafeBlocks>; no intrinsics, no lambdas, no Parallel.For:

using System;

unsafe class Summer
{
    public double* V; public int N; public double K;

    public double SumPlain()
    {
        double sum = 0;
        for (var j = 0; j < N; j++)
            sum += V[j];
        return sum;
    }

    public double SumScalar(int i)
    {
        double sum = 0;
        for (var j = 0; j < N; j++)
            sum += K / ((i + j) * (i + j + 1) / 2 + i + 1);
        return sum;
    }
}

unsafe class Program
{
    static void Main(string[] args)
    {
        int n = args.Length > 0 ? int.Parse(args[0]) : 100;
        double* v = stackalloc double[n];
        for (int j = 0; j < n; j++) v[j] = 1.0;
        var s = new Summer { V = v, N = n, K = 2.0 };
        Console.WriteLine($"{s.SumPlain():F3} {s.SumScalar(3):F3}");
    }
}
DOTNET_TieredCompilation=0 DOTNET_JitDisasm='Summer:*' dotnet run -c Release

Summer:SumPlain — .NET 9.0.17 (FullOpts, 48 bytes; loop is 5 instructions)

G_M000_IG02:
       vxorps   xmm0, xmm0, xmm0
       xor      eax, eax
       mov      ecx, dword ptr [rdi+0x18]        ; N
       test     ecx, ecx
       jle      SHORT G_M000_IG05
G_M000_IG03:                                     ; pre-header
       mov      rdx, qword ptr [rdi+0x08]        ; V, loaded once
       align    [11 bytes for IG04]
G_M000_IG04:                                     ; loop
       movsxd   rdi, eax
       vaddsd   xmm0, xmm0, qword ptr [rdx+8*rdi]
       inc      eax
       cmp      eax, ecx
       jl       SHORT G_M000_IG04
G_M000_IG05:
       pop      rbp
       ret

Summer:SumPlain — .NET 10.0.9 (FullOpts, 52 bytes; loop is 6 instructions; byte-identical on 11.0.0-rc.1)

G_M000_IG02:
       vxorps   xmm0, xmm0, xmm0
       xor      eax, eax
       mov      ecx, dword ptr [rdi+0x18]        ; N
       test     ecx, ecx
       jle      SHORT G_M000_IG04
       align    [15 bytes for IG03]
G_M000_IG03:                                     ; loop — there is no pre-header
       mov      rdx, qword ptr [rdi+0x08]        ; V, loaded every iteration
       movsxd   rsi, eax
       vaddsd   xmm0, xmm0, qword ptr [rdx+8*rsi]
       inc      eax
       cmp      eax, ecx
       jl       SHORT G_M000_IG03
G_M000_IG04:
       pop      rbp
       ret

Summer:SumScalar — same shape with a scalar field

.NET 9 puts vmovsd xmm1, qword ptr [rdi+0x10] (the field K) in the pre-header; .NET 10 and 11 execute it as the first instruction of the loop block, once per iteration. Loop: 15 instructions on .NET 9 vs 16 on .NET 10.

Tier1 is affected too

Calling the three methods 200 000 times, sleeping 200 ms, and calling them 200 000 times again (so they go Instrumented Tier0 → Tier1 with PGO data) gives Tier1 code with the same difference. Summer:SumPlain Tier1 on .NET 9 has the pre-header with mov rdx, qword ptr [rdi+0x08] before the loop; on .NET 10 the same load is the first instruction of the loop block. The listings are identical to the FullOpts ones above apart from the block numbering.

Run-time effect

Where I found it — Benchmarks Game spectral-norm, C# program #5, n = 5500, NativeAOT, pinned to 4 cores, median of 7 runs. The inner loop is a lambda inside Parallel.For; its body is instruction-for-instruction the same on all versions except for the un-hoisted closure-field load (28 vs 29 instructions per 2 elements):

Build Elapsed CPU time
.NET 9 0.344 s 1.15 s
.NET 10 0.353 s 1.22 s
.NET 11 RC1 0.362 s 1.23 s
.NET 10 / 11 with the captured v and n copied to locals before the loop 0.330 s / 0.329 s 1.12 s

That loop is divide-bound, so the extra load costs only ~7%. In the minimal SumPlain loop above it is one extra instruction on a 5-instruction loop body.

In the NativeAOT build there was a second visible difference: the .NET 9 loop head was padded to a 64-byte boundary (the loop occupied 2 cache lines), while on .NET 10/11 the loop head landed at byte 55 of a cache line (3 lines). Copying the fields to locals restored both the hoisting and the alignment padding, so the two may be connected, but I have not verified that.

Analysis

  • The loop bodies contain no stores at all, so the field loads are invariant by inspection. On .NET 9 the loop-hoisting phase created a pre-header and moved them; on .NET 10 no pre-header exists in the final code at all. That points at the decision to hoist/create a pre-header for the loop, not at a per-candidate profitability call — even the trivial sum += V[j] loop is affected, and both pointer and scalar fields are.
  • Not specific to closures, unsafe pointers, hardware intrinsics, tiering mode, or NativeAOT: the plain instance-method repro reproduces it under FullOpts, under Tier1 with PGO, and under ILC.
  • Workaround: copy the fields into locals before the loop (var v = V; var n = N;). On .NET 10/11 that produces exactly the .NET 9 code.
  • I can provide a JitDump for Summer:SumPlain from a checked runtime, or bisect between 9 and 10 previews, if that helps.

Activity

  1. added
    untriaged
    on Sep 21, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 21, 2026
  3. dotnet-policy-service commented on Sep 21, 2026

    @dotnet-policy-service
    Contributor

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

  4. removed
    untriaged
    on Sep 24, 2026
  5. added this to the 12.0.0 milestone on Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions