Skip to content

Commit

Permalink
[PassBuilder] Support O0 in default pipelines
Browse files Browse the repository at this point in the history
The default and pre-link pipeline builders currently require you to
call a separate method for optimization level O0, even though they
have perfectly well-defined O0 optimization pipelines.

Accept O0 optimization level and call buildO0DefaultPipeline()
internally, so all consumers don't need to repeat this.

Differential Revision: https://reviews.llvm.org/D146200
  • Loading branch information
nikic committed Mar 17, 2023
1 parent 951a980 commit a8f6b57
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 49 deletions.
4 changes: 1 addition & 3 deletions clang/lib/CodeGen/BackendUtil.cpp
Expand Up @@ -973,9 +973,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
MPM.addPass(InstrProfiling(*Options, false));
});

if (CodeGenOpts.OptimizationLevel == 0) {
MPM = PB.buildO0DefaultPipeline(Level, IsLTO || IsThinLTO);
} else if (IsThinLTO) {
if (IsThinLTO) {
MPM = PB.buildThinLTOPreLinkDefaultPipeline(Level);
} else if (IsLTO) {
MPM = PB.buildLTOPreLinkDefaultPipeline(Level);
Expand Down
6 changes: 1 addition & 5 deletions clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
Expand Up @@ -103,11 +103,7 @@ static void RunOptimizationPasses(raw_ostream &OS, Module &M,
PB.registerLoopAnalyses(LAM);
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);

ModulePassManager MPM;
if (OL == OptimizationLevel::O0)
MPM = PB.buildO0DefaultPipeline(OL);
else
MPM = PB.buildPerModuleDefaultPipeline(OL);
ModulePassManager MPM = PB.buildPerModuleDefaultPipeline(OL);
MPM.addPass(PrintModulePass(OS));

MPM.run(M, MAM);
Expand Down
5 changes: 1 addition & 4 deletions flang/lib/Frontend/FrontendActions.cpp
Expand Up @@ -726,10 +726,7 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {

// Create the pass manager.
llvm::ModulePassManager mpm;
if (opts.OptimizationLevel == 0)
mpm = pb.buildO0DefaultPipeline(level, opts.PrepareForFullLTO ||
opts.PrepareForThinLTO);
else if (opts.PrepareForFullLTO)
if (opts.PrepareForFullLTO)
mpm = pb.buildLTOPreLinkDefaultPipeline(level);
else if (opts.PrepareForThinLTO)
mpm = pb.buildThinLTOPreLinkDefaultPipeline(level);
Expand Down
15 changes: 0 additions & 15 deletions llvm/include/llvm/Passes/PassBuilder.h
Expand Up @@ -259,11 +259,6 @@ class PassBuilder {
/// optimization and code generation. It is particularly tuned to fit well
/// when IR coming into the LTO phase was first run through \c
/// addPreLinkLTODefaultPipeline, and the two coordinate closely.
///
/// Note that \p Level cannot be `O0` here. The pipelines produced are
/// only intended for use when attempting to optimize code. If frontends
/// require some transformations for semantic reasons, they should explicitly
/// build them.
ModulePassManager
buildThinLTODefaultPipeline(OptimizationLevel Level,
const ModuleSummaryIndex *ImportSummary);
Expand All @@ -275,11 +270,6 @@ class PassBuilder {
/// run. It works to minimize the IR which needs to be analyzed without
/// making irreversible decisions which could be made better during the LTO
/// run.
///
/// Note that \p Level cannot be `O0` here. The pipelines produced are
/// 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);

/// Build an LTO default optimization pipeline to a pass manager.
Expand All @@ -288,11 +278,6 @@ class PassBuilder {
/// optimization and code generation. It is particularly tuned to fit well
/// when IR coming into the LTO phase was first run through \c
/// addPreLinkLTODefaultPipeline, and the two coordinate closely.
///
/// Note that \p Level cannot be `O0` here. The pipelines produced are
/// only intended for use when attempting to optimize code. If frontends
/// require some transformations for semantic reasons, they should explicitly
/// build them.
ModulePassManager buildLTODefaultPipeline(OptimizationLevel Level,
ModuleSummaryIndex *ExportSummary);

Expand Down
6 changes: 0 additions & 6 deletions llvm/lib/Passes/PassBuilder.cpp
Expand Up @@ -1270,12 +1270,6 @@ Error PassBuilder::parseModulePass(ModulePassManager &MPM,
.Case("O3", OptimizationLevel::O3)
.Case("Os", OptimizationLevel::Os)
.Case("Oz", OptimizationLevel::Oz);
if (L == OptimizationLevel::O0 && Matches[1] != "thinlto" &&
Matches[1] != "lto") {
MPM.addPass(buildO0DefaultPipeline(L, Matches[1] == "thinlto-pre-link" ||
Matches[1] == "lto-pre-link"));
return Error::success();
}

// This is consistent with old pass manager invoked via opt, but
// inconsistent with clang. Clang doesn't enable loop vectorization
Expand Down
10 changes: 4 additions & 6 deletions llvm/lib/Passes/PassBuilderPipelines.cpp
Expand Up @@ -1398,8 +1398,8 @@ PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
ModulePassManager
PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
bool LTOPreLink) {
assert(Level != OptimizationLevel::O0 &&
"Must request optimizations for the default pipeline!");
if (Level == OptimizationLevel::O0)
return buildO0DefaultPipeline(Level, LTOPreLink);

ModulePassManager MPM;

Expand Down Expand Up @@ -1440,8 +1440,8 @@ PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,

ModulePassManager
PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level) {
assert(Level != OptimizationLevel::O0 &&
"Must request optimizations for the default pipeline!");
if (Level == OptimizationLevel::O0)
return buildO0DefaultPipeline(Level, /*LTOPreLink*/true);

ModulePassManager MPM;

Expand Down Expand Up @@ -1552,8 +1552,6 @@ ModulePassManager PassBuilder::buildThinLTODefaultPipeline(

ModulePassManager
PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level) {
assert(Level != OptimizationLevel::O0 &&
"Must request optimizations for the default pipeline!");
// FIXME: We should use a customized pre-link pipeline!
return buildPerModuleDefaultPipeline(Level,
/* LTOPreLink */ true);
Expand Down
6 changes: 1 addition & 5 deletions mlir/lib/ExecutionEngine/OptUtils.cpp
Expand Up @@ -84,11 +84,7 @@ mlir::makeOptimizingTransformer(unsigned optLevel, unsigned sizeLevel,
pb.crossRegisterProxies(lam, fam, cgam, mam);

ModulePassManager mpm;
if (*ol == OptimizationLevel::O0)
mpm.addPass(pb.buildO0DefaultPipeline(*ol));
else
mpm.addPass(pb.buildPerModuleDefaultPipeline(*ol));

mpm.addPass(pb.buildPerModuleDefaultPipeline(*ol));
mpm.run(*m, mam);
return Error::success();
};
Expand Down
Expand Up @@ -214,11 +214,7 @@ void JITEngine::opt(TargetMachine *TM, TargetLibraryInfoImpl *TLII, Module &M,
PB.registerLoopAnalyses(LAM);
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);

if (OptLevel)
MPM.addPass(PB.buildPerModuleDefaultPipeline(getOptLevel(OptLevel)));
else
MPM.addPass(PB.buildO0DefaultPipeline(getOptLevel(OptLevel)));

MPM.addPass(PB.buildPerModuleDefaultPipeline(getOptLevel(OptLevel)));
MPM.run(M, MAM);
}

Expand Down

0 comments on commit a8f6b57

Please sign in to comment.