Skip to content

JIT: (bug) assert genActualType(op1) == genActualType(op2) - range check cloning builds mixed-width (int/long) guard conditions #134459

Closed

Description

@EgorBo

A constant nint/long array index produces a native-width (TYP_LONG) bounds check. optRangeCheckCloning accepts such checks as candidates but hardcodes TYP_INT for the guard nodes it synthesizes, so it builds relops whose operands have different actual types. A Checked JIT asserts in the remorph that runs inside the same phase; a Release JIT silently accepts the type-invalid IR.

Minimal repro

using System;
using System.Runtime.CompilerServices;

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static void Test(int[] arr)
    {
        arr[(nint)0] = 1;
        arr[(nint)1] = 2;
        arr[(nint)2] = 3;
        arr[(nint)3] = 4;
    }

    public static void Main()
    {
        int[] a = new int[8];
        Test(a);
        Console.WriteLine(string.Join(",", a));
    }
}
set DOTNET_TieredCompilation=0
corerun out\rcc.dll

Expected

1,2,3,4,0,0,0,0

Actual

Assert failure(PID 2680 [0x00000a78], Thread: 105160 [0x19ac8]): Assertion failed
'genActualType(op1) == genActualType(op2)' in 'Program:Test(int[])'
during 'Clone blocks with range checks' (IL size 25; hash 0xb79562b4; FullOpts)

    File: C:\prj\runtime-main2\src\coreclr\jit\morph.cpp:9269

Process fail-fast (0xC0000602). The malformed guards built by optRangeCheckCloning_DoClone (DOTNET_JitDump=Test):

Lower bound check:
[000089] J----------   \--*  GT        int
[000087] -----------      +--*  CNS_INT   int    0
[000088] -----------      \--*  CNS_INT   long   0 $100

Upper bound check:
[000092] J----------   \--*  GT        int
[000085] -----------      +--*  LCL_VAR   long   V02 cse0   u:1 $200
[000091] -----------      \--*  CNS_INT   int    3

Notes

  • main @ 498a04c34ea, Windows x64, Checked JIT. AggressiveOptimization only makes FullOpts deterministic; plain tier-1 rejit reaches the same shape, so this also fires with a fully default configuration once the method is promoted (for example arr[0L]=1; arr[5L]=2; arr[9L]=3; arr[13L]=4; called in a warm-up loop).
  • Other index spellings that reproduce identically: arr[0L] = 1; arr[1L] = 2; arr[2L] = 3; arr[3L] = 4;.
  • fgMorphIndexAddr builds a TYP_I_IMPL GT_BOUNDS_CHECK for a native-int index, whose length operand is CAST(long <- ARR_LENGTH).
  • BoundsCheckInfo::Initialize (rangecheckcloning.cpp:71) gates the index type only on the non-constant path (TypeOfVN(idxVN) != TYP_INT). The constant path is entered via GenTree::IsIntCnsFitsInI32(), which tests the constant value, not the node type, so a TYP_LONG GT_CNS_INT is treated as a pure offset and idxVN is overwritten with VNZeroForType(TYP_INT), erasing the width.
  • optRangeCheckCloning_DoClone (rangecheckcloning.cpp:232-273) then creates GT_ADD/GT_LT/GT_GT nodes hardcoded to TYP_INT over TYP_LONG operands. The assert is fgOptimizeRelationalComparisonWithConst (morph.cpp:9269).
  • Not a recent regression: it arrived with the feature ("Clone blocks with bounds checks", [Experiment] Clone blocks with bounds checks #112595, 5486a2664e7f, Feb 2025, .NET 10 timeframe). .NET 8/9 have no such phase.
  • Release builds cannot assert and produce correct output on .NET 10, .NET 11 rc.2 and main, but they still consume the invalid IR: the 64-bit array length ends up compared with a 32-bit cmp eax, 3, which is only benign because an array length always fits in int.
  • Impact: hard assert / fail-fast for Checked and Debug JIT users (CI legs, SPMI replay, JIT developers) on ordinary C#, plus a latent type hazard in Release.
  • Fix directions: reject the candidate unless idx->TypeIs(TYP_INT), or synthesize the guard nodes with the index's actual type instead of a hardcoded TYP_INT.

Activity

  1. added this to the 12.0.0 milestone on Sep 22, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 22, 2026
  3. dotnet-policy-service commented on Sep 22, 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 Oct 13, 2026
  5. added a commit that references this issue on Oct 14, 2026
    50131be
  6. locked and limited conversation to collaborators on Nov 14, 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