[RISC-V] Clang Miscompiles Segment RVV Intrinsics
Reduced test case:
#include <riscv_vector.h>
#define dataLen 12*8
int8_t a[dataLen]; int8_t b[dataLen];
int8_t c[dataLen]; int8_t d[dataLen];
int main(){
for (int i = 0; i < dataLen; ++i) { a[i] = -i; }
for (int i = 0; i < dataLen; ++i) { b[i] = i; }
for (int i = 0; i < dataLen; ++i) { c[i] = 0; d[i] = 0; }
int avl = dataLen;
int8_t* ptr_a = a; int8_t* ptr_b = b;
int8_t* ptr_c = c; int8_t* ptr_d = d;
for (size_t vl; avl > 0; avl -= vl * 8){
vl = __riscv_vsetvl_e8mf2(avl);
vint8mf2_t va = __riscv_vle8_v_i8mf2(ptr_a, vl);
__riscv_vse8_v_i8mf2(ptr_c, va, vl);
vint8mf2x8_t vb = __riscv_vlseg8e8_v_i8mf2x8(ptr_b, vl);
vint8mf2_t vd = __riscv_vget_v_i8mf2x8_i8mf2(vb, 0);
__riscv_vse8_v_i8mf2(ptr_d, vd, vl);
ptr_a += vl; ptr_c += vl; ptr_d += vl;
ptr_b += vl * 8;
}
for(int i=0; i<dataLen; ++i) { __builtin_printf("%d ", d[i]); } __builtin_printf("\n");
return 0;
}
Result:
$ clang -march=rv64gcv_zvfh -mabi=lp64d -Wno-psabi -static -O0 1.c -o a.out && qemu-riscv64 a.out
0 8 16 24 32 40 48 56 64 72 80 88 0 -8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
$ clang -march=rv64gcv_zvfh -mabi=lp64d -Wno-psabi -static -O3 1.c -o a.out && qemu-riscv64 a.out
0 8 16 24 32 40 48 56 64 72 80 88 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Clang version under test: ["17.0.6", "18-init", "18.1.8", "19-init", "19.1.4", "20-init"] QEMU version: 9.1.0
$ clang --version
clang version 19.1.4 (https://github.com/llvm/llvm-project.git aadaa00de76ed0c4987b97450dd638f63a385bed)
Target: riscv64-unknown-unknown-elf
Thread model: posix
InstalledDir: /home/compiler/llvm/19.1.4/bin
$ qemu-riscv64 --version
qemu-riscv64 version 9.1.0
Copyright (c) 2003-2024 Fabrice Bellard and the QEMU Project developers
@4vtomat
Let me take a look
This is more broken code - you're doing vl = __riscv_vsetvl_e8mf2(avl); but then doing avl -= vl*8; for stripmining loops you always need the vsetvl argument from one iteration to the next to decrease exactly by the result of the vsetvl. After the loop you have avl == -32, clearly showing undesirable behavior.
More generally, as-is there's rather little surface area where clang/llvm could even miscompile RVV intrinsics, as most of them are directly one-to-one translated to instructions (exceptions being vsetvl elimination (this should be thoroughly tested though), and some vmerge merging IIRC).
@llvm/issue-subscribers-backend-risc-v
Author: None (rvismith-project)
int main(){ for (int i = 0; i < dataLen; ++i) { a[i] = -i; } for (int i = 0; i < dataLen; ++i) { b[i] = i; } for (int i = 0; i < dataLen; ++i) { c[i] = 0; d[i] = 0; } int avl = dataLen; int8_t* ptr_a = a; int8_t* ptr_b = b; int8_t* ptr_c = c; int8_t* ptr_d = d; for (size_t vl; avl > 0; avl -= vl * 8){ vl = __riscv_vsetvl_e8mf2(avl); vint8mf2_t va = __riscv_vle8_v_i8mf2(ptr_a, vl); __riscv_vse8_v_i8mf2(ptr_c, va, vl); vint8mf2x8_t vb = __riscv_vlseg8e8_v_i8mf2x8(ptr_b, vl); vint8mf2_t vd = __riscv_vget_v_i8mf2x8_i8mf2(vb, 0); __riscv_vse8_v_i8mf2(ptr_d, vd, vl); ptr_a += vl; ptr_c += vl; ptr_d += vl; ptr_b += vl * 8; } for(int i=0; i<dataLen; ++i) { __builtin_printf("%d ", d[i]); } __builtin_printf("\n"); return 0; }
Result:
$ clang -march=rv64gcv_zvfh -mabi=lp64d -Wno-psabi -static -O0 1.c -o a.out && qemu-riscv64 a.out 0 8 16 24 32 40 48 56 64 72 80 88 0 -8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
$ clang -march=rv64gcv_zvfh -mabi=lp64d -Wno-psabi -static -O3 1.c -o a.out && qemu-riscv64 a.out 0 8 16 24 32 40 48 56 64 72 80 88 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Clang version under test: ["17.0.6", "18-init", "18.1.8", "19-init", "19.1.4", "20-init"]
QEMU version: 9.1.0
$ clang --version clang version 19.1.4 (https://github.com/llvm/llvm-project.git aadaa00de76ed0c4987b97450dd638f63a385bed) Target: riscv64-unknown-unknown-elf Thread model: posix InstalledDir: /home/compiler/llvm/19.1.4/bin $ qemu-riscv64 --version qemu-riscv64 version 9.1.0 Copyright (c) 2003-2024 Fabrice Bellard and the QEMU Project developers
</details>
Thanks for your reply. Based on my understanding of your reply, I should change the for-statement from for (size_t vl; avl > 0; avl -= vl * 8) to for (size_t vl; avl > 0; avl -= vl ), but I find that similar differences between -O0 and other optimization flags still exists.
I check the riscv-v-spec, and find:
The vl register gives the number of segments to move, which is equal to the number of elements transferred to each vector
register group.
Actually vl is the number of segments, and in each iteration vl * n elements are operated. For this reason, I think that avl -= vl*8 is appropriate.
I carefully check the assembly code and find the differences are from array out of bounds. At -O0, the execution order of assembly code is the same as C intrinsics: values of c is changed by vse8.v, and then vlseg8e8.v is executed. At other optimizations, all load instructions are before store instructions (https://godbolt.org/z/4TcWEMcdG):
li a4, 96
.LBB0_8:
vsetvli a5, a4, e8, mf2, ta, ma
vle8.v v8, (a3)
vlseg8e8.v v9, (a0)
vse8.v v8, (a1)
vse8.v v9, (a2)
add a3, a3, a5
add a1, a1, a5
add a2, a2, a5
slli s1, a5, 3
subw a4, a4, a5
add a0, a0, s1
bgtz a4, .LBB0_8
vlseg8e8.v gets the values out of b array (some values are from c array), and inconsistency in execution order of load-store instructions causes the different results.
Now the question is, it seems that the unsafety problem of out-of-bound array always exists whenever I use the segment load-store intrinsics, even in formal cases (array length is an integer multiple of n-field, loop variable decreases by the result of vsetvl, etc). This problem exists even if compilers are implemented with rvv intrinsics one-to-one translated to instructions. Noted that vector register length is unknown until runtime, this problem seems difficult to prevent and repair in C-level.
I understand the difficulty of implementing these interfaces on compilers. This looks like a vulnerability of rvv interfaces, and maybe it would be more appropriate to report this issue in rvv-intrinsic-doc. I try to propose a solution based on my limited knowledge on compiler implementation. There is a gap of vl implication between assembly-level and c-level for segment load-store:
- assembly-level: the number of segments;
- c-level: return value of
vsetvlandvsetvlmaxintrinsics (in formal programming style).
Actually, the number of segments * n-field <= avl. In current implementation for rvv intrinsics compilers, the above gap is ignored. See the assembly above, vlseg8e8.v uses the vl set by vsetvli a5, a4, e8, mf2, ta, ma, which is n-times the number of segments, not the number of segments.
I think rs1 of vsetvl should be "application segment length" (avl / n) rather than "application vector length" (avl). Extra vsetvl instructions are needed to update and restore the value of vl register.
There are two ways to fix your code:
- remove the
* 8fromavl -= vl * 8and divide the initialavlby8beforehand; i.e. this; - or, divide the
vsetvlargument by8, i.e. this.
Both of those approaches make the code follow my "for stripmining loops you always need the vsetvl argument from one iteration to the next to decrease exactly by the result of the vsetvl" rule one way or another.
vsetvls rs1 is always vector length; it's just that segment load/store operate on multiple vectors of that length within one instruction.
This is pretty clearly understandable and reasonable for code using segment loads/stores for their intended purpose, i.e. loading structures. e.g. a __riscv_vlseg3e32_v_f32m1x3(arr, 5) corresponds to loading from struct { float a, b, c; } arr[5], the first vector in the tuple being all five a values, second being the b values, etc. At no point does 3*5 == 15 play any part here.
There are two ways to fix your code:
- remove the
* 8fromavl -= vl * 8and divide the initialavlby8beforehand; i.e. this;- or, divide the
vsetvlargument by8, i.e. this.Both of those approaches make the code follow my "for stripmining loops you always need the
vsetvlargument from one iteration to the next to decrease exactly by the result of thevsetvl" rule one way or another.
vsetvlsrs1is always vector length; it's just that segment load/store operate on multiple vectors of that length within one instruction.This is pretty clearly understandable and reasonable for code using segment loads/stores for their intended purpose, i.e. loading structures. e.g. a
__riscv_vlseg3e32_v_f32m1x3(arr, 5)corresponds to loading fromstruct { float a, b, c; } arr[5], the first vector in the tuple being all fiveavalues, second being thebvalues, etc. At no point does3*5 == 15play any part here.
Thank you very much for your reply! Your fix is reasonable. Not a bug, an input with undefined behavior.