[SLP] Overlapping vector trees are unable to vectorize due to ExtractElement cost
In the below example, there are two SLP reduction vectorization opportunities that share the same load. On their own, they are both profitable. However, when together, neither is able to vectorize because the load value's have external uses so a cost is added to extract that element.
I put together a rough draft using the following strategy:
- When we find a tree that has external uses, cache the tree and its cost without the extraction cost if removing the extraction cost would make it profitable.
- In the future, if we find a tree that has external uses, check to see if we have cached those same external uses elsewhere. If we can account for all the external uses in the cache, do not add a cost for the external uses for the current tree. This is because we are assuming that the cached chains can also be vectorized so their will be no need for extracting any elements.
- Run SLP again so that we can now vectorize the cache chains (they didn't vectorize the first time due to external uses).
Would be interested in discussing this further, also happy to post my current work as an MR.
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
; RUN: opt -mtriple=riscv64 -mattr=+v,+m -passes=slp-vectorizer -slp-threshold=0 -S < %s | FileCheck %s
;define i32 @add_slp(ptr %p, i32 %i_stride) {
;; CHECK-LABEL: define i32 @add_slp(
;; CHECK-SAME: ptr [[P:%.*]], i32 [[I_STRIDE:%.*]]) {
;; CHECK-NEXT: [[TMP1:%.*]] = load <4 x i32>, ptr [[P]], align 1
;; CHECK-NEXT: [[ARRAYIDX_4:%.*]] = getelementptr i32, ptr [[P]], i64 4
;; CHECK-NEXT: [[LD_4:%.*]] = load i32, ptr [[ARRAYIDX_4]], align 1
;; CHECK-NEXT: [[TMP2:%.*]] = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> [[TMP1]])
;; CHECK-NEXT: [[OP_RDX:%.*]] = add i32 [[TMP2]], [[LD_4]]
;; CHECK-NEXT: ret i32 [[OP_RDX]]
;;
; %ld = load i32, ptr %p, align 1
; %arrayidx.1 = getelementptr i32, ptr %p, i64 1
; %ld.1 = load i32, ptr %arrayidx.1, align 1
; %add.1 = add i32 %ld, %ld.1
; %arrayidx.2 = getelementptr i32, ptr %p, i64 2
; %ld.2 = load i32, ptr %arrayidx.2, align 1
; %add.2 = add i32 %add.1, %ld.2
; %arrayidx.3 = getelementptr i32, ptr %p, i64 3
; %ld.3 = load i32, ptr %arrayidx.3, align 1
; %add.3 = add i32 %add.2, %ld.3
; %arrayidx.4 = getelementptr i32, ptr %p, i64 4
; %ld.4 = load i32, ptr %arrayidx.4, align 1
; %add.4 = add i32 %add.3, %ld.4
; ret i32 %add.4
;}
;
;define i32 @mul_slp(ptr %p, i32 %i_stride) {
;; CHECK-LABEL: define i32 @mul_slp(
;; CHECK-SAME: ptr [[P:%.*]], i32 [[I_STRIDE:%.*]]) {
;; CHECK-NEXT: [[TMP1:%.*]] = load <4 x i32>, ptr [[P]], align 1
;; CHECK-NEXT: [[TMP2:%.*]] = mul <4 x i32> [[TMP1]], [[TMP1]]
;; CHECK-NEXT: [[ARRAYIDX_4:%.*]] = getelementptr i32, ptr [[P]], i64 4
;; CHECK-NEXT: [[LD_4:%.*]] = load i32, ptr [[ARRAYIDX_4]], align 1
;; CHECK-NEXT: [[MUL_4:%.*]] = mul i32 [[LD_4]], [[LD_4]]
;; CHECK-NEXT: [[TMP3:%.*]] = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> [[TMP2]])
;; CHECK-NEXT: [[OP_RDX:%.*]] = add i32 [[TMP3]], [[MUL_4]]
;; CHECK-NEXT: ret i32 [[OP_RDX]]
;;
; %ld = load i32, ptr %p, align 1
; %mul = mul i32 %ld, %ld
; %arrayidx.1 = getelementptr i32, ptr %p, i64 1
; %ld.1 = load i32, ptr %arrayidx.1, align 1
; %mul.1 = mul i32 %ld.1, %ld.1
; %add11.1 = add i32 %mul.1, %mul
; %arrayidx.2 = getelementptr i32, ptr %p, i64 2
; %ld.2 = load i32, ptr %arrayidx.2, align 1
; %mul.2 = mul i32 %ld.2, %ld.2
; %add11.2 = add i32 %mul.2, %add11.1
; %arrayidx.3 = getelementptr i32, ptr %p, i64 3
; %ld.3 = load i32, ptr %arrayidx.3, align 1
; %mul.3 = mul i32 %ld.3, %ld.3
; %add11.3 = add i32 %mul.3, %add11.2
; %arrayidx.4 = getelementptr i32, ptr %p, i64 4
; %ld.4 = load i32, ptr %arrayidx.4, align 1
; %mul.4 = mul i32 %ld.4, %ld.4
; %add11.4 = add i32 %mul.4, %add11.3
; ret i32 %add11.4
;}
define i32 @partial_slp(ptr %p, i32 %i_stride) {
; CHECK-LABEL: define i32 @partial_slp(
; CHECK-SAME: ptr [[P:%.*]], i32 [[I_STRIDE:%.*]]) #[[ATTR0:[0-9]+]] {
; CHECK-NEXT: [[LD:%.*]] = load i32, ptr [[P]], align 1
; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[LD]], [[LD]]
; CHECK-NEXT: [[ARRAYIDX_4:%.*]] = getelementptr i32, ptr [[P]], i64 1
; CHECK-NEXT: [[LD_4:%.*]] = load i32, ptr [[ARRAYIDX_4]], align 1
; CHECK-NEXT: [[TMP5:%.*]] = add i32 [[LD]], [[LD_4]]
; CHECK-NEXT: [[MUL_5:%.*]] = mul i32 [[LD_4]], [[LD_4]]
; CHECK-NEXT: [[TMP2:%.*]] = add i32 [[MUL_5]], [[MUL]]
; CHECK-NEXT: [[ARRAYIDX_2:%.*]] = getelementptr i32, ptr [[P]], i64 2
; CHECK-NEXT: [[ADD_2:%.*]] = load i32, ptr [[ARRAYIDX_2]], align 1
; CHECK-NEXT: [[OP_RDX1:%.*]] = add i32 [[TMP5]], [[ADD_2]]
; CHECK-NEXT: [[TMP3:%.*]] = mul i32 [[ADD_2]], [[ADD_2]]
; CHECK-NEXT: [[OP_RDX2:%.*]] = add i32 [[TMP3]], [[TMP2]]
; CHECK-NEXT: [[ARRAYIDX_3:%.*]] = getelementptr i32, ptr [[P]], i64 3
; CHECK-NEXT: [[LD_3:%.*]] = load i32, ptr [[ARRAYIDX_3]], align 1
; CHECK-NEXT: [[OP_RDX3:%.*]] = add i32 [[OP_RDX1]], [[LD_3]]
; CHECK-NEXT: [[MUL_3:%.*]] = mul i32 [[LD_3]], [[LD_3]]
; CHECK-NEXT: [[ADD11_3:%.*]] = add i32 [[MUL_3]], [[OP_RDX2]]
; CHECK-NEXT: [[ARRAYIDX_5:%.*]] = getelementptr i32, ptr [[P]], i64 4
; CHECK-NEXT: [[OP_RDX4:%.*]] = load i32, ptr [[ARRAYIDX_5]], align 1
; CHECK-NEXT: [[OP_RDX5:%.*]] = add i32 [[OP_RDX3]], [[OP_RDX4]]
; CHECK-NEXT: [[MUL_4:%.*]] = mul i32 [[OP_RDX4]], [[OP_RDX4]]
; CHECK-NEXT: [[ADD11_4:%.*]] = add i32 [[MUL_4]], [[ADD11_3]]
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[OP_RDX5]], [[ADD11_4]]
; CHECK-NEXT: ret i32 [[ADD]]
;
%ld = load i32, ptr %p, align 1
%mul = mul i32 %ld, %ld
%arrayidx.1 = getelementptr i32, ptr %p, i64 1
%ld.1 = load i32, ptr %arrayidx.1, align 1
%add.1 = add i32 %ld, %ld.1
%mul.1 = mul i32 %ld.1, %ld.1
%add11.1 = add i32 %mul.1, %mul
%arrayidx.2 = getelementptr i32, ptr %p, i64 2
%ld.2 = load i32, ptr %arrayidx.2, align 1
%add.2 = add i32 %add.1, %ld.2
%mul.2 = mul i32 %ld.2, %ld.2
%add11.2 = add i32 %mul.2, %add11.1
%arrayidx.3 = getelementptr i32, ptr %p, i64 3
%ld.3 = load i32, ptr %arrayidx.3, align 1
%add.3 = add i32 %add.2, %ld.3
%mul.3 = mul i32 %ld.3, %ld.3
%add11.3 = add i32 %mul.3, %add11.2
%arrayidx.4 = getelementptr i32, ptr %p, i64 4
%ld.4 = load i32, ptr %arrayidx.4, align 1
%add.4 = add i32 %add.3, %ld.4
%mul.4 = mul i32 %ld.4, %ld.4
%add11.4 = add i32 %mul.4, %add11.3
%add = add i32 %add.4, %add11.4
ret i32 %add
}
Yes, need to implement something like this. But before, need to implement "forest" vectorization support, i.e. multiple trees vectorization at once
Yes, need to implement something like this. But before, need to implement "forest" vectorization support, i.e. multiple trees vectorization at once
Thanks for the response. For the "forest" support, do you have an idea on how you would approach the problem? One possibly naive solution would be to look at all "external uses" and see if they can be used to start an additional tree. If not, check the "external uses" uses. For instance, if the following Load -> Zext -> Store is the current SLP Tree, where Zext has external uses, we would:
- Attempt to start a second SLP tree from the external users,
{add0, ..., add15}. - When unsuccessful, we would look at the users of
{add0, ..., add15},{store0, ...store15}, and we would try to start an SLP tree from those instructions. - Use the score of both trees as the score of the "forest"
Load <16 x i8>
|
zext to <16 x i16> ---------> External Uses { add0, ... add15}
| |
store <16 x i16> {store0, ..., store15}
Forest vectorization supposed to support multi-tree vectorization and avoid use of too long vectors. We need to start with this one at first
Just to make sure we're (@JeffreyALaw) understanding correctly, the concern is that by enabling multi-tree vectorization, we will create vectorization opportunities with long vector lengths that will lead to a performance regression? If that is the case, do we have an idea of what heuristics we could use for cost-ing large vectors? Certain instructions will scale differently, for instance the cost of large vector arithmetic might scale linearly, while the cost of shuffling could scale exponentially.
No, we need to fix the issue with the lon vectors. Instead of the long vectors within a single tree we need to generate multiple trees with a register-length vectors
Thanks for the clarification.
My understanding is that we currently cap our vectorization at getMaxmimumVF() which is set by TargetTransformInfo. So for the following:
void test(long int * restrict x, long int * restrict y) {
#pragma clang loop unroll(full)
for (int i = 0; i < 32; ++i)
x[i] = y[i] + 1;
}
We would create a store chain of 32-elements, but if getMaximumVF() returns 16, we would build a tree and then vectorize just the first 16 elements. After that, we would look at the next 16 elements at build a tree/vectorize those as well.
Is the direction that you have in mind to build the multiple trees in parallel (i.e. build two 16-element trees at the same time) and then vectorize them together at the end? This could give us better performance in cases where it is beneficial to shuffle elements across trees.
Yes, that's the idea.
I'm going to use the following as a motivating example since it would be a reasonable use case for this change, though we'll need to consider more complicated cases as well:
void test(long int * restrict x, long int * restrict y) {
#pragma clang loop unroll(full)
for (int i = 0; i < 8; ++i) {
x[2 * i] = y[i] + 1;
x[2 * i + 1] = y[8 + i] + 1;
}
}
Compiling with -slp-max-reg-size=512 so that the total width is the size of 2 registers, the SLP vectorizer vectorizes 2 chains of 8 elements. Currently, after SLPVectorizerPass and the following VectorCombinePass, we get the following pattern, note that is the same pattern repeated for each of the two chains.
%0 = call <12 x i64> @llvm.masked.load.v12i64.p0(ptr align 8 %y, <12 x i1> <i1 true, i1 true, i1 true, i1 true, i1 false, i1 false, i1 false, i1 false, i1 true, i1 true, i1 true, i1 true>, <12 x i64> poison), !tbaa !10
%1 = shufflevector <12 x i64> %0, <12 x i64> poison, <8 x i32> <i32 0, i32 8, i32 1, i32 9, i32 2, i32 10, i32 3, i32 11>
%2 = add nsw <8 x i64> %1, splat (i64 1)
store <8 x i64> %2, ptr %x, align 8, !tbaa !10
%arrayidx.4 = getelementptr inbounds nuw i8, ptr %y, i64 32
%arrayidx2.4 = getelementptr inbounds nuw i8, ptr %x, i64 64
%3 = call <12 x i64> @llvm.masked.load.v12i64.p0(ptr align 8 %arrayidx.4, <12 x i1> <i1 true, i1 true, i1 true, i1 true, i1 false, i1 false, i1 false, i1 false, i1 true, i1 true, i1 true, i1 true>, <12 x i64> poison), !tbaa !10
%4 = shufflevector <12 x i64> %3, <12 x i64> poison, <8 x i32> <i32 0, i32 8, i32 1, i32 9, i32 2, i32 10, i32 3, i32 11>
%5 = add nsw <8 x i64> %4, splat (i64 1)
store <8 x i64> %5, ptr %arrayidx2.4, align 8, !tbaa !10
However, it would be possible to instead remove these masked loads in favor of a unit stride vector load:
%y_lower = load <8 x i64>, ptr %y, align 8, !tbaa !10
%y.offset = getelementptr inbounds nuw i8, ptr %y, i64 64
%y_upper = load <8 x i64>, ptr %y.offset, align 8, !tbaa !10
%shuffle0 = shufflevector <8 x i64> %y_lower, <8 x i64> %y_upper, <8 x i32> <i32 0, i32 8, i32 1, i32 9, i32 2, i32 10, i32 3, i32 11>
%shuffle1 = shufflevector <8 x i64> %y_lower, <8 x i64> %y_upper, <8 x i32> <i32 4, i32 12, i32 5, i32 13, i32 6, i32 14, i32 7, i32 15>
%add0 = add nsw <8 x i64> %shuffle0, splat (i64 1)
%add1 = add nsw <8 x i64> %shuffle1, splat (i64 1)
store <8 x i64> %2, ptr %x, align 8, !tbaa !10
%x.offset = getelementptr inbounds nuw i8, ptr %x, i64 64
store <8 x i64> %add0, ptr %x, align 8, !tbaa !10
store <8 x i64> %add1, ptr %x.offset, align 8, !tbaa !10
As for how to change the SLP-Vectorizer for this along the lines as we discussed above, I would propose that:
- When we collect elements to start vectorizing from and
# elements > Max-VF, rather than peeling off the first VF elements, creating an entire tree, and vectorizing that tree, we would peel of the first VF elements and construct just the first TreeEntry. - We would then start a second tree using the remaining elements that we collected that did not get included in the first TreeEntry, now building two trees in parallel
- When a TreeEntry is created that requires shuffling or striding, we can look across the other trees to see if we can shuffle across trees more efficiently. We would add a
SuperTreeEntryclass that could be part of 2 different trees. - When we go to calculate cost, we calculate a combined cost for all of the trees together. An open question here is how do we determine if we would be better off only vectorizing a subset of the trees rather than all of them?
Please let me know if you have any thoughts on that proposal, I can sketch out a MR once ready.
I think we just need to add an extra level in the VectorizableTree array. In this case vectorizagion across the trees will be supported automatically. I mean, we should not create a vector of BoUpSLP entities, need to continue keeping just a single instance with the multilevel VectorizableTree
Agreed that we should keep just a single BoUpSLP object.
As for identifying cross tree vectorization opportunities, should that be done while creating the trees so that it can be accounted for in the cost modeling?
Agreed that we should keep just a single BoUpSLP object.
As for identifying cross tree vectorization opportunities, should that be done while creating the trees so that it can be accounted for in the cost modeling?
Most probably, you won't need to do anything about it, it should be handled automatically
Sounds good and thanks for the help. I will put together a MR with the following commits, will focus on just the store chains initially:
- Expand
VectorizableTreeto be multi-level. - Refactor
vectorizeTree()flow so that it acceptsTreeNumberas an input. - Adjust
vectorizeStores()andvectorizeStoreChain()so that all tree's are created first, and then the trees are all vectorized after all are created.