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

[RISC-V] Miscompile on rv64gcv with -O3

Open ewlu opened this issue 1 year ago • 10 comments

Testcase:

long a;
long c;
signed char d = 2;
char e;
unsigned f;
short j;
long long ac;
int i = 2320478;
_Bool l = 1;
_Bool m;
int n = 9;
int q = 18004875;
unsigned u[15][15][15];
long long w[5][15];
void g(int i, short j, _Bool l, _Bool m, int n, int q,
       unsigned u[][15][15], long long w[][15]) {
  if (n) {
    for (signed char x = 0; x < (char)q + 132; x += i - 90)
      for (signed y = 0; y < 14; y += ~50 + 54) {
        c = w[x][x];
        d *= l && u[x][y][y];
        e = u[x][x][y];
      }
  }
  f += a ? m : 0;
}
int main() {
  for (int x = 0; x < 15; ++x)
    for (int y = 0; y < 15; ++y)
      for (int z = 0; z < 15; ++z)
        u[x][y][z] = 807197787;
  g(i, j, l, m, n, q, u, w);
  ac = d;
  __builtin_printf("%llu\n", ac);
}

Commands

# riscv
$ /scratch/ewlu/daily-upstream-build/build-gcv/bin/clang -march=rv64gcv_zvl256b -flto -O3 ./red.c -o user-config.out -fsigned-char -fno-strict-aliasing -fwrapv
$ QEMU_CPU=rv64,vlen=256,rvv_ta_all_1s=true,rvv_ma_all_1s=true,v=true,vext_spec=v1.0,zve32f=true,zve64f=true timeout --verbose -k 0.1 4 /scratch/ewlu/daily-upstream-build/build-gcv/bin/qemu-riscv64 user-config.out 1
0

$ /scratch/ewlu/daily-upstream-build/build-gcv/bin/clang -march=rv64gcv_zvl256b -flto -O2 ./red.c -o user-config.out -fsigned-char -fno-strict-aliasing -fwrapv 
$ QEMU_CPU=rv64,vlen=256,rvv_ta_all_1s=true,rvv_ma_all_1s=true,v=true,vext_spec=v1.0,zve32f=true,zve64f=true timeout --verbose -k 0.1 4 /scratch/ewlu/daily-upstream-build/build-gcv/bin/qemu-riscv64 user-config.out 1
2

# x86
$ ./native.out
2

bisected to 25e43334a86804c77d460493e37b57274257461a as the first bad commit

Tried reducing testcase more by eliminating dead code but that causes the failure to disappear. Also at some point during manual reduction, -O2 also started to fail but now it's generating expected output. If anyone wants, I can provide the unreduced testcase with the -O2 failure as well.

Found via fuzzer.

ewlu avatar Apr 02 '25 17:04 ewlu

CC @preames

topperc avatar Apr 02 '25 17:04 topperc

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

Author: Edwin Lu (ewlu)

Testcase: ```c long a; long c; signed char d = 2; char e; unsigned f; short j; long long ac; int i = 2320478; _Bool l = 1; _Bool m; int n = 9; int q = 18004875; unsigned u[15][15][15]; long long w[5][15]; void g(int i, short j, _Bool l, _Bool m, int n, int q, unsigned u[][15][15], long long w[][15]) { if (n) { for (signed char x = 0; x < (char)q + 132; x += i - 90) for (signed y = 0; y < 14; y += ~50 + 54) { c = w[x][x]; d *= l && u[x][y][y]; e = u[x][x][y]; } } f += a ? m : 0; } int main() { for (int x = 0; x < 15; ++x) for (int y = 0; y < 15; ++y) for (int z = 0; z < 15; ++z) u[x][y][z] = 807197787; g(i, j, l, m, n, q, u, w); ac = d; __builtin_printf("%llu\n", ac); } ```

Commands

# riscv
$ /scratch/ewlu/daily-upstream-build/build-gcv/bin/clang -march=rv64gcv_zvl256b -flto -O3 ./red.c -o user-config.out -fsigned-char -fno-strict-aliasing -fwrapv
$ QEMU_CPU=rv64,vlen=256,rvv_ta_all_1s=true,rvv_ma_all_1s=true,v=true,vext_spec=v1.0,zve32f=true,zve64f=true timeout --verbose -k 0.1 4 /scratch/ewlu/daily-upstream-build/build-gcv/bin/qemu-riscv64 user-config.out 1
0

$ /scratch/ewlu/daily-upstream-build/build-gcv/bin/clang -march=rv64gcv_zvl256b -flto -O2 ./red.c -o user-config.out -fsigned-char -fno-strict-aliasing -fwrapv 
$ QEMU_CPU=rv64,vlen=256,rvv_ta_all_1s=true,rvv_ma_all_1s=true,v=true,vext_spec=v1.0,zve32f=true,zve64f=true timeout --verbose -k 0.1 4 /scratch/ewlu/daily-upstream-build/build-gcv/bin/qemu-riscv64 user-config.out 1
2

# x86
$ ./native.out
2

bisected to 25e43334a86804c77d460493e37b57274257461a as the first bad commit

Tried reducing testcase more by eliminating dead code but that causes the failure to disappear. Also at some point during manual reduction, -O2 also started to fail but now it's generating expected output. If anyone wants, I can provide the unreduced testcase with the -O2 failure as well.

Found via fuzzer.

llvmbot avatar Apr 02 '25 17:04 llvmbot

@topperc is anyone working on this already? If not could I get assigned?

bababuck avatar Apr 05 '25 04:04 bababuck

@topperc is anyone working on this already? If not could I get assigned?

Sure

topperc avatar Apr 05 '25 05:04 topperc

Just to note - I tried to reproduce this today without LTO, and failed. I'll try again later today with LTO, and see if I can extract a triggering IR example to share.

preames avatar Apr 07 '25 16:04 preames

I noticed the same behavior, tried to reproduce without -flto on Spike and was unable to.

bababuck avatar Apr 07 '25 16:04 bababuck

I've identified the cause of the failure. Patch forthcoming.

preames avatar Apr 07 '25 21:04 preames

The bug is that we're matching the isSpanSplatShuffle predicate as if undef elements in the mask can be rewritten as late non-undef elements, but then we actually use the original mask without rewriting. We appear to have the same bug in isLocalRepeatingShuffle.

Thankfully, neither of the changes seem to have made it into release branches.

Illustrative bad match: t208: v16i32 = vector_shuffle<u,u,u,u,u,u,u,u,u,u,u,3,2,1,0,u> t232, undef:v16i32

preames avatar Apr 07 '25 21:04 preames

Any idea why this required LTO to occur?

bababuck avatar Apr 07 '25 21:04 bababuck

Any idea why this required LTO to occur?

It looks like we vectorize more aggressively for LTO.

preames avatar Apr 07 '25 21:04 preames