<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/134126>134126</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V] Miscompile on rv64gcv with -O3
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ewlu
      </td>
    </tr>
</table>

<pre>
    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.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsVkuz4jYT_TVi02XKlp8sWPD4qJrF1HxJZrKlZEkGTWSJq4e5sMhvT0k2A1xml1RWoW5dtdXdR0enW7KJteKgOF-ico3K7Yx4d9Rmyc_Sz1rNLsuv3DpKLEf5CqUrVKXjH0XpSmp1AILy9c2mox0RGdAjMcAA5VvAoyPO8NH2agrrpqSjNg6-P6CN8BOmUA7EiJXjtKibcXq_1lqCjI7scaq_p6noXdwn3sbwJk2Lpi4_0PGoXGclKrcv4wdmZ1SuX_2DFgwOCDeRMMIbuG0Mb2Biezf7YEaON-MN4Q1KVzD-nlj9jBPewDOjJ-cCUL2OaKIDhBv1OAXQaRNmH8v1HqVJUb6O5ib4gwPhxRsgvIYsx5MTr0OogAQWKcKLH6SfUS93wEsEzIrJHvP_LNOIWxbP3MKPxtywq_dxW-8_ZL6FMEB4FaIkIFwhXEWlpvDLw_CUxSOw_wD8GonqbbRvY3djTQDlO-gB5au4t3AyYkwoYU-EQrh53M-kSfC-KJyFDozAeP1-F_Ih5VXDh5TLT7QPSdd70vUl6fqYBD_R7IrKbQRo0jpb1HVT33SJvR36LnZ07OXYxbGDQ_eCD__OYYUphYyFZNPzft96IZ1Q-5MRynVRK4xwKaVH5UbFh004-TeIUdsfd0-w09VG9z1RzH704ByMsHSIdgEI7yw1xNEjwrtwrSG8Y0TIS-JP1hlO-iTQYQjv4pgc6BDsUMMdlUQd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