Skip to content

Commit

Permalink
[NFC] Clean up PassBuilder
Browse files Browse the repository at this point in the history
Make DebugLogging a member variable so that users of PassBuilder don't
need to pass it around so much.

Move call to TargetMachine::registerPassBuilderCallbacks() within
PassBuilder so users don't need to remember to call it.

Reviewed By: asbirlea

Differential Revision: https://reviews.llvm.org/D90437
  • Loading branch information
aeubanks committed Oct 30, 2020
1 parent 10f2a0d commit 2e31727
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 217 deletions.
16 changes: 5 additions & 11 deletions clang/lib/CodeGen/BackendUtil.cpp
Expand Up @@ -1203,7 +1203,7 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
PassInstrumentationCallbacks PIC;
StandardInstrumentations SI(CodeGenOpts.DebugPassManager);
SI.registerCallbacks(PIC);
PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC);
PassBuilder PB(CodeGenOpts.DebugPassManager, TM.get(), PTO, PGOOpt, &PIC);

// Attempt to load pass plugins and register their callbacks with PB.
for (auto &PluginFN : CodeGenOpts.PassPlugins) {
Expand Down Expand Up @@ -1241,9 +1241,6 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
PB.registerLoopAnalyses(LAM);
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);

if (TM)
TM->registerPassBuilderCallbacks(PB, CodeGenOpts.DebugPassManager);

ModulePassManager MPM(CodeGenOpts.DebugPassManager);

if (!CodeGenOpts.DisableLLVMPasses) {
Expand Down Expand Up @@ -1280,7 +1277,7 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
if (PGOOpt && (PGOOpt->Action == PGOOptions::IRInstr ||
PGOOpt->Action == PGOOptions::IRUse))
PB.addPGOInstrPassesForO0(
MPM, CodeGenOpts.DebugPassManager,
MPM,
/* RunProfileGen */ (PGOOpt->Action == PGOOptions::IRInstr),
/* IsCS */ false, PGOOpt->ProfileFile,
PGOOpt->ProfileRemappingFile);
Expand Down Expand Up @@ -1407,18 +1404,15 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
});

if (IsThinLTO) {
MPM = PB.buildThinLTOPreLinkDefaultPipeline(
Level, CodeGenOpts.DebugPassManager);
MPM = PB.buildThinLTOPreLinkDefaultPipeline(Level);
MPM.addPass(CanonicalizeAliasesPass());
MPM.addPass(NameAnonGlobalPass());
} else if (IsLTO) {
MPM = PB.buildLTOPreLinkDefaultPipeline(Level,
CodeGenOpts.DebugPassManager);
MPM = PB.buildLTOPreLinkDefaultPipeline(Level);
MPM.addPass(CanonicalizeAliasesPass());
MPM.addPass(NameAnonGlobalPass());
} else {
MPM = PB.buildPerModuleDefaultPipeline(Level,
CodeGenOpts.DebugPassManager);
MPM = PB.buildPerModuleDefaultPipeline(Level);
}
}

Expand Down
82 changes: 29 additions & 53 deletions llvm/include/llvm/Passes/PassBuilder.h
Expand Up @@ -118,6 +118,7 @@ class PipelineTuningOptions {
/// of the built-in passes, and those may reference these members during
/// construction.
class PassBuilder {
bool DebugLogging;
TargetMachine *TM;
PipelineTuningOptions PTO;
Optional<PGOOptions> PGOOpt;
Expand Down Expand Up @@ -259,11 +260,10 @@ class PassBuilder {
unsigned getSizeLevel() const { return SizeLevel; }
};

explicit PassBuilder(TargetMachine *TM = nullptr,
explicit PassBuilder(bool DebugLogging = false, TargetMachine *TM = nullptr,
PipelineTuningOptions PTO = PipelineTuningOptions(),
Optional<PGOOptions> PGOOpt = None,
PassInstrumentationCallbacks *PIC = nullptr)
: TM(TM), PTO(PTO), PGOOpt(PGOOpt), PIC(PIC) {}
PassInstrumentationCallbacks *PIC = nullptr);

/// Cross register the analysis managers through their proxies.
///
Expand Down Expand Up @@ -321,8 +321,7 @@ class PassBuilder {
/// \p Phase indicates the current ThinLTO phase.
FunctionPassManager
buildFunctionSimplificationPipeline(OptimizationLevel Level,
ThinLTOPhase Phase,
bool DebugLogging = false);
ThinLTOPhase Phase);

/// Construct the core LLVM module canonicalization and simplification
/// pipeline.
Expand All @@ -339,16 +338,13 @@ class PassBuilder {
/// build them.
///
/// \p Phase indicates the current ThinLTO phase.
ModulePassManager
buildModuleSimplificationPipeline(OptimizationLevel Level,
ThinLTOPhase Phase,
bool DebugLogging = false);
ModulePassManager buildModuleSimplificationPipeline(OptimizationLevel Level,
ThinLTOPhase Phase);

/// Construct the module pipeline that performs inlining as well as
/// the inlining-driven cleanups.
ModuleInlinerWrapperPass buildInlinerPipeline(OptimizationLevel Level,
ThinLTOPhase Phase,
bool DebugLogging = false);
ThinLTOPhase Phase);

/// Construct the core LLVM module optimization pipeline.
///
Expand All @@ -364,7 +360,6 @@ class PassBuilder {
/// require some transformations for semantic reasons, they should explicitly
/// build them.
ModulePassManager buildModuleOptimizationPipeline(OptimizationLevel Level,
bool DebugLogging = false,
bool LTOPreLink = false);

/// Build a per-module default optimization pipeline.
Expand All @@ -379,7 +374,6 @@ class PassBuilder {
/// require some transformations for semantic reasons, they should explicitly
/// build them.
ModulePassManager buildPerModuleDefaultPipeline(OptimizationLevel Level,
bool DebugLogging = false,
bool LTOPreLink = false);

/// Build a pre-link, ThinLTO-targeting default optimization pipeline to
Expand All @@ -394,9 +388,7 @@ class PassBuilder {
/// only intended for use when attempting to optimize code. If frontends
/// require some transformations for semantic reasons, they should explicitly
/// build them.
ModulePassManager
buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
bool DebugLogging = false);
ModulePassManager buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level);

/// Build an ThinLTO default optimization pipeline to a pass manager.
///
Expand All @@ -410,7 +402,7 @@ class PassBuilder {
/// require some transformations for semantic reasons, they should explicitly
/// build them.
ModulePassManager
buildThinLTODefaultPipeline(OptimizationLevel Level, bool DebugLogging,
buildThinLTODefaultPipeline(OptimizationLevel Level,
const ModuleSummaryIndex *ImportSummary);

/// Build a pre-link, LTO-targeting default optimization pipeline to a pass
Expand All @@ -425,8 +417,7 @@ class PassBuilder {
/// only intended for use when attempting to optimize code. If frontends
/// require some transformations for semantic reasons, they should explicitly
/// build them.
ModulePassManager buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
bool DebugLogging = false);
ModulePassManager buildLTOPreLinkDefaultPipeline(OptimizationLevel Level);

/// Build an LTO default optimization pipeline to a pass manager.
///
Expand All @@ -440,7 +431,6 @@ class PassBuilder {
/// require some transformations for semantic reasons, they should explicitly
/// build them.
ModulePassManager buildLTODefaultPipeline(OptimizationLevel Level,
bool DebugLogging,
ModuleSummaryIndex *ExportSummary);

/// Build the default `AAManager` with the default alias analysis pipeline
Expand Down Expand Up @@ -487,8 +477,7 @@ class PassBuilder {
/// specifically want the pass to run under a adaptor directly. This is
/// preferred when a pipeline is largely of one type, but one or just a few
/// passes are of different types(See PassBuilder.cpp for examples).
Error parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
bool DebugLogging = false);
Error parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText);

/// {{@ Parse a textual pass pipeline description into a specific PassManager
///
Expand All @@ -497,12 +486,9 @@ class PassBuilder {
/// this is the valid pipeline text:
///
/// function(lpass)
Error parsePassPipeline(CGSCCPassManager &CGPM, StringRef PipelineText,
bool DebugLogging = false);
Error parsePassPipeline(FunctionPassManager &FPM, StringRef PipelineText,
bool DebugLogging = false);
Error parsePassPipeline(LoopPassManager &LPM, StringRef PipelineText,
bool DebugLogging = false);
Error parsePassPipeline(CGSCCPassManager &CGPM, StringRef PipelineText);
Error parsePassPipeline(FunctionPassManager &FPM, StringRef PipelineText);
Error parsePassPipeline(LoopPassManager &LPM, StringRef PipelineText);
/// @}}

/// Parse a textual alias analysis pipeline into the provided AA manager.
Expand Down Expand Up @@ -681,12 +667,10 @@ class PassBuilder {
bool DebugLogging)> &C);

/// Add PGOInstrumenation passes for O0 only.
void addPGOInstrPassesForO0(ModulePassManager &MPM, bool DebugLogging,
bool RunProfileGen, bool IsCS,
std::string ProfileFile,
void addPGOInstrPassesForO0(ModulePassManager &MPM, bool RunProfileGen,
bool IsCS, std::string ProfileFile,
std::string ProfileRemappingFile);


/// Returns PIC. External libraries can use this to register pass
/// instrumentation callbacks.
PassInstrumentationCallbacks *getPassInstrumentationCallbacks() const {
Expand All @@ -695,38 +679,30 @@ class PassBuilder {

private:
// O1 pass pipeline
FunctionPassManager buildO1FunctionSimplificationPipeline(
OptimizationLevel Level, ThinLTOPhase Phase, bool DebugLogging = false);
FunctionPassManager
buildO1FunctionSimplificationPipeline(OptimizationLevel Level,
ThinLTOPhase Phase);

static Optional<std::vector<PipelineElement>>
parsePipelineText(StringRef Text);

Error parseModulePass(ModulePassManager &MPM, const PipelineElement &E,
bool DebugLogging);
Error parseCGSCCPass(CGSCCPassManager &CGPM, const PipelineElement &E,
bool DebugLogging);
Error parseFunctionPass(FunctionPassManager &FPM, const PipelineElement &E,
bool DebugLogging);
Error parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
bool DebugLogging);
Error parseModulePass(ModulePassManager &MPM, const PipelineElement &E);
Error parseCGSCCPass(CGSCCPassManager &CGPM, const PipelineElement &E);
Error parseFunctionPass(FunctionPassManager &FPM, const PipelineElement &E);
Error parseLoopPass(LoopPassManager &LPM, const PipelineElement &E);
bool parseAAPassName(AAManager &AA, StringRef Name);

Error parseLoopPassPipeline(LoopPassManager &LPM,
ArrayRef<PipelineElement> Pipeline,
bool DebugLogging);
ArrayRef<PipelineElement> Pipeline);
Error parseFunctionPassPipeline(FunctionPassManager &FPM,
ArrayRef<PipelineElement> Pipeline,
bool DebugLogging);
ArrayRef<PipelineElement> Pipeline);
Error parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
ArrayRef<PipelineElement> Pipeline,
bool DebugLogging);
ArrayRef<PipelineElement> Pipeline);
Error parseModulePassPipeline(ModulePassManager &MPM,
ArrayRef<PipelineElement> Pipeline,
bool DebugLogging);
ArrayRef<PipelineElement> Pipeline);

void addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
OptimizationLevel Level, bool RunProfileGen, bool IsCS,
std::string ProfileFile,
void addPGOInstrPasses(ModulePassManager &MPM, OptimizationLevel Level,
bool RunProfileGen, bool IsCS, std::string ProfileFile,
std::string ProfileRemappingFile);
void invokePeepholeEPCallbacks(FunctionPassManager &, OptimizationLevel);

Expand Down
11 changes: 5 additions & 6 deletions llvm/lib/LTO/LTOBackend.cpp
Expand Up @@ -223,7 +223,7 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
PassInstrumentationCallbacks PIC;
StandardInstrumentations SI(Conf.DebugPassManager);
SI.registerCallbacks(PIC);
PassBuilder PB(TM, Conf.PTO, PGOOpt, &PIC);
PassBuilder PB(Conf.DebugPassManager, TM, Conf.PTO, PGOOpt, &PIC);
AAManager AA;

// Parse a custom AA pipeline if asked to.
Expand Down Expand Up @@ -270,10 +270,9 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
}

if (IsThinLTO)
MPM = PB.buildThinLTODefaultPipeline(OL, Conf.DebugPassManager,
ImportSummary);
MPM = PB.buildThinLTODefaultPipeline(OL, ImportSummary);
else
MPM = PB.buildLTODefaultPipeline(OL, Conf.DebugPassManager, ExportSummary);
MPM = PB.buildLTODefaultPipeline(OL, ExportSummary);
MPM.run(Mod, MAM);

// FIXME (davide): verify the output.
Expand All @@ -283,7 +282,7 @@ static void runNewPMCustomPasses(const Config &Conf, Module &Mod,
TargetMachine *TM, std::string PipelineDesc,
std::string AAPipelineDesc,
bool DisableVerify) {
PassBuilder PB(TM);
PassBuilder PB(Conf.DebugPassManager, TM);
AAManager AA;

// Parse a custom AA pipeline if asked to.
Expand Down Expand Up @@ -722,4 +721,4 @@ bool lto::loadReferencedModules(
OwnedImportsLifetimeManager.push_back(std::move(*MBOrErr));
}
return true;
}
}

0 comments on commit 2e31727

Please sign in to comment.