Issue 241273
Summary [X86] vXi8 shift via wide shift without freeze; poison byte poisons its neighbour (miscompile at -O3)
Labels new issue
Assignees
Reporter jeffchen006
    A `v16i8` shift is lowered as a `v8i16` shift plus an AND mask. A poison byte in the source makes the whole 16-bit word poison, so the defined neighbour byte becomes poison too. Later known-bits folds remove the AND, and `ctpop` gives a wrong value.

**Reproducer** (`repro.c`)
```c
/* Correct output: 4.  q = {0, 0, 29, 42, 29} << {1, 7, 6, 7, 0}; lane 4 is 29 << 0 = 29,
   and popcount(29 = 0b11101) = 4.  clang -O3 -march=x86-64-v4 prints 3 (x86 vXi8 shift
   lowering lets the poison padding lane 5 spoil lane 4 inside the popcount). */
int printf(const char *, ...);
typedef unsigned char c5 __attribute__((ext_vector_type(5)));
c5 g = {0, 0, 29, 42, 0};
int main(void) {
  c5 q = g.s01232 << (c5){1, 7, 6, 7, 0};
  c5 p = __builtin_elementwise_popcount(q);
  printf("%d\n", p[4]);
  return 0;
}
```

**Commands and output** (clang version 24.0.0git (https://github.com/llvm/llvm-project.git 8207e9143575), Target: x86_64-unknown-linux-gnu):
```console
$ clang -O0 -march=x86-64-v4 repro.c -o ./o0
$ ./o0
4
$ clang -O3 -march=x86-64-v4 repro.c -o ./o3
$ ./o3
3
```
Expected (as `-O0`): `4`. `-O3` prints `3`.

**Why the expected output is right**
`unsigned char` lanes; shift amounts 1, 7, 6, 7, 0 are all < 8; lane 4 = `29 << 0` = 29 and `popcount(29) = 4`. The padding lanes exist only after the back end widens `<5 x i8>` to `v16i8`. UBSan/ASan/MSan at -O0 are clean.

**Potential Root cause**
`llvm/lib/Target/X86/X86ISelLowering.cpp`, `LowerShiftByScalarImmediate` ("Make a large shift"): `VSRLI v8i16 (bitcast R), 4` then `AND 0x0F` per byte (here from `LowerVectorCTPOPInRegLUT`). In DAG semantics the v8i16 word with a poison byte is fully poison, so byte 4 becomes poison. `computeKnownBits` of BUILD_VECTOR skips POISON lanes, so bit 7 of byte 4 is "known zero" and `SimplifyDemandedBits` removes the `and 15` (dagcombine #17). The SRA path of the same function already freezes `R`.

**Possible Proposed fix**
```diff
     if (Op.getOpcode() == ISD::SHL) {
+      R = DAG.getFreeze(R);   // the wide shift mixes neighbour bytes
       SDValue SHL = getTargetVShiftByConstNode(X86ISD::VSHLI, dl, ShiftVT, R, ShiftAmt, DAG);
 ...
     if (Op.getOpcode() == ISD::SRL) {
+ R = DAG.getFreeze(R);
       SDValue SRL = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, ShiftVT, R, ShiftAmt, DAG);
```
(only when `!DAG.isGuaranteedNotToBePoison(R)`.)

**Reduced IR**
```llvm
define i8 @f(<16 x i8> %x) "target-cpu"="x86-64-v4" {
 %s = shl <16 x i8> %x, <i8 1, i8 7, i8 6, i8 7, i8 0, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison, i8 poison>
  %c = call <16 x i8> @llvm.ctpop.v16i8(<16 x i8> %s)
  %e = extractelement <16 x i8> %c, i64 4
  ret i8 %e
}
```
Lane 4 of `ctpop(shl x, ...)` with x lane 4 = 29 must be 4; `llc` (x86-64-v4) gives 3 when lane 5 of x has bit 3 set.

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

Reply via email to