<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/88834>88834</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[RISC-V][SLPVectorizer] rv64gcv miscompile
</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
signed char b = 1;
int c = 1;
int d[11];
char e[11];
unsigned *f = (unsigned *) d;
int main() {
d[5] = -1868904051;
for (int i = 0; i < 8; i += c)
e[i] = ({
long j = f[i];
b > j ? b : j;
});
__builtin_printf("%hhd\n", e[5]);
}
```
Commands:
```
/scratch/tc-testing/tc-apr-15/build-rv64gcv/build-llvm-linux/bin/clang -march=rv64gcv -flto -O1 red.c -o user-config.out -fno-strict-aliasing
> /scratch/tc-testing/tc-apr-15/build-rv64gcv/bin/qemu-riscv64 user-config.out
-115
/scratch/tc-testing/tc-apr-15/build-rv64gcv/build-llvm-linux/bin/clang -march=rv64gcv -flto -O2 red.c -o user-config.out -fno-strict-aliasing
> /scratch/tc-testing/tc-apr-15/build-rv64gcv/bin/qemu-riscv64 user-config.out
1
```
Reduced LLVM IR:
```llvm ir
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "riscv64-unknown-linux-gnu"
@d = global [11 x i32] zeroinitializer
@e = global [11 x i8] zeroinitializer
@.str = constant [6 x i8] c"%hhd\0A\00"
define i32 @main() #0 {
for.body.preheader:
store i32 -1868904051, ptr getelementptr inbounds ([11 x i32], ptr @d, i64 0, i64 5), align 4
%conv.4 = zext i32 0 to i64
%cond.4 = tail call i64 @llvm.smax.i64(i64 %conv.4, i64 0)
%conv5.4 = trunc i64 %cond.4 to i8
store i8 %conv5.4, ptr getelementptr inbounds ([11 x i8], ptr @e, i64 0, i64 4), align 1
%0 = load i32, ptr getelementptr inbounds ([11 x i32], ptr @d, i64 0, i64 5), align 4
%conv.5 = zext i32 %0 to i64
%cond.5 = tail call i64 @llvm.smax.i64(i64 %conv.5, i64 1)
%conv5.5 = trunc i64 %cond.5 to i8
store i8 %conv5.5, ptr getelementptr inbounds ([11 x i8], ptr @e, i64 0, i64 5), align 1
%1 = load i8, ptr getelementptr inbounds ([11 x i8], ptr @e, i64 0, i64 5), align 1
%conv8 = zext i8 %1 to i32
%call = tail call i32 (ptr, ...) @printf(ptr @.str, i32 %conv8)
ret i32 0
}
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #1
declare i32 @printf(ptr, ...)
; uselistorder directives
uselistorder ptr @llvm.smax.i64, { 1, 0 }
attributes #0 = { "target-features"="+64bit,+v" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
```
Godbolt showing the optimization: https://godbolt.org/z/7EE4GhzsP
Found via fuzzer.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMV1tv2zwS_TX0y0CCREm28uAHX-KiQBdbtIu-FhQ1ktlSpJeknMS_fkFKtmWn6aIf-l2MQOFtZg7PIUcjZq1oFeKSFGtSbGesd3ttlgfmjODfIyOO2s4qXb8s_4PWcWaRZCuSbEmyIvNk-ONDPziqge-ZgQpItoWUZOthTigH_AdjNSnWaUqK7WU0mOOr4V6N7gldNcERoeV0kNAHqG98d0woQks_QRbjOISIBSm2wUeUlvPyIcmT4goLoNHGe_c-RFiWkGwdmhsoxyZd-wlO6MNo5SGLs18f9hrS_6RWLXwLk824chLR_zxlj2HJLrRX8O1mBVlsfbTL0NevVS-kE-rrwQjlmrBVSmix39ek2KjQ2QRcfr9TW-_qVsKxG54b3XVM1fa10GOX7iw3zPE9oTvHI4fWCdUOHXYwUVoQuvPg6sgc53nLj5e-lMcukkL1z37I67PjkqkWoo4ZvifZdrSAqJFOQ_TvFAzWMYdIQ2_RRFyrRrSx7h1EjdKR9QfVRUwKZ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