Issue 237704
Summary [SimpleLoopUnswitch] Partial unswitching omits freeze using facts from an in-loop branch
Labels miscompilation, loopoptim, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/zbYb6zEz9
alive: https://alive2.llvm.org/ce/z/cf63qw

```llvm
define i32 @f(i32 %n) {
entry:
  %incremented = add nsw i32 %n, 1
  %positive = icmp sgt i32 %incremented, 0
  br label %header

header:
  br i1 true, label %exit, label %guard

guard:
  br i1 %positive, label %body, label %body

body:
  %condition = and i1 false, %positive
  br i1 %condition, label %taken, label %header

taken:
  ret i32 0

exit:
  ret i32 0
}

define i32 @main() {
entry:
  %r = call i32 @f(i32 2147483647)
 ret i32 %r
}
```

```console
$ opt -S '-passes=loop-mssa(simple-loop-unswitch<nontrivial>)' issue-repro.ll
...
define i32 @f(i32 %n) {
entry:
  %incremented = add nsw i32 %n, 1
  %positive = icmp sgt i32 %incremented, 0
  br i1 %positive, label %entry.split, label %entry.split.us

entry.split.us: ; preds = %entry
  br label %header.us

header.us: ; preds = %body.us, %entry.split.us
  br i1 true, label %exit.split.us, label %guard.us

guard.us: ; preds = %header.us
  br i1 false, label %body.us, label %body.us

body.us:                                          ; preds = %guard.us, %guard.us
  br label %header.us

exit.split.us: ; preds = %header.us
  br label %exit

entry.split: ; preds = %entry
  br label %header

header:                                           ; preds = %body, %entry.split
  br i1 true, label %exit.split, label %guard

guard: ; preds = %header
  br i1 true, label %body, label %body

body:                                             ; preds = %guard, %guard
  %condition = and i1 false, true
  br i1 %condition, label %taken, label %header

taken: ; preds = %body
  ret i32 0

exit.split: ; preds = %header
  br label %exit

exit: ; preds = %exit.split.us, %exit.split
  ret i32 0
}
...

$ alive-tv --disable-undef-input --disable-poison-input --src-unroll=2 --tgt-unroll=2 '-passes=loop-mssa(simple-loop-unswitch<nontrivial>)' issue-repro.ll
...
Transformation doesn't verify!

ERROR: Source is more defined than target

NOTE: The counterexample is unique.

Example:
i32 %n = #x7fffffff (2147483647)

Source:
i32 %incremented = poison
i1 %positive = poison
  >> Jump to %header
  >> Jump to %exit

Target:
i32 %incremented = poison
i1 %positive = poison
UB triggered on br
...
Summary:
  1 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors

$ cat issue-repro-control.ll
define i32 @f(i32 %n) {
entry:
  %incremented = add nsw i32 %n, 1
  %positive = icmp sgt i32 %incremented, 0
  br label %header

header:
  br i1 true, label %exit, label %guard

guard:
  br i1 true, label %body, label %body

body:
  %condition = and i1 false, %positive
  br i1 %condition, label %taken, label %header

taken:
  ret i32 0

exit:
  ret i32 0
}

define i32 @main() {
entry:
  %r = call i32 @f(i32 2147483647)
  ret i32 %r
}

$ opt -S '-passes=loop-mssa(simple-loop-unswitch<nontrivial>)' issue-repro-control.ll
...
define i32 @f(i32 %n) {
entry:
  %incremented = add nsw i32 %n, 1
  %positive = icmp sgt i32 %incremented, 0
 %positive.fr = freeze i1 %positive
  br i1 %positive.fr, label %entry.split, label %entry.split.us

entry.split.us:                                   ; preds = %entry
  br label %header.us

header.us: ; preds = %body.us, %entry.split.us
  br i1 true, label %exit.split.us, label %guard.us

guard.us: ; preds = %header.us
  br i1 true, label %body.us, label %body.us

body.us:                                          ; preds = %guard.us, %guard.us
  br label %header.us

exit.split.us: ; preds = %header.us
  br label %exit

entry.split: ; preds = %entry
  br label %header

header:                                           ; preds = %body, %entry.split
  br i1 true, label %exit.split, label %guard

guard: ; preds = %header
  br i1 true, label %body, label %body

body:                                             ; preds = %guard, %guard
  %condition = and i1 false, true
  br i1 %condition, label %taken, label %header

taken: ; preds = %body
  ret i32 0

exit.split: ; preds = %header
  br label %exit

exit: ; preds = %exit.split.us, %exit.split
  ret i32 0
}
...

$ alive-tv --disable-undef-input --disable-poison-input --src-unroll=2 --tgt-unroll=2 '-passes=loop-mssa(simple-loop-unswitch<nontrivial>)' issue-repro-control.ll
...
Summary:
  2 correct transformations
  0 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
```

### suggested fix
In `unswitchNontrivialInvariants`, compute per-leaf `isGuaranteedNotToBeUndefOrPoison` results at the preheader terminator before splitting/cloning changes the CFG.
Pass those decisions to `buildPartialUnswitchConditionalBranch` instead of querying at the in-loop `BI`.
The branch in `%guard` proves `%positive` non-poison only inside the loop, so it cannot justify omitting the preheader freeze.

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

Reply via email to