Skip to content

Commit ac75d32

Browse files
authored
[SandboxVec][VecUtils] Filter out instructions not in BB in VecUtils:getLowest() (llvm#124360)
This patch changes the functionality of `VecUtils::getLowest(Vals, BB)` such that it filters out any instructions in `Vals` that are not in BB. This is useful when Vals contains instructions from different BBs, because in that case we are only interested in one BB.
1 parent d910fbc commit ac75d32

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,27 @@ class VecUtils {
111111
return LowestI;
112112
}
113113
/// \Returns the lowest instruction in \p Vals, or nullptr if no instructions
114-
/// are found or if not in the same BB.
115-
static Instruction *getLowest(ArrayRef<Value *> Vals) {
116-
// Find the first Instruction in Vals.
117-
auto It = find_if(Vals, [](Value *V) { return isa<Instruction>(V); });
114+
/// are found. Skips instructions not in \p BB.
115+
static Instruction *getLowest(ArrayRef<Value *> Vals, BasicBlock *BB) {
116+
// Find the first Instruction in Vals that is also in `BB`.
117+
auto It = find_if(Vals, [BB](Value *V) {
118+
return isa<Instruction>(V) && cast<Instruction>(V)->getParent() == BB;
119+
});
118120
// If we couldn't find an instruction return nullptr.
119121
if (It == Vals.end())
120122
return nullptr;
121123
Instruction *FirstI = cast<Instruction>(*It);
122124
// Now look for the lowest instruction in Vals starting from one position
123125
// after FirstI.
124126
Instruction *LowestI = FirstI;
125-
auto *LowestBB = LowestI->getParent();
126127
for (auto *V : make_range(std::next(It), Vals.end())) {
127128
auto *I = dyn_cast<Instruction>(V);
128129
// Skip non-instructions.
129130
if (I == nullptr)
130131
continue;
131-
// If the instructions are in different BBs return nullptr.
132-
if (I->getParent() != LowestBB)
133-
return nullptr;
132+
// Skips instructions not in \p BB.
133+
if (I->getParent() != BB)
134+
continue;
134135
// If `LowestI` comes before `I` then `I` is the new lowest.
135136
if (LowestI->comesBefore(I))
136137
LowestI = I;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static SmallVector<Value *, 4> getOperand(ArrayRef<Value *> Bndl,
5454
/// of BB if no instruction found in \p Vals.
5555
static BasicBlock::iterator getInsertPointAfterInstrs(ArrayRef<Value *> Vals,
5656
BasicBlock *BB) {
57-
auto *BotI = VecUtils::getLastPHIOrSelf(VecUtils::getLowest(Vals));
57+
auto *BotI = VecUtils::getLastPHIOrSelf(VecUtils::getLowest(Vals, BB));
5858
if (BotI == nullptr)
5959
// We are using BB->begin() (or after PHIs) as the fallback insert point.
6060
return BB->empty()

llvm/test/Transforms/SandboxVectorizer/cross_bbs.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ define void @cross_bbs(ptr %ptr) {
88
; CHECK-NEXT: [[PTR1:%.*]] = getelementptr i8, ptr [[PTR]], i32 1
99
; CHECK-NEXT: [[L0:%.*]] = load i8, ptr [[PTR0]], align 1
1010
; CHECK-NEXT: [[L1:%.*]] = load i8, ptr [[PTR1]], align 1
11-
; CHECK-NEXT: [[PACK:%.*]] = insertelement <2 x i8> poison, i8 [[L0]], i32 0
12-
; CHECK-NEXT: [[PACK1:%.*]] = insertelement <2 x i8> [[PACK]], i8 [[L1]], i32 1
1311
; CHECK-NEXT: br label %[[BB:.*]]
1412
; CHECK: [[BB]]:
13+
; CHECK-NEXT: [[PACK:%.*]] = insertelement <2 x i8> poison, i8 [[L0]], i32 0
14+
; CHECK-NEXT: [[PACK1:%.*]] = insertelement <2 x i8> [[PACK]], i8 [[L1]], i32 1
1515
; CHECK-NEXT: store <2 x i8> [[PACK1]], ptr [[PTR0]], align 1
1616
; CHECK-NEXT: ret void
1717
;

llvm/test/Transforms/SandboxVectorizer/pack.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ define void @packFromOtherBB(ptr %ptr, i8 %val) {
5959
; CHECK-NEXT: [[ENTRY:.*]]:
6060
; CHECK-NEXT: [[ADD0:%.*]] = add i8 [[VAL]], 0
6161
; CHECK-NEXT: [[MUL1:%.*]] = mul i8 [[VAL]], 1
62-
; CHECK-NEXT: [[PACK:%.*]] = insertelement <2 x i8> poison, i8 [[ADD0]], i32 0
63-
; CHECK-NEXT: [[PACK1:%.*]] = insertelement <2 x i8> [[PACK]], i8 [[MUL1]], i32 1
6462
; CHECK-NEXT: br label %[[LOOP:.*]]
6563
; CHECK: [[LOOP]]:
6664
; CHECK-NEXT: [[PHI0:%.*]] = phi i8 [ 0, %[[ENTRY]] ], [ 1, %[[LOOP]] ]
6765
; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ 0, %[[ENTRY]] ], [ 1, %[[LOOP]] ]
66+
; CHECK-NEXT: [[PACK:%.*]] = insertelement <2 x i8> poison, i8 [[ADD0]], i32 0
67+
; CHECK-NEXT: [[PACK1:%.*]] = insertelement <2 x i8> [[PACK]], i8 [[MUL1]], i32 1
6868
; CHECK-NEXT: [[GEP0:%.*]] = getelementptr i8, ptr [[PTR]], i64 0
6969
; CHECK-NEXT: store <2 x i8> [[PACK1]], ptr [[GEP0]], align 1
7070
; CHECK-NEXT: br label %[[LOOP]]

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -461,24 +461,33 @@ define void @foo(i8 %v) {
461461

462462
// Check getLowest(ArrayRef<Value *>)
463463
SmallVector<sandboxir::Value *> C1Only({C1});
464-
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1Only), nullptr);
464+
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1Only, &BB), nullptr);
465+
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1Only, &BB0), nullptr);
465466
SmallVector<sandboxir::Value *> AOnly({IA});
466-
EXPECT_EQ(sandboxir::VecUtils::getLowest(AOnly), IA);
467+
EXPECT_EQ(sandboxir::VecUtils::getLowest(AOnly, &BB), IA);
468+
EXPECT_EQ(sandboxir::VecUtils::getLowest(AOnly, &BB0), nullptr);
467469
SmallVector<sandboxir::Value *> AC1({IA, C1});
468-
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1), IA);
470+
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1, &BB), IA);
471+
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1, &BB0), nullptr);
469472
SmallVector<sandboxir::Value *> C1A({C1, IA});
470-
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1A), IA);
473+
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1A, &BB), IA);
474+
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1A, &BB0), nullptr);
471475
SmallVector<sandboxir::Value *> AC1B({IA, C1, IB});
472-
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1B), IB);
476+
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1B, &BB), IB);
477+
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1B, &BB0), nullptr);
473478
SmallVector<sandboxir::Value *> ABC1({IA, IB, C1});
474-
EXPECT_EQ(sandboxir::VecUtils::getLowest(ABC1), IB);
479+
EXPECT_EQ(sandboxir::VecUtils::getLowest(ABC1, &BB), IB);
480+
EXPECT_EQ(sandboxir::VecUtils::getLowest(ABC1, &BB0), nullptr);
475481
SmallVector<sandboxir::Value *> AC1C2({IA, C1, C2});
476-
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1C2), IA);
482+
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1C2, &BB), IA);
483+
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1C2, &BB0), nullptr);
477484
SmallVector<sandboxir::Value *> C1C2C3({C1, C2, C3});
478-
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1C2C3), nullptr);
485+
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1C2C3, &BB), nullptr);
486+
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1C2C3, &BB0), nullptr);
479487

480488
SmallVector<sandboxir::Value *> DiffBBs({BB0I, IA});
481-
EXPECT_EQ(sandboxir::VecUtils::getLowest(DiffBBs), nullptr);
489+
EXPECT_EQ(sandboxir::VecUtils::getLowest(DiffBBs, &BB0), BB0I);
490+
EXPECT_EQ(sandboxir::VecUtils::getLowest(DiffBBs, &BB), IA);
482491
}
483492

484493
TEST_F(VecUtilsTest, GetLastPHIOrSelf) {

0 commit comments

Comments
 (0)