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

[FMF] Missed optimization: integer-derived no-NaN fact not used for ordered fcmp lowering

Open 134ARG opened this issue 6 months ago • 3 comments

When looking at SPEC CPU 2017 with LLVM, I found a possible missed optimization in benchspec/CPU/538.imagick_r/src/magick/gem.c, function ConvertRGBToHSB, which could be extracted and compiled alone as:

typedef unsigned short Quantum;
static const double QuantumScale = 1.0/65535.0;
void ConvertRGBToHSB(Quantum red, Quantum green, Quantum blue,
                     double *hue, double *saturation, double *brightness) {
  double b, delta, g, max, min, r;
  *hue=0.0; *saturation=0.0; *brightness=0.0;
  r=(double) red; g=(double) green; b=(double) blue;
  min=r < g ? r : g; if (b < min) min=b;
  max=r > g ? r : g; if (b > max) max=b;
  if (max == 0.0) return;
  delta=max-min;
  *saturation=delta/max;
  *brightness=QuantumScale*max;
  if (delta == 0.0) return;
  if (r == max) *hue=(g-b)/delta;
  else if (g == max) *hue=2.0+(b-r)/delta;
  else *hue=4.0+(r-g)/delta;
  *hue/=6.0;
  if (*hue < 0.0) *hue+=1.0;
}

The LLVM IR and x86 asm with O3 are like: https://godbolt.org/z/11o6zjsaK.

The relevant source line is:

if (delta == 0.0) return;

In the generated LLVM IR this becomes:

%23 = fcmp oeq double %20, 0.000000e+00, !dbg !66

and X86 lowers it as:

ucomisd xmm3, xmm5
jne     .LBB0_2
jnp     .LBB0_8

Here delta is derived from unsigned short inputs by uitofp, min/max selection, and delta = max - min. Therefore r, g, b, min, max, and delta are finite and never NaN. The compare therefore has a no-NaN operand fact in this context.

However, this fact is not exposed on the final fcmp, so X86 conservatively lowers the ordered equality test with an unordered/parity branch. If I manually mark the compare as:

%23 = fcmp nnan oeq double %20, 0.000000e+00

then X86 already emits the simpler branch shape with the unordered/parity arm removed: https://godbolt.org/z/d7h9vc4Wv, which becomes:

ucomisd xmm3, xmm5
je      .LBB0_8

So this does not seem to require a new X86 lowering rule. The missing piece appears to be preserving or exposing the no-NaN fact from the integer-derived min/max/delta chain to the final compare.

A slightly modified example shows that this is not only a one-branch effect. If the same delta == 0.0 compare result has multiple users:

typedef unsigned short Quantum;
static const double QuantumScale = 1.0 / 65535.0;

int hsb_multi(Quantum red, Quantum green, Quantum blue,
              double *sat, double *bright, int *a, int *b, int *v) {
  double r = (double)red;
  double g = (double)green;
  double bl = (double)blue;

  double min = r < g ? r : g;
  if (bl < min) min = bl;

  double max = r > g ? r : g;
  if (bl > max) max = bl;

  if (max == 0.0) return 0;

  double delta = max - min;
  *sat = delta / max;
  *bright = QuantumScale * max;

  int c = (delta == 0.0);
  // add multiple usages of the comparison result
  if (c) *a = 10; else *a = 20;
  *v = c ? 123 : 456;
  if (c) *b = 30; else *b = 40;
  return c ? 1 : 2;
}

The resulting LLVM IR and x86 assembly for current LLVM are like: https://godbolt.org/z/fd547rMjG.

Again, delta is never NaN for the same source-level reason. In current LLVM, without the nnan fact on the compare, X86 materializes ordered equality using the parity flag:

  ucomisd
  setnp
  sete
  and
  ...
  cmovne ...
  cmovne ...
  cmovne ...

and then uses that result for the downstream conditional moves.

If I add nnan only to the same fcmp: https://godbolt.org/z/r3o9eaWTh, the parity handling disappears, and the downstream uses are driven by the simpler equality/non-equality condition:

  ucomisd
  ...
  cmove ...
  cmove ...
  setne
  cmove ...

134ARG avatar May 27 '26 07:05 134ARG

@llvm/issue-subscribers-backend-x86

Author: 134ARG

When looking at SPEC CPU 2017 with LLVM, I found a possible missed optimization in `benchspec/CPU/538.imagick_r/src/magick/gem.c`, function `ConvertRGBToHSB`, which could be extracted and compiled alone as:
typedef unsigned short Quantum;
static const double QuantumScale = 1.0/65535.0;
void ConvertRGBToHSB(Quantum red, Quantum green, Quantum blue,
                     double *hue, double *saturation, double *brightness) {
  double b, delta, g, max, min, r;
  *hue=0.0; *saturation=0.0; *brightness=0.0;
  r=(double) red; g=(double) green; b=(double) blue;
  min=r &lt; g ? r : g; if (b &lt; min) min=b;
  max=r &gt; g ? r : g; if (b &gt; max) max=b;
  if (max == 0.0) return;
  delta=max-min;
  *saturation=delta/max;
  *brightness=QuantumScale*max;
  if (delta == 0.0) return;
  if (r == max) *hue=(g-b)/delta;
  else if (g == max) *hue=2.0+(b-r)/delta;
  else *hue=4.0+(r-g)/delta;
  *hue/=6.0;
  if (*hue &lt; 0.0) *hue+=1.0;
}

The LLVM IR and x86 asm with O3 are like: https://godbolt.org/z/11o6zjsaK.

The relevant source line is:

if (delta == 0.0) return;

In the generated LLVM IR this becomes:

%23 = fcmp oeq double %20, 0.000000e+00, !dbg !66

and X86 lowers it as:

ucomisd xmm3, xmm5
jne     .LBB0_2
jnp     .LBB0_8

Here delta is derived from unsigned short inputs by uitofp, min/max selection, and delta = max - min. Therefore r, g, b, min, max, and delta are finite and never NaN. The compare therefore has a no-NaN operand fact in this context.

However, this fact is not exposed on the final fcmp, so X86 conservatively lowers the ordered equality test with an unordered/parity branch. If I manually mark the compare as:

%23 = fcmp nnan oeq double %20, 0.000000e+00

then X86 already emits the simpler branch shape with the unordered/parity arm removed: https://godbolt.org/z/d7h9vc4Wv, which becomes:

ucomisd xmm3, xmm5
je      .LBB0_8

So this does not seem to require a new X86 lowering rule. The missing piece appears to be preserving or exposing the no-NaN fact from the integer-derived min/max/delta chain to the final compare.

A slightly modified example shows that this is not only a one-branch effect. If the same delta == 0.0 compare result has multiple users:

typedef unsigned short Quantum;
static const double QuantumScale = 1.0 / 65535.0;

int hsb_multi(Quantum red, Quantum green, Quantum blue,
              double *sat, double *bright, int *a, int *b, int *v) {
  double r = (double)red;
  double g = (double)green;
  double bl = (double)blue;

  double min = r &lt; g ? r : g;
  if (bl &lt; min) min = bl;

  double max = r &gt; g ? r : g;
  if (bl &gt; max) max = bl;

  if (max == 0.0) return 0;

  double delta = max - min;
  *sat = delta / max;
  *bright = QuantumScale * max;

  int c = (delta == 0.0);
  // add multiple usages of the comparison result
  if (c) *a = 10; else *a = 20;
  *v = c ? 123 : 456;
  if (c) *b = 30; else *b = 40;
  return c ? 1 : 2;
}

The resulting LLVM IR and x86 assembly for current LLVM are like: https://godbolt.org/z/fd547rMjG.

Again, delta is never NaN for the same source-level reason. In current LLVM, without the nnan fact on the compare, X86 materializes ordered equality using the parity flag:

  ucomisd
  setnp
  sete
  and
  ...
  cmovne ...
  cmovne ...
  cmovne ...

and then uses that result for the downstream conditional moves.

If I add nnan only to the same fcmp: https://godbolt.org/z/r3o9eaWTh, the parity handling disappears, and the downstream uses are driven by the simpler equality/non-equality condition:

  ucomisd
  ...
  cmove ...
  cmove ...
  setne
  cmove ...

Lack of FMF flags appears to be a middle-end issue not a x86 backend issue?

RKSimon avatar May 27 '26 14:05 RKSimon

Lack of FMF flags appears to be a middle-end issue not a x86 backend issue?

Yes. x86 is just the symptom of missing the nnan flag. edited.

134ARG avatar Jun 11 '26 08:06 134ARG