Issue 234702
Summary [InstCombine] Miscompilation where replacing one use of a nsz value lets its uses disagree on the sign of zero
Labels new issue
Assignees
Reporter sairam2661
    **Fuzzer Generated Test**
**Reproducer**
https://alive2.llvm.org/ce/z/CsKsum

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

**Description**
Since `maxnum` of a known-positive and a known-negative value is positive one, InstCombine (`SimplifyMultipleUseDemandedFPClass`) replaces the `ret` use of `%m` with `%p` while the `store` keeps `%m`, but with `nsz` `maxnum(+0.0, -0.0)` may return -0.0.

As a result, the target can store -0.0 and return +0.0, which the source cannot.

The multiple use path does the same for `fneg nsz (fabs x)` and `copysign nsz`, because passing `NSZ=false` there only stops it from using `nsz`.

So, it does not ignore the fact that the instruction's own result is nondeterministic...?

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

```llvm
define nofpclass(snan) float @f(float nofpclass(nan ninf nnorm nsub nzero) %p, float nofpclass(nan pinf pnorm psub pzero) %n, ptr %ptr) {
  %m = call nsz float @llvm.maxnum.f32(float %p, float %n)
  store float %m, ptr %ptr
  ret float %m
}
```

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

```llvm
define nofpclass(snan) float @f(float nofpclass(nan ninf nzero nsub nnorm) %p, float nofpclass(nan pinf pzero psub pnorm) %n, ptr %ptr) {
  %m = call nsz float @llvm.maxnum.f32(float %p, float %n)
  store float %m, ptr %ptr, align 4
 ret float %p
}

declare float @llvm.maxnum.f32(float, float)
```

**Output**

```txt
define float @f(float nofpclass(63) %p, float nofpclass(963) %n, ptr %ptr) nofpclass(1) {
#0:
  %m = fmax nsz float nofpclass(63) %p, nofpclass(963) %n
  store float %m, ptr %ptr, align 4
 ret float %m
}
=>
define float @f(float nofpclass(63) %p, float nofpclass(963) %n, ptr %ptr) nofpclass(1) {
#0:
  %m = fmax nsz float nofpclass(63) %p, nofpclass(963) %n
  store float %m, ptr %ptr, align 4
 ret float nofpclass(63) %p
}
Transformation doesn't verify!

ERROR: Mismatch in memory

Example:
float nofpclass(63) %p = #x00000000 (+0.0)
float nofpclass(963) %n = #x84200000 (-1.880791e-36)
ptr %ptr = pointer(non-local, block_id=1, offset=0) / Address=#x04

Source:
float %m = #x80000000 (-0.0)

SOURCE MEMORY STATE
===================
NON-LOCAL BLOCKS:
Block 0 >	size: 0	align: 4	alloc type: 0	alive: false	address: #x00
Block 1 >	size: 9	align: 1	alloc type: 0	alive: true	address: #x04

Target:
float %m = #x80000000 (-0.0)

TARGET MEMORY STATE
===================
NON-LOCAL BLOCKS:
Block 0 >	size: 0	align: 4	alloc type: 0	alive: false	address: #x00
Block 1 >	size: 9	align: 1	alloc type: 0	alive: true	address: #x04

Mismatch in pointer(non-local, block_id=0, offset=2)
Source value: poison
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