| Issue |
233446
|
| Summary |
[TailCallElim] A read-only call after the self-call is moved above it although the self-call writes the memory it reads
|
| Labels |
miscompilation,
llvm:optimizations,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/nfqTd1zfY
```llvm
@G = global i32 0
define i32 @f(i32 %n) {
entry:
store i32 %n, ptr @G
%c = icmp eq i32 %n, 0
br i1 %c, label %base, label %rec
base:
ret i32 0
rec:
%r = call i32 @f(i32 0)
%x = call i32 @rd()
ret i32 %x
}
define i32 @rd() #0 {
%g = load i32, ptr @G
ret i32 %g
}
define i32 @main() {
%r = call i32 @f(i32 1)
ret i32 %r
}
attributes #0 = { nounwind willreturn memory(read) }
```
```console
$ opt -S -passes=tailcallelim repro.ll
define i32 @f(i32 %n) {
entry:
br label %tailrecurse
tailrecurse:
%n.tr = phi i32 [ %n, %entry ], [ 0, %rec ]
%ret.tr = phi i32 [ poison, %entry ], [ %current.ret.tr, %rec ]
%ret.known.tr = phi i1 [ false, %entry ], [ true, %rec ]
store i32 %n.tr, ptr @G, align 4
%c = icmp eq i32 %n.tr, 0
br i1 %c, label %base, label %rec
base:
%current.ret.tr1 = select i1 %ret.known.tr, i32 %ret.tr, i32 0
ret i32 %current.ret.tr1
rec:
%x = tail call i32 @rd()
%current.ret.tr = select i1 %ret.known.tr, i32 %ret.tr, i32 %x
br label %tailrecurse
}
$ llubi repro.ll; echo $?
0
$ opt -S -passes=tailcallelim repro.ll | llubi -; echo $?
1
```
### suggested fix
`canMoveAboveCall` (TailRecursionElimination.cpp) checks the self-call's mod/ref only for a `LoadInst`; a `nounwind willreturn memory(read)` call has no side effects and falls through to `return !is_contained(...)`.
Reject any other instruction that may read memory when the self-call may write it (or query AA for calls as the load path does). This is distinct from #232385 (speculation safety): with `speculatable` added to `@rd` (valid, it only loads an initialised global) the output and the wrong result are the same.
> Disclaimer: This test case was generated by a fuzzer, and the report was written with LLM assistance.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs