<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/86620>86620</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[RISC-V] Vector -flto -O2 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
unsigned a, b;
signed char d = 0xBF;
long c[12];
int main() {
c[9] = 0xFFFFF1FFF;
for (int e = 0; e < 12; e += d - 0xFFFFFFBC) {
a = c[e];
b = ({ b > (unsigned short)~c[e] ? b : (unsigned short)~c[e]; });
}
__builtin_printf("%X\n", a);
}
```
Commands:
```bash
> /scratch/tc-testing/tc-mar-25/build-rv64gcv/bin/clang -march=rv64gcv -flto -O2 -fuse-ld=lld red.c -o red.out -fwrapv
> /scratch/tc-testing/tc-mar-25/build-rv64gcv/bin/qemu-riscv64 red.out
1FFF
> /scratch/tc-testing/tc-mar-25/build-rv64gcv/bin/clang red.c -o red.out -fwrapv
> /scratch/tc-testing/tc-mar-25/build-rv64gcv/bin/qemu-riscv64 red.out
FFFF1FFF
```
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 i8 -65
@c = global [12 x i64] zeroinitializer
@b = global i32 0
@.str = constant [4 x i8] c"%X\0A\00"
define i32 @main() #0 {
entry:
store i64 68719419391, ptr getelementptr inbounds ([12 x i64], ptr @c, i64 0, i64 9), align 8
%0 = load i8, ptr @d, align 1
%conv12 = sext i8 %0 to i32
%b.promoted = load i32, ptr @b, align 4
br label %for.body
for.cond.cleanup: ; preds = %for.body
store i32 %.conv4, ptr @b, align 4
%call = tail call i32 (ptr, ...) @printf(ptr @.str, i32 %conv)
ret i32 0
for.body: ; preds = %for.body, %entry
%e.020 = phi i32 [ 0, %entry ], [ %add, %for.body ]
%.conv41819 = phi i32 [ %b.promoted, %entry ], [ %.conv4, %for.body ]
%idxprom = sext i32 %e.020 to i64
%arrayidx = getelementptr [12 x i64], ptr @c, i64 0, i64 %idxprom
%1 = load i64, ptr %arrayidx, align 8
%conv = trunc i64 %1 to i32
%conv3 = and i32 %conv, 1
%conv4 = xor i32 %conv3, 65535
%.conv4 = tail call i32 @llvm.umax.i32(i32 %.conv41819, i32 %conv4)
%sub = add i32 %e.020, 68
%add = add i32 %sub, %conv12
%cmp = icmp slt i32 %add, 12
br i1 %cmp, label %for.body, label %for.cond.cleanup
; uselistorder directives
uselistorder i32 %.conv4, { 1, 0 }
}
declare i32 @printf(ptr, ...)
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umax.i32(i32, i32) #1
attributes #0 = { "target-features"="+64bit,+v" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
!llvm.module.flags = !{!0}
!0 = !{i32 1, !"target-abi", !"lp64d"}
```
Commands:
```bash
> /scratch/tc-testing/tc-mar-25/build-rv64gcv/bin/clang -march=rv64gcv -O2 reduced.ll -o red.out -fwrapv
> /scratch/tc-testing/tc-mar-25/build-rv64gcv/bin/qemu-riscv64 red.out
FFFF1FFF
> /scratch/tc-testing/tc-mar-25/build-rv64gcv/bin/clang -march=rv64gcv -O2 -flto -fuse-ld=lld reduced.ll -o red.out -fwrapv
> /scratch/tc-testing/tc-mar-25/build-rv64gcv/bin/qemu-riscv64 red.out
1FFF
```
Found via fuzzer.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMWEmv27oV_jX0hpAgUYPlhRceKuABr3hAXhF0F1AkbbOhSJWkfH2z6G8vDiVLsp0BBVLkXQQOh8PvzB9pU-fkWQuxRcUeFccV7f3F2G1HvZXsc2Tl1bhVY_j79h_CeUadQNkOJUeU7FCZDP_YMO91gOKYInLADcr2w_q4yi7UYo5RdsTJbV9P28roM2ao2KcE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