| Issue |
234334
|
| Summary |
[TailCallElim] byval copy-back for the eliminated self-call overwrites the argument before a trailing load of it
|
| Labels |
miscompilation,
llvm:optimizations,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/c6xb8nre8
```llvm
%S = type { i32, i32 }
@g = global %S { i32 7, i32 0 }
@h = global %S { i32 5, i32 0 }
define i32 @f(ptr byval(%S) align 4 %p, i32 %n) {
entry:
%c = icmp eq i32 %n, 0
br i1 %c, label %base, label %rec
base:
%bv = load i32, ptr %p, align 4
ret i32 %bv
rec:
%r = call i32 @f(ptr byval(%S) @g, i32 0)
%v = load i32, ptr %p, align 4
%s = add i32 %r, %v
ret i32 %s
}
; f(@h, 1) = f(@g, 0) + 5 = 7 + 5 = 12
define i32 @main() {
%r = call i32 @f(ptr byval(%S) @h, i32 1)
ret i32 %r
}
```
```console
$ opt -passes=tailcallelim -S repro.ll | grep -A5 '^rec:'
rec: ; preds = %tailrecurse
call void @llvm.memcpy.p0.p0.i64(ptr align 1 %g, ptr align 1 @g, i64 8, i1 false)
call void @llvm.memcpy.p0.p0.i64(ptr align 1 %p, ptr align 1 %g, i64 8, i1 false)
%v = load i32, ptr %p, align 4
%s = add i32 %accumulator.tr, %v
br label %tailrecurse
$ llubi repro.ll; echo $?
12
$ opt -passes=tailcallelim -S repro.ll | llubi -; echo $?
14
$ cat repro.c
struct S { int a[6]; };
struct S g = {{7}}, h = {{5}};
__attribute__((noinline)) int f(struct S s, int n) {
if (n == 0)
return s.a[0];
int r = f(g, 0);
return r + s.a[0];
}
int main(void) { return f(h, 1); }
$ clang -O2 repro.c && ./a.out; echo $?
14
$ clang -O2 -fno-optimize-sibling-calls repro.c && ./a.out; echo $?
12
```
### suggested fix
`eliminateCall` leaves the instructions between the call and the `ret` after the call, but `copyLocalTempOfByValueOperandIntoArguments` writes the next iteration's value into `F.getArg(OpndIdx)` at the call (`IRBuilder<> Builder(CI)`), so a trailing load of the byval argument that `canMoveAboveCall` accepted reads the new value.
Emit the copy-back into the argument just before the new back-edge branch (after the trailing instructions), or refuse trailing instructions that may read a byval argument.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs