Skip to content

[SandboxVec][VecUtils] Filter out instructions not in BB in VecUtils:getLowest() #124360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,27 @@ class VecUtils {
return LowestI;
}
/// \Returns the lowest instruction in \p Vals, or nullptr if no instructions
/// are found or if not in the same BB.
static Instruction *getLowest(ArrayRef<Value *> Vals) {
// Find the first Instruction in Vals.
auto It = find_if(Vals, [](Value *V) { return isa<Instruction>(V); });
/// are found. Skips instructions not in \p BB.
static Instruction *getLowest(ArrayRef<Value *> Vals, BasicBlock *BB) {
// Find the first Instruction in Vals that is also in `BB`.
auto It = find_if(Vals, [BB](Value *V) {
return isa<Instruction>(V) && cast<Instruction>(V)->getParent() == BB;
});
// If we couldn't find an instruction return nullptr.
if (It == Vals.end())
return nullptr;
Instruction *FirstI = cast<Instruction>(*It);
// Now look for the lowest instruction in Vals starting from one position
// after FirstI.
Instruction *LowestI = FirstI;
auto *LowestBB = LowestI->getParent();
for (auto *V : make_range(std::next(It), Vals.end())) {
auto *I = dyn_cast<Instruction>(V);
// Skip non-instructions.
if (I == nullptr)
continue;
// If the instructions are in different BBs return nullptr.
if (I->getParent() != LowestBB)
return nullptr;
// Skips instructions not in \p BB.
if (I->getParent() != BB)
continue;
// If `LowestI` comes before `I` then `I` is the new lowest.
if (LowestI->comesBefore(I))
LowestI = I;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static SmallVector<Value *, 4> getOperand(ArrayRef<Value *> Bndl,
/// of BB if no instruction found in \p Vals.
static BasicBlock::iterator getInsertPointAfterInstrs(ArrayRef<Value *> Vals,
BasicBlock *BB) {
auto *BotI = VecUtils::getLastPHIOrSelf(VecUtils::getLowest(Vals));
auto *BotI = VecUtils::getLastPHIOrSelf(VecUtils::getLowest(Vals, BB));
if (BotI == nullptr)
// We are using BB->begin() (or after PHIs) as the fallback insert point.
return BB->empty()
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/SandboxVectorizer/cross_bbs.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ define void @cross_bbs(ptr %ptr) {
; CHECK-NEXT: [[PTR1:%.*]] = getelementptr i8, ptr [[PTR]], i32 1
; CHECK-NEXT: [[L0:%.*]] = load i8, ptr [[PTR0]], align 1
; CHECK-NEXT: [[L1:%.*]] = load i8, ptr [[PTR1]], align 1
; CHECK-NEXT: [[PACK:%.*]] = insertelement <2 x i8> poison, i8 [[L0]], i32 0
; CHECK-NEXT: [[PACK1:%.*]] = insertelement <2 x i8> [[PACK]], i8 [[L1]], i32 1
; CHECK-NEXT: br label %[[BB:.*]]
; CHECK: [[BB]]:
; CHECK-NEXT: [[PACK:%.*]] = insertelement <2 x i8> poison, i8 [[L0]], i32 0
; CHECK-NEXT: [[PACK1:%.*]] = insertelement <2 x i8> [[PACK]], i8 [[L1]], i32 1
; CHECK-NEXT: store <2 x i8> [[PACK1]], ptr [[PTR0]], align 1
; CHECK-NEXT: ret void
;
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/SandboxVectorizer/pack.ll
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ define void @packFromOtherBB(ptr %ptr, i8 %val) {
; CHECK-NEXT: [[ENTRY:.*]]:
; CHECK-NEXT: [[ADD0:%.*]] = add i8 [[VAL]], 0
; CHECK-NEXT: [[MUL1:%.*]] = mul i8 [[VAL]], 1
; CHECK-NEXT: [[PACK:%.*]] = insertelement <2 x i8> poison, i8 [[ADD0]], i32 0
; CHECK-NEXT: [[PACK1:%.*]] = insertelement <2 x i8> [[PACK]], i8 [[MUL1]], i32 1
; CHECK-NEXT: br label %[[LOOP:.*]]
; CHECK: [[LOOP]]:
; CHECK-NEXT: [[PHI0:%.*]] = phi i8 [ 0, %[[ENTRY]] ], [ 1, %[[LOOP]] ]
; CHECK-NEXT: [[PHI1:%.*]] = phi i8 [ 0, %[[ENTRY]] ], [ 1, %[[LOOP]] ]
; CHECK-NEXT: [[PACK:%.*]] = insertelement <2 x i8> poison, i8 [[ADD0]], i32 0
; CHECK-NEXT: [[PACK1:%.*]] = insertelement <2 x i8> [[PACK]], i8 [[MUL1]], i32 1
; CHECK-NEXT: [[GEP0:%.*]] = getelementptr i8, ptr [[PTR]], i64 0
; CHECK-NEXT: store <2 x i8> [[PACK1]], ptr [[GEP0]], align 1
; CHECK-NEXT: br label %[[LOOP]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,24 +461,33 @@ define void @foo(i8 %v) {

// Check getLowest(ArrayRef<Value *>)
SmallVector<sandboxir::Value *> C1Only({C1});
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1Only), nullptr);
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1Only, &BB), nullptr);
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1Only, &BB0), nullptr);
SmallVector<sandboxir::Value *> AOnly({IA});
EXPECT_EQ(sandboxir::VecUtils::getLowest(AOnly), IA);
EXPECT_EQ(sandboxir::VecUtils::getLowest(AOnly, &BB), IA);
EXPECT_EQ(sandboxir::VecUtils::getLowest(AOnly, &BB0), nullptr);
SmallVector<sandboxir::Value *> AC1({IA, C1});
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1), IA);
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1, &BB), IA);
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1, &BB0), nullptr);
SmallVector<sandboxir::Value *> C1A({C1, IA});
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1A), IA);
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1A, &BB), IA);
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1A, &BB0), nullptr);
SmallVector<sandboxir::Value *> AC1B({IA, C1, IB});
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1B), IB);
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1B, &BB), IB);
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1B, &BB0), nullptr);
SmallVector<sandboxir::Value *> ABC1({IA, IB, C1});
EXPECT_EQ(sandboxir::VecUtils::getLowest(ABC1), IB);
EXPECT_EQ(sandboxir::VecUtils::getLowest(ABC1, &BB), IB);
EXPECT_EQ(sandboxir::VecUtils::getLowest(ABC1, &BB0), nullptr);
SmallVector<sandboxir::Value *> AC1C2({IA, C1, C2});
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1C2), IA);
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1C2, &BB), IA);
EXPECT_EQ(sandboxir::VecUtils::getLowest(AC1C2, &BB0), nullptr);
SmallVector<sandboxir::Value *> C1C2C3({C1, C2, C3});
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1C2C3), nullptr);
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1C2C3, &BB), nullptr);
EXPECT_EQ(sandboxir::VecUtils::getLowest(C1C2C3, &BB0), nullptr);

SmallVector<sandboxir::Value *> DiffBBs({BB0I, IA});
EXPECT_EQ(sandboxir::VecUtils::getLowest(DiffBBs), nullptr);
EXPECT_EQ(sandboxir::VecUtils::getLowest(DiffBBs, &BB0), BB0I);
EXPECT_EQ(sandboxir::VecUtils::getLowest(DiffBBs, &BB), IA);
}

TEST_F(VecUtilsTest, GetLastPHIOrSelf) {
Expand Down
Loading