Skip to content

Commit 47d9473

Browse files
authored
[SandboxVec][BottomUpVec] Fix ownership of Legality (#143018)
Fix the ownership of `Legality` member variable of BottomUpVec. It should get created in runOnFunction() and get destroyed when the function returns.
1 parent 7db847d commit 47d9473

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ namespace llvm::sandboxir {
3434
/// transaction, depending on the cost.
3535
class BottomUpVec final : public RegionPass {
3636
bool Change = false;
37-
std::unique_ptr<LegalityAnalysis> Legality;
3837
/// The original instructions that are potentially dead after vectorization.
3938
DenseSet<Instruction *> DeadInstrCandidates;
4039
/// Maps scalars to vectors.
@@ -88,12 +87,12 @@ class BottomUpVec final : public RegionPass {
8887
/// Recursively try to vectorize \p Bndl and its operands. This populates the
8988
/// `Actions` vector.
9089
Action *vectorizeRec(ArrayRef<Value *> Bndl, ArrayRef<Value *> UserBndl,
91-
unsigned Depth);
90+
unsigned Depth, LegalityAnalysis &Legality);
9291
/// Generate vector instructions based on `Actions` and return the last vector
9392
/// created.
9493
Value *emitVectors();
9594
/// Entry point for vectorization starting from \p Seeds.
96-
bool tryVectorize(ArrayRef<Value *> Seeds);
95+
bool tryVectorize(ArrayRef<Value *> Seeds, LegalityAnalysis &Legality);
9796

9897
public:
9998
BottomUpVec() : RegionPass("bottom-up-vec") {}

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,14 @@ void BottomUpVec::collectPotentiallyDeadInstrs(ArrayRef<Value *> Bndl) {
278278
}
279279

280280
Action *BottomUpVec::vectorizeRec(ArrayRef<Value *> Bndl,
281-
ArrayRef<Value *> UserBndl, unsigned Depth) {
281+
ArrayRef<Value *> UserBndl, unsigned Depth,
282+
LegalityAnalysis &Legality) {
282283
bool StopForDebug =
283284
DebugBndlCnt++ >= StopBundle && StopBundle != StopBundleDisabled;
284285
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "canVectorize() Bundle:\n";
285286
VecUtils::dump(Bndl));
286-
const auto &LegalityRes = StopForDebug ? Legality->getForcedPackForDebugging()
287-
: Legality->canVectorize(Bndl);
287+
const auto &LegalityRes = StopForDebug ? Legality.getForcedPackForDebugging()
288+
: Legality.canVectorize(Bndl);
288289
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Legality: " << LegalityRes << "\n");
289290
auto ActionPtr =
290291
std::make_unique<Action>(&LegalityRes, Bndl, UserBndl, Depth);
@@ -297,14 +298,16 @@ Action *BottomUpVec::vectorizeRec(ArrayRef<Value *> Bndl,
297298
break;
298299
case Instruction::Opcode::Store: {
299300
// Don't recurse towards the pointer operand.
300-
Action *OpA = vectorizeRec(getOperand(Bndl, 0), Bndl, Depth + 1);
301+
Action *OpA =
302+
vectorizeRec(getOperand(Bndl, 0), Bndl, Depth + 1, Legality);
301303
Operands.push_back(OpA);
302304
break;
303305
}
304306
default:
305307
// Visit all operands.
306308
for (auto OpIdx : seq<unsigned>(I->getNumOperands())) {
307-
Action *OpA = vectorizeRec(getOperand(Bndl, OpIdx), Bndl, Depth + 1);
309+
Action *OpA =
310+
vectorizeRec(getOperand(Bndl, OpIdx), Bndl, Depth + 1, Legality);
308311
Operands.push_back(OpA);
309312
}
310313
break;
@@ -476,16 +479,17 @@ Value *BottomUpVec::emitVectors() {
476479
return NewVec;
477480
}
478481

479-
bool BottomUpVec::tryVectorize(ArrayRef<Value *> Bndl) {
482+
bool BottomUpVec::tryVectorize(ArrayRef<Value *> Bndl,
483+
LegalityAnalysis &Legality) {
480484
Change = false;
481485
if (LLVM_UNLIKELY(BottomUpInvocationCnt++ >= StopAt &&
482486
StopAt != StopAtDisabled))
483487
return false;
484488
DeadInstrCandidates.clear();
485-
Legality->clear();
489+
Legality.clear();
486490
Actions.clear();
487491
DebugBndlCnt = 0;
488-
vectorizeRec(Bndl, {}, /*Depth=*/0);
492+
vectorizeRec(Bndl, {}, /*Depth=*/0, Legality);
489493
LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "BottomUpVec: Vectorization Actions:\n";
490494
Actions.dump());
491495
emitVectors();
@@ -498,16 +502,16 @@ bool BottomUpVec::runOnRegion(Region &Rgn, const Analyses &A) {
498502
assert(SeedSlice.size() >= 2 && "Bad slice!");
499503
Function &F = *SeedSlice[0]->getParent()->getParent();
500504
IMaps = std::make_unique<InstrMaps>();
501-
Legality = std::make_unique<LegalityAnalysis>(
502-
A.getAA(), A.getScalarEvolution(), F.getParent()->getDataLayout(),
503-
F.getContext(), *IMaps);
505+
LegalityAnalysis Legality(A.getAA(), A.getScalarEvolution(),
506+
F.getParent()->getDataLayout(), F.getContext(),
507+
*IMaps);
504508

505509
// TODO: Refactor to remove the unnecessary copy to SeedSliceVals.
506510
SmallVector<Value *> SeedSliceVals(SeedSlice.begin(), SeedSlice.end());
507511
// Try to vectorize starting from the seed slice. The returned value
508512
// is true if we found vectorizable code and generated some vector
509513
// code for it. It does not mean that the code is profitable.
510-
return tryVectorize(SeedSliceVals);
514+
return tryVectorize(SeedSliceVals, Legality);
511515
}
512516

513517
} // namespace sandboxir

0 commit comments

Comments
 (0)