Issue 234044
Summary [VectorCombine] foldSelectsFromBitcast makes a guarded out-of-range extractelement unconditional
Labels miscompilation, generated by fuzzer, llvm:vectorcombine
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/ocPE7nMcP
alive: https://alive2.llvm.org/ce/z/PvRqCa

```llvm
define i8 @f(<4 x i32> %v, i64 %i) {
  %c = icmp ult i64 %i, 16
  %bc = bitcast <4 x i32> %v to <16 x i8>
  %e0 = extractelement <16 x i8> %bc, i64 0
  %s0 = select i1 %c, i8 %e0, i8 0
  %e1 = extractelement <16 x i8> %bc, i64 %i
  %s1 = select i1 %c, i8 %e1, i8 0
  %r = add i8 %s0, %s1
  ret i8 %r
}
```

```console
$ opt -S -passes=vector-combine repro.ll
define i8 @f(<4 x i32> %v, i64 %i) {
  %c = icmp ult i64 %i, 16
  %1 = select i1 %c, <4 x i32> %v, <4 x i32> zeroinitializer
  %2 = bitcast <4 x i32> %1 to <16 x i8>
  %s0 = extractelement <16 x i8> %2, i64 0
  %s1 = extractelement <16 x i8> %2, i64 %i
  %r = add i8 %s0, %s1
  ret i8 %r
}

# alive link: @src = "" @tgt = output above
$ alive-tv --disable-undef-input --disable-poison-input src-tgt.ll
ERROR: Target is more poisonous than source
i64 %i = #x0000000000000010 (16)
Source value: #x00 (0)
Target value: poison
```

### suggested fix
`VectorCombine::foldSelectsFromBitcast` replaces `select %cond, (extractelement %bc, %idx), 0` with an `extractelement` of the selected vector that is no longer guarded by `%cond`, so an index that is out of range (or poison) only when `%cond` is false turns 0 into poison.
Only collect extracts whose index is a constant below `DstVecTy->getNumElements()`, or freeze and clamp a variable index.

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

Reply via email to