Skip to content

Commit 721f975

Browse files
author
Evgeniy Brevnov
committed
Use PassGate from LLVMContext if any otherwise global one
Differential Revision: https://reviews.llvm.org/D137149
1 parent 8e3545a commit 721f975

17 files changed

+82
-66
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -851,9 +851,10 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
851851
PrintPassOptions PrintPassOpts;
852852
PrintPassOpts.Indent = DebugPassStructure;
853853
PrintPassOpts.SkipAnalyses = DebugPassStructure;
854-
StandardInstrumentations SI(CodeGenOpts.DebugPassManager ||
855-
DebugPassStructure,
856-
/*VerifyEach*/ false, PrintPassOpts);
854+
StandardInstrumentations SI(
855+
TheModule->getContext(),
856+
(CodeGenOpts.DebugPassManager || DebugPassStructure),
857+
/*VerifyEach*/ false, PrintPassOpts);
857858
SI.registerCallbacks(PIC, &FAM);
858859
PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC);
859860

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,8 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
694694
llvm::PassInstrumentationCallbacks pic;
695695
llvm::PipelineTuningOptions pto;
696696
llvm::Optional<llvm::PGOOptions> pgoOpt;
697-
llvm::StandardInstrumentations si(opts.DebugPassManager);
697+
llvm::StandardInstrumentations si(
698+
llvmModule->getContext(), opts.DebugPassManager);
698699
si.registerCallbacks(pic, &fam);
699700
llvm::PassBuilder pb(tm.get(), pto, pgoOpt, &pic);
700701

llvm/include/llvm/IR/OptBisect.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class OptPassGate {
2929

3030
/// IRDescription is a textual description of the IR unit the pass is running
3131
/// over.
32-
virtual bool shouldRunPass(const Pass *P, StringRef IRDescription) {
32+
virtual bool shouldRunPass(const StringRef PassName,
33+
StringRef IRDescription) {
3334
return true;
3435
}
3536

@@ -55,7 +56,8 @@ class OptBisect : public OptPassGate {
5556
/// Checks the bisect limit to determine if the specified pass should run.
5657
///
5758
/// This forwards to checkPass().
58-
bool shouldRunPass(const Pass *P, StringRef IRDescription) override;
59+
bool shouldRunPass(const StringRef PassName,
60+
StringRef IRDescription) override;
5961

6062
/// isEnabled() should return true before calling shouldRunPass().
6163
bool isEnabled() const override { return BisectLimit != Disabled; }
@@ -89,7 +91,7 @@ class OptBisect : public OptPassGate {
8991

9092
/// Singleton instance of the OptBisect class, so multiple pass managers don't
9193
/// need to coordinate their uses of OptBisect.
92-
OptBisect &getOptBisector();
94+
OptPassGate &getGlobalPassGate();
9395

9496
} // end namespace llvm
9597

llvm/include/llvm/Passes/StandardInstrumentations.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ class OptNoneInstrumentation {
7474
bool shouldRun(StringRef PassID, Any IR);
7575
};
7676

77-
class OptBisectInstrumentation {
77+
class OptPassGateInstrumentation {
78+
LLVMContext &Context;
7879
bool HasWrittenIR = false;
79-
8080
public:
81-
OptBisectInstrumentation() = default;
81+
OptPassGateInstrumentation(LLVMContext &Context) : Context(Context) {}
82+
bool shouldRun(StringRef PassName, Any IR);
8283
void registerCallbacks(PassInstrumentationCallbacks &PIC);
8384
};
8485

@@ -528,7 +529,7 @@ class StandardInstrumentations {
528529
TimePassesHandler TimePasses;
529530
TimeProfilingPassesHandler TimeProfilingPasses;
530531
OptNoneInstrumentation OptNone;
531-
OptBisectInstrumentation OptBisect;
532+
OptPassGateInstrumentation OptPassGate;
532533
PreservedCFGCheckerInstrumentation PreservedCFGChecker;
533534
IRChangedPrinter PrintChangedIR;
534535
PseudoProbeVerifier PseudoProbeVerification;
@@ -540,7 +541,8 @@ class StandardInstrumentations {
540541
bool VerifyEach;
541542

542543
public:
543-
StandardInstrumentations(bool DebugLogging, bool VerifyEach = false,
544+
StandardInstrumentations(LLVMContext &Context, bool DebugLogging,
545+
bool VerifyEach = false,
544546
PrintPassOptions PrintPassOpts = PrintPassOptions());
545547

546548
// Register all the standard instrumentation callbacks. If \p FAM is nullptr

llvm/lib/Analysis/CallGraphSCCPass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,8 @@ static std::string getDescription(const CallGraphSCC &SCC) {
751751
bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const {
752752
OptPassGate &Gate =
753753
SCC.getCallGraph().getModule().getContext().getOptPassGate();
754-
return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(SCC));
754+
return Gate.isEnabled() &&
755+
!Gate.shouldRunPass(this->getPassName(), getDescription(SCC));
755756
}
756757

757758
char DummyCGSCCPass::ID = 0;

llvm/lib/Analysis/LoopPass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,8 @@ bool LoopPass::skipLoop(const Loop *L) const {
373373
return false;
374374
// Check the opt bisect limit.
375375
OptPassGate &Gate = F->getContext().getOptPassGate();
376-
if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(*L)))
376+
if (Gate.isEnabled() &&
377+
!Gate.shouldRunPass(this->getPassName(), getDescription(*L)))
377378
return true;
378379
// Check for the OptimizeNone attribute.
379380
if (F->hasOptNone()) {

llvm/lib/Analysis/RegionPass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ static std::string getDescription(const Region &R) {
283283
bool RegionPass::skipRegion(Region &R) const {
284284
Function &F = *R.getEntry()->getParent();
285285
OptPassGate &Gate = F.getContext().getOptPassGate();
286-
if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(R)))
286+
if (Gate.isEnabled() &&
287+
!Gate.shouldRunPass(this->getPassName(), getDescription(R)))
287288
return true;
288289

289290
if (F.hasOptNone()) {

llvm/lib/IR/LLVMContextImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void LLVMContextImpl::getSyncScopeNames(
240240
/// singleton OptBisect if not explicitly set.
241241
OptPassGate &LLVMContextImpl::getOptPassGate() const {
242242
if (!OPG)
243-
OPG = &getOptBisector();
243+
OPG = &getGlobalPassGate();
244244
return *OPG;
245245
}
246246

llvm/lib/IR/OptBisect.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020

2121
using namespace llvm;
2222

23+
static OptBisect &getOptBisector() {
24+
static OptBisect OptBisector;
25+
return OptBisector;
26+
}
27+
2328
static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
2429
cl::init(OptBisect::Disabled), cl::Optional,
2530
cl::cb<void, int>([](int Limit) {
26-
llvm::getOptBisector().setLimit(Limit);
31+
getOptBisector().setLimit(Limit);
2732
}),
2833
cl::desc("Maximum optimization to perform"));
2934

@@ -34,25 +39,16 @@ static void printPassMessage(const StringRef &Name, int PassNum,
3439
<< "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
3540
}
3641

37-
bool OptBisect::shouldRunPass(const Pass *P, StringRef IRDescription) {
38-
assert(isEnabled());
39-
40-
return checkPass(P->getPassName(), IRDescription);
41-
}
42-
43-
bool OptBisect::checkPass(const StringRef PassName,
44-
const StringRef TargetDesc) {
42+
bool OptBisect::shouldRunPass(const StringRef PassName,
43+
StringRef IRDescription) {
4544
assert(isEnabled());
4645

4746
int CurBisectNum = ++LastBisectNum;
4847
bool ShouldRun = (BisectLimit == -1 || CurBisectNum <= BisectLimit);
49-
printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
48+
printPassMessage(PassName, CurBisectNum, IRDescription, ShouldRun);
5049
return ShouldRun;
5150
}
5251

5352
const int OptBisect::Disabled;
5453

55-
OptBisect &llvm::getOptBisector() {
56-
static OptBisect OptBisector;
57-
return OptBisector;
58-
}
54+
OptPassGate &llvm::getGlobalPassGate() { return getOptBisector(); }

llvm/lib/IR/Pass.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ static std::string getDescription(const Module &M) {
6262

6363
bool ModulePass::skipModule(Module &M) const {
6464
OptPassGate &Gate = M.getContext().getOptPassGate();
65-
return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(M));
65+
return Gate.isEnabled() &&
66+
!Gate.shouldRunPass(this->getPassName(), getDescription(M));
6667
}
6768

6869
bool Pass::mustPreserveAnalysisID(char &AID) const {
@@ -172,7 +173,8 @@ static std::string getDescription(const Function &F) {
172173

173174
bool FunctionPass::skipFunction(const Function &F) const {
174175
OptPassGate &Gate = F.getContext().getOptPassGate();
175-
if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(F)))
176+
if (Gate.isEnabled() &&
177+
!Gate.shouldRunPass(this->getPassName(), getDescription(F)))
176178
return true;
177179

178180
if (F.hasOptNone()) {

0 commit comments

Comments
 (0)