Skip to content

JIT: Assertion failed 'isContainable || supportsRegOptional' #118466

Closed

Description

@AndyAyersMS

From fuzzlyn (from #118368, but happens in main without that PR).

// Generated by Fuzzlyn v3.3 on 2025-08-05 17:40:02
// Run on X86 Windows
// Seed: 9882026655407373813-vectort,vector128,vector256,x86aes,x86avx,x86avx2,x86avx512bw,x86avx512bwvl,x86avx512cd,x86avx512cdvl,x86avx512dq,x86avx512dqvl,x86avx512f,x86avx512fvl,x86bmi1,x86bmi2,x86fma,x86lzcnt,x86pclmulqdq,x86popcnt,x86sse,x86sse2,x86sse3,x86sse41,x86sse42,x86ssse3,x86x86base
// Reduced from 124.1 KiB to 1.5 KiB in 00:04:19
// Hits JIT assert for Release:
// Assertion failed 'isContainable || supportsRegOptional' in 'Program:M16(byte)' during 'Generate code' (IL size 72; hash 0x646d3daa; FullOpts)
// 
//     File: D:\a\_work\1\s\src\coreclr\jit\hwintrinsiccodegenxarch.cpp Line: 61
// 
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

public class Program
{
    public static IRuntime s_rt;
    public static byte s_12;
    public static bool s_18;
    public static Vector128<long> s_30;
    public static void Main()
    {
        CollectibleALC alc = new CollectibleALC();
        System.Reflection.Assembly asm = alc.LoadFromAssemblyPath(System.Reflection.Assembly.GetExecutingAssembly().Location);
        System.Reflection.MethodInfo mi = asm.GetType(typeof(Program).FullName).GetMethod(nameof(MainInner));
        System.Type runtimeTy = asm.GetType(typeof(Runtime).FullName);
        mi.Invoke(null, new object[] { System.Activator.CreateInstance(runtimeTy) });
    }

    public static void MainInner(IRuntime rt)
    {
        s_rt = rt;
        M16(s_12);
    }

    public static void M16(byte arg0)
    {
        try
        {
            s_rt.WriteLine("c_66", 4294967195U);
        }
        catch (System.Exception)when (s_18)
        {
        }

        arg0 %= arg0;
        var vr2 = arg0 & 4294967295U;
        var vr1 = Vector128.Create<long>(vr2);
        s_30 = Sse2.ShiftLeftLogical128BitLane(vr1, 0);
    }
}

public interface IRuntime
{
    void WriteLine<T>(string site, T value);
}

public class Runtime : IRuntime
{
    public void WriteLine<T>(string site, T value) => System.Console.WriteLine(value);
}

public class CollectibleALC : System.Runtime.Loader.AssemblyLoadContext
{
    public CollectibleALC() : base(true)
    {
    }
}

Run with TC=0:

// Assertion failed 'isContainable || supportsRegOptional' in 'Program:M16(byte)' during 'Generate code' (IL size 72; hash 0x646d3daa; FullOpts)
// 
//     File: D:\a\_work\1\s\src\coreclr\jit\hwintrinsiccodegenxarch.cpp Line: 61

Activity

  1. added
    untriaged
    on Aug 6, 2025
  2. added
    area-CodeGen-coreclr
    on Aug 6, 2025
  3. removed
    untriaged
    on Aug 6, 2025
  4. added this to the 10.0.0 milestone on Aug 6, 2025
  5. self-assigned this
    on Aug 6, 2025
  6. dotnet-policy-service commented on Aug 6, 2025

    @dotnet-policy-service
    Contributor

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

  7. AndyAyersMS commented on Aug 6, 2025

    @AndyAyersMS
    MemberAuthor

    Indeed this seems similar to #114654 but here it's LSRA that decides to mark the operand as contained.

    During codegen, we have:

    Generating: N145 (  2,  2) [000025] -c---+-----                   t25 =    LCL_VAR   ubyte  V00 arg0         u:2 NA (last use) REG NA $289
    Generating: N147 (???,???) [000097] -----------                   t97 =    CNS_INT   int    0 REG edx
    IN0015:        xor      edx, edx
                                                                            /--*  t25    ubyte
                                                                            +--*  t97    int
    Generating: N149 (???,???) [000098] -c---------                   t98 = *  LONG      long   REG NA
                                                                            /--*  t98    long
    Generating: N151 (???,???) [000100] -----------                  t100 = *  HWINTRINSIC simd16 16 long CreateScalarUnsafe REG mm0
    

    But the local is ubyte so we don't have enough length to load it from memory.

    Seems like we need to spot this during lowering or long decomposition and fix the IR there?

  8. AndyAyersMS commented on Aug 7, 2025

    @AndyAyersMS
    MemberAuthor

    FYI @dotnet/jit-contrib

  9. AndyAyersMS commented on Aug 7, 2025

    @AndyAyersMS
    MemberAuthor

    @saucecontrol this looks similar to some issues you've fixed recently, any thoughts?

  10. saucecontrol commented on Aug 7, 2025

    @saucecontrol
    Member

    Yeah, this looks like it's from my changes. I'll give it a look.

  11. saucecontrol commented on Aug 8, 2025

    @saucecontrol
    Member

    I see the problem. Here, I'm marking the LCL_VAR ubyte RegOptional and then containing the GT_LONG under the CreateScalarUnsafe HWIntrinsic:

    if (!varTypeIsSmall(longOp) && IsContainableMemoryOp(longOp) &&
    IsSafeToContainMem(node, longOp))
    {
    MakeSrcContained(node, longOp);
    }
    else if (IsSafeToMarkRegOptional(node, longOp))
    {
    MakeSrcRegOptional(node, longOp);
    }

    And then in codegen, CreateScalarUnsafe(GT_LONG) will break down to movd+pinsrd.

    genHWIntrinsic_R_RM(node, ins, baseAttr, targetReg, loPart, instOptions);
    inst_RV_RV_TT_IV(INS_pinsrd, EA_16BYTE, targetReg, targetReg, hiPart, 0x01,
    !compiler->canUseVexEncoding(), instOptions);

    Since the LCL_VAR ubyte was marked RegOptional, LSRA is setting it to be contained, which I thought was ok, because the actual stack slot has to be 4 bytes anyway.

    With the assert removed, codegen looks like this:

    IN0016: 000050 vmovd    xmm0, dword ptr [V00 ebp-0x08]
    IN0017: 000055 vpinsrd  xmm0, xmm0, edx, 1
    IN0018: 00005B vpbroadcastq xmm0, xmm0

    So there are two possibilities:

    1. The assert is wrong, because it's totally fine to contain the LCL_VAR ubyte, given we can actually read 4 bytes there (and assuming the upper bytes are guaranteed to be zero) or
    2. Those assumptions are incorrect, and I shouldn't be marking a LCL_VAR of small type RegOptional in the first place.
  12. AndyAyersMS commented on Aug 8, 2025

    @AndyAyersMS
    MemberAuthor

    I believe you can only assume the storage is wide enough and properly zeroed for locals that are not lvNormalizeOnLoad.

  13. saucecontrol commented on Aug 8, 2025

    @saucecontrol
    Member

    Ok, thanks. I'll fix up that containment logic, then.

  14. locked and limited conversation to collaborators on Sep 11, 2025
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