|
11 | 11 | #include "llvm/IR/AutoUpgrade.h"
|
12 | 12 | #include "llvm/IR/AssemblyAnnotationWriter.h"
|
13 | 13 | #include "llvm/IR/IntrinsicInst.h"
|
| 14 | +#include "llvm/IR/Verifier.h" |
| 15 | +#include "llvm/Passes/PassBuilder.h" |
| 16 | +#include "llvm/Passes/StandardInstrumentations.h" |
14 | 17 | #include "llvm/Support/CBindingWrapping.h"
|
15 | 18 | #include "llvm/Support/FileSystem.h"
|
16 | 19 | #include "llvm/Support/Host.h"
|
|
30 | 33 | #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
|
31 | 34 | #include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
|
32 | 35 | #endif
|
| 36 | +#include "llvm/Transforms/Utils/CanonicalizeAliases.h" |
| 37 | +#include "llvm/Transforms/Utils/NameAnonGlobals.h" |
33 | 38 |
|
34 | 39 | using namespace llvm;
|
35 | 40 | using namespace llvm::legacy;
|
@@ -294,6 +299,34 @@ static CodeGenOpt::Level fromRust(LLVMRustCodeGenOptLevel Level) {
|
294 | 299 | }
|
295 | 300 | }
|
296 | 301 |
|
| 302 | +enum class LLVMRustPassBuilderOptLevel { |
| 303 | + O0, |
| 304 | + O1, |
| 305 | + O2, |
| 306 | + O3, |
| 307 | + Os, |
| 308 | + Oz, |
| 309 | +}; |
| 310 | + |
| 311 | +static PassBuilder::OptimizationLevel fromRust(LLVMRustPassBuilderOptLevel Level) { |
| 312 | + switch (Level) { |
| 313 | + case LLVMRustPassBuilderOptLevel::O0: |
| 314 | + return PassBuilder::O0; |
| 315 | + case LLVMRustPassBuilderOptLevel::O1: |
| 316 | + return PassBuilder::O1; |
| 317 | + case LLVMRustPassBuilderOptLevel::O2: |
| 318 | + return PassBuilder::O2; |
| 319 | + case LLVMRustPassBuilderOptLevel::O3: |
| 320 | + return PassBuilder::O3; |
| 321 | + case LLVMRustPassBuilderOptLevel::Os: |
| 322 | + return PassBuilder::Os; |
| 323 | + case LLVMRustPassBuilderOptLevel::Oz: |
| 324 | + return PassBuilder::Oz; |
| 325 | + default: |
| 326 | + report_fatal_error("Bad PassBuilderOptLevel."); |
| 327 | + } |
| 328 | +} |
| 329 | + |
297 | 330 | enum class LLVMRustRelocMode {
|
298 | 331 | Default,
|
299 | 332 | Static,
|
@@ -571,6 +604,85 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target, LLVMPassManagerRef PMR,
|
571 | 604 | return LLVMRustResult::Success;
|
572 | 605 | }
|
573 | 606 |
|
| 607 | +extern "C" void |
| 608 | +LLVMRustOptimizeWithNewPassManager( |
| 609 | + LLVMModuleRef ModuleRef, |
| 610 | + LLVMTargetMachineRef TMRef, |
| 611 | + LLVMRustPassBuilderOptLevel OptLevelRust, |
| 612 | + bool NoPrepopulatePasses, bool VerifyIR, |
| 613 | + bool PrepareForThinLTO, bool PrepareForLTO, bool UseThinLTOBuffers, |
| 614 | + bool MergeFunctions, bool UnrollLoops, bool SLPVectorize, bool LoopVectorize, |
| 615 | + bool DisableSimplifyLibCalls) { |
| 616 | + Module *TheModule = unwrap(ModuleRef); |
| 617 | + TargetMachine *TM = unwrap(TMRef); |
| 618 | + PassBuilder::OptimizationLevel OptLevel = fromRust(OptLevelRust); |
| 619 | + |
| 620 | + // FIXME: MergeFunctions not supported by NewPM. |
| 621 | + (void) MergeFunctions; |
| 622 | + |
| 623 | + PipelineTuningOptions PTO; |
| 624 | + PTO.LoopUnrolling = UnrollLoops; |
| 625 | + PTO.LoopInterleaving = UnrollLoops; |
| 626 | + PTO.LoopVectorization = LoopVectorize; |
| 627 | + PTO.SLPVectorization = SLPVectorize; |
| 628 | + |
| 629 | + // FIXME: What's this? |
| 630 | + PassInstrumentationCallbacks PIC; |
| 631 | + StandardInstrumentations SI; |
| 632 | + SI.registerCallbacks(PIC); |
| 633 | + |
| 634 | + // FIXME: PGOOpt |
| 635 | + Optional<PGOOptions> PGOOpt; |
| 636 | + PassBuilder PB(TM, PTO, PGOOpt, &PIC); |
| 637 | + |
| 638 | + bool DebugPassManager = false; |
| 639 | + LoopAnalysisManager LAM(DebugPassManager); |
| 640 | + FunctionAnalysisManager FAM(DebugPassManager); |
| 641 | + CGSCCAnalysisManager CGAM(DebugPassManager); |
| 642 | + ModuleAnalysisManager MAM(DebugPassManager); |
| 643 | + |
| 644 | + FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); }); |
| 645 | + |
| 646 | + Triple TargetTriple(TheModule->getTargetTriple()); |
| 647 | + std::unique_ptr<TargetLibraryInfoImpl> TLII(new TargetLibraryInfoImpl(TargetTriple)); |
| 648 | + if (DisableSimplifyLibCalls) |
| 649 | + TLII->disableAllFunctions(); |
| 650 | + FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); }); |
| 651 | + |
| 652 | + PB.registerModuleAnalyses(MAM); |
| 653 | + PB.registerCGSCCAnalyses(CGAM); |
| 654 | + PB.registerFunctionAnalyses(FAM); |
| 655 | + PB.registerLoopAnalyses(LAM); |
| 656 | + PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); |
| 657 | + PB.registerPipelineStartEPCallback([VerifyIR](ModulePassManager &MPM) { |
| 658 | + if (VerifyIR) |
| 659 | + MPM.addPass(VerifierPass()); |
| 660 | + }); |
| 661 | + |
| 662 | + ModulePassManager MPM(DebugPassManager); |
| 663 | + if (!NoPrepopulatePasses) { |
| 664 | + if (OptLevel == PassBuilder::O0) { |
| 665 | + MPM.addPass(AlwaysInlinerPass(/*InsertLifetimeIntrinsics=*/false)); |
| 666 | + // FIXME: PGO? |
| 667 | + } else { |
| 668 | + // FIXME: Sanitizers? PGO? |
| 669 | + if (PrepareForThinLTO) { |
| 670 | + MPM = PB.buildThinLTOPreLinkDefaultPipeline(OptLevel, DebugPassManager); |
| 671 | + } else if (PrepareForLTO) { |
| 672 | + MPM = PB.buildLTOPreLinkDefaultPipeline(OptLevel, DebugPassManager); |
| 673 | + } else { |
| 674 | + MPM = PB.buildPerModuleDefaultPipeline(OptLevel, DebugPassManager); |
| 675 | + } |
| 676 | + } |
| 677 | + } |
| 678 | + |
| 679 | + if (UseThinLTOBuffers) { |
| 680 | + MPM.addPass(CanonicalizeAliasesPass()); |
| 681 | + MPM.addPass(NameAnonGlobalPass()); |
| 682 | + } |
| 683 | + |
| 684 | + MPM.run(*TheModule, MAM); |
| 685 | +} |
574 | 686 |
|
575 | 687 | // Callback to demangle function name
|
576 | 688 | // Parameters:
|
|
0 commit comments