Skip to content

Commit 6409799

Browse files
authored
[SandboxVec][Legality] Pack from different BBs (#124363)
When the inputs of the pack come from different BBs we need to make sure we emit the pack instructions at the correct place.
1 parent 280c7d7 commit 6409799

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ enum class ResultReason {
9191
DiffTypes,
9292
DiffMathFlags,
9393
DiffWrapFlags,
94+
DiffBBs,
9495
NotConsecutive,
9596
CantSchedule,
9697
Unimplemented,
@@ -127,6 +128,8 @@ struct ToStr {
127128
return "DiffMathFlags";
128129
case ResultReason::DiffWrapFlags:
129130
return "DiffWrapFlags";
131+
case ResultReason::DiffBBs:
132+
return "DiffBBs";
130133
case ResultReason::NotConsecutive:
131134
return "NotConsecutive";
132135
case ResultReason::CantSchedule:

llvm/lib/Transforms/Vectorize/SandboxVectorizer/Legality.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ const LegalityResult &LegalityAnalysis::canVectorize(ArrayRef<Value *> Bndl,
214214
dumpBndl(Bndl););
215215
return createLegalityResult<Pack>(ResultReason::NotInstructions);
216216
}
217+
// Pack if not in the same BB.
218+
auto *BB = cast<Instruction>(Bndl[0])->getParent();
219+
if (any_of(drop_begin(Bndl),
220+
[BB](auto *V) { return cast<Instruction>(V)->getParent() != BB; }))
221+
return createLegalityResult<Pack>(ResultReason::DiffBBs);
217222

218223
auto CollectDescrs = getHowToCollectValues(Bndl);
219224
if (CollectDescrs.hasVectorInputs()) {

llvm/test/Transforms/SandboxVectorizer/pack.ll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,30 @@ loop:
8888
exit:
8989
ret void
9090
}
91+
92+
define void @packFromDiffBBs(ptr %ptr, i8 %v) {
93+
; CHECK-LABEL: define void @packFromDiffBBs(
94+
; CHECK-SAME: ptr [[PTR:%.*]], i8 [[V:%.*]]) {
95+
; CHECK-NEXT: [[ENTRY:.*:]]
96+
; CHECK-NEXT: [[ADD0:%.*]] = add i8 [[V]], 1
97+
; CHECK-NEXT: br label %[[BB:.*]]
98+
; CHECK: [[BB]]:
99+
; CHECK-NEXT: [[ADD1:%.*]] = add i8 [[V]], 2
100+
; CHECK-NEXT: [[PACK:%.*]] = insertelement <2 x i8> poison, i8 [[ADD0]], i32 0
101+
; CHECK-NEXT: [[PACK1:%.*]] = insertelement <2 x i8> [[PACK]], i8 [[ADD1]], i32 1
102+
; CHECK-NEXT: [[GEP0:%.*]] = getelementptr i8, ptr [[PTR]], i64 0
103+
; CHECK-NEXT: store <2 x i8> [[PACK1]], ptr [[GEP0]], align 1
104+
; CHECK-NEXT: ret void
105+
;
106+
entry:
107+
%add0 = add i8 %v, 1
108+
br label %bb
109+
110+
bb:
111+
%add1 = add i8 %v, 2
112+
%gep0 = getelementptr i8, ptr %ptr, i64 0
113+
%gep1 = getelementptr i8, ptr %ptr, i64 1
114+
store i8 %add0, ptr %gep0
115+
store i8 %add1, ptr %gep1
116+
ret void
117+
}

llvm/unittests/Transforms/Vectorize/SandboxVectorizer/LegalityTest.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,24 @@ struct LegalityTest : public testing::Test {
5757
}
5858
};
5959

60+
static sandboxir::BasicBlock *getBasicBlockByName(sandboxir::Function *F,
61+
StringRef Name) {
62+
for (sandboxir::BasicBlock &BB : *F)
63+
if (BB.getName() == Name)
64+
return &BB;
65+
llvm_unreachable("Expected to find basic block!");
66+
}
67+
6068
TEST_F(LegalityTest, LegalitySkipSchedule) {
6169
parseIR(C, R"IR(
6270
define void @foo(ptr %ptr, <2 x float> %vec2, <3 x float> %vec3, i8 %arg, float %farg0, float %farg1, i64 %v0, i64 %v1, i32 %v2) {
71+
entry:
6372
%gep0 = getelementptr float, ptr %ptr, i32 0
6473
%gep1 = getelementptr float, ptr %ptr, i32 1
74+
store float %farg0, ptr %gep1
75+
br label %bb
76+
77+
bb:
6578
%gep3 = getelementptr float, ptr %ptr, i32 3
6679
%ld0 = load float, ptr %gep0
6780
%ld0b = load float, ptr %gep0
@@ -89,10 +102,14 @@ define void @foo(ptr %ptr, <2 x float> %vec2, <3 x float> %vec3, i8 %arg, float
89102

90103
sandboxir::Context Ctx(C);
91104
auto *F = Ctx.createFunction(LLVMF);
92-
auto *BB = &*F->begin();
93-
auto It = BB->begin();
105+
auto *EntryBB = getBasicBlockByName(F, "entry");
106+
auto It = EntryBB->begin();
94107
[[maybe_unused]] auto *Gep0 = cast<sandboxir::GetElementPtrInst>(&*It++);
95108
[[maybe_unused]] auto *Gep1 = cast<sandboxir::GetElementPtrInst>(&*It++);
109+
auto *St1Entry = cast<sandboxir::StoreInst>(&*It++);
110+
111+
auto *BB = getBasicBlockByName(F, "bb");
112+
It = BB->begin();
96113
[[maybe_unused]] auto *Gep3 = cast<sandboxir::GetElementPtrInst>(&*It++);
97114
auto *Ld0 = cast<sandboxir::LoadInst>(&*It++);
98115
auto *Ld0b = cast<sandboxir::LoadInst>(&*It++);
@@ -162,6 +179,14 @@ define void @foo(ptr %ptr, <2 x float> %vec2, <3 x float> %vec3, i8 %arg, float
162179
EXPECT_EQ(cast<sandboxir::Pack>(Result).getReason(),
163180
sandboxir::ResultReason::DiffWrapFlags);
164181
}
182+
{
183+
// Check DiffBBs
184+
const auto &Result =
185+
Legality.canVectorize({St0, St1Entry}, /*SkipScheduling=*/true);
186+
EXPECT_TRUE(isa<sandboxir::Pack>(Result));
187+
EXPECT_EQ(cast<sandboxir::Pack>(Result).getReason(),
188+
sandboxir::ResultReason::DiffBBs);
189+
}
165190
{
166191
// Check DiffTypes for unary operands that have a different type.
167192
const auto &Result = Legality.canVectorize({Trunc64to8, Trunc32to8},

0 commit comments

Comments
 (0)