Skip to content

Commit e978f6b

Browse files
committed
[LTO] Support new PM in ThinLTOCodeGenerator.
This patch adds initial support for using the new pass manager when doing ThinLTO via libLTO. Reviewed By: steven_wu Differential Revision: https://reviews.llvm.org/D102627
1 parent f880bd2 commit e978f6b

File tree

6 files changed

+120
-4
lines changed

6 files changed

+120
-4
lines changed

llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ class ThinLTOCodeGenerator {
226226
OptLevel = (NewOptLevel > 3) ? 3 : NewOptLevel;
227227
}
228228

229+
/// Enable or disable the new pass manager.
230+
void setUseNewPM(unsigned Enabled) { UseNewPM = Enabled; }
231+
232+
/// Enable or disable debug output for the new pass manager.
233+
void setDebugPassManager(unsigned Enabled) { DebugPassManager = Enabled; }
234+
229235
/// Disable CodeGen, only run the stages till codegen and stop. The output
230236
/// will be bitcode.
231237
void disableCodeGen(bool Disable) { DisableCodeGen = Disable; }
@@ -341,6 +347,14 @@ class ThinLTOCodeGenerator {
341347

342348
/// IR Optimization Level [0-3].
343349
unsigned OptLevel = 3;
350+
351+
/// Flag to indicate whether the new pass manager should be used for IR
352+
/// optimizations.
353+
bool UseNewPM = LLVM_ENABLE_NEW_PASS_MANAGER;
354+
355+
/// Flag to indicate whether debug output should be enabled for the new pass
356+
/// manager.
357+
bool DebugPassManager = false;
344358
};
345359
}
346360
#endif

llvm/lib/LTO/ThinLTOCodeGenerator.cpp

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "llvm/ADT/Statistic.h"
1818
#include "llvm/ADT/StringExtras.h"
19+
#include "llvm/Analysis/AliasAnalysis.h"
1920
#include "llvm/Analysis/ModuleSummaryAnalysis.h"
2021
#include "llvm/Analysis/ProfileSummaryInfo.h"
2122
#include "llvm/Analysis/TargetLibraryInfo.h"
@@ -37,6 +38,8 @@
3738
#include "llvm/LTO/SummaryBasedOptimizations.h"
3839
#include "llvm/MC/SubtargetFeature.h"
3940
#include "llvm/Object/IRObjectFile.h"
41+
#include "llvm/Passes/PassBuilder.h"
42+
#include "llvm/Passes/StandardInstrumentations.h"
4043
#include "llvm/Remarks/HotnessThresholdParser.h"
4144
#include "llvm/Support/CachePruning.h"
4245
#include "llvm/Support/Debug.h"
@@ -262,6 +265,68 @@ static void optimizeModule(Module &TheModule, TargetMachine &TM,
262265
PM.run(TheModule);
263266
}
264267

268+
static void optimizeModuleNewPM(Module &TheModule, TargetMachine &TM,
269+
unsigned OptLevel, bool Freestanding,
270+
bool DebugPassManager,
271+
ModuleSummaryIndex *Index) {
272+
Optional<PGOOptions> PGOOpt;
273+
LoopAnalysisManager LAM;
274+
FunctionAnalysisManager FAM;
275+
CGSCCAnalysisManager CGAM;
276+
ModuleAnalysisManager MAM;
277+
278+
PassInstrumentationCallbacks PIC;
279+
StandardInstrumentations SI(DebugPassManager);
280+
SI.registerCallbacks(PIC, &FAM);
281+
PipelineTuningOptions PTO;
282+
PTO.LoopVectorization = true;
283+
PTO.SLPVectorization = true;
284+
PassBuilder PB(&TM, PTO, PGOOpt, &PIC);
285+
286+
std::unique_ptr<TargetLibraryInfoImpl> TLII(
287+
new TargetLibraryInfoImpl(Triple(TM.getTargetTriple())));
288+
if (Freestanding)
289+
TLII->disableAllFunctions();
290+
FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
291+
292+
AAManager AA = PB.buildDefaultAAPipeline();
293+
294+
// Register the AA manager first so that our version is the one used.
295+
FAM.registerPass([&] { return std::move(AA); });
296+
297+
// Register all the basic analyses with the managers.
298+
PB.registerModuleAnalyses(MAM);
299+
PB.registerCGSCCAnalyses(CGAM);
300+
PB.registerFunctionAnalyses(FAM);
301+
PB.registerLoopAnalyses(LAM);
302+
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
303+
304+
ModulePassManager MPM;
305+
306+
PassBuilder::OptimizationLevel OL;
307+
308+
switch (OptLevel) {
309+
default:
310+
llvm_unreachable("Invalid optimization level");
311+
case 0:
312+
OL = PassBuilder::OptimizationLevel::O0;
313+
break;
314+
case 1:
315+
OL = PassBuilder::OptimizationLevel::O1;
316+
break;
317+
case 2:
318+
OL = PassBuilder::OptimizationLevel::O2;
319+
break;
320+
case 3:
321+
OL = PassBuilder::OptimizationLevel::O3;
322+
break;
323+
}
324+
325+
MPM.addPass(PB.buildThinLTODefaultPipeline(OL, Index));
326+
327+
MPM.run(TheModule, MAM);
328+
}
329+
265330
static void
266331
addUsedSymbolToPreservedGUID(const lto::InputFile &File,
267332
DenseSet<GlobalValue::GUID> &PreservedGUID) {
@@ -421,7 +486,8 @@ ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
421486
const GVSummaryMapTy &DefinedGlobals,
422487
const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
423488
bool DisableCodeGen, StringRef SaveTempsDir,
424-
bool Freestanding, unsigned OptLevel, unsigned count) {
489+
bool Freestanding, unsigned OptLevel, unsigned count,
490+
bool UseNewPM, bool DebugPassManager) {
425491

426492
// "Benchmark"-like optimization: single-source case
427493
bool SingleModule = (ModuleMap.size() == 1);
@@ -461,7 +527,11 @@ ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
461527
saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
462528
}
463529

464-
optimizeModule(TheModule, TM, OptLevel, Freestanding, &Index);
530+
if (UseNewPM)
531+
optimizeModuleNewPM(TheModule, TM, OptLevel, Freestanding, DebugPassManager,
532+
&Index);
533+
else
534+
optimizeModule(TheModule, TM, OptLevel, Freestanding, &Index);
465535

466536
saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
467537

@@ -1132,7 +1202,8 @@ void ThinLTOCodeGenerator::run() {
11321202
*TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
11331203
ExportList, GUIDPreservedSymbols,
11341204
ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
1135-
DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count);
1205+
DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count,
1206+
UseNewPM, DebugPassManager);
11361207

11371208
// Commit to the cache (if enabled)
11381209
CacheEntry.write(*OutputBuffer);

llvm/test/ThinLTO/X86/diagnostic-handler-remarks-with-hotness.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
; with -lto-pass-remarks-with-hotness.
66

77
; RUN: llvm-lto -thinlto-action=run \
8+
; RUN: -use-new-pm=false \
89
; RUN: -lto-pass-remarks-output=%t.yaml \
910
; RUN: -lto-pass-remarks-with-hotness \
1011
; RUN: -exported-symbol _func2 \

llvm/test/ThinLTO/X86/diagnostic-handler-remarks.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
; Optimization records are collected regardless of the diagnostic handler
55
; RUN: rm -f %t.yaml.thin.0.yaml %t.yaml.thin.1.yaml
66
; RUN: llvm-lto -thinlto-action=run \
7+
; RUN: -use-new-pm=false \
78
; RUN: -lto-pass-remarks-output=%t.yaml \
89
; RUN: -lto-pass-remarks-filter=inline \
910
; RUN: -lto-pass-remarks-format=yaml \

llvm/test/ThinLTO/X86/newpm-basic.ll

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
; RUN: opt -module-summary %s -o %t1.bc
22
; RUN: llvm-lto2 run %t1.bc -o %t.o \
33
; RUN: -r=%t1.bc,_tinkywinky,pxl \
4-
; RUN: -use-new-pm
4+
; RUN: -debug-pass-manager \
5+
; RUN: -use-new-pm 2>&1 | FileCheck --check-prefix=NEWPM %s
6+
7+
; RUN: llvm-lto -thinlto-action=run -exported-symbol tinkywinky \
8+
; RUN: -use-new-pm \
9+
; RUN: -debug-pass-manager \
10+
; RUN: %t1.bc 2>&1 | FileCheck --check-prefix=NEWPM %s
11+
12+
; RUN: llvm-lto -thinlto-action=run -exported-symbol tinkywinky \
13+
; RUN: -use-new-pm=false \
14+
; RUN: -debug-pass-manager \
15+
; RUN: -debug-pass=Structure \
16+
; RUN: %t1.bc 2>&1 | FileCheck --check-prefix=LEGACYPM %s
17+
18+
; Check that the selected pass manager is used for middle-end optimizations by
19+
; checking the debug output for IPSCCP.
20+
21+
; NEWPM-NOT: Interprocedural Sparse Conditional Constant Propagation
22+
; NEWPM: Running pass: IPSCCPPass on [module]
23+
; NEWPM-NOT: Interprocedural Sparse Conditional Constant Propagation
24+
25+
; LEGACYPM-NOT: IPSCCPPass
26+
; LEGACYPM: Interprocedural Sparse Conditional Constant Propagation
27+
; LEGACYPM-NOT: IPSCCPPass
528

629
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
730
target triple = "x86_64-apple-macosx10.11.0"

llvm/tools/llvm-lto/llvm-lto.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ static cl::opt<bool>
227227
cl::desc("Run LTO passes using the new pass manager"),
228228
cl::init(LLVM_ENABLE_NEW_PASS_MANAGER), cl::Hidden);
229229

230+
static cl::opt<bool>
231+
DebugPassManager("debug-pass-manager", cl::init(false), cl::Hidden,
232+
cl::desc("Print pass management debugging information"));
233+
230234
namespace {
231235

232236
struct ModuleInfo {
@@ -557,6 +561,8 @@ class ThinLTOProcessing {
557561
ThinGenerator.setCacheMaxSizeFiles(ThinLTOCacheMaxSizeFiles);
558562
ThinGenerator.setCacheMaxSizeBytes(ThinLTOCacheMaxSizeBytes);
559563
ThinGenerator.setFreestanding(EnableFreestanding);
564+
ThinGenerator.setUseNewPM(UseNewPM);
565+
ThinGenerator.setDebugPassManager(DebugPassManager);
560566

561567
// Add all the exported symbols to the table of symbols to preserve.
562568
for (unsigned i = 0; i < ExportedSymbols.size(); ++i)

0 commit comments

Comments
 (0)