|
16 | 16 |
|
17 | 17 | #include "llvm/ADT/Statistic.h"
|
18 | 18 | #include "llvm/ADT/StringExtras.h"
|
| 19 | +#include "llvm/Analysis/AliasAnalysis.h" |
19 | 20 | #include "llvm/Analysis/ModuleSummaryAnalysis.h"
|
20 | 21 | #include "llvm/Analysis/ProfileSummaryInfo.h"
|
21 | 22 | #include "llvm/Analysis/TargetLibraryInfo.h"
|
|
37 | 38 | #include "llvm/LTO/SummaryBasedOptimizations.h"
|
38 | 39 | #include "llvm/MC/SubtargetFeature.h"
|
39 | 40 | #include "llvm/Object/IRObjectFile.h"
|
| 41 | +#include "llvm/Passes/PassBuilder.h" |
| 42 | +#include "llvm/Passes/StandardInstrumentations.h" |
40 | 43 | #include "llvm/Remarks/HotnessThresholdParser.h"
|
41 | 44 | #include "llvm/Support/CachePruning.h"
|
42 | 45 | #include "llvm/Support/Debug.h"
|
@@ -262,6 +265,68 @@ static void optimizeModule(Module &TheModule, TargetMachine &TM,
|
262 | 265 | PM.run(TheModule);
|
263 | 266 | }
|
264 | 267 |
|
| 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 | + |
265 | 330 | static void
|
266 | 331 | addUsedSymbolToPreservedGUID(const lto::InputFile &File,
|
267 | 332 | DenseSet<GlobalValue::GUID> &PreservedGUID) {
|
@@ -421,7 +486,8 @@ ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
|
421 | 486 | const GVSummaryMapTy &DefinedGlobals,
|
422 | 487 | const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
|
423 | 488 | bool DisableCodeGen, StringRef SaveTempsDir,
|
424 |
| - bool Freestanding, unsigned OptLevel, unsigned count) { |
| 489 | + bool Freestanding, unsigned OptLevel, unsigned count, |
| 490 | + bool UseNewPM, bool DebugPassManager) { |
425 | 491 |
|
426 | 492 | // "Benchmark"-like optimization: single-source case
|
427 | 493 | bool SingleModule = (ModuleMap.size() == 1);
|
@@ -461,7 +527,11 @@ ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
|
461 | 527 | saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
|
462 | 528 | }
|
463 | 529 |
|
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); |
465 | 535 |
|
466 | 536 | saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
|
467 | 537 |
|
@@ -1132,7 +1202,8 @@ void ThinLTOCodeGenerator::run() {
|
1132 | 1202 | *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
|
1133 | 1203 | ExportList, GUIDPreservedSymbols,
|
1134 | 1204 | ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
|
1135 |
| - DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count); |
| 1205 | + DisableCodeGen, SaveTempsDir, Freestanding, OptLevel, count, |
| 1206 | + UseNewPM, DebugPassManager); |
1136 | 1207 |
|
1137 | 1208 | // Commit to the cache (if enabled)
|
1138 | 1209 | CacheEntry.write(*OutputBuffer);
|
|
0 commit comments