llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Known not-NaN or not-inf should get the same optimizations as `nnan`/`ninf` (missed `minimumnum` optimizations)

Open tgross35 opened this issue 3 months ago • 2 comments

Code:

define noundef double @minimumnum_nsz_assume_nann(double noundef %x, double noundef %y) unnamed_addr {
start:
  %_4 = fcmp ord double %x, 0.0
  %_6 = fcmp ord double %y, 0.0
  tail call void @llvm.assume(i1 %_4)
  tail call void @llvm.assume(i1 %_6)
  %0 = tail call nsz double @llvm.minimumnum.f64(double %x, double %y)
  ret double %0
}

define noundef double @minimumnum_nsz_nann(double noundef %x, double noundef %y) unnamed_addr {
start:
  %0 = tail call nsz nnan double @llvm.minimumnum.f64(double %x, double %y)
  ret double %0
}

I would expect the two functions to get the same output because minimumnum in minimumnum_nsz_nann knows that x and y are YaN (via nnan) and in minimumnum_nsz_assume_nann it's known via the assume. However, on aarch64, they get different outputs:

minimumnum_nsz_assume_nann:
        fminnm  d1, d1, d1
        fminnm  d0, d0, d0
        fminnm  d0, d0, d1
        ret

minimumnum_nsz_nann:
        fminnm  d0, d0, d1
        ret

I noticed this missed optimization on aarch64 but expect that a more general optimization should help a number of targets and functions.

Link: https://llvm.godbolt.org/z/aeWWKW4ro

tgross35 avatar Aug 07 '26 07:08 tgross35

Found an example that affects x86 as well. This time x and y are known to be YaN because we return a default value otherwise:

; return 0 if either is NaN, minimumnum otherwise
define noundef double @minimumnum_nsz_diverges(double noundef %x, double noundef %y) unnamed_addr {
start:
  %either_nan = fcmp uno double %x, %y
  %0 = tail call nsz double @llvm.minimumnum.f64(double %x, double %y)
  %ret = select i1 %either_nan, double 0.0, double %0
  ret double %ret
}

; return 0 if either is NaN, minimumnum otherwise
define noundef double @minimumnum_nsz_diverges_nnan(double noundef %x, double noundef %y) unnamed_addr {
start:
  %either_nan = fcmp uno double %x, %y
  %0 = tail call nsz nnan double @llvm.minimumnum.f64(double %x, double %y)
  %ret = select i1 %either_nan, double 0.0, double %0
  ret double %ret
}
minimumnum_nsz_diverges:
        movapd  xmm2, xmm1
        cmpunordsd      xmm2, xmm1
        movapd  xmm3, xmm2
        andpd   xmm3, xmm0
        movapd  xmm4, xmm0
        minsd   xmm4, xmm1
        andnpd  xmm2, xmm4
        orpd    xmm2, xmm3
        cmpunordsd      xmm0, xmm1
        andnpd  xmm0, xmm2
        ret

minimumnum_nsz_diverges_nnan:
        movapd  xmm2, xmm0
        minsd   xmm2, xmm1
        cmpunordsd      xmm0, xmm1
        andnpd  xmm0, xmm2
        ret

Another sample input using a bitwise check for NaN (in this case, infinity as well). This kind of pattern would be great to detect because it's used all the time in float libraries.

define noundef double @minimumnum_nsz_diverges_soft(double noundef %x, double noundef %y) unnamed_addr {
start:
  %xi = bitcast double %x to i64
  %yi = bitcast double %y to i64
  %ex = and i64 %xi, 9218868437227405312 ; 0x7ffu64 << 52
  %ey = and i64 %yi, 9218868437227405312 ; 0x7ffu64 << 52
  %x_nan_inf = icmp eq i64 %ex, 9218868437227405312
  %y_nan_inf = icmp eq i64 %ey, 9218868437227405312
  %either_nan_inf = or i1 %x_nan_inf, %y_nan_inf
  %min = tail call nsz double @llvm.minimumnum.f64(double %x, double %y)
  %ret = select i1 %either_nan_inf, double 0.0, double %min
  ret double %ret
}

Examples+output at https://llvm.godbolt.org/z/W9e4Ev594

tgross35 avatar Aug 07 '26 08:08 tgross35

Hi, I'd like to work on this.

dretoh avatar Aug 30 '26 03:08 dretoh