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

X86: Inefficient lowering of constant vector variable shl

Open llvmbot opened this issue 13 years ago • 9 comments

Bugzilla Link 15141
Version trunk
OS All
Depends On llvm/llvm-bugzilla-archive#30787 llvm/llvm-bugzilla-archive#36319
Reporter LLVM Bugzilla Contributor
CC @d0k,@delena,@RKSimon,@ZviRackover

Extended Description

The code below is lowered to the following sequence.

define <4 x i32> @f22oo(<4 x i32> %in) {
entry:
   %T = shl <4 x i32> %in, <i32 0, i32 1, i32 2, i32 4>
   ret <4 x i32> %T
}

_f22oo:                                 ## @f22oo
	.cfi_startproc
## BB#0:                                ## %entry
	vmovdqa	LCPI2_0(%rip), %xmm1
	vpslld	$23, %xmm1, %xmm1
	vpaddd	LCPI2_1(%rip), %xmm1, %xmm1
	vcvttps2dq	%xmm1, %xmm1
	vpmulld	%xmm0, %xmm1, %xmm0
	ret
	.cfi_endproc

We could convert this code into "mul <1 , 2, 4, 8>".

llvmbot avatar Feb 02 '13 02:02 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#36319

RKSimon avatar Nov 27 '21 00:11 RKSimon

mentioned in issue llvm/llvm-bugzilla-archive#30787

RKSimon avatar Nov 26 '21 22:11 RKSimon

Current Codegen: https://c.godbolt.org/z/hxcYb9T99

The original test case was fixed long ago.

The "{1,1,1,1} << {i, i+1, i+2, i+3}" case still has the problem identified in [Bug #​30787] where we fail to vectorize the 'i' element as 'i+0'.

RKSimon avatar Jul 08 '21 10:07 RKSimon

I am also worried about the cast of "1 << K", where 1 is a vector of 1s and K is a consecutive mask (i, i+1, i+2, i+3). We see this in a spec benchmark.

If we can confirm (i >= 0) then https://reviews.llvm.org/D26000 should help with this.

RKSimon avatar Oct 26 '16 19:10 RKSimon

On AVX2 targets you do get VPSLLVD:

vpsllvd	LCPI0_0(%rip), %xmm0, %xmm0
retq

The vpmulld codegen is for an AVX1 only target (btver2).

Older (pre-SSE4.1) targets are having to do the multiply the hard way but still appears to give better results than scalarization (but not by much - almost in the noise):

movdqa	LCPI0_0(%rip), %xmm1    ## xmm1 = [1,2,4,16]
pshufd	$245, %xmm0, %xmm2      ## xmm2 = xmm0[1,1,3,3]
pmuludq	%xmm1, %xmm0
pshufd	$232, %xmm0, %xmm0      ## xmm0 = xmm0[0,2,2,3]
pshufd	$245, %xmm1, %xmm1      ## xmm1 = xmm1[1,1,3,3]
pmuludq	%xmm2, %xmm1
pshufd	$232, %xmm1, %xmm1      ## xmm1 = xmm1[0,2,2,3]
punpckldq	%xmm1, %xmm0    ## xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
retq

RKSimon avatar Mar 24 '15 13:03 RKSimon

On AVX2 we should use variable shift VPSLLVD xmm1, xmm2, xmm3/m128

Found this as I'm currently looking at missing vector constant folding. r213798 fixed the missing FP_TO_SINT vector constant fold:

vpmulld

LCPI0_0(%rip), %xmm0, %xmm0 retq

Nadav - I'm curious about optimizing the

consecutive mask shifts, can you point me to the particular spec benchmark that does this please?

delena avatar Mar 24 '15 13:03 delena

Found this as I'm currently looking at missing vector constant folding. r213798 fixed the missing FP_TO_SINT vector constant fold:

vpmulld LCPI0_0(%rip), %xmm0, %xmm0 retq

Nadav - I'm curious about optimizing the consecutive mask shifts, can you point me to the particular spec benchmark that does this please?

RKSimon avatar Mar 24 '15 12:03 RKSimon

I am also worried about the cast of "1 << K", where 1 is a vector of 1s and K is a consecutive mask (i, i+1, i+2, i+3). We see this in a spec benchmark.

llvmbot avatar Feb 04 '13 18:02 llvmbot

With r174327 we're a lot closer:

cvttps2dq	LCPI0_0(%rip), %xmm1
pmulld	%xmm0, %xmm1
movdqa	%xmm1, %xmm0
ret

there's still a missing vector FP_TO_SINT constant folding opportunity.

d0k avatar Feb 04 '13 18:02 d0k