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

Vectorization improvement opportunity for max/min

Open serguei-katkov opened this issue 9 years ago • 5 comments

Bugzilla Link 35284
Version trunk
OS Linux
CC @topperc,@hfinkel,@RKSimon,@rotateright

Extended Description

This is about possibly better vectorization coding for float/double max/min.

Let's consider the following c++ program (https://godbolt.org/g/yWxTCB):

float test(float a[], int N) {
  float max = -1;
  for (int i = 0; i < N; i++)
    max = max < a[i] ? a[i] : max;
  return max;
}

clang trunk now generates a main loop with the following pattern:

movss + maxss (N times, where N might be 4 or 8).

icc 18 is able to use maxps packed instruction.

Even the code looking as:

movss 16 times (load values to xmm0-xmm15 registers)
ucommiss + ga (16 times)

and movaps in case of ga on slowpath

seems might be better.

I do not know what code would be best here but potentially there is an opportunity here...

serguei-katkov avatar Nov 13 '17 08:11 serguei-katkov

Before we get this case to vectorize, we probably want to make sure the existing vectorization is working as expected - bug 35538 (#34886).

rotateright avatar Dec 05 '17 20:12 rotateright

Agreed.

serguei-katkov avatar Nov 27 '17 04:11 serguei-katkov

thank you for the response but after thinking more about this case I still cannot understand why the loop from example cannot be vectorized.

I didn't mean to imply that the loop can't be vectorized. We just have to be careful that we get the right results in the presence of NaN values.

The x86 max/min instructions have different behavior than the IEEE or libm definitions with respect to NaN. For x86, we have:

"If only one value is a NaN (SNaN or QNaN) for this instruction, the second source operand, either a NaN or a valid floating-point value, is written to the result. If instead of this behavior, it is required that the NaN from either source operand be returned, the action of MAXSS can be emulated using a sequence of instructions, such as, a comparison followed by AND, ANDN and OR."

I'm guessing that we need to enhance the loop vectorizer to see that possibility as a pattern for vectorization (for x86 at least).

rotateright avatar Nov 26 '17 18:11 rotateright

Hi Sanjay,

thank you for the response but after thinking more about this case I still cannot understand why the loop from example cannot be vectorized.

As I understand maxps also ignores NaN values.

As I see it:

mov xmm0, (-1, -1, -1, -1)

loop:
mov xmm1, [read 4 floats from memory]
maxps xmm1, xmm0                     ; All NaNs in xmm1 will be ignored
mov xmm0, [read 4 floats from memory]
maxps xmm0, xmm1                     ; All NaNs in xmm0 will be ignored
back branch to loop.

so to me icc 18 does a right usage of maxps here.

Do I miss anything here?

Another interesting observation: icc 18 does not use maxss in handling tail but instead it uses comiss with branch. It seems it is faster, at least I see that code with ucomiss and branch works faster than maxps. Is there any way I can simply disable the maxps usage and force you ucomiss usage for instruction selections to write some micro benchmark and check it?

Moreover icc 18 uses the branch probability 50% for comiss while I guess that in average the branch has less probability (expecting that elements in an array is not sorted), however it is questionable.

serguei-katkov avatar Nov 23 '17 06:11 serguei-katkov

The code as written and compiled with plain -O2 as shown in the godbolt link will ignore all NaN values in a[].

It does vectorize if you don't have strict FP math:

$ ./clang -O2 -S -o - 35284.c -ffinite-math-only | grep max 
	maxps	%xmm0, %xmm2
	maxps	%xmm1, %xmm3
	maxps	%xmm2, %xmm0
	maxps	%xmm3, %xmm1
	maxps	%xmm0, %xmm2
	maxps	%xmm1, %xmm3
	maxps	%xmm1, %xmm0
	maxps	%xmm1, %xmm0
	maxps	%xmm1, %xmm0
	maxss	%xmm1, %xmm0

rotateright avatar Nov 13 '17 14:11 rotateright