Skip to content

JIT: (bug) [ARM64] load after a post-indexed ldr with the same immediate is wrongly replaced by mov (wrong result) #135522

Closed

Description

@EgorBo

On arm64, a load that follows a post-indexed ldr x1, [x0], #9 and uses the same immediate (ldr x0, [x0, #9]) is wrongly replaced by mov x0, x1, which produces a wrong result.

Minimal Repro

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct S { public long A; public byte B; }

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static long Test(Span<S> s)
    {
        if (s.Length < 4) return 0;
        long a = s[0].A;
        s = s.Slice(1);
        long b = s[1].A;
        return a ^ b;
    }

    public static int Main()
    {
        S[] data = new S[8];
        for (int i = 0; i < data.Length; i++) data[i].A = i + 1;
        long expected = 1 ^ 3;
        long actual = Test(data);
        Console.WriteLine($"Expected: {expected}, Actual: {actual}");
        return actual == expected ? 100 : 1;
    }
}

Expected

Expected: 2, Actual: 2

Actual

Expected: 2, Actual: 0

The second load is replaced by a mov of the first load's result:

ldr     x1, [x0], #0x09
mov     x0, x1              ; should be `ldr x0, [x0, #0x09]`
eor     x0, x1, x0

Regression?

Yes. .NET 10.0.12 doesn't reproduce (it emits ldr x2, [x0], #0x09 ... ldr x0, [x0, #0x09]); main reproduces with both Checked and Release JIT.

Notes

  • IsOptimizableLdrToMov (emitarm64.cpp) compares fmt/imm/base but doesn't check whether the previous ldr is post-indexed (idInsOpt() == INS_OPTS_POST_INDEX), so the writeback immediate (9) is treated as a load displacement.
  • IsOptimizableLdrStrWithPair has the same missing guard.

Activity

  1. added this to the 12.0.0 milestone on Oct 9, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 9, 2026
  3. dotnet-policy-service commented on Oct 9, 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 Nov 18, 2026
    5297736
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions