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

[X86][AVX512] fabs(a) < fabs(b) ? fabs(a) : fabs(b) should lower to vrange/vminmax

Open Andarwinux opened this issue 5 months ago • 9 comments

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125166 http://www.ecs.umass.edu/arith-2018/pdf/arith25_18.pdf

float src( float a, float b) {
   return std::abs(a) < std::abs(b) ? std::abs(a) : std::abs(b);
}

__m128 tgt(__m128 a, __m128 b) {
   return _mm_range_ss(a,b,10);
}

.LCPI0_0:
        .long   0x7fffffff
src(float, float):
        vbroadcastss    xmm2, dword ptr [rip + .LCPI0_0]
        vandps  xmm0, xmm0, xmm2
        vandps  xmm1, xmm1, xmm2
        vminss  xmm0, xmm0, xmm1
        ret

tgt(float vector[4], float vector[4]):
        vrangess        xmm0, xmm0, xmm1, 10
        ret

https://godbolt.org/z/xeeY5qMEv

Andarwinux avatar Jun 29 '26 00:06 Andarwinux

@llvm/issue-subscribers-backend-x86

Author: Andarwinux

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125166 http://www.ecs.umass.edu/arith-2018/pdf/arith25_18.pdf
float src( float a, float b) {
   return std::abs(a) &lt; std::abs(b) ? a : b;
}

__m128 tgt(__m128 a, __m128 b) {
   return _mm_range_ss(a,b,14);
}

.LCPI0_0:
        .long   0x7fffffff
src(float, float):
        vbroadcastss    xmm2, dword ptr [rip + .LCPI0_0]
        vandps  xmm3, xmm0, xmm2
        vandps  xmm2, xmm1, xmm2
        vcmpltss        k1, xmm3, xmm2
        vmovss  xmm1 {k1}, xmm1, xmm0
        vmovaps xmm0, xmm1
        ret

tgt(float vector[4], float vector[4]):
        vrangess        xmm0, xmm0, xmm1, 14
        ret

https://godbolt.org/z/xeeY5qMEv

I would like to work on this issue .

FathimaHaris avatar Jun 30 '26 03:06 FathimaHaris

@RKSimon

Andarwinux avatar Jun 30 '26 04:06 Andarwinux

@EugeneZelenko Could you assign this issue to @FathimaHaris?

Andarwinux avatar Jun 30 '26 09:06 Andarwinux

@FathimaHaris Hopefully you can come up with something simple for this test case that is relatively trivial to incrementally add to in future patches to make use of what X86ISD::VRANGE/VRANGES is actually capable of (COPYSIGN handling etc.). Also, watch out for NAN/INF handling.....

RKSimon avatar Jun 30 '26 09:06 RKSimon

Hi @Andarwinux , I've started working on this and wanted to flag a couple of things before I go further.

The issue currently suggests imm8 = 14 for the VRANGE lowering, but based on the Intel SDM's encoding , that decodes to min-abs comparison with the sign forced negative unconditionally, which doesn't match the source semantics . I think imm8 = 6 is the right one instead — min-abs comparison with the sign of the selected operand preserved rather than forced.

Separately, I noticed a case where VRANGE's hardware behavior doesn't line up with the source semantics when the two inputs have equal magnitude but opposite sign.

For example with a = -2.0, b = 2.0, the source fabs(a) < fabs(b) ? a : b evaluates to 2.0 since the comparison is false, but VRANGE in min-abs mode resolves that tie by always returning whichever operand is negative, giving -2.0 here. Since that depends on which operand happens to be negative at runtime rather than which branch the select would take, it's not something a different immediate can fix on its own.

Would appreciate any thoughts on either of these, or context on the original imm8 choice if I'm missing something. Thanks!

FathimaHaris avatar Jul 14 '26 04:07 FathimaHaris

@FathimaHaris I copied this from GCC Bugzilla. You're right, the semantics of vrange aren't consistent in this scenario.

But if it always returns a positive number, then it should be fine?

float minnumabs( float a, float b) {
   return fminf(fabs(a),fabs(b));
}

__m128 minnumabs_vrange(__m128 a, __m128 b) {
   return _mm_range_ss(a,b,10);
}

float absminnum( float a, float b) {
   return fabs(fminf(a,b));
}

__m128 absminnum_vrange(__m128 a, __m128 b) {
   return _mm_range_ss(a,b,8);
}

float minabs( float a, float b) {
   return fabs(a) < fabs(b) ? fabs(a) : fabs(b);
}

float minabs2( float a, float b) {
   return (a) < (b) ? fabs(a) : fabs(b);
}

__m128 minabs_vrange(__m128 a, __m128 b) {
   return _mm_range_ss(a,b,10);
}

__m128 minabs2_vrange(__m128 a, __m128 b) {
   return _mm_range_ss(a,b,8);
}

float minimumnum( float a, float b) {
   return fminimum_numf(a,b);
}

__m128 minimumnum_vrange(__m128 a, __m128 b) {
   return _mm_range_ss(a,b,4);
}

float minimumnumabs( float a, float b) {
   return fminimum_numf(fabs(a),fabs(b));
}

__m128 minimumnumabs_vrange(__m128 a, __m128 b) {
   return _mm_range_ss(a,b,10);
}

float absminimumnum( float a, float b) {
   return fabs(fminimum_numf(a,b));
}

__m128 absminimumnum_vrange(__m128 a, __m128 b) {
   return _mm_range_ss(a,b,8);
}

float minnum( float a, float b) {
   return fminf(a,b);
}

__m128 minnum_vrange(__m128 a, __m128 b) {
   return _mm_range_ss(a,b,4);
}

But I'm not sure about NaN.

vrange's qNaN behavior looks similar to minnum/minimumnum, but different from minimum/ternary operation? While avx10.2 vminmax seems to support both behaviors.

https://godbolt.org/z/Pq39zWYTT

Andarwinux avatar Jul 14 '26 07:07 Andarwinux

@FathimaHaris any progress?

Andarwinux avatar Oct 19 '26 14:10 Andarwinux

@Andarwinux I’m sorry for the long delay on this. I wasn’t able to work on it for a while due to personal reasons. I’m back on it now and will share an update soon. Thank you for your patience, and please let me know if you’d prefer to reassign it in the meantime.

FathimaHaris avatar Nov 05 '26 02:11 FathimaHaris