| Issue |
235466
|
| Summary |
[SROA] PHI load speculation introduces UB before a non-returning memory(none) call
|
| Labels |
miscompilation,
llvm:transforms,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/bafM6qPe5
alive: https://alive2.llvm.org/ce/z/ZXjtsZ
```llvm
define void @spin() memory(none) {
entry:
br label %loop
loop:
br label %loop
}
define i32 @main() {
entry:
%local = alloca i32, align 4
br i1 true, label %then, label %join
then:
br label %join
join:
%p = phi ptr [ %local, %entry ], [ null, %then ]
call void @spin()
%value = load i32, ptr %p, align 4
ret i32 %value
}
```
```console
$ opt -passes=verify -disable-output repro.ll
$ opt -S -passes=sroa repro.ll -o out.ll
$ sed -n '/^define i32 @main/,/^}/p' out.ll
define i32 @main() {
entry:
br i1 true, label %then, label %join
then: ; preds = %entry
%p.sroa.speculate.load.then = load i32, ptr null, align 4
br label %join
join: ; preds = %then, %entry
%p.sroa.speculated = phi i32 [ undef, %entry ], [ %p.sroa.speculate.load.then, %then ]
call void @spin()
ret i32 %p.sroa.speculated
}
$ llubi --max-steps 10000 repro.ll
Stacktrace:
#0 br label %loop at @spin repro.ll:5
#1 call void @spin() at @main repro.ll:16
Error: Exceeded maximum number of execution steps.
error: Execution of function 'main' failed.
$ llubi --max-steps 10000 out.ll
Stacktrace:
#0 %p.sroa.speculate.load.then = load i32, ptr null, align 4 at @main out.ll:18
Immediate UB detected: Invalid memory access via a pointer with nullary provenance.
error: Execution of function 'main' failed.
$ alive-tv --disable-undef-input --disable-poison-input --src-unroll=2 --tgt-unroll=2 repro.ll out.ll
...
Transformation doesn't verify!
ERROR: Source is more defined than target
...
Source:
...
void = function did not return!
...
Target:
>> Jump to %then
i32 %p.sroa.speculate.load.then = UB triggered!
...
Summary:
1 correct transformations
1 incorrect transformations
0 failed-to-prove transformations
0 Alive2 errors
```
### suggested fix
`isSafePHIToSpeculate` in `llvm/lib/Transforms/Scalar/SROA.cpp` only checks `mayWriteToMemory()` between the PHI and the load.
Also require `isGuaranteedToTransferExecutionToSuccessor(&*BBI)` for each instruction in that range before allowing speculation.
The single-successor predecessor shortcut assumes reaching the PHI guarantees reaching the load; `memory(none)` does not imply `willreturn`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs