Issue 234423
Summary [InstCombine] Miscompilation where shrinking a double copysign to float can give a NaN the wrong sign
Labels new issue
Assignees
Reporter sairam2661
    **Fuzzer Generated Test**
**Reproducer**
https://alive2.llvm.org/ce/z/unaVE_

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

**Description**
`optimizeDoubleFP` shrinks `copysign(fpext %x, fpext %y)` to `fpext(copysign(%x, %y))`, but `fpext` of a NaN may change the sign: for a NaN `%x` and `%y = +0.0`, the target may return a negative NaN.

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

```llvm
define double @f(float nofpclass(inf zero sub norm) %x, float %y) {
  %x.ext = fpext float %x to double
  %y.ext = fpext float %y to double
  %r = call double @copysign(double %x.ext, double %y.ext)
  ret double %r
}

declare double @copysign(double, double)
```

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

```llvm
define double @f(float nofpclass(inf zero sub norm) %x, float %y) {
  %copysignf = call float @llvm.copysign.f32(float %x, float %y)
  %r = fpext float %copysignf to double
  ret double %r
}

declare double @copysign(double, double)

declare float @copysignf(float, float) #0

declare float @llvm.copysign.f32(float, float)

attributes #0 = { nocallback nofree nounwind willreturn memory(none) }
```

**Output**

```
declare double @copysign(double, double)

define double @f(float nofpclass(1020) %x, float %y) {
#0:
  %x.ext = fpext float nofpclass(1020) %x to double
  %y.ext = fpext float %y to double
  %r = copysign double %x.ext, %y.ext
  ret double %r
}
=>
define double @f(float nofpclass(1020) %x, float %y) {
#0:
 %copysignf = copysign float nofpclass(1020) %x, %y
  %r = fpext float %copysignf to double
  ret double %r
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
float nofpclass(1020) %x = #x7f800082 (SNaN)
float %y = #x00000000 (+0.0)

Source:
double %x.ext = #xfff8000000000000 (QNaN)
double %y.ext = #x0000000000000000 (+0.0)
double %r = #x7ff8000000000000 (QNaN)

Target:
float %copysignf = #x7f800082 (SNaN)
double %r = #xfff8001040000000 (QNaN)
Source value: #x7ff8000000000000 (QNaN)
Target value: #xfff8001040000000 (QNaN)

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