Skip to content

JIT: (bug) Exponential compile time and memory in assertion prop (unmemoized PHI walk in optAssertionVNIsNonNull) #134462

Open

Description

@EgorBo

A small method with 10 sequential if (a[i] is { } t && ...) x = t; statements takes tens of seconds and gigabytes of memory to JIT. Almost all of that time is spent in Assertion prop.

Minimal Repro

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

public class Program
{
    public static void Main()
    {
        var sw = Stopwatch.StartNew();
        RuntimeHelpers.PrepareMethod(typeof(Program).GetMethod(nameof(Test))!.MethodHandle);
        Console.WriteLine($"JIT time: {sw.ElapsedMilliseconds} ms");
    }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static int Test(string?[] a, int m)
    {
        string x = "abc";
        if (a[0] is { } t0 && (m & 1) > 0 && (m & 2) > 0 && (m & 4) > 0 && (m & 8) > 0 && (m & 16) > 0 && (m & 32) > 0) x = t0;
        if (a[1] is { } t1 && (m & 1) > 0 && (m & 2) > 0 && (m & 4) > 0 && (m & 8) > 0 && (m & 16) > 0 && (m & 32) > 0) x = t1;
        if (a[2] is { } t2 && (m & 1) > 0 && (m & 2) > 0 && (m & 4) > 0 && (m & 8) > 0 && (m & 16) > 0 && (m & 32) > 0) x = t2;
        if (a[3] is { } t3 && (m & 1) > 0 && (m & 2) > 0 && (m & 4) > 0 && (m & 8) > 0 && (m & 16) > 0 && (m & 32) > 0) x = t3;
        if (a[4] is { } t4 && (m & 1) > 0 && (m & 2) > 0 && (m & 4) > 0 && (m & 8) > 0 && (m & 16) > 0 && (m & 32) > 0) x = t4;
        if (a[5] is { } t5 && (m & 1) > 0 && (m & 2) > 0 && (m & 4) > 0 && (m & 8) > 0 && (m & 16) > 0 && (m & 32) > 0) x = t5;
        if (a[6] is { } t6 && (m & 1) > 0 && (m & 2) > 0 && (m & 4) > 0 && (m & 8) > 0 && (m & 16) > 0 && (m & 32) > 0) x = t6;
        if (a[7] is { } t7 && (m & 1) > 0 && (m & 2) > 0 && (m & 4) > 0 && (m & 8) > 0 && (m & 16) > 0 && (m & 32) > 0) x = t7;
        if (a[8] is { } t8 && (m & 1) > 0 && (m & 2) > 0 && (m & 4) > 0 && (m & 8) > 0 && (m & 16) > 0 && (m & 32) > 0) x = t8;
        if (a[9] is { } t9 && (m & 1) > 0 && (m & 2) > 0 && (m & 4) > 0 && (m & 8) > 0 && (m & 16) > 0 && (m & 32) > 0) x = t9;
        return x.Length;
    }
}

Expected

The method compiles in a few milliseconds.

Actual

On main (Checked, windows-x64), JIT time: ~20000 ms and the peak working set is about 2.9 GB.

Regression?

Yes. .NET 8.0.31, 9.0.20 and 10.0.12 compile this in about 1 ms. .NET 11.0.0-rc.2.26467.112 takes about 10 s and 2.9 GB.

Notes

optAssertionVNIsNonNull walks PHIs recursively through optVisitReachingAssertions. The walk only has a depth limit of 10 and keeps no visited set. Each join has about 7 predecessors with the same PHI VN, so the walk visits about 7^10 nodes, and each visit allocates 2 arena BitVecs.
Possible fix: add a visited set, a memo, or a node budget for each query, and reuse the BitVec scratch.
This is the optAssertionVNIsNonNull counterpart of #133857, which covers optAssertionVNIsSubtype.

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. AndreasPK commented on Oct 5, 2026

    @AndreasPK

    I've hit this bug in a project of mine where it caused a non-trivial overhead on startup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclr

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions