Skip to content

Volatile reads of a promoted struct's field lose volatile semantics when the struct is address-exposed #136936

Closed

Description

@NinoFloris

A volatile field read through a promoted struct local is rewritten by local morph into a plain read of the promoted field local, and the volatile flag is dropped. Later phases then treat it as an ordinary value: repeated reads are folded, and no acquire barrier is emitted.

public struct Counters { public volatile int Volatile; public int Plain; }

[MethodImpl(MethodImplOptions.NoInlining)] static void Expose(ref Counters c) { c.Plain++; }

[MethodImpl(MethodImplOptions.NoInlining)]
static int SumVolatileLocal()
{
    Counters c = default;
    Expose(ref c);
    return c.Volatile + c.Volatile + c.Volatile + c.Volatile;
}

arm64, main, FullOpts:

    ldr     w0, [fp, #0x18]   // [V02 tmp1]
    lsl     w0, w0, #2

The non-promoted parameter version keeps the expected four acquire loads. Volatile.Read is also fine because its intrinsic expansion takes the correct path instead of:

if (node->IsVolatile() && (!addr->OperIs(GT_FIELD_ADDR) || ((addr->gtFlags & GTF_FLD_DEREFERENCED) == 0)))
{
// TODO-Bug: transforming volatile indirections like this is not legal. The above condition is a quirk.
return false;

Activity

  1. added
    untriaged
    on Nov 3, 2026
  2. added
    area-CodeGen-coreclr
    on Nov 3, 2026
  3. dotnet-policy-service commented on Nov 3, 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. removed
    untriaged
    on Nov 6, 2026
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions