Issue 241254
Summary [X86] AVX vselect(y != 0, 0/y, x) becomes its true arm when x is a forwarded zero (miscompile at -O3)
Labels new issue
Assignees
Reporter jeffchen006
    With `-mavx`, `select(y != 0, 0.0 / y, x)` with `x == 0` (from memset forwarding in the DAG) is replaced by its true arm `fdiv 0, 0` = NaN. The condition is not used. The -O3 IR is correct; the error is in the X86 SelectionDAG.

**Reproducer** (`repro.c`)
```c
/* All b[i] and e[i] stay 0: b[i] is never 4, so b[i] = b[i] + 0 = 0, and
   e[i] = 0 because b[i] is 0 (the division is never evaluated). The last
   loop never sets a, so the program must print 0. Clang -O3 -mavx prints 1. */
int printf(const char *, ...);
int a;
double b[16];
double c;
int main(void) {
  double d = c;             /* a local copy is needed */
  double e[16];
  for (int i = 0; i < 16; i++)
    b[i] = e[i] = 0;
  for (int i = 0; i < 16; i++) {
    b[i] = b[i] == 4 ? d : (e[i] ? b[i] / e[i] : b[i]) + e[i];
    e[i] = b[i] ? 0 / (double)i : 0;
  }
  for (int i = 0; i < 6; i++)
    if (e[i]) {
      a = 1;
      i = 1;
    }
  printf("%d\n", a);
  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 -mavx repro.c -o o0
$ ./o0
0
$ clang -O3 -mavx repro.c -o o3
$ ./o3
1
```
Expected (as `-O0`): `0`. `-O3` prints `1`.

**Why the expected output is right**
Zero-initialized global `c`; local `g` fully written before reads; `0/(double)f` is evaluated only if `c[f] != 0` (never); `c[f] / g[f]` only if `g[f] != 0` (never). UBSan/ASan/MSan at -O0 are clean. The difference goes away with `-fno-builtin` only because the memset that LoopIdiom makes is needed for the trigger.

**Potential Root cause**
Not fully pinned (no assertions build). The `-O3` IR is correct. With `-debug-counter=dagcombine=0-92` the result is right, with `0-93` it is wrong: at combine #93 the value stored to `%g` becomes the constant `fdiv 0, 0`, so the vselect is replaced by its true arm. Needed: the true arm is a positive-zero-over-y NaN (`fdiv -0.0, y` is right); x is zero only through DAG memset forwarding; AVX/AVX2 without AVX-512 (VBLENDV / `vandpd` lowering of `vselect(M, X, 0)`). Most likely an X86 vselect combine (`combineSelect` / `combineVSelectWithAllOnesOrZeros`) that reuses a BLENDV mask (only the sign bit demanded) as a full AND mask.

**Possible Proposed fix**
No reliable diff without the exact combine. Next step: `llc -O2 -debug-_only_=dagcombine -debug-counter=dagcombine=0-93` on the IR below with an assertions build, and read the "Replacing ... With" line of combine #93. If it is BLENDV-mask reuse: when a vselect whose condition was simplified for BLENDV is later turned into AND/ANDN, sign-splat the condition first or re-create it from the setcc.

**Reduced IR**
```llvm
target triple = "x86_64-unknown-linux-gnu"
@c = global [8 x double] zeroinitializer
define double @f() #0 {
  %g = alloca [4 x double], align 32
  call void @llvm.memset.p0.i64(ptr align 32 %g, i8 0, i64 32, i1 false)
  call void @llvm.memset.p0.i64(ptr align 32 @c, i8 0, i64 64, i1 false)
  %x = load <4 x double>, ptr @c, align 32
  store <4 x double> zeroinitializer, ptr @c, align 32
  %y = load <4 x double>, ptr %g, align 32
  %ne = fcmp une <4 x double> %y, zeroinitializer
  %q = fdiv <4 x double> zeroinitializer, %y
  %s = select <4 x i1> %ne, <4 x double> %q, <4 x double> %x
  store <4 x double> %s, ptr %g, align 32
  %z = load <4 x double>, ptr %g, align 32
  %ne2 = fcmp une <4 x double> %z, zeroinitializer
  %s2 = select <4 x i1> %ne2, <4 x double> %z, <4 x double> zeroinitializer
  store <4 x double> %s2, ptr getelementptr (i8, ptr @c, i64 32), align 32
  %r = load double, ptr %g, align 32
  ret double %r
}
declare void @llvm.memset.p0.i64(ptr, i8, i64, i1)
attributes #0 = { "target-features"="+avx" }
```
`llc -O2` on this IR (+avx) returns 1 from `@f` (expected 0); with SSE4.2 or AVX-512F it returns 0.

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

Reply via email to