Skip to content

JIT Bug: Code Generation Error under inlining (Release only) #122138

@LadislavLang

Description

@LadislavLang

Description

The provided C# code throws an unexpected System.Exception inside $Method2$ when compiled in the Release configuration using .NET 10.0.0.
The issue is a JIT code generation error related to the optimization path activated by the method attributes and repeated execution. The error specifically manifests during the comparison of int? types.

The input values are consistently $999$ and $999$, meaning the comparison $value1 != value2$ should always be $false$. However, the highly optimized JIT-compiled code erroneously evaluates this comparison as $true$, causing the exception.

Reproduction Steps

// 1. Compile the code below using .NET 10.0.0 in Release configuration.
// 2. Run the application.

using System;
using System.Runtime.CompilerServices;

class Program
{
    [MethodImpl(MethodImplOptions.NoOptimization)]
    static void Main()
    {
        var test = new Test();
        while (true)
        {
            test.Method1(0, 0, 0, 999, 999);
        }
    }
}

class Test
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Method1(int a, int b, int c, int value1, int? value2)
    {
        Method2(1, 2, 3, value1, value2);
    }

    public void Method2(long a, int b, int c, int? value1, int? value2)
    {
        try
        {
            if (value1 != value2)
            {
                throw new Exception($"{value1} != {value2}");
            }
        }
        catch
        {
            throw;
        }
    }
}

Expected behavior

The program should run indefinitely without throwing an exception.

Actual behavior

The program throws $System.Exception$ shortly after startup, indicating the JIT incorrectly evaluated the comparison of two equal non-null $int?$ values in the optimized code.

Regression?

net9.0 in not affected

Known Workarounds

No response

Configuration

OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.100\

Target framework: net10.0

Other information

No response

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions