Issue 234688
Summary [InstCombine] Miscompilation while dividing a float by itself under no-infinities
Labels new issue
Assignees
Reporter sairam2661
    **Fuzzer Generated Test**
**Reproducer**
https://alive2.llvm.org/ce/z/JhBUjG

**Test Commit**
[e633c7da8ff144a11b3d875956fbaea06b969070](https://github.com/llvm/llvm-project/commit/e633c7da8ff144a11b3d875956fbaea06b969070)

**Description**
Since `x / x` is NaN when `x` is zero, infinite or NaN and 1.0 otherwise, InstCombine (`SimplifyDemandedUseFPClass`) expands it into `select (x == 0 || fabs(x) == +inf), nan, 1.0`. However, it copies the `fdiv`'s `ninf` onto the new `fcmp ueq fabs(x), +inf`, whose `+inf` operand makes it poison for every `x`.

**Steps to reproduce**
- Minimized test case (src), `input.ll`

```llvm
define nofpclass(snan) half @f(half noundef %x) #0 {
 %d = fdiv ninf half %x, %x
  ret half %d
}

attributes #0 = { denormal_fpenv(ieee|preservesign) }
```

- InstCombine output (tgt), `output.ll`

```llvm
define nofpclass(snan) half @f(half noundef %x) #0 {
  %1 = fcmp ninf ueq half %x, 0.000000e+00
  %2 = call ninf half @llvm.fabs.f16(half %x)
  %3 = fcmp ninf ueq half %2, +inf
  %4 = or i1 %3, %1
  %5 = select ninf i1 %4, half +qnan, half 1.000000e+00
  ret half %5
}

declare half @llvm.fabs.f16(half)

attributes #0 = { denormal_fpenv(ieee|preservesign) }
```

**Output**

```txt
define half @f(half noundef %x) nofpclass(1) denormal-fp-math=ieee,preserve-sign {
#0:
 %d = fdiv ninf half noundef %x, noundef %x
  ret half %d
}
=>
define half @f(half noundef %x) nofpclass(1) denormal-fp-math=ieee,preserve-sign {
#0:
  %#1 = fcmp ninf ueq half noundef %x, 0x0000
  %#2 = fabs ninf half noundef %x
  %#3 = fcmp ninf ueq half %#2, 0x7c00
  %#4 = or i1 %#3, %#1
 %#5 = select ninf i1 %#4, half 0x7e00, half 0x3c00
  ret half %#5
}
Transformation doesn't verify!

ERROR: Target is more poisonous than source

Example:
half noundef %x = #x0000 (+0.0)

Source:
half %d = #xfe03 (QNaN)

Target:
i1 %#1 = #x1 (1)
half %#2 = #x0000 (+0.0)
i1 %#3 = poison
i1 %#4 = poison
half %#5 = poison
Source value: #xfe03 (QNaN)
Target value: poison

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
```

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

Reply via email to