Issue 176001
Summary [RISCV] Wrong code at -O1 on riscv64-unknown-linux-musl
Labels backend:RISC-V, miscompilation, generated by fuzzer
Assignees
Reporter XChy
    Reduced C testcase:
```c
#include <stdio.h>
#include <riscv_bitmanip.h>
#define BS_VEC(type, num) type __attribute__((vector_size(num * sizeof(type))))
#define BARRIER_u8(x) (uint8_t)(__riscv_orc_b_32(1) + x - 0xff)
#define BARRIER_u64(x) (uint64_t)(__riscv_orc_b_64(1) + x - 0xffull)
#define BARRIER_v4u32(x) (__riscv_orc_b_32(0) ? (BS_VEC(uint32_t, 4)){} : x)
#define BARRIER_v16u8(x) (__riscv_orc_b_32(0) ? (BS_VEC(uint8_t, 16)){} : x)
// BARRIER_xxxx(x) should be equivalent to x.
int main()
{
    uint32_t volatile BS_COND_0 = 9002699;
    switch (BS_COND_0)
    {
        case 3: goto BS_LABEL_5;
 case 4: goto BS_LABEL_8;
    }
    BS_VEC(uint64_t, 32) BS_VAR_1[52] = {};
BS_LABEL_9:
    if (BARRIER_u8(0)) goto BS_LABEL_8;
    uint64_t LOCAL_CHECKSUM = 0;
BS_LABEL_7:
    uint16_t l_7 = 35;
BS_LABEL_5:
 int volatile BS_COND_2 = 0;
    if (BS_COND_2) goto BS_LABEL_9;
    uint8_t BS_TEMP_239 = BARRIER_u64(0);
    BS_VEC(uint64_t, 32)
    BS_TEMP_240 = { BS_TEMP_239, BS_TEMP_239, BS_TEMP_239, BS_TEMP_239,
 BS_TEMP_239, BS_TEMP_239, BS_TEMP_239, BS_TEMP_239,
 BS_TEMP_239, BS_TEMP_239, BS_TEMP_239, BS_TEMP_239,
 BS_TEMP_239, BS_TEMP_239, BS_TEMP_239, BS_TEMP_239,
 BS_TEMP_239, BS_TEMP_239, BS_TEMP_239, BS_TEMP_239,
 BS_TEMP_239, BS_TEMP_239, BS_TEMP_239, BS_TEMP_239,
 BS_TEMP_239, BS_TEMP_239, BS_TEMP_239, BS_TEMP_239,
 BS_TEMP_239, BS_TEMP_239, BS_TEMP_239, BS_TEMP_239 };
BS_LABEL_8:
 BS_VEC(uint64_t, 32) BS_TEMP_241 = BS_VAR_1[2];
    BS_VEC(uint64_t, 32)
 BS_TEMP_242 = __builtin_convertvector(
 __builtin_shufflevector((BS_VEC(uint8_t, 16)){},
 BARRIER_v16u8((BS_VEC(uint8_t, 16)){ 66 }), 2,
 0, 5, 7, 0, 1, 8, 7, 4, 3, 0, 4, 2, 8, 9, 4, 0,
 0, 2, 0, 9, 20, 4, 2, 16, 6, 8, 4, 5, 4, 5, 5),
 BS_VEC(uint64_t, 32));
    BS_VAR_1[0] =
        __builtin_convertvector(
 __builtin_shufflevector(
 BARRIER_v4u32(((BS_VEC(uint32_t, 4)){ 35, 0, l_7, 0 })),
 (BS_VEC(uint32_t, 4)){ 0, 0, 0, 35 }, 0, 4, 4, 0, 7, 2, 6, 0, 0,
 5, 6, 6, 7, 0, 3, 7, 2, 6, 7, 6, 4, 0, 3, 4, 0, 3, 5, 0, 4, 7,
 0, 4),
            BS_VEC(uint64_t, 32))
        <= (BS_VEC(uint64_t, 32)){
               BS_TEMP_240[0],
               0,
               0,
 BS_TEMP_241[3] ? BS_TEMP_240[3] << 3 : BS_TEMP_242[3],
 0,
               0,
               0,
               0,
 0,
               BS_TEMP_241[9] ? BS_TEMP_240[9] << 9 : BS_TEMP_242[9],
               0,
               0,
               0,
 BS_TEMP_240[3],
               0,
               5,
 BS_TEMP_242[16],
               0,
               BS_TEMP_241[18] < 4 ? BS_TEMP_240[8] << 8 : BS_TEMP_242[18],
               BS_TEMP_241[19],
 BS_TEMP_240[0],
               BS_TEMP_240[21] << BS_TEMP_241[21],
               BS_TEMP_240[22],
 BS_TEMP_240[3],
               BS_TEMP_240[24],
 BS_TEMP_240[25],
               BS_TEMP_240[6],
 BS_TEMP_240[7],
               BS_TEMP_241[28],
 BS_TEMP_240[9],
               BS_TEMP_240[0],
               1 << BS_TEMP_241[1]
           };
    uint32_t volatile BS_COND_4 = 5;
    if (BS_COND_4 == 7) goto BS_LABEL_7;
    for (uint32_t BS_TEMP_243 = 0; BS_TEMP_243 < 2; BS_TEMP_243++)
        LOCAL_CHECKSUM ^= BS_VAR_1[BS_TEMP_243][21];
    uint64_t BS_CHECKSUM = LOCAL_CHECKSUM;
 printf("BackSmith Checksum = 0x%016llx\n", BS_CHECKSUM);
}
```

Reproducible commands:
qemu-riscv64 version 10.1.2
```bash
> clang --target=riscv64-unknown-linux-musl -march=rv64gcvb_zicond --gcc-toolchain=<toolchain> --sysroot=<sysroot> -static -O0 a.c
> qemu-riscv64 a.out
BackSmith Checksum = 0xffffffffffffffff
> clang --target=riscv64-unknown-linux-musl -march=rv64gcvb_zicond --gcc-toolchain=<toolchain> --sysroot=<sysroot> -static -O1 a.c
> qemu-riscv64 a.out
BackSmith Checksum = 0x0000000000000000
```

Undefined behaviour check (replace BARRIER_xxx(x) with x): https://godbolt.org/z/fbWxeE8x4
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to