Skip to content

JIT: (bug) APX: movbe load from a stack local into r16-r31 is encoded as a store (opcode 0x61 instead of 0x60) #135389

Open

Description

@EgorBo

With APX and MOVBE, BinaryPrimitives.ReverseEndianness of a stack local whose result goes to an extended GPR (r16-r31) is emitted with the EVEX MAP4 MOVBE store opcode. The disassembler prints a load, but on APX hardware the instruction overwrites the local with garbage and never loads the value.

Minimal Repro

using System;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Consume(ref int v) { v++; }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static int Test(int[] a)
    {
        int v = a[14];
        Consume(ref v);
        int x0 = a[0], x1 = a[1], x2 = a[2], x3 = a[3], x4 = a[4], x5 = a[5], x6 = a[6], x7 = a[7];
        int x8 = a[8], x9 = a[9], x10 = a[10], x11 = a[11], x12 = a[12], x13 = a[13];
        int r = BinaryPrimitives.ReverseEndianness(v);
        int s1 = r + x0 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12 + x13;
        int s2 = r ^ x0 ^ x1 ^ x2 ^ x3 ^ x4 ^ x5 ^ x6 ^ x7 ^ x8 ^ x9 ^ x10 ^ x11 ^ x12 ^ x13;
        return s1 - s2;
    }

    static void Main()
    {
        int[] a = new int[15];
        for (int i = 0; i < a.Length; i++) a[i] = i + 1;
        Console.WriteLine(Test(a));
    }
}

Compile for APX + MOVBE, e.g. ReadyToRun or NativeAOT with --instruction-set x86-64-v3,apx. No APX hardware is needed to see the encoding.

Expected

A load: movbe r23d, dword ptr [rsp+0x28] = 62 E4 7C 08 60 7C 24 28 (EVEX MAP4 opcode 0x60 = MOVBE r, r/m).

Actual

       62E47C08617C2428     movbe    r23d, dword ptr [rsp+0x28]     ; opcode 0x61 = MOVBE m32, r32 (store)

Same bytes from crossgen2 and ILC. The address-mode path is correct: ReverseEndianness(a[14]) emits 62E47C08607B48 movbe r23d, dword ptr [rbx+0x48].

Regression?

No, present since APX support was added: reproduces on .NET 10.0, 11.0-rc2 and main. .NET 8.0/9.0 don't support apx.

Notes

  • genCodeForBswap picks INS_movbe_apx for an EGPR target with a contained memory operand.
  • emitOutputSV (emitxarch.cpp) unconditionally does code |= 0x01 for 2/4/8-byte operands, turning the RM opcode 0x60 into the MR opcode 0x61; emitOutputAM already exempts INS_movbe_apx.
  • Fix: apply the same exemption in emitOutputSV (and check emitOutputCV, which has the same pattern).

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
    apx
    on Oct 8, 2026
  5. JulieLeeMSFT commented on Oct 8, 2026

    @JulieLeeMSFT
    Member
  6. Ruihan-Yin commented on Oct 8, 2026

    @Ruihan-Yin
    Member

    Looking into it, thanks for the notice.

    Hi @EgorBo, just to confirm: since this instance was intentionally compiled with APX enabled, this is only an encoding issue and not related to any LSRA misbehavior, correct?

  7. EgorBo commented on Oct 8, 2026

    @EgorBo
    MemberAuthor

    8. Looking into it, thanks for the notice.
    Hi @EgorBo, just to confirm: since this instance was intentionally compiled with APX enabled, this is only an encoding issue and not related to any LSRA misbehavior, correct?

    not related to any LSRA misbehavior, correct?

    Hard to say, I don't have any APX hw to confirm

  8. Ruihan-Yin commented on Oct 9, 2026

    @Ruihan-Yin
    Member

    #135451
    Submitted a fix for the encoding issue. Tested on APX capable hardware, the newly added regression test was failing on main, and now passes with the fix in place. I don't see any evidence suggesting this is an LSRA issue. From the emitter side, however, it appears the wrong opcode was selected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

apxarea-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions