| Issue |
241268
|
| Summary |
[SelectionDAG] ExpandVSELECT does not freeze the arms; poison of the unchosen arm leaks (miscompile at -O3)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
jeffchen006
|
Without SSE4.1, a vector select is expanded to `or(and(A, M), and(B, ~M))`. The unchosen arm `A` has poison lanes (`sub nuw`). `and(poison, 0)` is poison in the DAG, so later known-bits folds remove the mask and the lane gets a wrong value.
**Reproducer** (`repro.c`)
```c
/* h is a one-element vector {0}, so o = all ones >> 0 = all ones and the
divisor is o % 1004879015 & 7 | 1 = 3; n() = {0, 1, 0, ...} % 3 = {0, 1, 0, ...}.
show() prints the running XOR of all bytes seen: 1 after the first call and
0 after the second. Correct output: "a 1", "b 0". Clang -O3 (SSE2) prints
"a ffffffff": lane 1 of n() is -1, not 1. */
int printf(const char *, ...);
typedef unsigned v8u __attribute__((vector_size(32)));
typedef int v8i __attribute__((vector_size(32)));
typedef char v8c __attribute__((vector_size(8)));
typedef _Bool v8b __attribute__((ext_vector_type(8)));
__attribute__((vector_size(1))) char h;
long long x;
void show(const char *s, void *p, int size) {
char *b = p;
for (int i = 0; i < size; i++)
x = x ^ b[i];
printf("%s %llx\n", s, x);
}
v8i n(void) {
v8u o = ~(v8u){} >> (v8u){5094310} % (__builtin_reduce_add(h) | 1);
return __builtin_convertvector(__builtin_convertvector((v8c){0, 5}, v8b), v8u) %
(o % 1004879015 & 7 | 1);
}
int main(void) {
int l[8];
v8i p = n();
for (int i = 0; i < 8; i++)
l[i] = p[i];
show("a", l, sizeof l);
p = n();
for (int i = 0; i < 8; i++)
l[i] = p[i];
show("b", l, sizeof l);
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 -w repro.c -o o0
$ ./o0
a 1
b 0
$ clang -O3 -w repro.c -o o3
$ ./o3
a ffffffff
b 0
```
Expected (as `-O0`): `a 1` / `b 0`. `-O3` prints `a ffffffff` / `b 0`.
**Why the expected output is right**
Shift count `5094310 % 1 = 0`; all `%` divisors are `|1`; a reduce-add of a one-element zero vector; comma operators only drop values. The IR is correct (the select does not choose the poison lanes). UBSan/ASan/MSan at -O0 are clean.
**Potential Root cause**
`llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp`, `VectorLegalizer::ExpandVSELECT`: SSE2 has no blend (`X86TargetLowering::LowerVSELECT` returns `SDValue()` without SSE4.1), so the select becomes `or(and(%8, M), and(splat 1, not M))` with `%8 = sub nuw ...` poison in unchosen lanes. Skipping only dagcombine #92 or #104 gives the right result: #92 makes `sub(and(%8,M), not M)`, then a known-bits fold uses `nuw` to drop `and(%8, M)`.
**Possible Proposed fix**
```diff
+ if (!DAG.isGuaranteedNotToBePoison(Op1)) Op1 = DAG.getFreeze(Op1);
+ if (!DAG.isGuaranteedNotToBePoison(Op2)) Op2 = DAG.getFreeze(Op2);
SDValue NotMask = DAG.getNOT(DL, Mask, VT);
```
(the same for other places that lower a vselect to AND/OR.)
**Reduced IR**
```llvm
target triple = "x86_64-unknown-linux-gnu"
define i32 @f(i8 %a, i8 %b, ptr %out) {
%3 = or i8 %a, 1
%4 = sext i8 %3 to i32
%5 = insertelement <8 x i32> zeroinitializer, i32 %4, i64 0
%6 = icmp eq <8 x i32> %5, splat (i32 1)
%7 = shufflevector <8 x i32> %5, <8 x i32> zeroinitializer, <8 x i32> zeroinitializer
%8 = sub nuw <8 x i32> <i32 0, i32 0, i32 -1, i32 0, i32 -1, i32 0, i32 0, i32 0>, %7
%9 = select <8 x i1> %6, <8 x i32> %8, <8 x i32> splat (i32 1)
%10 = extractelement <8 x i32> %9, i64 1
store i32 %10, ptr %out
%15 = sext i8 %b to i32
%16 = insertelement <8 x i32> zeroinitializer, i32 %15, i64 0
%17 = icmp eq <8 x i32> %16, zeroinitializer
%18 = sub <8 x i32> <i32 0, i32 0, i32 -1, i32 0, i32 -1, i32 0, i32 0, i32 0>, %16
%19 = select <8 x i1> %17, <8 x i32> %18, <8 x i32> zeroinitializer
%20 = extractelement <8 x i32> %19, i64 0
ret i32 %20
}
```
`llc -O2` on this IR stores 0 for `f(1, ...)` lane 1; the right value is 1 (right with `-mattr=+sse4.1`).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs