Skip to content

JIT: (bug) RBO jump threading leaves stale PHI SSA that strength reduction treats as loop-invariant (wrong result) #135812

Description

@EgorBo

Redundant branch opts threads the second if (c) through the dominating identical one, orphaning the join block that holds the PHI of y while a later use of that PHI stays in place. "Optimize Induction Variables" then treats the orphaned def as loop-invariant and reads the never-assigned y (0) in the loop preheader, producing a wrong result.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int[] a, int k, int m, bool c, int n)
    {
        int sum = 0;
        for (int i = 0; i < n; i++)
        {
            int y;
            if (c) y = k; else y = m;
            if (c) sum += a[2 * i + y];
        }
        return sum;
    }

    static void Main()
    {
        int[] a = new int[200];
        for (int i = 0; i < a.Length; i++) a[i] = i;
        Console.WriteLine(Test(a, 2, 5, true, 50));
    }
}

Expected

2550 (sum of a[2*i + 2] for i < 50).

Actual

2450. With DOTNET_JitMinOpts=1 the result is correct (2550).

DOTNET_JitDisasm=Test shows k and m are never read: the strength-reduced IV rat0 is initialized to 0 (xor edx, edx) and only steps by 2, i.e. y is treated as 0.

Without AggressiveOptimization and with default tiering, the result is correct at tier-0 and becomes 2450 once the method is rejitted at tier-1.

Regression?

Yes, a regression in .NET 9. .NET 8 (8.0.x) prints 2550; .NET 9, .NET 10 (10.0.13), 11.0 RC1 and main print 2450.

Notes

  • Reproduced with a Checked JIT built from main @ c6d6546 and with the released 11.0 RC1 runtime.
  • Root cause: for non-PHI RBO, optJumpThreadCheck neglects PHI stores ("This can leave SSA in an incorrect state"). After threading, BB05 (holding PHI V07.4) is unreachable but BB06 still uses V07.4.
  • SCEV (ScalarEvolutionContext::AnalyzeNew, scev.cpp) treats an SSA def whose block is not in the loop as invariant, so StrengthReductionContext emits rat0 = LCL_VAR y in the preheader.
  • Related: A JIT bug in IV or RBO #130189 (closed) hardened only optWidenPrimaryIV for the same root cause, and Investigate repairing SSA after RBO #130241 (open) tracks repairing SSA after RBO in general; this is another test case for it. A fix would be to repair/invalidate SSA when optJumpThreadDom orphans a block with live PHI defs, or to make SCEV reject defs from unreachable blocks.

Activity

  1. added this to the 12.0.0 milestone on Oct 14, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 14, 2026
  3. dotnet-policy-service commented on Oct 14, 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 20, 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