Skip to content

Commit

Permalink
[NewPM] Add extension points to LTO pipeline in PassBuilder
Browse files Browse the repository at this point in the history
This PR adds two extension points to the default LTO pipeline in PassBuilder, one at the beginning and one at the end. These two extension points already existed in the old pass manager, the aim is to replicate the same functionality in the new one.

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D120491
  • Loading branch information
EliaGeretto authored and aeubanks committed Feb 25, 2022
1 parent 6f07191 commit 942efa5
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
22 changes: 22 additions & 0 deletions llvm/include/llvm/Passes/PassBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,24 @@ class PassBuilder {
OptimizerLastEPCallbacks.push_back(C);
}

/// Register a callback for a default optimizer pipeline extension point
///
/// This extension point allows adding optimizations at the start of the full
/// LTO pipeline.
void registerFullLinkTimeOptimizationEarlyEPCallback(
const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
FullLinkTimeOptimizationEarlyEPCallbacks.push_back(C);
}

/// Register a callback for a default optimizer pipeline extension point
///
/// This extension point allows adding optimizations at the end of the full
/// LTO pipeline.
void registerFullLinkTimeOptimizationLastEPCallback(
const std::function<void(ModulePassManager &, OptimizationLevel)> &C) {
FullLinkTimeOptimizationLastEPCallbacks.push_back(C);
}

/// Register a callback for parsing an AliasAnalysis Name to populate
/// the given AAManager \p AA
void registerParseAACallback(
Expand Down Expand Up @@ -600,6 +618,10 @@ class PassBuilder {
VectorizerStartEPCallbacks;
SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
OptimizerLastEPCallbacks;
SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
FullLinkTimeOptimizationEarlyEPCallbacks;
SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
FullLinkTimeOptimizationLastEPCallbacks;
// Module callbacks
SmallVector<std::function<void(ModulePassManager &, OptimizationLevel)>, 2>
PipelineStartEPCallbacks;
Expand Down
12 changes: 12 additions & 0 deletions llvm/lib/Passes/PassBuilderPipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,9 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
// Convert @llvm.global.annotations to !annotation metadata.
MPM.addPass(Annotation2MetadataPass());

for (auto &C : FullLinkTimeOptimizationEarlyEPCallbacks)
C(MPM, Level);

// Create a function that performs CFI checks for cross-DSO calls with targets
// in the current module.
MPM.addPass(CrossDSOCFIPass());
Expand All @@ -1464,6 +1467,9 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
// in ICP.
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true));

for (auto &C : FullLinkTimeOptimizationLastEPCallbacks)
C(MPM, Level);

// Emit annotation remarks.
addAnnotationRemarksPass(MPM);

Expand Down Expand Up @@ -1543,6 +1549,9 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
// pipeline).
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true));

for (auto &C : FullLinkTimeOptimizationLastEPCallbacks)
C(MPM, Level);

// Emit annotation remarks.
addAnnotationRemarksPass(MPM);

Expand Down Expand Up @@ -1714,6 +1723,9 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
if (PTO.MergeFunctions)
MPM.addPass(MergeFunctionsPass());

for (auto &C : FullLinkTimeOptimizationLastEPCallbacks)
C(MPM, Level);

// Emit annotation remarks.
addAnnotationRemarksPass(MPM);

Expand Down
2 changes: 2 additions & 0 deletions llvm/test/Other/new-pm-O0-ep-callbacks.ll
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
; RUN: opt -disable-output -debug-pass-manager -passes-ep-pipeline-start=no-op-module -passes='default<O0>' 2>&1 < %s | FileCheck %s
; RUN: opt -disable-output -debug-pass-manager -passes-ep-pipeline-early-simplification=no-op-module -passes='default<O0>' 2>&1 < %s | FileCheck %s
; RUN: opt -disable-output -debug-pass-manager -passes-ep-optimizer-last=no-op-function -passes='default<O0>' 2>&1 < %s | FileCheck %s
; RUN: opt -disable-output -debug-pass-manager -passes-ep-full-link-time-optimization-early=no-op-module -passes='lto<O0>' 2>&1 < %s | FileCheck %s
; RUN: opt -disable-output -debug-pass-manager -passes-ep-full-link-time-optimization-last=no-op-module -passes='lto<O0>' 2>&1 < %s | FileCheck %s

; CHECK: Running pass: NoOp

Expand Down
11 changes: 11 additions & 0 deletions llvm/test/Other/new-pm-lto-defaults.ll
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@
; RUN: -passes='lto<O1>' -S %s 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK-O1
; RUN: opt -disable-verify -verify-cfg-preserved=0 -eagerly-invalidate-analyses=0 -debug-pass-manager \
; RUN: -passes='lto<O1>' -S %s -passes-ep-full-link-time-optimization-early=no-op-module \
; RUN: -passes-ep-full-link-time-optimization-last=no-op-module 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK-O1 --check-prefix=CHECK-EP
; RUN: opt -disable-verify -verify-cfg-preserved=0 -eagerly-invalidate-analyses=0 -debug-pass-manager \
; RUN: -passes='lto<O2>' -S %s 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK-O23SZ \
; RUN: --check-prefix=CHECK-O2
; RUN: opt -disable-verify -verify-cfg-preserved=0 -eagerly-invalidate-analyses=0 -debug-pass-manager \
; RUN: -passes='lto<O2>' -S %s -passes-ep-full-link-time-optimization-early=no-op-module \
; RUN: -passes-ep-full-link-time-optimization-last=no-op-module 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK-O23SZ \
; RUN: --check-prefix=CHECK-O2 --check-prefix=CHECK-EP
; RUN: opt -disable-verify -verify-cfg-preserved=0 -eagerly-invalidate-analyses=0 -debug-pass-manager \
; RUN: -passes='lto<O3>' -S %s 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK-O23SZ \
; RUN: --check-prefix=CHECK-O3
Expand All @@ -26,6 +35,7 @@
; RUN: --check-prefix=CHECK-O3 --check-prefix=CHECK-EP-Peephole

; CHECK-O: Running pass: Annotation2Metadata
; CHECK-EP-NEXT: Running pass: NoOpModulePass
; CHECK-O-NEXT: Running pass: CrossDSOCFIPass
; CHECK-O-NEXT: Running pass: OpenMPOptPass
; CHECK-O-NEXT: Running pass: GlobalDCEPass
Expand Down Expand Up @@ -134,6 +144,7 @@
; CHECK-O23SZ-NEXT: Running pass: SimplifyCFGPass
; CHECK-O23SZ-NEXT: Running pass: EliminateAvailableExternallyPass
; CHECK-O23SZ-NEXT: Running pass: GlobalDCEPass
; CHECK-EP-NEXT: Running pass: NoOpModulePass
; CHECK-O-NEXT: Running pass: AnnotationRemarksPass on foo
; CHECK-O-NEXT: Running pass: PrintModulePass

Expand Down
29 changes: 29 additions & 0 deletions llvm/tools/opt/NewPMDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ static cl::opt<std::string> OptimizerLastEPPipeline(
cl::desc("A textual description of the module pass pipeline inserted at "
"the OptimizerLast extension point into default pipelines"),
cl::Hidden);
static cl::opt<std::string> FullLinkTimeOptimizationEarlyEPPipeline(
"passes-ep-full-link-time-optimization-early",
cl::desc("A textual description of the module pass pipeline inserted at "
"the FullLinkTimeOptimizationEarly extension point into default "
"pipelines"),
cl::Hidden);
static cl::opt<std::string> FullLinkTimeOptimizationLastEPPipeline(
"passes-ep-full-link-time-optimization-last",
cl::desc("A textual description of the module pass pipeline inserted at "
"the FullLinkTimeOptimizationLast extension point into default "
"pipelines"),
cl::Hidden);

// Individual pipeline tuning options.
extern cl::opt<bool> DisableLoopUnrolling;
Expand Down Expand Up @@ -229,6 +241,23 @@ static void registerEPCallbacks(PassBuilder &PB) {
ExitOnError Err("Unable to parse OptimizerLastEP pipeline: ");
Err(PB.parsePassPipeline(PM, OptimizerLastEPPipeline));
});
if (tryParsePipelineText<ModulePassManager>(
PB, FullLinkTimeOptimizationEarlyEPPipeline))
PB.registerFullLinkTimeOptimizationEarlyEPCallback(
[&PB](ModulePassManager &PM, OptimizationLevel) {
ExitOnError Err(
"Unable to parse FullLinkTimeOptimizationEarlyEP pipeline: ");
Err(PB.parsePassPipeline(PM,
FullLinkTimeOptimizationEarlyEPPipeline));
});
if (tryParsePipelineText<ModulePassManager>(
PB, FullLinkTimeOptimizationLastEPPipeline))
PB.registerFullLinkTimeOptimizationLastEPCallback(
[&PB](ModulePassManager &PM, OptimizationLevel) {
ExitOnError Err(
"Unable to parse FullLinkTimeOptimizationLastEP pipeline: ");
Err(PB.parsePassPipeline(PM, FullLinkTimeOptimizationLastEPPipeline));
});
}

#define HANDLE_EXTENSION(Ext) \
Expand Down

0 comments on commit 942efa5

Please sign in to comment.