From bed6a6e7e06bd3a5a5af15d7a6935d3ba2dc0da4 Mon Sep 17 00:00:00 2001 From: Johannes Doerfert Date: Tue, 13 Jun 2023 16:39:49 -0700 Subject: [PATCH] [Attributor][NFC] Make the MustBeExecutedContextExplorer optional For a lightweight pass we do not want to instantiate or use the MustBeExecutedContextExplorer. This simply allows such a configuration. While at it, the explorer is now allocated with the bump allocator. --- llvm/include/llvm/Transforms/IPO/Attributor.h | 40 ++++++++++--------- llvm/lib/Transforms/IPO/Attributor.cpp | 8 ++-- .../Transforms/IPO/AttributorAttributes.cpp | 14 ++++--- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/llvm/include/llvm/Transforms/IPO/Attributor.h b/llvm/include/llvm/Transforms/IPO/Attributor.h index 90ed050dde9b..365301a7e5e9 100644 --- a/llvm/include/llvm/Transforms/IPO/Attributor.h +++ b/llvm/include/llvm/Transforms/IPO/Attributor.h @@ -1164,24 +1164,26 @@ constexpr bool AnalysisGetter::HasLegacyWrapper< /// instance down in the abstract attributes. struct InformationCache { InformationCache(const Module &M, AnalysisGetter &AG, - BumpPtrAllocator &Allocator, SetVector *CGSCC) - : DL(M.getDataLayout()), Allocator(Allocator), - Explorer( - /* ExploreInterBlock */ true, /* ExploreCFGForward */ true, - /* ExploreCFGBackward */ true, - /* LIGetter */ - [&](const Function &F) { return AG.getAnalysis(F); }, - /* DTGetter */ - [&](const Function &F) { - return AG.getAnalysis(F); - }, - /* PDTGetter */ - [&](const Function &F) { - return AG.getAnalysis(F); - }), - AG(AG), TargetTriple(M.getTargetTriple()) { + BumpPtrAllocator &Allocator, SetVector *CGSCC, + bool UseExplorer = true) + : DL(M.getDataLayout()), Allocator(Allocator), AG(AG), + TargetTriple(M.getTargetTriple()) { if (CGSCC) initializeModuleSlice(*CGSCC); + if (UseExplorer) + Explorer = new (Allocator) MustBeExecutedContextExplorer( + /* ExploreInterBlock */ true, /* ExploreCFGForward */ true, + /* ExploreCFGBackward */ true, + /* LIGetter */ + [&](const Function &F) { return AG.getAnalysis(F); }, + /* DTGetter */ + [&](const Function &F) { + return AG.getAnalysis(F); + }, + /* PDTGetter */ + [&](const Function &F) { + return AG.getAnalysis(F); + }); } ~InformationCache() { @@ -1193,6 +1195,8 @@ struct InformationCache { using AA::InstExclusionSetTy; for (auto *BES : BESets) BES->~InstExclusionSetTy(); + if (Explorer) + Explorer->~MustBeExecutedContextExplorer(); } /// Apply \p CB to all uses of \p F. If \p LookThroughConstantExprUses is @@ -1275,7 +1279,7 @@ struct InformationCache { } /// Return MustBeExecutedContextExplorer - MustBeExecutedContextExplorer &getMustBeExecutedContextExplorer() { + MustBeExecutedContextExplorer *getMustBeExecutedContextExplorer() { return Explorer; } @@ -1381,7 +1385,7 @@ private: BumpPtrAllocator &Allocator; /// MustBeExecutedContextExplorer - MustBeExecutedContextExplorer Explorer; + MustBeExecutedContextExplorer *Explorer = nullptr; /// A map with knowledge retained in `llvm.assume` instructions. RetainedKnowledgeMap KnowledgeMap; diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp index df8cb5c3ee90..5e3c2f92730d 100644 --- a/llvm/lib/Transforms/IPO/Attributor.cpp +++ b/llvm/lib/Transforms/IPO/Attributor.cpp @@ -1204,11 +1204,13 @@ bool IRPosition::getAttrsFromAssumes(Attribute::AttrKind AK, LLVMContext &Ctx = AssociatedValue.getContext(); unsigned AttrsSize = Attrs.size(); - MustBeExecutedContextExplorer &Explorer = + MustBeExecutedContextExplorer *Explorer = A.getInfoCache().getMustBeExecutedContextExplorer(); - auto EIt = Explorer.begin(getCtxI()), EEnd = Explorer.end(getCtxI()); + if (!Explorer) + return false; + auto EIt = Explorer->begin(getCtxI()), EEnd = Explorer->end(getCtxI()); for (const auto &It : A2K) - if (Explorer.findInContextOf(It.first, EIt, EEnd)) + if (Explorer->findInContextOf(It.first, EIt, EEnd)) Attrs.push_back(Attribute::get(Ctx, AK, It.second.Max)); return AttrsSize != Attrs.size(); } diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp index 75ddda689dfe..58297b5fdc9c 100644 --- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp +++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp @@ -607,10 +607,12 @@ static void followUsesInMBEC(AAType &AA, Attributor &A, StateType &S, for (const Use &U : AA.getIRPosition().getAssociatedValue().uses()) Uses.insert(&U); - MustBeExecutedContextExplorer &Explorer = + MustBeExecutedContextExplorer *Explorer = A.getInfoCache().getMustBeExecutedContextExplorer(); + if (!Explorer) + return; - followUsesInContext(AA, A, Explorer, &CtxI, Uses, S); + followUsesInContext(AA, A, *Explorer, &CtxI, Uses, S); if (S.isAtFixpoint()) return; @@ -655,7 +657,7 @@ static void followUsesInMBEC(AAType &AA, Attributor &A, StateType &S, // } // } - Explorer.checkForAllContext(&CtxI, Pred); + Explorer->checkForAllContext(&CtxI, Pred); for (const BranchInst *Br : BrInsts) { StateType ParentState; @@ -667,7 +669,7 @@ static void followUsesInMBEC(AAType &AA, Attributor &A, StateType &S, StateType ChildState; size_t BeforeSize = Uses.size(); - followUsesInContext(AA, A, Explorer, &BB->front(), Uses, ChildState); + followUsesInContext(AA, A, *Explorer, &BB->front(), Uses, ChildState); // Erase uses which only appear in the child. for (auto It = Uses.begin() + BeforeSize; It != Uses.end();) @@ -7022,7 +7024,7 @@ ChangeStatus AAHeapToStackFunction::updateImpl(Attributor &A) { const auto &LivenessAA = A.getAAFor(*this, IRPosition::function(*F), DepClassTy::NONE); - MustBeExecutedContextExplorer &Explorer = + MustBeExecutedContextExplorer *Explorer = A.getInfoCache().getMustBeExecutedContextExplorer(); bool StackIsAccessibleByOtherThreads = @@ -7148,7 +7150,7 @@ ChangeStatus AAHeapToStackFunction::updateImpl(Attributor &A) { return false; } Instruction *CtxI = isa(AI.CB) ? AI.CB : AI.CB->getNextNode(); - if (!Explorer.findInContextOf(UniqueFree, CtxI)) { + if (!Explorer || !Explorer->findInContextOf(UniqueFree, CtxI)) { LLVM_DEBUG( dbgs() << "[H2S] unique free call might not be executed with the allocation "