[fuzzer-found] [RISCV] Miscompile at -O2 since 23.1.0: masked interleaved load with one active field becomes a full `vlsegN` and reads the masked-off elements
Since #185602, RISCVTargetLowering::lowerInterleavedLoad lowers a masked interleaved load with a single active field to a full N-field segment load and relies on a later DAG combine to turn it into a strided load. That combine does not run when the tuning has optimized-nfN-segment-load-store, which the generic tuning has for N = 2 (on main also for N = 3 and 4, since #231919). The vlsegN then stays and reads every field, including the ones the mask excludes. The loop below reads b[0], b[2], ..., b[126]; the compiled code also reads b[127] and faults if that element is on an unmapped page. clang 23.1.6 and main are affected, 22.1.8 is not.
Reproducer
godbolt (RISC-V clang trunk, 23.1.0 and 22.1.0): https://godbolt.org/z/6fvroboPe
void f(short *restrict a, const short *restrict b) {
for (long i = 0; i < 64; i++)
a[i] = b[2 * i];
}
$ clang --target=riscv64-linux-gnu -O2 -march=rv64gcv_zvl256b -mrvv-vector-bits=zvl -S repro.c -o -
f:
addi a2, a0, 128
vsetvli a3, zero, e16, m2, ta, ma
.LBB0_1:
vlseg2e16.v v8, (a1)
vs2r.v v8, (a0)
addi a0, a0, 64
addi a1, a1, 128
bne a0, a2, .LBB0_1
ret
Each of the two iterations loads 32 segments of two fields, so b[0] to b[127] are read; the source reads only the even indices, up to b[126].
The same code comes out of plain -O2 -march=rv64gcv when the loop has #pragma clang loop vectorize_width(4).
To see the fault, put b at the end of a mapped page:
#include <stdio.h>
#include <sys/mman.h>
void f(short *restrict a, const short *restrict b);
int main(void) {
static short a[64];
/* One readable page followed by an inaccessible one. b[0..126] ends at the page boundary. */
char *m = mmap(0, 8192, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (m == MAP_FAILED || mprotect(m, 4096, PROT_READ | PROT_WRITE))
return 2;
short *b = (short *)(m + 4096) - 127;
for (int i = 0; i < 127; i++)
b[i] = (short)i;
f(a, b);
printf("%d\n", a[63]);
return 0;
}
$ clang --target=riscv64-linux-gnu -O2 -march=rv64gcv_zvl256b -mrvv-vector-bits=zvl -c repro.c
$ clang --target=riscv64-linux-gnu -static main.c repro.o -o repro
$ qemu-riscv64 -cpu rv64,v=true,vlen=256 ./repro
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
With repro.c built at -O0, by clang 22.1.8 with the same flags, or by main with -Xclang -target-feature -Xclang -optimized-nf2-segment-load-store added, the program prints 126.
IR
godbolt (llc trunk, llc trunk with the tuning off, llc 23.1.0, llc 22.1.0): https://godbolt.org/z/a6eYTq5qG
The loop vectorizer's output is correct. It marks the gaps in the mask of the wide load:
target triple = "riscv64-unknown-linux-gnu"
; Reads p[0], p[2], p[4], p[6] only.
define <4 x i16> @f(ptr %p) "target-features"="+v" {
%wide = call <8 x i16> @llvm.masked.load.v8i16.p0(ptr align 2 %p, <8 x i1> <i1 true, i1 false, i1 true, i1 false, i1 true, i1 false, i1 true, i1 false>, <8 x i16> poison)
%even = shufflevector <8 x i16> %wide, <8 x i16> poison, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
ret <4 x i16> %even
}
$ llc repro.ll -o -
f:
vsetivli zero, 4, e16, mf2, ta, ma
vlseg2e16.v v8, (a0)
ret
$ llc -mattr=-optimized-nf2-segment-load-store repro.ll -o -
f:
li a1, 4
vsetivli zero, 4, e16, mf2, ta, ma
vlse16.v v8, (a0), a1
ret
22.1.8 keeps the mask:
f:
li a1, 85
vsetivli zero, 8, e16, m1, ta, ma
vmv.s.x v0, a1
vle16.v v8, (a0), v0.t
vsetivli zero, 4, e16, mf2, ta, ma
vnsrl.wi v8, v8, 0
ret
What goes wrong
-
The interleaved-access pass recognises the gap mask (
-debug-only=interleaved-access: "With nominal factor 2 and actual factor 1") and callsRISCVTargetLowering::lowerInterleavedLoadwithGapMask= 0b01. -
Before #185602 the function returned false when fewer than two fields were active, and the masked load was kept. Since that change, a single active field takes the "normal segmented load" branch, which emits the full factor and drops the gap mask:
%1 = call { <4 x i16>, <4 x i16> } @llvm.riscv.seg2.load.mask.v4i16.p0.i64(ptr %p, <4 x i1> splat (i1 true), i64 4) %2 = extractvalue { <4 x i16>, <4 x i16> } %1, 0 -
The change expects the
RISCVISD::TUPLE_EXTRACTcombine inRISCVTargetLowering::PerformDAGCombineto rewrite this asvlse. That combine is an optimization and returns early if the tuple has more than one use or ifSubtarget.hasOptimizedSegmentLoadStore(NF)is true. When it does not fire, the segment load is selected as written, and a segment load accesses all fields of every active segment.
Notes
-
Group size and CPU,
llcon main, one loaded field out of N in amasked.loadof<4*N x i16>:-mcpu/-mattrfull vlsegNvlse-mattr=+v,generic-rv64,spacemit-x60N = 2, 3, 4 N = 5 to 8 sifive-x280,sifive-x390N = 2 N = 3 to 8 sifive-p670,syntacore-scr7,xiangshan-kunminghunone N = 2 to 8 -
On 23.1.6 only N = 2 becomes a full
vlsegN; N = 3 to 8 becomevlse. -
Tested: main at 5a464bdd79c1 (2026-11-27) and 9833b0c710ef, the 23.1.6 release, the 22.1.8 release. QEMU 11.1.3 and 8.2.2 user mode give the same results. Not tested on hardware.
-
#185602 and its parent were not built; the attribution rests on the diff, on 22.1.8 being correct, and on the
-optimized-nf2-segment-load-storeexperiment above. -
In the fuzzer's programs the same thing happens at
-O2,-Osand-Oz, with tail-folded loops (the gap mask ANDed with the lane mask, a maskedvlsegN) and with 8-bit and 16-bit elements, at-march=rv64gcvwith and without-mrvv-vector-bits=zvl. -
Plain
-O2 -march=rv64gcvwithout the pragma compiles the function above with a scalable width and avlse16.v, which is correct.
This problem was found with a fuzzer; the C function above was written by hand from its test case. The report was written with LLM assistance.
@llvm/issue-subscribers-backend-risc-v
Author: Nirmal (ubmaxxer)
Reproducer
godbolt (RISC-V clang trunk, 23.1.0 and 22.1.0): https://godbolt.org/z/6fvroboPe
void f(short *restrict a, const short *restrict b) {
for (long i = 0; i < 64; i++)
a[i] = b[2 * i];
}
$ clang --target=riscv64-linux-gnu -O2 -march=rv64gcv_zvl256b -mrvv-vector-bits=zvl -S repro.c -o -
f:
addi a2, a0, 128
vsetvli a3, zero, e16, m2, ta, ma
.LBB0_1:
vlseg2e16.v v8, (a1)
vs2r.v v8, (a0)
addi a0, a0, 64
addi a1, a1, 128
bne a0, a2, .LBB0_1
ret
Each of the two iterations loads 32 segments of two fields, so b[0] to b[127] are read; the source reads only the even indices, up to b[126].
The same code comes out of plain -O2 -march=rv64gcv when the loop has #pragma clang loop vectorize_width(4).
To see the fault, put b at the end of a mapped page:
#include <stdio.h>
#include <sys/mman.h>
void f(short *restrict a, const short *restrict b);
int main(void) {
static short a[64];
/* One readable page followed by an inaccessible one. b[0..126] ends at the page boundary. */
char *m = mmap(0, 8192, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (m == MAP_FAILED || mprotect(m, 4096, PROT_READ | PROT_WRITE))
return 2;
short *b = (short *)(m + 4096) - 127;
for (int i = 0; i < 127; i++)
b[i] = (short)i;
f(a, b);
printf("%d\n", a[63]);
return 0;
}
$ clang --target=riscv64-linux-gnu -O2 -march=rv64gcv_zvl256b -mrvv-vector-bits=zvl -c repro.c
$ clang --target=riscv64-linux-gnu -static main.c repro.o -o repro
$ qemu-riscv64 -cpu rv64,v=true,vlen=256 ./repro
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
With repro.c built at -O0, by clang 22.1.8 with the same flags, or by main with -Xclang -target-feature -Xclang -optimized-nf2-segment-load-store added, the program prints 126.
IR
godbolt (llc trunk, llc trunk with the tuning off, llc 23.1.0, llc 22.1.0): https://godbolt.org/z/a6eYTq5qG
The loop vectorizer's output is correct. It marks the gaps in the mask of the wide load:
target triple = "riscv64-unknown-linux-gnu"
; Reads p[0], p[2], p[4], p[6] only.
define <4 x i16> @<!-- -->f(ptr %p) "target-features"="+v" {
%wide = call <8 x i16> @<!-- -->llvm.masked.load.v8i16.p0(ptr align 2 %p, <8 x i1> <i1 true, i1 false, i1 true, i1 false, i1 true, i1 false, i1 true, i1 false>, <8 x i16> poison)
%even = shufflevector <8 x i16> %wide, <8 x i16> poison, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
ret <4 x i16> %even
}
$ llc repro.ll -o -
f:
vsetivli zero, 4, e16, mf2, ta, ma
vlseg2e16.v v8, (a0)
ret
$ llc -mattr=-optimized-nf2-segment-load-store repro.ll -o -
f:
li a1, 4
vsetivli zero, 4, e16, mf2, ta, ma
vlse16.v v8, (a0), a1
ret
22.1.8 keeps the mask:
f:
li a1, 85
vsetivli zero, 8, e16, m1, ta, ma
vmv.s.x v0, a1
vle16.v v8, (a0), v0.t
vsetivli zero, 4, e16, mf2, ta, ma
vnsrl.wi v8, v8, 0
ret
What goes wrong
-
The interleaved-access pass recognises the gap mask (
-debug-only=interleaved-access: "With nominal factor 2 and actual factor 1") and callsRISCVTargetLowering::lowerInterleavedLoadwithGapMask= 0b01. -
Before #185602 the function returned false when fewer than two fields were active, and the masked load was kept. Since that change, a single active field takes the "normal segmented load" branch, which emits the full factor and drops the gap mask:
%1 = call { <4 x i16>, <4 x i16> } @<!-- -->llvm.riscv.seg2.load.mask.v4i16.p0.i64(ptr %p, <4 x i1> splat (i1 true), i64 4) %2 = extractvalue { <4 x i16>, <4 x i16> } %1, 0 -
The change expects the
RISCVISD::TUPLE_EXTRACTcombine inRISCVTargetLowering::PerformDAGCombineto rewrite this asvlse. That combine is an optimization and returns early if the tuple has more than one use or ifSubtarget.hasOptimizedSegmentLoadStore(NF)is true. When it does not fire, the segment load is selected as written, and a segment load accesses all fields of every active segment.
Notes
-
Group size and CPU,
llcon main, one loaded field out of N in amasked.loadof<4*N x i16>:-mcpu/-mattrfull vlsegNvlse-mattr=+v,generic-rv64,spacemit-x60N = 2, 3, 4 N = 5 to 8 sifive-x280,sifive-x390N = 2 N = 3 to 8 sifive-p670,syntacore-scr7,xiangshan-kunminghunone N = 2 to 8 -
On 23.1.6 only N = 2 becomes a full
vlsegN; N = 3 to 8 becomevlse. -
Tested: main at 5a464bdd79c1 (2026-11-27) and 9833b0c710ef, the 23.1.6 release, the 22.1.8 release. QEMU 11.1.3 and 8.2.2 user mode give the same results. Not tested on hardware.
-
#185602 and its parent were not built; the attribution rests on the diff, on 22.1.8 being correct, and on the
-optimized-nf2-segment-load-storeexperiment above. -
In the fuzzer's programs the same thing happens at
-O2,-Osand-Oz, with tail-folded loops (the gap mask ANDed with the lane mask, a maskedvlsegN) and with 8-bit and 16-bit elements, at-march=rv64gcvwith and without-mrvv-vector-bits=zvl. -
Plain
-O2 -march=rv64gcvwithout the pragma compiles the function above with a scalable width and avlse16.v, which is correct.
This problem was found with a fuzzer; the C function above was written by hand from its test case. The report was written with LLM assistance.
Going to take a look if no one's already working on this.
A second symptom of the same one-active-field branch (#185602): it can also crash the compiler, and a fix that only makes the later strided-load combine run would not cover it.
long f(const long *b, int n) {
long s = 0;
#pragma clang loop vectorize_width(8)
for (int i = 0; i < n; i += 3)
s += b[i];
return s;
}
$ clang --target=riscv64-linux-gnu -Oz -march=rv64gcv -c repro.c
Invalid RISCV vector tuple type
UNREACHABLE executed at llvm/include/llvm/CodeGenTypes/MachineValueType.h:538!
lowerInterleavedLoad checks legality for the one active field, then emits a segment load for all three (llvm.riscv.seg3.load.mask.v8i64). At VLEN 128 <8 x i64> is a group of 4 registers, so three fields need a tuple type that does not exist, and lowering the intrinsic hits the llvm_unreachable.
main (be271f498e42) and 23.1.6 crash; 22.1.8 compiles. From C I only have it at -Oz with the pragma.
IR (llc -O2 crashes the same way)
target triple = "riscv64-unknown-linux-gnu"
define <8 x i64> @f(ptr %p) "target-features"="+v" {
%wide = call <24 x i64> @llvm.masked.load.v24i64.p0(ptr align 8 %p, <24 x i1> <i1 true, i1 false, i1 false, i1 true, i1 false, i1 false, i1 true, i1 false, i1 false, i1 true, i1 false, i1 false, i1 true, i1 false, i1 false, i1 true, i1 false, i1 false, i1 true, i1 false, i1 false, i1 true, i1 false, i1 false>, <24 x i64> poison)
%f0 = shufflevector <24 x i64> %wide, <24 x i64> poison, <8 x i32> <i32 0, i32 3, i32 6, i32 9, i32 12, i32 15, i32 18, i32 21>
ret <8 x i64> %f0
}