Skip to content

Commit 1be9827

Browse files
authored
[SandboxVec][BottomUpVec] Implement packing of vectors (#116447)
Up until now we could only support packing of scalar elements. This patch fixes this by implementing packing of vector elements, by generating extractelement and insertelement instruction pairs.
1 parent 0d38f64 commit 1be9827

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,32 @@ Value *BottomUpVec::createPack(ArrayRef<Value *> ToPack) {
181181
// An element can be either scalar or vector. We need to generate different
182182
// IR for each case.
183183
if (Elm->getType()->isVectorTy()) {
184-
llvm_unreachable("Unimplemented");
184+
unsigned NumElms =
185+
cast<FixedVectorType>(Elm->getType())->getNumElements();
186+
for (auto ExtrLane : seq<int>(0, NumElms)) {
187+
// We generate extract-insert pairs, for each lane in `Elm`.
188+
Constant *ExtrLaneC =
189+
ConstantInt::getSigned(Type::getInt32Ty(Ctx), ExtrLane);
190+
// This may return a Constant if Elm is a Constant.
191+
auto *ExtrI =
192+
ExtractElementInst::create(Elm, ExtrLaneC, WhereIt, Ctx, "VPack");
193+
if (!isa<Constant>(ExtrI))
194+
WhereIt = std::next(cast<Instruction>(ExtrI)->getIterator());
195+
Constant *InsertLaneC =
196+
ConstantInt::getSigned(Type::getInt32Ty(Ctx), InsertIdx++);
197+
// This may also return a Constant if ExtrI is a Constant.
198+
auto *InsertI = InsertElementInst::create(
199+
LastInsert, ExtrI, InsertLaneC, WhereIt, Ctx, "VPack");
200+
if (!isa<Constant>(InsertI)) {
201+
LastInsert = InsertI;
202+
WhereIt = std::next(cast<Instruction>(LastInsert)->getIterator());
203+
}
204+
}
185205
} else {
186206
Constant *InsertLaneC =
187207
ConstantInt::getSigned(Type::getInt32Ty(Ctx), InsertIdx++);
188-
// This may be folded into a Constant if LastInsert is a Constant. In that
189-
// case we only collect the last constant.
208+
// This may be folded into a Constant if LastInsert is a Constant. In
209+
// that case we only collect the last constant.
190210
LastInsert = InsertElementInst::create(LastInsert, Elm, InsertLaneC,
191211
WhereIt, Ctx, "Pack");
192212
if (auto *NewI = dyn_cast<Instruction>(LastInsert))

llvm/test/Transforms/SandboxVectorizer/bottomup_basic.ll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,26 @@ define void @cant_vectorize_seeds(ptr %ptr) {
187187
ret void
188188
}
189189

190+
define void @pack_vectors(ptr %ptr, ptr %ptr2) {
191+
; CHECK-LABEL: define void @pack_vectors(
192+
; CHECK-SAME: ptr [[PTR:%.*]], ptr [[PTR2:%.*]]) {
193+
; CHECK-NEXT: [[PTR0:%.*]] = getelementptr <2 x float>, ptr [[PTR]], i32 0
194+
; CHECK-NEXT: [[PTR1:%.*]] = getelementptr float, ptr [[PTR]], i32 2
195+
; CHECK-NEXT: [[LD0:%.*]] = load <2 x float>, ptr [[PTR0]], align 8
196+
; CHECK-NEXT: [[LD1:%.*]] = load float, ptr [[PTR2]], align 4
197+
; CHECK-NEXT: [[VPACK:%.*]] = extractelement <2 x float> [[LD0]], i32 0
198+
; CHECK-NEXT: [[VPACK1:%.*]] = insertelement <3 x float> poison, float [[VPACK]], i32 0
199+
; CHECK-NEXT: [[VPACK2:%.*]] = extractelement <2 x float> [[LD0]], i32 1
200+
; CHECK-NEXT: [[VPACK3:%.*]] = insertelement <3 x float> [[VPACK1]], float [[VPACK2]], i32 1
201+
; CHECK-NEXT: [[PACK:%.*]] = insertelement <3 x float> [[VPACK3]], float [[LD1]], i32 2
202+
; CHECK-NEXT: store <3 x float> [[PACK]], ptr [[PTR0]], align 8
203+
; CHECK-NEXT: ret void
204+
;
205+
%ptr0 = getelementptr <2 x float>, ptr %ptr, i32 0
206+
%ptr1 = getelementptr float, ptr %ptr, i32 2
207+
%ld0 = load <2 x float>, ptr %ptr0
208+
%ld1 = load float, ptr %ptr2
209+
store <2 x float> %ld0, ptr %ptr0
210+
store float %ld1, ptr %ptr1
211+
ret void
212+
}

0 commit comments

Comments
 (0)