| Issue |
235318
|
| Summary |
[SimplifyCFG] Switch lookup table is one entry short for a full-range index, turning the default result into poison
|
| Labels |
miscompilation,
llvm:transforms,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/171aWraYY
alive: https://alive2.llvm.org/ce/z/nu9MVy
```llvm
target datalayout = "e-n8:16:32:64"
define i4 @f(i4 %x) {
entry:
switch i4 %x, label %default [
i4 5, label %end
i4 6, label %end
i4 7, label %end
]
end:
%r = phi i4 [ 9, %default ], [ 0, %entry ], [ 0, %entry ], [ 0, %entry ]
ret i4 %r
default:
br label %end
}
define i32 @main() {
%r = call i4 @f(i4 -1)
%z = zext i4 %r to i32
ret i32 %z
}
```
```console
$ opt -passes=verify -disable-output repro.ll
$ opt -S -passes='simplifycfg<switch-to-lookup>' repro.ll -o out.ll
$ sed -n '/^define i4 @f/,/^}/p' out.ll
define i4 @f(i4 %x) {
entry:
%switch.cast = zext i4 %x to i60
%switch.shiftamt = mul nuw nsw i60 %switch.cast, 4
%switch.downshift = lshr i60 -461168604419090023, %switch.shiftamt
%switch.masked = trunc i60 %switch.downshift to i4
ret i4 %switch.masked
}
$ llubi repro.ll; echo $?
9
$ llubi out.ll; echo $?
error: Execution of function 'main' resulted in poison return value.
1
$ alive-tv --disable-undef-input --disable-poison-input repro.ll out.ll
...
Transformation doesn't verify!
ERROR: Target is more poisonous than source
NOTE: The counterexample is unique.
Example:
i4 %x = #xf (15, -1)
...
i60 %switch.shiftamt = #x00000000000003c (60)
i60 %switch.downshift = poison
...
Source value: #x9 (9, -7)
Target value: poison
```
### suggested fix
In `simplifySwitchLookup` (`llvm/lib/Transforms/Utils/SimplifyCFG.cpp`), the table-growth block uses `CR.getUpper()` as a size even for a full set, then clears `DefaultIsReachable`.
For a full `i4` range that bound is 15, so index 15 shifts the 60-bit bitmap by 60 instead of returning the default value 9.
Handle `CR.isFullSet()` explicitly: retain the range check, or grow to all `2^W` entries using a non-wrapping size calculation and check `wouldFitInRegister` against that size.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs