Skip to content

Commit

Permalink
[SLP]Adjust assertion check for scalars in several insertelements.
Browse files Browse the repository at this point in the history
If the same scalar is inserted several times into the same buildvector,
the mask index can be used already. In this case need to check, that
this scalar is already part of the vectorized buildvector.
  • Loading branch information
alexey-bataev committed May 9, 2022
1 parent 72831a5 commit cce80bd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
11 changes: 10 additions & 1 deletion llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Expand Up @@ -6592,7 +6592,16 @@ InstructionCost BoUpSLP::getTreeCost(ArrayRef<Value *> VectorizedVals) {
SmallVectorImpl<int> &Mask = ShuffleMasks[VecId][ScalarTE];
if (Mask.empty())
Mask.assign(FTy->getNumElements(), UndefMaskElem);
assert(Mask[InIdx] == UndefMaskElem &&
// InsertElement should not be used already or the scalar is part of
// TreeEntry, which is operand of the root insertelement instructions.
assert((Mask[InIdx] == UndefMaskElem ||
any_of(ScalarTE->UserTreeIndices,
[](const EdgeInfo &EI) {
return EI.EdgeIdx == 1 &&
EI.UserTE->getOpcode() ==
Instruction::InsertElement &&
!EI.UserTE->isAltShuffle();
})) &&
"InsertElementInstruction used already.");
Mask[InIdx] = EU.Lane;
DemandedElts[VecId].setBit(InIdx);
Expand Down
25 changes: 25 additions & 0 deletions llvm/test/Transforms/SLPVectorizer/X86/buildvector-shuffle.ll
Expand Up @@ -41,3 +41,28 @@ entry:
}

declare float @llvm.fmuladd.f32(float, float, float)

define void @test(float %a) {
; CHECK-LABEL: @test(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TMP0:%.*]] = insertelement <2 x float> poison, float [[A:%.*]], i32 0
; CHECK-NEXT: [[TMP1:%.*]] = insertelement <2 x float> [[TMP0]], float [[A]], i32 1
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
; CHECK-NEXT: [[TMP2:%.*]] = fadd <2 x float> zeroinitializer, [[TMP1]]
; CHECK-NEXT: [[TMP3:%.*]] = extractelement <2 x float> [[TMP2]], i32 0
; CHECK-NEXT: [[AGG:%.*]] = insertelement <2 x float> [[TMP2]], float [[TMP3]], i64 1
; CHECK-NEXT: br label [[LOOP]]
;
entry:
br label %loop

loop:
%add.i157 = fadd float 0.000000e+00, %a
%add23.i = fadd float 0.000000e+00, %a
%insert = insertelement <2 x float> zeroinitializer, float %add.i157, i64 0
%insert.i = insertelement <2 x float> %insert, float %add23.i, i64 1
%agg = insertelement <2 x float> %insert.i, float %add.i157, i64 1
br label %loop
}

0 comments on commit cce80bd

Please sign in to comment.