Issue 233696
Summary [InstCombine] Bitcast-of-shuffle fold shuffles the already shifted vector again for the next bitcast user
Labels miscompilation, llvm:instcombine, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/6sfq4z5q4
alive: https://alive2.llvm.org/ce/z/J9FSxE

```llvm
declare void @use_i32(i32)
declare void @use_float(float)

define void @f(<4 x i16> %x) {
  %s = shufflevector <4 x i16> %x, <4 x i16> poison, <2 x i32> <i32 1, i32 2>
  %b1 = bitcast <2 x i16> %s to i32
  call void @use_i32(i32 %b1)
  %b2 = bitcast <2 x i16> %s to float
  call void @use_float(float %b2)
  ret void
}
```

```console
$ opt -S -passes=instcombine repro.ll
define void @f(<4 x i16> %x) {
  %s.extract = shufflevector <4 x i16> %x, <4 x i16> poison, <4 x i32> <i32 1, i32 2, i32 poison, i32 poison>
  %s.bc = bitcast <4 x i16> %s.extract to <2 x float>
  %s.extract1 = extractelement <2 x float> %s.bc, i64 0
  %s.extract2 = shufflevector <4 x i16> %x, <4 x i16> poison, <4 x i32> <i32 2, i32 poison, i32 poison, i32 poison>
  %s.bc3 = bitcast <4 x i16> %s.extract2 to <2 x i32>
  %s.extract4 = extractelement <2 x i32> %s.bc3, i64 0
  call void @use_i32(i32 %s.extract4)
  call void @use_float(float %s.extract1)
  ret void
}

$ alive-tv --disable-undef-input --disable-poison-input --passes=instcombine repro.ll
ERROR: Source is more defined than target
Example:
<4 x i16> %x = < #x0003 (3), #x0003 (3), #x0003 (3), #x0003 (3) >
Source:
i32 %b1 = #x00030003 (196611)
...
Target:
<4 x i16> %s.extract2 = < #x0003 (3), poison, poison, poison >
i32 %s.extract4 = poison
```

### suggested fix
In `InstCombinerImpl::visitShuffleVectorInst` (InstCombineVectorOps.cpp), the vector/array type-punning fold sets `Value *V = LHS;` before the loop over the bitcast users and overwrites `V` with the lane-shifting shuffle when `BegIdx` is not aligned.
Every later unaligned user then shuffles the already shifted vector by `BegIdx` again (`<x[2], poison, ...>` instead of `<x[1], x[2], ...>`). Use a per-iteration source (`Value *Src = "" inside the loop).

> Disclaimer: This test case was generated by a fuzzer, and the report was written with LLM assistance.

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

Reply via email to