[InstCombine] Miscompilation where replacing one use of a nsz value lets its uses disagree on the sign of zero
Fuzzer Generated Test Reproducer https://alive2.llvm.org/ce/z/CsKsum
Test 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
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
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
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
Another reproducer for this, still failing on 9dce18f0e. It uses llvm.maximum/llvm.minimum with a constant
zero operand. There is also a generic-nsz variant (fneg) at the end. I'd also like to ask the semantics question
this depends on.
define nofpclass(snan) float @maximum_nsz_multiuse(float nofpclass(nan pinf psub pnorm) %x, ptr %p) {
%r = call nsz float @llvm.maximum.f32(float 0.0, float %x)
store float %r, ptr %p
ret float %r
}
$ opt -passes=instcombine -S test.ll
%r = call nsz float @llvm.maximum.f32(float %x, float 0.000000e+00)
store float %r, ptr %p, align 4
ret float 0.000000e+00
(llvm.minimum with 0.0 becomes ret float %x while the store keeps %r.)
Question: is the nsz choice made once per execution of the instruction (all uses see the same zero), or can it
differ per use? LangRef for min/max says "if the two inputs are zeros with opposite sign, either input may be
returned". Read as a single choice per execution, %x = -0.0 gives %r = +0.0 or -0.0, and the store and the
return in the source always agree. The target can store -0.0 (if the call returns %x, as x86 maxss
does for equal inputs) and return +0.0, which the source cannot do. The NSZ=false argument in the multiple-use
path ("Cannot use NSZ in the multiple use case") implies per-execution semantics, but the instruction keeps its
own nsz, so its remaining uses can still differ from the non-nsz answer.
Caveat: I argue this from LangRef rather than from an alive2 witness. alive2's min/max nsz model is looser than
LangRef (it also lets maximum nsz(+0.0, +0.0) return -0.0), so its "Mismatch in memory" counterexamples print the
same bytes for source and target. It also flags a harmless control, minimum nsz(1.0, x<=0) -> x, where
opposite-sign zero inputs cannot occur.
Generic nsz variant (fneg(fneg X) -> X while the inner fneg nsz keeps another use). This one is weaker,
because generic nsz is only "the sign of a zero result may be treated as insignificant". For %x = +0.0, the
source's lanes 0 and 2 always have opposite signs, but the target can make them both +0.0:
define <4 x float> @fneg_shuf(<4 x float> %x) {
%nx = fneg ninf nsz <4 x float> %x
%ny = fneg nnan ninf <4 x float> %nx
%r = shufflevector <4 x float> %nx, <4 x float> %ny, <4 x i32> <i32 0, i32 1, i32 4, i32 5>
ret <4 x float> %r
}
; instcombine: %r = shufflevector <4 x float> %nx, <4 x float> %x, ...
Found with a mutation-based IR fuzzer. Triage, test-case minimization and parts of this write-up were assisted by an LLM; verified the reproducer on 9dce18f0e.
Assisted-by: Claude (Anthropic)