Skip to content

JIT: (bug) volatile load dropped when byteField >= 0 is folded to true (spin loop hangs) #134477

Closed

Description

@EgorBo

When volatileByte >= 0 is folded to true, the volatile load is removed along with its acquire barrier. That lets a non-volatile load get hoisted out of the loop, and the loop never ends.

Minimal Repro

using System;
using System.Runtime.CompilerServices;
using System.Threading;

public class Program
{
    public static volatile byte s_state;
    static int s_data;

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static void Test()
    {
        while (s_state >= 0)
        {
            if (s_data != 0)
                break;
        }
    }

    public static void Main()
    {
        var t = new Thread(Test) { IsBackground = true };
        t.Start();
        Thread.Sleep(100);
        s_data = 1;
        Console.WriteLine(t.Join(2000) ? "Pass" : "Hang");
    }
}

Expected

Pass

Actual

Hang

       mov      eax, dword ptr [(reloc)]   ; s_data hoisted, s_state load gone
G_M24707_IG03:
       test     eax, eax
       je       SHORT G_M24707_IG03

Regression?

Yes, a regression in .NET 9. .NET 8 passes. .NET 9, 10, 11 RC2 and main hang.

Notes

fgOptimizeRelationalComparisonWithFullRangeConst only checks GTF_SIDE_EFFECT, so the non-faulting volatile IND (which only has GTF_ORDER_SIDEEFF) gets discarded. ECMA-335 I.12.6.7 forbids removing volatile operations. It also reproduces without AggressiveOptimization (via OSR/Tier1).
This is related to #134194 (volatile loads dropped), but the drop happens at a different place.

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 16, 2026
    8af938a
  6. locked and limited conversation to collaborators on Nov 16, 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