Skip to content

JIT: (bug) Strength reduction produces wrong values: AddRecMayOverflow treats a non-constant add-recurrence start as zero #133759

Closed

Description

@EgorBo

Minimal repro

using System;
using System.Runtime.CompilerServices;

public static class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static void Test(int seed, long[] dst)
    {
        int c = seed;
        for (int i = 0; i < 10; i++)
        {
            dst[i] = (uint)c * 8L;
            c++;
        }
    }

    public static int Main()
    {
        long[] actual = new long[10];
        Test(-3, actual);

        long[] expected = new long[10];
        for (int i = 0, c = -3; i < 10; i++, c++)
            expected[i] = (uint)c * 8L;

        Console.WriteLine("expected: " + string.Join(",", expected));
        Console.WriteLine("actual:   " + string.Join(",", actual));
        for (int i = 0; i < 10; i++)
            if (actual[i] != expected[i]) { Console.WriteLine("FAILED"); return 101; }
        Console.WriteLine("PASSED");
        return 100;
    }
}

Expected

34359738344,34359738352,34359738360,0,8,16,24,32,40,48 (the int counter wraps through zero, so (uint)c restarts at 0). Reproduced with DOTNET_JitEnableStrengthReduction=0.

Actual

34359738344,34359738352,34359738360,34359738368,34359738376,34359738384,34359738392,34359738400,34359738408,34359738416 — the sequence keeps climbing past 2^32*8, exit code 101.

Notes

scev.cpp:1738-1742 in AddRecMayOverflow returns "cannot overflow" whenever Start->GetConstantValue() fails, i.e. treats any symbolic start as 0, contradicting the comment at scev.cpp:1731 ("only ... addRec = <L, 0, 1>"). Simplify (scev.cpp:1132-1136) then distributes the zext into <L, V02.2, 1> producing <L, zext(V02.2), 1>, and strength reduction materializes it as a new 64-bit primary IV. Correct shape would be the step guard's form: if (!GetConstantValue(...) || (startCns != 0)) return true;.

Activity

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

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 11.0.100-rc.2.26467.112
  5. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    @jakobbotsch would you consider backporting the fix from #133766 to release/11.0?

    It currently only exists in main (12.0). The repro still fails on the shipped 11.0 RC2 SDK (11.0.100-rc.2.26467.112), while 8.0.425, 9.0.318 and 10.0.401 all behave correctly, so this is a regression introduced during the 11.0 cycle rather than a long-standing bug — 11.0 would ship with it.

    Note: this comment was produced by an AI agent; the repro was re-run against all four released SDKs to confirm the results above.

  6. jakobbotsch commented on Sep 22, 2026

    @jakobbotsch
    Member

    The regression here is that 304e5a8 makes us recognize some more IR for SCEV analysis and then the issue can be triggered. But the issue exists back to 9.0 with other shapes, e.g.

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static void Test(int seed, long[] dst)
    {
        int c = seed;
        for (int i = 0; i < 10; i++)
        {
            dst[i] = ((long)c & 0xFFFFFFFFL) * 8;
            c++;
        }
    }

    reproduces it too in .NET 9.

    I don't think this deserves a backport, I think it is very unlikely the user will have a provably overflowing arithmetic operation like this in combination with the cast required.

  7. locked and limited conversation to collaborators on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions