llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[RISC-V] clang++ gets different results at `-O0`

Open ams-x9v2b7kq opened this issue 1 year ago • 3 comments

Link: https://godbolt.org/z/c83W59h1s results:

clang++ test.cpp -march=rv64gcv_zvfh -mabi=lp64d -O0 -o a.out && $QEMU -L $SYSROOT a.out 6400000 clang++ test.cpp -march=rv64gcv_zvfh -mabi=lp64d -O1 -o a.out && $QEMU -L $SYSROOT a.out 000000

version:

$ clang++ --version
clang version 21.1.1 (https://github.com/llvm/llvm-project.git 5a86dc996c26299de63effc927075dcbfb924167)
Target: riscv64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/2025-compiler/riscv64-glibc-ubuntu-24.04-llvm/bin

code:

#include <riscv_vector.h>
uint8_t a[1]; int16_t b[6]; int8_t c[1]; uint8_t d[6];
int main() {
  for (int e = 0; e < 6; ++e) { b[e] = 84; }
  for (size_t f = 0, ae = 6, g; ae; ae -= g) {
    g = __riscv_vsetvl_e8m8(ae);
    vuint8m8_t i = __riscv_vle8_v_u8m8(&a[f], g);
    vbool1_t j = __riscv_vmseq_vx_u8m8_b1(i, 1, g);
    vint16m1_t k = __riscv_vle16_v_i16m1(&b[f], g);
    vint8m8_t l = __riscv_vle8_v_i8m8(&c[f], g);
    vint16m8_t a = __riscv_vcreate_v_i16m1_i16m8(k, k, k, k, k, k, k, k);
    vbool1_t af = __riscv_vmsle_vv_i8m8_b1(l, l, g);
    __riscv_vsm_v_b1(&d[f], __riscv_vmand_mm_b1(af, j, g), g);
  }
  for (int e = 0; e < 6; ++e) __builtin_printf("%u", d[e]);
}

ams-x9v2b7kq avatar Nov 26 '25 06:11 ams-x9v2b7kq

According to the spec, mask vector load/store operates as byte load/store, and its evl=ceil(vl/8). So __riscv_vsm_v_b1(&d[f], __riscv_vmand_mm_b1(af, j, g), g); would still store 1 byte to memory when vl is 6. This means that the top 2 bits are dirty values, and the code should not use those.

jacquesguan avatar Nov 26 '25 07:11 jacquesguan

According to the spec, mask vector load/store operates as byte load/store, and its evl=ceil(vl/8). So __riscv_vsm_v_b1(&d[f], __riscv_vmand_mm_b1(af, j, g), g); would still store 1 byte to memory when vl is 6. This means that the top 2 bits are dirty values, and the code should not use those.

You are right. Thanks for your kind reply.

ams-x9v2b7kq avatar Nov 26 '25 07:11 ams-x9v2b7kq

@llvm/issue-subscribers-backend-risc-v

Author: None (ams-x9v2b7kq)

Link: https://godbolt.org/z/c83W59h1s results: > clang++ test.cpp -march=rv64gcv_zvfh -mabi=lp64d -O0 -o a.out && $QEMU -L $SYSROOT a.out 6400000 > clang++ test.cpp -march=rv64gcv_zvfh -mabi=lp64d -O1 -o a.out && $QEMU -L $SYSROOT a.out 000000

version:

$ clang++ --version
clang version 21.1.1 (https://github.com/llvm/llvm-project.git 5a86dc996c26299de63effc927075dcbfb924167)
Target: riscv64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/2025-compiler/riscv64-glibc-ubuntu-24.04-llvm/bin

code:

#include &lt;riscv_vector.h&gt;
uint8_t a[1]; int16_t b[6]; int8_t c[1]; uint8_t d[6];
int main() {
  for (int e = 0; e &lt; 6; ++e) { b[e] = 84; }
  for (size_t f = 0, ae = 6, g; ae; ae -= g) {
    g = __riscv_vsetvl_e8m8(ae);
    vuint8m8_t i = __riscv_vle8_v_u8m8(&amp;a[f], g);
    vbool1_t j = __riscv_vmseq_vx_u8m8_b1(i, 1, g);
    vint16m1_t k = __riscv_vle16_v_i16m1(&amp;b[f], g);
    vint8m8_t l = __riscv_vle8_v_i8m8(&amp;c[f], g);
    vint16m8_t a = __riscv_vcreate_v_i16m1_i16m8(k, k, k, k, k, k, k, k);
    vbool1_t af = __riscv_vmsle_vv_i8m8_b1(l, l, g);
    __riscv_vsm_v_b1(&amp;d[f], __riscv_vmand_mm_b1(af, j, g), g);
  }
  for (int e = 0; e &lt; 6; ++e) __builtin_printf("%u", d[e]);
}

llvmbot avatar Nov 26 '25 15:11 llvmbot