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

+4-3
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

+2-1
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

+5-3
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

+7-5
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

+2-1
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

+2-1
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

+2-1
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

+1-1
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

+10-14
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

+4-2
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()) {

llvm/lib/LTO/LTOBackend.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
256256
ModuleAnalysisManager MAM;
257257

258258
PassInstrumentationCallbacks PIC;
259-
StandardInstrumentations SI(Conf.DebugPassManager);
259+
StandardInstrumentations SI(Mod.getContext(), Conf.DebugPassManager);
260260
SI.registerCallbacks(PIC, &FAM);
261261
PassBuilder PB(TM, Conf.PTO, PGOOpt, &PIC);
262262

llvm/lib/LTO/ThinLTOCodeGenerator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ static void optimizeModule(Module &TheModule, TargetMachine &TM,
244244
ModuleAnalysisManager MAM;
245245

246246
PassInstrumentationCallbacks PIC;
247-
StandardInstrumentations SI(DebugPassManager);
247+
StandardInstrumentations SI(TheModule.getContext(), DebugPassManager);
248248
SI.registerCallbacks(PIC, &FAM);
249249
PipelineTuningOptions PTO;
250250
PTO.LoopVectorization = true;

llvm/lib/Passes/PassBuilderBindings.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ LLVMErrorRef LLVMRunPasses(LLVMModuleRef M, const char *Passes,
6565
PB.registerModuleAnalyses(MAM);
6666
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
6767

68-
StandardInstrumentations SI(Debug, VerifyEach);
68+
StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach);
6969
SI.registerCallbacks(PIC, &FAM);
7070
ModulePassManager MPM;
7171
if (VerifyEach) {

llvm/lib/Passes/StandardInstrumentations.cpp

+33-22
Original file line numberDiff line numberDiff line change
@@ -767,27 +767,35 @@ bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) {
767767
return ShouldRun;
768768
}
769769

770-
void OptBisectInstrumentation::registerCallbacks(
770+
bool OptPassGateInstrumentation::shouldRun(StringRef PassName, Any IR) {
771+
if (isIgnored(PassName))
772+
return true;
773+
774+
bool ShouldRun =
775+
Context.getOptPassGate().shouldRunPass(PassName, getIRName(IR));
776+
if (!ShouldRun && !this->HasWrittenIR && !OptBisectPrintIRPath.empty()) {
777+
// FIXME: print IR if limit is higher than number of opt-bisect
778+
// invocations
779+
this->HasWrittenIR = true;
780+
const Module *M = unwrapModule(IR, /*Force=*/true);
781+
assert((M && &M->getContext() == &Context) && "Missing/Mismatching Module");
782+
std::error_code EC;
783+
raw_fd_ostream OS(OptBisectPrintIRPath, EC);
784+
if (EC)
785+
report_fatal_error(errorCodeToError(EC));
786+
M->print(OS, nullptr);
787+
}
788+
return ShouldRun;
789+
}
790+
791+
void OptPassGateInstrumentation::registerCallbacks(
771792
PassInstrumentationCallbacks &PIC) {
772-
if (!getOptBisector().isEnabled())
793+
OptPassGate &PassGate = Context.getOptPassGate();
794+
if (!PassGate.isEnabled())
773795
return;
774-
PIC.registerShouldRunOptionalPassCallback([this](StringRef PassID, Any IR) {
775-
if (isIgnored(PassID))
776-
return true;
777-
bool ShouldRun = getOptBisector().checkPass(PassID, getIRName(IR));
778-
if (!ShouldRun && !this->HasWrittenIR && !OptBisectPrintIRPath.empty()) {
779-
// FIXME: print IR if limit is higher than number of opt-bisect
780-
// invocations
781-
this->HasWrittenIR = true;
782-
const Module *M = unwrapModule(IR, /*Force=*/true);
783-
assert(M && "expected Module");
784-
std::error_code EC;
785-
raw_fd_ostream OS(OptBisectPrintIRPath, EC);
786-
if (EC)
787-
report_fatal_error(errorCodeToError(EC));
788-
M->print(OS, nullptr);
789-
}
790-
return ShouldRun;
796+
797+
PIC.registerShouldRunOptionalPassCallback([this](StringRef PassName, Any IR) {
798+
return this->shouldRun(PassName, IR);
791799
});
792800
}
793801

@@ -2037,8 +2045,11 @@ void DotCfgChangeReporter::registerCallbacks(
20372045
}
20382046

20392047
StandardInstrumentations::StandardInstrumentations(
2040-
bool DebugLogging, bool VerifyEach, PrintPassOptions PrintPassOpts)
2041-
: PrintPass(DebugLogging, PrintPassOpts), OptNone(DebugLogging),
2048+
LLVMContext &Context, bool DebugLogging, bool VerifyEach,
2049+
PrintPassOptions PrintPassOpts)
2050+
: PrintPass(DebugLogging, PrintPassOpts),
2051+
OptNone(DebugLogging),
2052+
OptPassGate(Context),
20422053
PrintChangedIR(PrintChanged == ChangePrinter::Verbose),
20432054
PrintChangedDiff(PrintChanged == ChangePrinter::DiffVerbose ||
20442055
PrintChanged == ChangePrinter::ColourDiffVerbose,
@@ -2099,7 +2110,7 @@ void StandardInstrumentations::registerCallbacks(
20992110
PrintPass.registerCallbacks(PIC);
21002111
TimePasses.registerCallbacks(PIC);
21012112
OptNone.registerCallbacks(PIC);
2102-
OptBisect.registerCallbacks(PIC);
2113+
OptPassGate.registerCallbacks(PIC);
21032114
if (FAM)
21042115
PreservedCFGChecker.registerCallbacks(PIC, *FAM);
21052116
PrintChangedIR.registerCallbacks(PIC);

llvm/tools/opt/NewPMDriver.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
354354
PrintPassOptions PrintPassOpts;
355355
PrintPassOpts.Verbose = DebugPM == DebugLogging::Verbose;
356356
PrintPassOpts.SkipAnalyses = DebugPM == DebugLogging::Quiet;
357-
StandardInstrumentations SI(DebugPM != DebugLogging::None, VerifyEachPass,
358-
PrintPassOpts);
357+
StandardInstrumentations SI(M.getContext(), DebugPM != DebugLogging::None,
358+
VerifyEachPass, PrintPassOpts);
359359
SI.registerCallbacks(PIC, &FAM);
360360
DebugifyEachInstrumentation Debugify;
361361
DebugifyStatsMap DIStatsMap;

llvm/unittests/IR/LegacyPassManagerTest.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,8 @@ namespace llvm {
359359
struct CustomOptPassGate : public OptPassGate {
360360
bool Skip;
361361
CustomOptPassGate(bool Skip) : Skip(Skip) { }
362-
bool shouldRunPass(const Pass *P, StringRef IRDescription) override {
363-
if (P->getPassKind() == PT_Module)
364-
return !Skip;
365-
return OptPassGate::shouldRunPass(P, IRDescription);
362+
bool shouldRunPass(const StringRef PassName, StringRef IRDescription) override {
363+
return !Skip;
366364
}
367365
bool isEnabled() const override { return true; }
368366
};

llvm/unittests/IR/PassManagerTest.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ TEST_F(PassManagerTest, FunctionPassCFGChecker) {
826826
FunctionAnalysisManager FAM;
827827
FunctionPassManager FPM;
828828
PassInstrumentationCallbacks PIC;
829-
StandardInstrumentations SI(/*DebugLogging*/ true);
829+
StandardInstrumentations SI(M->getContext(), /*DebugLogging*/ true);
830830
SI.registerCallbacks(PIC, &FAM);
831831
FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
832832
FAM.registerPass([&] { return DominatorTreeAnalysis(); });
@@ -872,7 +872,7 @@ TEST_F(PassManagerTest, FunctionPassCFGCheckerInvalidateAnalysis) {
872872
FunctionAnalysisManager FAM;
873873
FunctionPassManager FPM;
874874
PassInstrumentationCallbacks PIC;
875-
StandardInstrumentations SI(/*DebugLogging*/ true);
875+
StandardInstrumentations SI(M->getContext(), /*DebugLogging*/ true);
876876
SI.registerCallbacks(PIC, &FAM);
877877
FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
878878
FAM.registerPass([&] { return DominatorTreeAnalysis(); });
@@ -937,7 +937,7 @@ TEST_F(PassManagerTest, FunctionPassCFGCheckerWrapped) {
937937
FunctionAnalysisManager FAM;
938938
FunctionPassManager FPM;
939939
PassInstrumentationCallbacks PIC;
940-
StandardInstrumentations SI(/*DebugLogging*/ true);
940+
StandardInstrumentations SI(M->getContext(), /*DebugLogging*/ true);
941941
SI.registerCallbacks(PIC, &FAM);
942942
FAM.registerPass([&] { return PassInstrumentationAnalysis(&PIC); });
943943
FAM.registerPass([&] { return DominatorTreeAnalysis(); });

0 commit comments

Comments
 (0)