<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/80052>80052</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
RISCV64 miscompile at -O2/-O1
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
patrick-rivos
</td>
</tr>
</table>
<pre>
Testcase:
```c
int printf(const char *, ...);
int a[10000];
int b, c, d, e, f;
char g, h;
int j[100][100];
int m(long n) {
b = ((b >> 4) ^ (b ^ n));
b = ((b >> 4)) ^ a[(b ^ 0x0f) & 0x0F];
b = ((b >> 4)) ^ a[(b ^ 0x0f) & 0x0F];
b = ((b >> 4)) ^ a[(b ^ 0x0f) & 0x0F];
printf("%s %lX\n", &g, b ^ 0xFFFFFFFFUL);
return 0;
}
int main() {
int i = 0;
for (; c < 256; c++)
a[c] = c;
volatile int k = 3;
h = 0;
for (; (h <= 4); h += 1) {
int *l = &d;
*l = k;
k = 0;
}
for (; i < 6; i++)
for (; e < 1; e++) {
m(j[i][e]);
if (f)
printf("%d%d\n", i, e);
}
return m(d);
}
```
Commands:
```bash
> /scratch/tc-testing/llvm-jan-24/build/bin/clang -O2 red.c -o user-config-O2.out
> QEMU_CPU=rv64 /scratch/tc-testing/llvm-jan-24/build/bin/qemu-riscv64 user-config-O2.out
FFFFFFF0
FFFFFFFC
> /scratch/tc-testing/llvm-jan-24/build/bin/clang -O0 red.c -o user-config-O0.out
> QEMU_CPU=rv64 /scratch/tc-testing/llvm-jan-24/build/bin/qemu-riscv64 user-config-O0.out
FFFFFFF0
FFFFFFFF
```
This raw testcase differs at -O2, but after feeding it through bugpoint, I got a .bc file that differs at -01.
```bash
> /scratch/tc-testing/llvm-jan-24/build/bin/clang red.c -O2 -o user-config.bc -emit-llvm -c
> /scratch/tc-testing/llvm-jan-24/build/build-llvm-linux/bin/bugpoint user-config.bc --run-llc
> /scratch/tc-testing/llvm-jan-24/build/build-llvm-linux/bin/clang bugpoint-reduced-named-md.bc -O0 -o bugpoint-O0.out
> QEMU_CPU=rv64 /scratch/tc-testing/llvm-jan-24/build/bin/qemu-riscv64 bugpoint-O0.out
FFFFFFF0
FFFFFFFF
> /scratch/tc-testing/llvm-jan-24/build/build-llvm-linux/bin/clang bugpoint-reduced-named-md.bc -O1 -o bugpoint-O1.out
> QEMU_CPU=rv64 /scratch/tc-testing/llvm-jan-24/build/bin/qemu-riscv64 bugpoint-O1.out
FFFFFFF0
FFFFFFFC
```
[bugpoint.zip](https://github.com/llvm/llvm-project/files/14103541/bugpoint.zip)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVllv4zYQ_jX0y0ACRR22HvQQ2xGwQIv02BR9KyiSkphIlEtS2W1_fUFalqUku9trgzUUHZzhN_PNxVBjZKOEKFC6R-lxQ0fbDro4Uaslewy0fBrMphr4H8V7YSyjRqD4BuEjwjcow-eLnb-lsnDSUtkakR0blLHAWqoBkRtEDhCGISI5ivdXbYrSfYQxxig9rgSV28DcjbubcLd61vCojVtrV7seznAObH5ZyntEdt2gGlCI5IC2kwigAhQfAZEdIjv3foviW0i8UnoL58X01m9bUvjMxstex3Dejz_i2gtI5t7LpX_fLtacUkQIIqkBRNLuV5QelF84uG0-GRewcvrdf7cOlhZ21ArwvIS2x0VyqFTeyCozTiI9GbxAqgftycV7YIDiA5A08x-I7P2VXzTBM2UoPXoQtgB5GjpqZSe8jUcvjhfi9nNWEdk5-cGpJGeWbgPZu4XoGYczC0RuuikrGV-gwlXyeF1-fGF9DtbKEenpe_LyFfILTeE1I_86ay7cdM3hGkie20e4xyp9nkjt4OqVDfd7ViPc_10rRE5NvIBb0JnqwjnAlzqzyjxozp-Hoe-p4ublIKqoaael2PVtaZimlrWIlJYFVhgrVYNI2XVPffBAVUASRMpqlB13T1eAJeuoaiC4I6AFDxkEA4xG6IANqpZNcEfCYbRXIz_efn__2-GHexQf9VOW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