llvm-project
llvm-project copied to clipboard
[AggressiveInstCombine] Load merging reads past an object before a non-returning call
reproducer
godbolt: https://godbolt.org/z/YcP7G39nr alive: https://alive2.llvm.org/ce/z/mHRguy
target triple = "x86_64-unknown-linux-gnu"
define void @spin() memory(none) {
entry:
br label %loop
loop:
br label %loop
}
define i16 @f(ptr %p) {
%lo = load i8, ptr %p, align 1
call void @spin()
%p1 = getelementptr i8, ptr %p, i64 1
%hi = load i8, ptr %p1, align 1
%lo.ext = zext i8 %lo to i16
%hi.ext = zext i8 %hi to i16
%hi.shl = shl i16 %hi.ext, 8
%r = or i16 %lo.ext, %hi.shl
ret i16 %r
}
define i32 @main() {
%obj = alloca i8, align 1
store i8 7, ptr %obj, align 1
%r = call i16 @f(ptr %obj)
%r.ext = zext i16 %r to i32
ret i32 %r.ext
}
$ opt -S -passes=aggressive-instcombine issue-repro.ll -o out.ll
$ llubi --max-steps 100000 issue-repro.ll
...
Error: Exceeded maximum number of execution steps.
error: Execution of function 'main' failed.
$ llubi --max-steps 100000 --verbose out.ll
...
Immediate UB detected: Memory access is out of bounds. Accessed size: 2, Address: 0x8, Object base: 0x8, Object size: 1.
error: Execution of function 'main' failed.
$ alive-tv --disable-undef-input --disable-poison-input --smt-to=20000 issue-repro.ll out.ll
...
ERROR: Source is more defined than target
...
Source:
i8 %lo = #x00 (0)
void = function did not return!
...
Target:
i16 %lo = UB triggered!
...
Summary:
2 correct transformations
1 incorrect transformations
0 failed-to-prove transformations
0 Alive2 errors
suggested fix
In foldLoadsRecursive, also check whether intervening instructions are guaranteed to transfer execution to their successor.
Do not create the wider load at LOps.RootInsert across a call that may not return.
Keep the existing alias and memory-write checks.
Proposed fix: #239276