Skip to content

JIT: (bug) Volatile loads whose value is discarded are deleted, allowing CSE across an acquire read #134194

Open

Description

@EgorBo

A volatile load whose result is thrown away (e.g. s_flag * 0) is removed entirely, so ordinary loads can be CSEd across it — an observable memory-model violation.

Minimal Repro

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

public class Program
{
    static volatile int s_flag;
    static int s_data;

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static bool SawUpdate()
    {
        int a = s_data;
        int acquire = s_flag * 0; // volatile read - must not be removed
        int b = s_data;
        return a != b || acquire != 0;
    }

    public static void Main()
    {
        new Thread(() => { while (true) { s_data++; } }) { IsBackground = true }.Start();
        for (int i = 0; i < 100000000; i++)
        {
            if (SawUpdate()) { Console.WriteLine("reloaded"); return; }
        }
        Console.WriteLine("volatile read deleted; s_data CSEd across the acquire");
    }
}

Expected

reloaded

Actual

volatile read deleted; s_data CSEd across the acquire

SawUpdate compiles to xor eax, eax / ret — no memory access at all remains. Removing the * 0 makes the same method reload s_data correctly.

Notes

Also repros on released .NET 10.0.12.
Two distinct paths delete the load, both because the "has side effects" predicates ignore GTF_IND_VOLATILE / GTF_ORDER_SIDEEFF: for this source Roslyn folds x * 0 and emits volatile.ldsfld + pop, and the importer's CEE_POP handling discards it. Hiding the zero behind an AggressiveInlining helper makes the JIT do the folding instead, and then gtWrapWithSideEffects/gtExtractSideEffList (via gtNodeHasSideEffects) drops it the same way.

Activity

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

    @MihuBot

    I'm a bot. Here are possible related and/or duplicate issues (I may be wrong):

  5. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 8.0.425
    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
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