Issue 234328
Summary [ArgPromotion] Self-recursive call passes the promoted entry value after the function stored to the argument's memory
Labels miscompilation, ipo, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/6r6E99Y4v

```llvm
@G = global i32 1, align 4

define internal i32 @f(ptr %p, i1 %cond) {
entry:
  br i1 %cond, label %load, label %store

load:
  %v = load i32, ptr %p, align 4
  ret i32 %v

store:
  store i32 0, ptr @G, align 4
  %r = call i32 @f(ptr %p, i1 true)
  ret i32 %r
}

define i32 @main() {
  %r = call i32 @f(ptr @G, i1 false)
  ret i32 %r
}
```

```console
$ opt -S -passes=argpromotion repro.ll
define internal i32 @f(i32 %p.0.val, i1 %cond) {
...
store:
 store i32 0, ptr @G, align 4
  %r = call i32 @f(i32 %p.0.val, i1 true)
 ret i32 %r
}

define i32 @main() {
  %G.val = load i32, ptr @G, align 4
 %r = call i32 @f(i32 %G.val, i1 false)
  ret i32 %r
}

$ llubi repro.ll; echo $?
0
$ opt -S -passes=argpromotion repro.ll | llubi -; echo $?
1
```

### suggested fix
For a self-recursive call that passes the argument through (PR #78735), `doPromotion` inserts a load before the call and then retargets it to the promoted entry value, so it is only correct if nothing modifies the memory between the function entry and the recursive call.
`findArgParts` checks that path (`canInstructionRangeModRef` / `canBasicBlockModify`) only for the loads. For a non-`byval` argument, run the same check for every call in `RecursiveCalls`, and refuse promotion when it fails.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to