| Issue |
234330
|
| Summary |
[InstCombine] extractelement of a bitcast insertelement reads the wrong bits when the lane counts are not multiples
|
| Labels |
miscompilation,
llvm:instcombine,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/zfEfKq7Yq
alive: https://alive2.llvm.org/ce/z/r_TAkD
```llvm
; <3 x i16> lane 1 is bits 16..31: bits 16..23 of %v[0] and bits 0..7 of %s
define i16 @f(<2 x i24> %v, i24 %s) {
%ins = insertelement <2 x i24> %v, i24 %s, i64 1
%bc = bitcast <2 x i24> %ins to <3 x i16>
%e = extractelement <3 x i16> %bc, i64 1
ret i16 %e
}
; <3 x i16> lane 2 is bits 32..47: bits 8..23 of %s
define i16 @g(<2 x i24> %v, i24 %s) {
%ins = insertelement <2 x i24> %v, i24 %s, i64 1
%bc = bitcast <2 x i24> %ins to <3 x i16>
%e = extractelement <3 x i16> %bc, i64 2
ret i16 %e
}
; control: the lane counts divide, folded correctly
define i16 @ok(<2 x i32> %v, i32 %s) {
%ins = insertelement <2 x i32> %v, i32 %s, i64 1
%bc = bitcast <2 x i32> %ins to <4 x i16>
%e = extractelement <4 x i16> %bc, i64 2
ret i16 %e
}
```
```console
$ opt -passes=instcombine -S repro.ll
define i16 @f(<2 x i24> %v, i24 %s) {
%e = trunc i24 %s to i16
ret i16 %e
}
define i16 @g(<2 x i24> %v, i24 %s) {
%1 = bitcast <2 x i24> %v to <3 x i16>
%e = extractelement <3 x i16> %1, i64 2
ret i16 %e
}
define i16 @ok(<2 x i32> %v, i32 %s) {
%e = trunc i32 %s to i16
ret i16 %e
}
# alive link (--disable-undef-input --disable-poison-input -passes=instcombine), excerpt
@f: ERROR: Value mismatch
<2 x i24> %v = < #x000000 (0), #x000003 (3) >
i24 %s = #x000001 (1)
Source value: #x0100 (256)
Target value: #x0001 (1)
@g: ERROR: Value mismatch
<2 x i24> %v = < #x000003 (3), #xffff00 (16776960, -256) >
i24 %s = #x000000 (0)
Source value: #x0000 (0)
Target value: #xffff (65535, -1)
@ok: Transformation seems to be correct!
```
### suggested fix
In `foldBitcastExtElt`, the `NumSrcElts < NumElts` path computes `NarrowingRatio = NumElts / NumSrcElts` (3 / 2 = 1 here) and assumes each destination lane lies inside one source lane; both the trunc branch (`@f`) and the drop-the-insert branch (`@g`) rely on that.
Bail out of that path unless `NumElts` is a multiple of `NumSrcElts`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs