https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114180
Bug ID: 114180 Summary: RISC-V: missing vsetvl changes tail policy and causes wrong codegen Product: gcc Version: 13.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: camel-cdr at protonmail dot com Target Milestone: --- There is a codegen bug for RVV intrinsics in gcc 13.2.0, tested on native hardware and cross-compilers. It's fixed in on trunk, but I'm not sure how your policies on back-porting fixes is. I wasn't able to find an existing bug report that matches the bug, but I think this patch might be the one that fixed it in trunk: https://gcc.gnu.org/pipermail/gcc-patches/2024-January/643934.html Reproduction: $ cat test.c #include <riscv_vector.h> int validate_ascii(const char *buf, size_t len) { size_t vlmax = __riscv_vsetvlmax_e8m8(); vint8m8_t mask = __riscv_vmv_v_x_i8m8(0, vlmax); for (size_t vl; len > 0; len -= vl, buf += vl) { vl = __riscv_vsetvl_e8m8(len); vint8m8_t v = __riscv_vle8_v_i8m8((int8_t*)buf, vl); mask = __riscv_vor_vv_i8m8_tu(mask, mask, v, vl); } return __riscv_vfirst_m_b1(__riscv_vmslt_vx_i8m8_b1(mask, 0, vlmax), vlmax) < 0; } $ gcc-13 -march=rv64gcv -O2 -S test.c $ cat test.s validate_ascii: vsetvli a4,zero,e8,m8,ta,ma vmv.v.i v24,0 beq a1,zero,.L2 .L3: vsetvli a5,a1,e8,m8,ta,ma # <--- should be tu,ma sub a1,a1,a5 vle8.v v8,0(a0) add a0,a0,a5 vor.vv v24,v24,v8 bne a1,zero,.L3 vsetvli a4,zero,e8,m8,ta,ma .L2: vmslt.vi v8,v24,0 vfirst.m a0,v8 srli a0,a0,63 ret (output slightly cleaned up, and annotated) See also, for online reproduction: https://godbolt.org/z/jsbT4dErs