[X86][AVX] separate stores are not being merged into a single 256bit store.
| Bugzilla Link | 21711 |
| Resolution | FIXED |
| Resolved on | Sep 27, 2015 15:15 |
| Version | trunk |
| OS | Linux |
| CC | @hfinkel,@RKSimon,@rotateright |
Extended Description
This is similar to bug 21709. However, bug 21709 is about 256bit loads. Thi one is related to store instructions.
Here is the source code:
////// foo.c ////// #include <x86intrin.h>
void f1(__m128 v, float *ptr) { ptr[0] = v[0]; ptr[1] = v[1]; ptr[2] = v[2]; ptr[3] = v[3]; }
void f2(__m256 v, float *ptr) { ptr[0] = v[0]; ptr[1] = v[1]; ptr[2] = v[2]; ptr[3] = v[3]; ptr[4] = v[4]; ptr[5] = v[5]; ptr[6] = v[6]; ptr[7] = v[7]; }
void f3(__m256 v, __m128 *ptr) { ptr[0] = (__m128) __builtin_shufflevector(v, v, 0, 1, 2, 3); ptr[1] = (__m128) __builtin_shufflevector(v, v, 4, 5, 6, 7); } /////////////
$ clang foo.c -march=btver2 -O2 -S -o - -emit-llvm
Function 'f1' is correctly vectorized into:
define void @f1(<4 x float> %v, float* nocapture %ptr) { entry: %0 = bitcast float* %ptr to <4 x float>* store <4 x float> %v, <4 x float>* %0, align 4, !tbaa !1 ret void }
This helps the backend to efficiently generate a single vmovups for it.
(here is the codegen for 'f1' with llc -mcpu=btver2).
f1: vmovups %xmm0, (%rdi) retq
--
However, function 'f2' doesn't get vectorized and we end up with the following IR:
define void @f2(<8 x float> %v, float* nocapture %ptr) { entry: %vecext = extractelement <8 x float> %v, i32 0 store float %vecext, float* %ptr, align 4, !tbaa !1 %vecext1 = extractelement <8 x float> %v, i32 1 %arrayidx2 = getelementptr inbounds float* %ptr, i64 1 store float %vecext1, float* %arrayidx2, align 4, !tbaa !1 %vecext3 = extractelement <8 x float> %v, i32 2 %arrayidx4 = getelementptr inbounds float* %ptr, i64 2 store float %vecext3, float* %arrayidx4, align 4, !tbaa !1 %vecext5 = extractelement <8 x float> %v, i32 3 %arrayidx6 = getelementptr inbounds float* %ptr, i64 3 store float %vecext5, float* %arrayidx6, align 4, !tbaa !1 %vecext7 = extractelement <8 x float> %v, i32 4 %arrayidx8 = getelementptr inbounds float* %ptr, i64 4 store float %vecext7, float* %arrayidx8, align 4, !tbaa !1 %vecext9 = extractelement <8 x float> %v, i32 5 %arrayidx10 = getelementptr inbounds float* %ptr, i64 5 store float %vecext9, float* %arrayidx10, align 4, !tbaa !1 %vecext11 = extractelement <8 x float> %v, i32 6 %arrayidx12 = getelementptr inbounds float* %ptr, i64 6 store float %vecext11, float* %arrayidx12, align 4, !tbaa !1 %vecext13 = extractelement <8 x float> %v, i32 7 %arrayidx14 = getelementptr inbounds float* %ptr, i64 7 store float %vecext13, float* %arrayidx14, align 4, !tbaa !1 ret void }
The assembly generated for function 'f2' is therefore quite horrible:
f2: vmovss %xmm0, (%rdi) vextractps $1, %xmm0, 4(%rdi) vextractps $2, %xmm0, 8(%rdi) vextractps $3, %xmm0, 12(%rdi) vextractf128 $1, %ymm0, %xmm0 vmovss %xmm0, 16(%rdi) vextractps $1, %xmm0, 20(%rdi) vextractps $2, %xmm0, 24(%rdi) vextractps $3, %xmm0, 28(%rdi) vzeroupper retq
On AVX targets with feature FastUAMem and !SlowUAMem32, it should be instead: vmovups %ymm0, (%rdi)
Function 'f3' is already vectorized and the LLVM IR looks much better than the one from 'f2'.
define void @f3(<8 x float> %v, <4 x float>* nocapture %ptr) { entry: %shuffle = shufflevector <8 x float> %v, <8 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3> store <4 x float> %shuffle, <4 x float>* %ptr, align 16, !tbaa !5 %shuffle1 = shufflevector <8 x float> %v, <8 x float> undef, <4 x i32> <i32 4, i32 5, i32 6, i32 7> %arrayidx2 = getelementptr inbounds <4 x float>* %ptr, i64 1 store <4 x float> %shuffle1, <4 x float>* %arrayidx2, align 16, !tbaa !5 ret void }
The compiler should however further vectorize that code coalescing the two stores.
With -mcpu=btver2, llc generates the following assembly for function 'f3'.
f3: vmovaps %xmm0, (%rdi) vextractf128 $1, %ymm0, 16(%rdi) vzeroupper retq
Ideally, for btver2 (and other !SlowUAMem32 targets) we should get a 256-bit store: vmovups %ymm0, (%rdi)
The 'f3' case is finally (I hope) resolved with: http://llvm.org/viewvc/llvm-project?view=revision&revision=248622
Merging stores is tough; r227242 was reverted in r227272 because it caused an infinite loop in test-suite on aarch64. Reopening.
Merging of vector stores (the 'f3' test case) optimized with: http://reviews.llvm.org/D7208
http://llvm.org/viewvc/llvm-project?view=revision&revision=227242
Merging of extracted vector elements, round 2: http://llvm.org/viewvc/llvm-project?view=revision&revision=226845
Hopefully it sticks this time.
There's still a small change needed to get the 'f3' test to use a 32-byte store.
Second try for the merge vector elements patch: http://reviews.llvm.org/D6850
The previous patch would try to merge stores of vector elements and scalar loads. This might be a win in some cases, but for now, I just added a check to bail out if we have mixed sources for the stores.
I have a patch to merge store elements from a build_vector up for review here: http://reviews.llvm.org/D6698
That patch is checked in at: http://llvm.org/viewvc/llvm-project?view=revision&revision=224611
That patch was reverted in r225031 because it caused a crash. I'll investigate soon.
I filed comment 6 as bug 21990.
The final patch (I hope) that is needed to close this bug is to merge the 16-byte vector stores for the 'f3' example for !SlowUAMem32 targets.
DAGCombiner::MergeConsecutiveStores() currently ignores vector stores, so I think this will be a simple fix to allow that.
I have a patch to merge store elements from a build_vector up for review here: http://reviews.llvm.org/D6698
That patch is checked in at: http://llvm.org/viewvc/llvm-project?view=revision&revision=224611
Almost there...
Michael's patch was committed here: http://llvm.org/viewvc/llvm-project?view=revision&revision=224429
I have a patch to merge store elements from a build_vector up for review here: http://reviews.llvm.org/D6698
(I don't know if it's just me, but I haven't seen mails appear on any of the llvm mailing lists for over a day.)
Patch to avoid shuffle disasters posted for review: http://reviews.llvm.org/D6622
I abandoned that patch.
I think Michael Kuperstein has a better fix with: http://reviews.llvm.org/D6678
Patch to avoid shuffle disasters posted for review: http://reviews.llvm.org/D6622
This is a prerequisite for the store merging patch to avoid a SandyBridge regression.
Status update: I have a patch for DAGCombiner that produces the optimal code for Jaguar and Haswell for the 'f2' test case, but the codegen goes even further to hell on SandyBridge for 256-bit vectors. The x86 backend splits the 256-bit store for SandyBridge (SlowUAMem32), and we end up with this mess:
$ ./llc -mcpu=sandybridge store_v8f32.ll -o - vextractf128 $1, %ymm0, %xmm1 vpermilps $-27, %xmm1, %xmm2 ## xmm2 = xmm1[1,1,2,3] vinsertps $16, %xmm2, %xmm1, %xmm2 ## xmm2 = xmm1[0],xmm2[0],xmm1[2,3] vpermilpd $1, %xmm1, %xmm3 ## xmm3 = xmm1[1,0] vinsertps $32, %xmm3, %xmm2, %xmm2 ## xmm2 = xmm2[0,1],xmm3[0],xmm2[3] vpermilps $-25, %xmm1, %xmm1 ## xmm1 = xmm1[3,1,2,3] vinsertps $48, %xmm1, %xmm2, %xmm1 ## xmm1 = xmm2[0,1,2],xmm1[0] vmovups %xmm1, 16(%rdi) vpermilps $-27, %xmm0, %xmm1 ## xmm1 = xmm0[1,1,2,3] vinsertps $16, %xmm1, %xmm0, %xmm1 ## xmm1 = xmm0[0],xmm1[0],xmm0[2,3] vpermilpd $1, %xmm0, %xmm2 ## xmm2 = xmm0[1,0] vinsertps $32, %xmm2, %xmm1, %xmm1 ## xmm1 = xmm1[0,1],xmm2[0],xmm1[3] vpermilps $-25, %xmm0, %xmm0 ## xmm0 = xmm0[3,1,2,3] vinsertps $48, %xmm0, %xmm1, %xmm0 ## xmm0 = xmm1[0,1,2],xmm0[0] vmovups %xmm0, (%rdi) vzeroupper retq
Independent of the extract_vector_elt problem, MergeConsecutiveStores() doesn't try to use vector stores on constants besides zero. I suppose this is to avoid loading a vector of constants, but that may not be a bad trade-off.
Example test case modified from one of the tests in test/CodeGen/X86/MergeConsecutiveStores.ll:
%struct.B = type { i32, i32, i32, i32, i32, i32, i32, i32 }
define void @merge_const_store_vec(%struct.B* nocapture %p) { %a = getelementptr inbounds %struct.B* %p, i64 0, i32 0 store i32 1, i32* %a, align 4 %b = getelementptr inbounds %struct.B* %p, i64 0, i32 1 store i32 2, i32* %b, align 4 %c = getelementptr inbounds %struct.B* %p, i64 0, i32 2 store i32 3, i32* %c, align 4 %d = getelementptr inbounds %struct.B* %p, i64 0, i32 3 store i32 4, i32* %d, align 4 %e = getelementptr inbounds %struct.B* %p, i64 0, i32 4 store i32 5, i32* %e, align 4 %f = getelementptr inbounds %struct.B* %p, i64 0, i32 5 store i32 6, i32* %f, align 4 %g = getelementptr inbounds %struct.B* %p, i64 0, i32 6 store i32 7, i32* %g, align 4 %h = getelementptr inbounds %struct.B* %p, i64 0, i32 7 store i32 8, i32* %h, align 4 ret void }
This currently produces:
movabsq $8589934593, %rax ## imm = 0x200000001 movabsq $17179869187, %rcx ## imm = 0x400000003 movabsq $25769803781, %rdx ## imm = 0x600000005 movq %rax, (%rdi) movq %rcx, 8(%rdi) movabsq $34359738375, %rcx ## imm = 0x800000007 movq %rdx, 16(%rdi) movq %rcx, 24(%rdi) retq
But it could be (on an AVX machine), 32-byte ops: movups VEC_CONST_POOL,%ymm movups %ymm, (%rdi) retq
- DAGCombiner::MergeConsecutiveStores() is called from visitSTORE().
Why does this not do what you need?
It certainly looks like the right logic, but it's currently tripping up here: // Perform an early exit check. Do not bother looking at stored values that // are not constants or loads.
In our case, the store operands are "extract_vector_elt" nodes, so I'll see if we can adjust that check and make this work.
Hmmm...seems like all the pieces are already here; DAGCombiner does have a consecutive store detector:
- DAGCombiner::MergeConsecutiveStores() is called from visitSTORE().
Why does this not do what you need?
- The PPC backend has a mostly duplicated version of SelectionDAG::isConsecutiveLoad() that handles stores; it's called isConsecutiveLSLoc().
It does, and it is used as part of the unaligned vector load lowering to figure out whether over-loading legal. If this particular function is useful for something else, we can certainly put it in DAGCombine.
If #1 isn't the answer, then x86 should be able to do something like #2. In that case, we should probably lift the code from PPC and make it work in general for SelectionDAG.
cc'ing Hal for any advice.
Hmmm...seems like all the pieces are already here; DAGCombiner does have a consecutive store detector:
-
DAGCombiner::MergeConsecutiveStores() is called from visitSTORE().
-
The PPC backend has a mostly duplicated version of SelectionDAG::isConsecutiveLoad() that handles stores; it's called isConsecutiveLSLoc().
If #1 isn't the answer, then x86 should be able to do something like #2. In that case, we should probably lift the code from PPC and make it work in general for SelectionDAG.
cc'ing Hal for any advice.
I think this can be handled in DAGCombiner in an architecture-independent way. DAGCombiner has a "isConsecutiveLoad()" function but is lacking that functionality for stores. As noted in bug 21710, there seems to be a bug in the logic for checking consecutive accesses though.
I think part of the problem is that the SLP vectorizer doesn't attempt to work on anything over 128-bits: bug 17170.
assigned to @rotateright