Skip to content

JIT: (bug) zero-extension of a lock xadd result is wrongly elided, so (uint)Interlocked.Add(ref long, ...) keeps the upper 32 bits #135385

Closed

Description

@EgorBo

The x64 emitter doesn't treat xadd as writing its register operand, so the mov eax, eax zero-extension that follows it is removed as redundant.

Minimal Repro

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

public class Program
{
    static long s_val = -1;

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static ulong Test(ref long loc) => (uint)(Interlocked.Add(ref loc, 5) - 5);

    public static void Main() => Console.WriteLine(Test(ref s_val));
}

Expected

4294967295

Actual

18446744073709551615
       mov      eax, 5
       lock
       xadd     qword ptr [rcx], rax
       ret                              ; missing: mov eax, eax

Regression?

No. Reproduces on .NET 8.0, 9.0, 10.0, 11.0-rc2 and main.

Notes

  • emitIns_ARX_R (emitxarch.cpp) gives only xchg the IF_ARW_RRW format; xadd gets IF_ARW_RRD (register read-only), and emitIsInstrWritingToReg doesn't special-case INS_xadd.
  • So IsRedundantMov -> AreUpperBitsZero(rax, EA_4BYTE) walks past the xadd to the 4-byte mov eax, 5 and drops the mov eax, eax.
  • Fix: model xadd as IF_ARW_RRW (like xchg) and/or teach emitIsInstrWritingToReg that it writes its register operand.

Activity

  1. added this to the 12.0.0 milestone on Oct 8, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 8, 2026
  3. dotnet-policy-service commented on Oct 8, 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. added a commit that references this issue on Oct 14, 2026
    d24a264
  5. locked and limited conversation to collaborators on Nov 14, 2026
  6. self-assigned this
    on Nov 17, 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-coreclrbug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions