[ARM] Load/store optimization overwrites a live base register when forming VLDMIA (miscompile)
When ARM's load/store optimization merges vldr loads into vldmia, it materializes the new base in the original base
register while that register is still live. A later merged load then adds its offset to the already-advanced register and
reads the wrong memory.
- Reproduces with
llc14.0.6 and 20.1.2 (Ubuntu packages), and on Compiler Explorer withllc20.1.0, 21.1.0, 22.1.0 and assertions-enabled trunk (2026-09-14). No assertion fires. - Correct code with
--arm-load-store-opt=false,-O0,-fast-iselor-regalloc=basic. - Found through a failing Eigen test on 32-bit ARM built with clang 14 (
Array<Quaterniond, 2, 3>products). Reduced withllvm-reduceagainst a differential oracle: element 5 must match at-O0and at-O1without the pass, and differ at-O1with it. Inputs containingnull,undeforpoisonwere rejected.
Reproducer
llc -O1 -mtriple=armv7-unknown-linux-gnueabihf -mattr=+neon,+vfp4 -relocation-model=pic repro.ll
repro.ll
target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
target triple = "armv7-unknown-linux-gnueabihf"
; void run_impl(ptr k): dst = *k[0]; lhs = k[1][+8]; rhs = k[1][+16]. Elements are quaternions {x, y, z, w} of doubles
; (32 bytes). Computes one coefficient of dst[4] and dst[5] = lhs[1] * rhs[5]; the latter reads rhs[9] once miscompiled.
define void @run_impl(ptr %k) {
%dst_eval = load ptr, ptr %k, align 4
%dst = load ptr, ptr %dst_eval, align 4
%src_eval_p = getelementptr i8, ptr %k, i32 4
%src_eval = load ptr, ptr %src_eval_p, align 4
%lhs_p = getelementptr i8, ptr %src_eval, i32 8
%lhs = load ptr, ptr %lhs_p, align 4
%l0y_p = getelementptr i8, ptr %lhs, i32 8
%l0z_p = getelementptr i8, ptr %lhs, i32 16
%l0w_p = getelementptr i8, ptr %lhs, i32 24
%rhs_p = getelementptr i8, ptr %src_eval, i32 16
%rhs = load ptr, ptr %rhs_p, align 4
%l1x_p = getelementptr i8, ptr %lhs, i32 32
%l1y_p = getelementptr i8, ptr %lhs, i32 40
%l1z_p = getelementptr i8, ptr %lhs, i32 48
%l1w_p = getelementptr i8, ptr %lhs, i32 56
%a0x = load double, ptr %lhs, align 16
%a0y = load double, ptr %l0y_p, align 8
%a0z = load double, ptr %l0z_p, align 16
%a0w = load double, ptr %l0w_p, align 8
%r4w_p = getelementptr i8, ptr %rhs, i32 152
%b4w = load double, ptr %r4w_p, align 8
%b0x = load double, ptr %rhs, align 16
%r4y_p = getelementptr i8, ptr %rhs, i32 136
%b4y = load double, ptr %r4y_p, align 8
%r4z_p = getelementptr i8, ptr %rhs, i32 144
%b4z = load double, ptr %r4z_p, align 16
%t0 = fmul double %a0x, %b4w
%t1 = call double @llvm.fmuladd.f64(double %a0w, double %b0x, double %t0)
%t2 = call double @llvm.fmuladd.f64(double %a0y, double %b4z, double %t1)
%t3 = call double @llvm.fmuladd.f64(double %a0z, double %b4y, double %t2)
%d4x_p = getelementptr i8, ptr %dst, i32 128
store double %t3, ptr %d4x_p, align 16
store double 0.000000e+00, ptr %dst, align 8
%a1x = load double, ptr %l1x_p, align 16
%a1y = load double, ptr %l1y_p, align 8
%a1z = load double, ptr %l1z_p, align 16
%a1w = load double, ptr %l1w_p, align 8
%r5w_p = getelementptr i8, ptr %rhs, i32 184
%b5w = load double, ptr %r5w_p, align 8
%r5x_p = getelementptr i8, ptr %rhs, i32 160
%b5x = load double, ptr %r5x_p, align 16
%na1x = fneg double %a1x
%u0 = fmul double %b5x, %na1x
%u1 = call double @llvm.fmuladd.f64(double %a1w, double %b5w, double %u0)
%r5y_p = getelementptr i8, ptr %rhs, i32 168
%b5y = load double, ptr %r5y_p, align 8
%na1y = fneg double %a1y
%u2 = call double @llvm.fmuladd.f64(double %na1y, double %b5y, double %u1)
%r5z_p = getelementptr i8, ptr %rhs, i32 176
%b5z = load double, ptr %r5z_p, align 16
%na1z = fneg double %a1z
%w = call double @llvm.fmuladd.f64(double %na1z, double %b5z, double %u2)
%v0 = fmul double %a1x, %b5w
%v1 = call double @llvm.fmuladd.f64(double %a1w, double %b5x, double %v0)
%v2 = call double @llvm.fmuladd.f64(double %a1y, double %b5z, double %v1)
%x = call double @llvm.fmuladd.f64(double %na1z, double %b5y, double %v2)
%p0 = fmul double %a1y, %b5w
%p1 = call double @llvm.fmuladd.f64(double %a1w, double %b5y, double %p0)
%p2 = call double @llvm.fmuladd.f64(double %a1z, double %b5x, double %p1)
%y = call double @llvm.fmuladd.f64(double %na1x, double %b5z, double %p2)
%s0 = fmul double %a1z, %b5w
%s1 = call double @llvm.fmuladd.f64(double %a1w, double %b5z, double %s0)
%s2 = call double @llvm.fmuladd.f64(double %a1x, double %b5y, double %s1)
%z = call double @llvm.fmuladd.f64(double %na1y, double %b5x, double %s2)
%d5x_p = getelementptr i8, ptr %dst, i32 160
store double %x, ptr %d5x_p, align 16
%d5y_p = getelementptr i8, ptr %dst, i32 168
store double %y, ptr %d5y_p, align 8
%d5z_p = getelementptr i8, ptr %dst, i32 176
store double %z, ptr %d5z_p, align 16
%d5w_p = getelementptr i8, ptr %dst, i32 184
store double %w, ptr %d5w_p, align 8
ret void
}
declare double @llvm.fmuladd.f64(double, double, double)
run_impl(k) reads dst = *k[0], lhs = k[1][+8] and rhs = k[1][+16]. The arrays hold quaternions
{x, y, z, w} of doubles (32 bytes each). The function writes one coefficient of dst[4], then dst[5] = lhs[1] * rhs[5].
With the pass (llc 20.1.2; trunk emits the same sequence):
ldm r0, {r0, r3}
ldr r1, [r3, #8] @ r1 = lhs
ldr r2, [r3, #16] @ r2 = rhs
vldr d16, [r2]
add r2, r2, #136 @ r2 = rhs + 136, but rhs is still needed below
vldmia r2, {d17, d18, d19}
vldmia r1!, {d20, d21, d22, d23} @ writeback; the later lhs load is rebased to [r1]: correct
...
add r2, r2, #160 @ meant rhs + 160, computes rhs + 296
vldmia r2, {d16, d17, d18, d19}
vldmia r1, {d20, d21, d22, d23}
Without the pass:
ldr r3, [r0, #16]
vldr d16, [r3]
vldr d17, [r3, #136]
vldr d18, [r3, #144]
vldr d19, [r3, #152]
...
vldr d16, [r3, #160]
vldr d17, [r3, #168]
vldr d18, [r3, #176]
vldr d19, [r3, #184]
The lhs register is handled consistently: the writeback advances it by 32, and the next merge omits its #32. The
rhs register gets an in-place add with no writeback, yet the next merge still uses the offset from the original base.
Runtime check
driver.c builds the k layout and prints dst; run.sh links it against both builds and runs them under
qemu-arm-static. lhs[1] = {2, 2.25, 2.5, 2.75} and rhs[i] = {10i+1, 10i+2, 10i+3, 10i+4}.
good: element 5 = [237.5 286 270 -203]
bad: element 5 = [434 525 490 -350.5]
The bad result is lhs[1] times the 32 bytes at rhs + 296 = {92, 93, 94, 101}, for example
w = 2.75·101 − 2·92 − 2.25·93 − 2.5·94 = −350.5.
driver.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct { double c[4]; } Q; /* Eigen::Quaternion<double>: x, y, z, w */
extern void run_impl(void** kernel);
int main(void) {
Q* dst = aligned_alloc(16, 6 * sizeof(Q));
Q* lhs = aligned_alloc(16, 2 * sizeof(Q));
Q* rhs = aligned_alloc(16, 16 * sizeof(Q));
memset(dst, 0, 6 * sizeof(Q));
for (int i = 0; i < 2; ++i) for (int k = 0; k < 4; ++k) lhs[i].c[k] = 1 + i + 0.25 * k;
for (int i = 0; i < 16; ++i) for (int k = 0; k < 4; ++k) rhs[i].c[k] = 10 * i + 1 + k;
void* dst_eval[1] = {dst};
void* src_eval[5] = {0, 0, lhs, 0, rhs}; /* lhs data at +8, rhs data at +16 */
void* kernel[4] = {dst_eval, src_eval, 0, 0};
run_impl(kernel);
for (int i = 0; i < 6; ++i) printf("%d %.17g %.17g %.17g %.17g\n", i, dst[i].c[0], dst[i].c[1], dst[i].c[2], dst[i].c[3]);
return 0;
}
run.sh
#!/bin/bash
# Builds repro.ll with llc -O1 with and without the ARM load/store optimization, links the C driver, and runs both
# under QEMU. Element 5 must be lhs[1] * rhs[5] = [237.5 286 270 -203] (x y z w); the miscompiled build computes
# [434 525 490 -350.5], the same product with the 32 bytes at rhs + 296 in place of rhs[5].
# Usage: LLC=llc-20 CC=clang-20 ./run.sh (LLVM >= 15 for opaque pointers; needs qemu-arm-static and an
# arm-linux-gnueabihf sysroot)
set -e
cd "$(dirname "$0")"
LLC=${LLC:-llc-20}; CC=${CC:-clang-20}; SYSROOT=${SYSROOT:-/usr/arm-linux-gnueabihf}
T="-mtriple=armv7-unknown-linux-gnueabihf -relocation-model=pic -mattr=+neon,+vfp4 -filetype=obj"
C="--target=arm-linux-gnueabihf -march=armv7-a -mfpu=neon-vfpv4"
D=$(mktemp -d); trap 'rm -rf "$D"' EXIT
C_INCLUDE_PATH=$SYSROOT/include $CC $C -O0 -c driver.c -o "$D/driver.o"
$LLC -O1 $T repro.ll -o "$D/bad.o"
$LLC -O1 --arm-load-store-opt=false $T repro.ll -o "$D/good.o"
for v in good bad; do
LIBRARY_PATH=$SYSROOT/lib $CC $C "$D/driver.o" "$D/$v.o" -o "$D/$v"
printf '%-4s ' "$v:"; qemu-arm-static -L "$SYSROOT" "$D/$v" | awk '$1 == 5 { printf "element 5 = [%g %g %g %g]\n", $2, $3, $4, $5 }'
done
@rmlarsen I am looking to work on this issue please assign me this issue
Miscommunication, duplicate of https://github.com/llvm/llvm-project/issues/223630
@llvm/issue-subscribers-backend-arm
Author: Rasmus Munk Larsen (rmlarsen)
- Reproduces with
llc14.0.6 and 20.1.2 (Ubuntu packages), and on Compiler Explorer withllc20.1.0, 21.1.0, 22.1.0 and assertions-enabled trunk (2026-09-14). No assertion fires. - Correct code with
--arm-load-store-opt=false,-O0,-fast-iselor-regalloc=basic. - Found through a failing Eigen test on 32-bit ARM built with clang 14 (
Array<Quaterniond, 2, 3>products). Reduced withllvm-reduceagainst a differential oracle: element 5 must match at-O0and at-O1without the pass, and differ at-O1with it. Inputs containingnull,undeforpoisonwere rejected.
Reproducer
llc -O1 -mtriple=armv7-unknown-linux-gnueabihf -mattr=+neon,+vfp4 -relocation-model=pic repro.ll
<details><summary>repro.ll</summary>
target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
target triple = "armv7-unknown-linux-gnueabihf"
; void run_impl(ptr k): dst = *k[0]; lhs = k[1][+8]; rhs = k[1][+16]. Elements are quaternions {x, y, z, w} of doubles
; (32 bytes). Computes one coefficient of dst[4] and dst[5] = lhs[1] * rhs[5]; the latter reads rhs[9] once miscompiled.
define void @<!-- -->run_impl(ptr %k) {
%dst_eval = load ptr, ptr %k, align 4
%dst = load ptr, ptr %dst_eval, align 4
%src_eval_p = getelementptr i8, ptr %k, i32 4
%src_eval = load ptr, ptr %src_eval_p, align 4
%lhs_p = getelementptr i8, ptr %src_eval, i32 8
%lhs = load ptr, ptr %lhs_p, align 4
%l0y_p = getelementptr i8, ptr %lhs, i32 8
%l0z_p = getelementptr i8, ptr %lhs, i32 16
%l0w_p = getelementptr i8, ptr %lhs, i32 24
%rhs_p = getelementptr i8, ptr %src_eval, i32 16
%rhs = load ptr, ptr %rhs_p, align 4
%l1x_p = getelementptr i8, ptr %lhs, i32 32
%l1y_p = getelementptr i8, ptr %lhs, i32 40
%l1z_p = getelementptr i8, ptr %lhs, i32 48
%l1w_p = getelementptr i8, ptr %lhs, i32 56
%a0x = load double, ptr %lhs, align 16
%a0y = load double, ptr %l0y_p, align 8
%a0z = load double, ptr %l0z_p, align 16
%a0w = load double, ptr %l0w_p, align 8
%r4w_p = getelementptr i8, ptr %rhs, i32 152
%b4w = load double, ptr %r4w_p, align 8
%b0x = load double, ptr %rhs, align 16
%r4y_p = getelementptr i8, ptr %rhs, i32 136
%b4y = load double, ptr %r4y_p, align 8
%r4z_p = getelementptr i8, ptr %rhs, i32 144
%b4z = load double, ptr %r4z_p, align 16
%t0 = fmul double %a0x, %b4w
%t1 = call double @<!-- -->llvm.fmuladd.f64(double %a0w, double %b0x, double %t0)
%t2 = call double @<!-- -->llvm.fmuladd.f64(double %a0y, double %b4z, double %t1)
%t3 = call double @<!-- -->llvm.fmuladd.f64(double %a0z, double %b4y, double %t2)
%d4x_p = getelementptr i8, ptr %dst, i32 128
store double %t3, ptr %d4x_p, align 16
store double 0.000000e+00, ptr %dst, align 8
%a1x = load double, ptr %l1x_p, align 16
%a1y = load double, ptr %l1y_p, align 8
%a1z = load double, ptr %l1z_p, align 16
%a1w = load double, ptr %l1w_p, align 8
%r5w_p = getelementptr i8, ptr %rhs, i32 184
%b5w = load double, ptr %r5w_p, align 8
%r5x_p = getelementptr i8, ptr %rhs, i32 160
%b5x = load double, ptr %r5x_p, align 16
%na1x = fneg double %a1x
%u0 = fmul double %b5x, %na1x
%u1 = call double @<!-- -->llvm.fmuladd.f64(double %a1w, double %b5w, double %u0)
%r5y_p = getelementptr i8, ptr %rhs, i32 168
%b5y = load double, ptr %r5y_p, align 8
%na1y = fneg double %a1y
%u2 = call double @<!-- -->llvm.fmuladd.f64(double %na1y, double %b5y, double %u1)
%r5z_p = getelementptr i8, ptr %rhs, i32 176
%b5z = load double, ptr %r5z_p, align 16
%na1z = fneg double %a1z
%w = call double @<!-- -->llvm.fmuladd.f64(double %na1z, double %b5z, double %u2)
%v0 = fmul double %a1x, %b5w
%v1 = call double @<!-- -->llvm.fmuladd.f64(double %a1w, double %b5x, double %v0)
%v2 = call double @<!-- -->llvm.fmuladd.f64(double %a1y, double %b5z, double %v1)
%x = call double @<!-- -->llvm.fmuladd.f64(double %na1z, double %b5y, double %v2)
%p0 = fmul double %a1y, %b5w
%p1 = call double @<!-- -->llvm.fmuladd.f64(double %a1w, double %b5y, double %p0)
%p2 = call double @<!-- -->llvm.fmuladd.f64(double %a1z, double %b5x, double %p1)
%y = call double @<!-- -->llvm.fmuladd.f64(double %na1x, double %b5z, double %p2)
%s0 = fmul double %a1z, %b5w
%s1 = call double @<!-- -->llvm.fmuladd.f64(double %a1w, double %b5z, double %s0)
%s2 = call double @<!-- -->llvm.fmuladd.f64(double %a1x, double %b5y, double %s1)
%z = call double @<!-- -->llvm.fmuladd.f64(double %na1y, double %b5x, double %s2)
%d5x_p = getelementptr i8, ptr %dst, i32 160
store double %x, ptr %d5x_p, align 16
%d5y_p = getelementptr i8, ptr %dst, i32 168
store double %y, ptr %d5y_p, align 8
%d5z_p = getelementptr i8, ptr %dst, i32 176
store double %z, ptr %d5z_p, align 16
%d5w_p = getelementptr i8, ptr %dst, i32 184
store double %w, ptr %d5w_p, align 8
ret void
}
declare double @<!-- -->llvm.fmuladd.f64(double, double, double)
</details>
run_impl(k) reads dst = *k[0], lhs = k[1][+8] and rhs = k[1][+16]. The arrays hold quaternions
{x, y, z, w} of doubles (32 bytes each). The function writes one coefficient of dst[4], then dst[5] = lhs[1] * rhs[5].
With the pass (llc 20.1.2; trunk emits the same sequence):
ldm r0, {r0, r3}
ldr r1, [r3, #<!-- -->8] @ r1 = lhs
ldr r2, [r3, #<!-- -->16] @ r2 = rhs
vldr d16, [r2]
add r2, r2, #<!-- -->136 @ r2 = rhs + 136, but rhs is still needed below
vldmia r2, {d17, d18, d19}
vldmia r1!, {d20, d21, d22, d23} @ writeback; the later lhs load is rebased to [r1]: correct
...
add r2, r2, #<!-- -->160 @ meant rhs + 160, computes rhs + 296
vldmia r2, {d16, d17, d18, d19}
vldmia r1, {d20, d21, d22, d23}
Without the pass:
ldr r3, [r0, #<!-- -->16]
vldr d16, [r3]
vldr d17, [r3, #<!-- -->136]
vldr d18, [r3, #<!-- -->144]
vldr d19, [r3, #<!-- -->152]
...
vldr d16, [r3, #<!-- -->160]
vldr d17, [r3, #<!-- -->168]
vldr d18, [r3, #<!-- -->176]
vldr d19, [r3, #<!-- -->184]
The lhs register is handled consistently: the writeback advances it by 32, and the next merge omits its #<!-- -->32. The
rhs register gets an in-place add with no writeback, yet the next merge still uses the offset from the original base.
Runtime check
driver.c builds the k layout and prints dst; run.sh links it against both builds and runs them under
qemu-arm-static. lhs[1] = {2, 2.25, 2.5, 2.75} and rhs[i] = {10i+1, 10i+2, 10i+3, 10i+4}.
good: element 5 = [237.5 286 270 -203]
bad: element 5 = [434 525 490 -350.5]
The bad result is lhs[1] times the 32 bytes at rhs + 296 = {92, 93, 94, 101}, for example
w = 2.75·101 − 2·92 − 2.25·93 − 2.5·94 = −350.5.
<details><summary>driver.c</summary>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct { double c[4]; } Q; /* Eigen::Quaternion<double>: x, y, z, w */
extern void run_impl(void** kernel);
int main(void) {
Q* dst = aligned_alloc(16, 6 * sizeof(Q));
Q* lhs = aligned_alloc(16, 2 * sizeof(Q));
Q* rhs = aligned_alloc(16, 16 * sizeof(Q));
memset(dst, 0, 6 * sizeof(Q));
for (int i = 0; i < 2; ++i) for (int k = 0; k < 4; ++k) lhs[i].c[k] = 1 + i + 0.25 * k;
for (int i = 0; i < 16; ++i) for (int k = 0; k < 4; ++k) rhs[i].c[k] = 10 * i + 1 + k;
void* dst_eval[1] = {dst};
void* src_eval[5] = {0, 0, lhs, 0, rhs}; /* lhs data at +8, rhs data at +16 */
void* kernel[4] = {dst_eval, src_eval, 0, 0};
run_impl(kernel);
for (int i = 0; i < 6; ++i) printf("%d %.17g %.17g %.17g %.17g\n", i, dst[i].c[0], dst[i].c[1], dst[i].c[2], dst[i].c[3]);
return 0;
}
</details>
<details><summary>run.sh</summary>
#!/bin/bash
# Builds repro.ll with llc -O1 with and without the ARM load/store optimization, links the C driver, and runs both
# under QEMU. Element 5 must be lhs[1] * rhs[5] = [237.5 286 270 -203] (x y z w); the miscompiled build computes
# [434 525 490 -350.5], the same product with the 32 bytes at rhs + 296 in place of rhs[5].
# Usage: LLC=llc-20 CC=clang-20 ./run.sh (LLVM >= 15 for opaque pointers; needs qemu-arm-static and an
# arm-linux-gnueabihf sysroot)
set -e
cd "$(dirname "$0")"
LLC=${LLC:-llc-20}; CC=${CC:-clang-20}; SYSROOT=${SYSROOT:-/usr/arm-linux-gnueabihf}
T="-mtriple=armv7-unknown-linux-gnueabihf -relocation-model=pic -mattr=+neon,+vfp4 -filetype=obj"
C="--target=arm-linux-gnueabihf -march=armv7-a -mfpu=neon-vfpv4"
D=$(mktemp -d); trap 'rm -rf "$D"' EXIT
C_INCLUDE_PATH=$SYSROOT/include $CC $C -O0 -c driver.c -o "$D/driver.o"
$LLC -O1 $T repro.ll -o "$D/bad.o"
$LLC -O1 --arm-load-store-opt=false $T repro.ll -o "$D/good.o"
for v in good bad; do
LIBRARY_PATH=$SYSROOT/lib $CC $C "$D/driver.o" "$D/$v.o" -o "$D/$v"
printf '%-4s ' "$v:"; qemu-arm-static -L "$SYSROOT" "$D/$v" | awk '$1 == 5 { printf "element 5 = [%g %g %g %g]\n", $2, $3, $4, $5 }'
done
</details>