Skip to content

JIT: unrolled loop re-dispatches its switch with the index folded to 0 (not backported to .NET 11) #137330

Description

@0xced

Description

The bug described in #132370 was fixed in .NET 10.0.13 onwards but is present in .NET 11.0.0.

Reproduction Steps

dotnet_runtime_issue_132370.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net10.0;net11.0</TargetFrameworks>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

Program.cs

// Adapted from https://github.com/dotnet/runtime/commit/915f2aee2b74485bda851be24b41c6a263e95d96#diff-6cc0e9a3e1afd4d6fa805cefdefd950986da94c16f7f34aa37f194d0df0fb597
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;

// When a loop containing a switch is unrolled, the switch value becomes a constant in each
// unrolled copy. Switch peeling would then "steal" the wrong operand of the compare it creates
// (sequencing the compare can swap its operands when both are constants), so every copy of the
// switch ended up dispatching on the dominant case value instead of on its own value.

public class Runtime_132370
{
    private readonly struct V3
    {
        public readonly long X;
        public readonly long Y;
        public readonly long Z;

        public V3(long x, long y, long z)
        {
            X = x;
            Y = y;
            Z = z;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static long Get(V3 value, int axis) => axis switch
        {
            0 => value.X,
            1 => value.Y,
            2 => value.Z,
            _ => throw new ArgumentOutOfRangeException(nameof(axis)),
        };
    }

    private static bool IsZero(V3 value)
    {
        for (int axis = 0; axis < 3; axis++)
        {
            if (V3.Get(value, axis) != 0)
            {
                return false;
            }
        }

        return true;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static int CountZero(V3[] values)
    {
        int zero = 0;

        for (int i = 0; i < values.Length; i++)
        {
            if (IsZero(values[i]))
            {
                zero++;
            }
        }

        return zero;
    }

    public static int Main()
    {
        var configuration = typeof(Runtime_132370).Assembly.GetCustomAttribute<AssemblyConfigurationAttribute>()?.Configuration;
        Console.WriteLine($"Running on .NET {Environment.Version} (configuration = {configuration})");

        if (configuration != "Release")
        {
            Console.WriteLine("🙅‍♂️ This test is only relevant for the Release configuration.");
            return -1;
        }

        // A mixture of values is needed so that case 0 is the dominant case in the profile.
        V3[] values = new V3[256];
        for (int i = 0; i < values.Length - 1; i++)
        {
            values[i] = new V3(1, 0, 0);
        }

        values[^1] = new V3(0, 0, 1);

        // None of the vectors is the zero vector, so this must always be 0. The bad code only
        // showed up once IsZero reached tier1 with profile data, so keep calling it, yielding in
        // between so that the background compilation has a chance to happen. In practice this
        // fails on the ~7th outer iteration when the JIT is broken.
        for (int i = 0; i < 50; i++)
        {
            for (int j = 0; j < 1000; j++)
            {
                if (CountZero(values) != 0)
                {
                    Console.WriteLine("❌ Hit JIT bug, see https://github.com/dotnet/runtime/issues/132370");
                    return 1;
                }
            }

            Thread.Sleep(16);
        }

        Console.WriteLine("✅ Looks like https://github.com/dotnet/runtime/issues/132370 is fixed");
        return 0;
    }
}

Run the program with Release configuration in .NET 11 to reproduce:

dotnet run -c Release -f net11.0

Expected behavior

The program prints this:

Running on .NET 11.0.0 (configuration = Release)
✅ Looks like https://github.com/dotnet/runtime/issues/132370 is fixed

Actual behavior

The program prints this:

Running on .NET 11.0.0 (configuration = Release)
❌ Hit JIT bug, see https://github.com/dotnet/runtime/issues/132370

Regression?

Yes, this was fixed in .NET 10. Note that running dotnet run -c Release -f net10.0 is fine.

Running on .NET 10.0.15 (configuration = Release)
✅ Looks like https://github.com/dotnet/runtime/issues/132370 is fixed

Known Workarounds

The workarounds that involve setting environment variables are already describe in #132370.

Configuration

  • .NET: 11.0.0
  • OS: macOS
  • Architecture: arm64
  • Probably not specific to that configuration

Other information

@EgorBo @JulieLeeMSFT It looks like an oversight that this was not included in the .NET 11 release.

Activity

  1. added
    untriaged
    on Nov 11, 2026
  2. added
    area-CodeGen-coreclr
    on Nov 11, 2026
  3. dotnet-policy-service commented on Nov 11, 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. self-assigned this
    on Nov 11, 2026
  5. removed
    untriaged
    on Nov 11, 2026
  6. AndyAyersMS commented on Nov 11, 2026

    @AndyAyersMS
    Member

    Yeah, it looks like we fixed this just after 11.0 branched. Starting a backport.

  7. added this to the 11.0.0 milestone on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions