Issue 171978
Summary [RISCV] Wrong code at -O2/O3
Labels backend:RISC-V, miscompilation
Assignees
Reporter XChy
    Reduced testcase:
```c
#include <stdint.h>
#include <stdio.h>
#define BS_VEC(type, num) type __attribute__((vector_size(num * sizeof(type))))
#define BITCAST(T, F, arg) (union { T dst }){ arg }.dst
uint64_t BS_CHECKSUM;

#include <riscv_bitmanip.h>
#define BARRIER_u32(x) (uint32_t)(__riscv_orc_b_32(1) + x - 0xff)

int16_t backsmith_snippet_246(uint64_t BS_ARG_0, BS_VEC(uint64_t, 32) BS_ARG_1)
{
 printf("%u\n", BARRIER_u32(0));
    BS_ARG_1[BARRIER_u32(0)] &= BS_ARG_0;
    return BS_ARG_1[0];
}
BS_VEC(int8_t, 32) backsmith_pure_2(int16_t)
{
    BS_VEC(uint32_t, 8) BS_VAR_1 = { 0, 7, 0, 0, 1, 800586355 };
    int16_t BS_TEMP_22 = backsmith_snippet_246(
 0,
        __builtin_convertvector(
 __builtin_shufflevector(BS_VAR_1, BS_VAR_1, 5, 5, 2, 9, 4, 4, 2, 0,
 2, 2, 4, 5, 3, 4, 0, 5, 7, 7, 3, 7, 0, 1, 8,
 4, 1, 2, 0, 4, 4, 2, 2, 2),
 BS_VEC(uint64_t, 32)));
    BS_VAR_1 = 0 == (BS_VEC(uint16_t, 16)){ BS_TEMP_22, BS_TEMP_22, BS_TEMP_22,
 BS_TEMP_22, BS_TEMP_22, BS_TEMP_22,
 BS_TEMP_22, BS_TEMP_22, BS_TEMP_22,
 BS_TEMP_22, BS_TEMP_22, BS_TEMP_22,
 BS_TEMP_22, BS_TEMP_22, BS_TEMP_22,
 BS_TEMP_22 };
    uint16_t BS_TEMP_81 = BS_VAR_1[3];
 BS_VAR_1 -= __builtin_convertvector(
        (BS_VEC(uint16_t, 8)){ BS_TEMP_81, BS_TEMP_81, BS_TEMP_81, BS_TEMP_81,
 BS_TEMP_81 },
        BS_VEC(uint32_t, 8));
    return __builtin_convertvector(
        __builtin_convertvector(
 __builtin_shufflevector(BS_VAR_1, BS_VAR_1, 0, 1, 4, 1, 7, 6, 3, 5,
 1, 5, 4, 0, 7, 4, 9, 4, 5, 2, 1, 9, 2, 7, 0,
 2, 4, 3, 4, 9, 1, 5, 6, 6),
 BS_VEC(uint32_t, 32))
            * __builtin_convertvector(
 __builtin_shufflevector(
                    (BS_VEC(uint8_t, 8)){ 0, 9 }, (BS_VEC(uint8_t, 8)){}, 3, 5,
                    9, 1, 1, 0, 2, 0, 4, 4, 3, 5, 5, 8, 3, 1, 8, 0, 2, 3, 5, 1,
                    2, 3, 1, 0, 3, 7, 2, 4, 7, 9),
                BS_VEC(uint32_t, 32)),
        BS_VEC(int8_t, 32));
}

int zero;
int64_t func_19()
{
    BS_VEC(uint32_t, 4) BS_VAR_3[7];
    for (int i = 0; i <= 2; i += 1)
    {
        for (; zero;)
        {
            for (uint32_t BS_TEMP_166; BS_TEMP_166; BS_TEMP_166++)
                for (uint32_t BS_TEMP_167; BS_TEMP_167 < 4; BS_TEMP_167++)
                {
                    BS_CHECKSUM ^= BS_VAR_3[BS_TEMP_166][BS_TEMP_167];
                BS_LABEL_9:
 }
            int volatile BS_COND_54;
            switch (BS_COND_54)
            case 7: goto BS_LABEL_9;
        }
 BS_VAR_3[backsmith_pure_2(0)[15]] = (BS_VEC(uint32_t, 4)){};
    }
 return 0;
}
void main() { func_19(); }
```

Command:
```sh
> clang --target=riscv64-unknown-linux-musl --sysroot=/opt/riscv64/riscv64-unknown-linux-musl/sysroot --gcc-toolchain=/opt/riscv64 -march=rv64gcvb_zicond -static -O2 test.c
> qemu-riscv64 a.out
Illegal instruction (core dumped)

> clang --target=riscv64-unknown-linux-musl --sysroot=/opt/riscv64/riscv64-unknown-linux-musl/sysroot --gcc-toolchain=/opt/riscv64 -march=rv64gcvb_zicond -static -O1 test.c
> qemu-riscv64 a.out
0
0
0
```

clang version 4427e34a6476721b53364e7dad97340dc38f5cb6
qemu-riscv64 version 10.1.2

If replace `#define BARRIER_u32(x) (uint32_t)(__riscv_orc_b_32(1) + x - 0xff)` with `#define BARRIER_u32(x) (uint32_t)(x)`, the program passes X86 UBSan and ASan: https://godbolt.org/z/zz9Wa88M1
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to