Skip to content

JIT x64: IsRedundantStackMov ignores EVEX embedded masks, dropping moves after a masked store to a stack local #135004

Description

@p6laris

emitter::IsRedundantStackMov doesn't consider embedded masking on either the current instruction or emitLastIns. A masked store to an address-exposed local reaches it through emitInsStoreInd and emitIns_S_R. If the next instruction is the same mov at the same size, with the same register and slot, it gets dropped.

A full reload is treated as the inverse move. HasSideEffect is false for 64-byte vmovups under EVEX, so this affects 512-bit; the 256-bit reload is kept. A full store hits the "same move" path, which doesn't check side effects, so 256-bit is affected there too.

[MethodImpl(MethodImplOptions.NoInlining)]
static void MaskedStoreThenLoad(Vector512<float>* src, Vector512<float>* maskp, Vector512<float>* dst)
{
    Vector512<float> y = *src + *src;
    Vector512<float> v = Vector512.Create(-1f);
    Avx512F.MaskStore((float*)&v, *maskp, y);
    *dst = v;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static float MaskedStoreThenStore256(Vector256<float>* src, Vector256<float>* maskp)
{
    Vector256<float> y = *src + *src;
    Vector256<float> v = Vector256.Create(-1f);
    Avx512F.VL.MaskStore((float*)&v, *maskp, y);
    Unsafe.Write(&v, y);
    return Sum256(&v); // NoInlining, Vector256.Sum(*p)
}

With src all 1.0 and only lane 0 selected in the mask, Vector512.Sum(*dst) should be -13 and the second method should return 16. Actual values are 32 and -5. Repros on 10.0.12 and main with DOTNET_TieredCompilation=0 (Tiger Lake).

       vmovups  zmmword ptr [rsp] {k1}, zmm0
                                            ; vmovups zmm0, zmmword ptr [rsp] dropped
       vmovups  zmmword ptr [r8], zmm0
       vmovups  ymmword ptr [rsp+0x20] {k1}, ymm0
                                            ; vmovups ymmword ptr [rsp+0x20], ymm0 dropped
       lea      rcx, [rsp+0x20]
       call     [StackMovRepro:Sum256(ptr):float]

Passing instOptions into IsRedundantStackMov and bailing when either instruction has an embedded mask fixes both. Happy to put up a PR.

Activity

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