|
12 | 12 | #include "llvm/IR/AutoUpgrade.h"
|
13 | 13 | #include "llvm/IR/AssemblyAnnotationWriter.h"
|
14 | 14 | #include "llvm/IR/IntrinsicInst.h"
|
| 15 | +#include "llvm/IR/Verifier.h" |
| 16 | +#include "llvm/Passes/PassBuilder.h" |
| 17 | +#if LLVM_VERSION_GE(9, 0) |
| 18 | +#include "llvm/Passes/StandardInstrumentations.h" |
| 19 | +#endif |
15 | 20 | #include "llvm/Support/CBindingWrapping.h"
|
16 | 21 | #include "llvm/Support/FileSystem.h"
|
17 | 22 | #include "llvm/Support/Host.h"
|
|
32 | 37 | #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
|
33 | 38 | #include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
|
34 | 39 | #endif
|
| 40 | +#if LLVM_VERSION_GE(9, 0) |
| 41 | +#include "llvm/Transforms/Utils/CanonicalizeAliases.h" |
| 42 | +#endif |
| 43 | +#include "llvm/Transforms/Utils/NameAnonGlobals.h" |
35 | 44 |
|
36 | 45 | using namespace llvm;
|
37 |
| -using namespace llvm::legacy; |
38 | 46 |
|
39 | 47 | typedef struct LLVMOpaquePass *LLVMPassRef;
|
40 | 48 | typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
|
@@ -314,6 +322,34 @@ static CodeGenOpt::Level fromRust(LLVMRustCodeGenOptLevel Level) {
|
314 | 322 | }
|
315 | 323 | }
|
316 | 324 |
|
| 325 | +enum class LLVMRustPassBuilderOptLevel { |
| 326 | + O0, |
| 327 | + O1, |
| 328 | + O2, |
| 329 | + O3, |
| 330 | + Os, |
| 331 | + Oz, |
| 332 | +}; |
| 333 | + |
| 334 | +static PassBuilder::OptimizationLevel fromRust(LLVMRustPassBuilderOptLevel Level) { |
| 335 | + switch (Level) { |
| 336 | + case LLVMRustPassBuilderOptLevel::O0: |
| 337 | + return PassBuilder::O0; |
| 338 | + case LLVMRustPassBuilderOptLevel::O1: |
| 339 | + return PassBuilder::O1; |
| 340 | + case LLVMRustPassBuilderOptLevel::O2: |
| 341 | + return PassBuilder::O2; |
| 342 | + case LLVMRustPassBuilderOptLevel::O3: |
| 343 | + return PassBuilder::O3; |
| 344 | + case LLVMRustPassBuilderOptLevel::Os: |
| 345 | + return PassBuilder::Os; |
| 346 | + case LLVMRustPassBuilderOptLevel::Oz: |
| 347 | + return PassBuilder::Oz; |
| 348 | + default: |
| 349 | + report_fatal_error("Bad PassBuilderOptLevel."); |
| 350 | + } |
| 351 | +} |
| 352 | + |
317 | 353 | enum class LLVMRustRelocMode {
|
318 | 354 | Default,
|
319 | 355 | Static,
|
@@ -604,6 +640,212 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target, LLVMPassManagerRef PMR,
|
604 | 640 | return LLVMRustResult::Success;
|
605 | 641 | }
|
606 | 642 |
|
| 643 | +enum class LLVMRustOptStage { |
| 644 | + PreLinkNoLTO, |
| 645 | + PreLinkThinLTO, |
| 646 | + PreLinkFatLTO, |
| 647 | + ThinLTO, |
| 648 | + FatLTO, |
| 649 | +}; |
| 650 | + |
| 651 | +struct LLVMRustSanitizerOptions { |
| 652 | + bool SanitizeMemory; |
| 653 | + bool SanitizeThread; |
| 654 | + bool SanitizeAddress; |
| 655 | + bool SanitizeRecover; |
| 656 | + int SanitizeMemoryTrackOrigins; |
| 657 | +}; |
| 658 | + |
| 659 | +extern "C" void |
| 660 | +LLVMRustOptimizeWithNewPassManager( |
| 661 | + LLVMModuleRef ModuleRef, |
| 662 | + LLVMTargetMachineRef TMRef, |
| 663 | + LLVMRustPassBuilderOptLevel OptLevelRust, |
| 664 | + LLVMRustOptStage OptStage, |
| 665 | + bool NoPrepopulatePasses, bool VerifyIR, bool UseThinLTOBuffers, |
| 666 | + bool MergeFunctions, bool UnrollLoops, bool SLPVectorize, bool LoopVectorize, |
| 667 | + bool DisableSimplifyLibCalls, |
| 668 | + LLVMRustSanitizerOptions *SanitizerOptions, |
| 669 | + const char *PGOGenPath, const char *PGOUsePath) { |
| 670 | +#if LLVM_VERSION_GE(9, 0) |
| 671 | + Module *TheModule = unwrap(ModuleRef); |
| 672 | + TargetMachine *TM = unwrap(TMRef); |
| 673 | + PassBuilder::OptimizationLevel OptLevel = fromRust(OptLevelRust); |
| 674 | + |
| 675 | + // FIXME: MergeFunctions is not supported by NewPM yet. |
| 676 | + (void) MergeFunctions; |
| 677 | + |
| 678 | + PipelineTuningOptions PTO; |
| 679 | + PTO.LoopUnrolling = UnrollLoops; |
| 680 | + PTO.LoopInterleaving = UnrollLoops; |
| 681 | + PTO.LoopVectorization = LoopVectorize; |
| 682 | + PTO.SLPVectorization = SLPVectorize; |
| 683 | + |
| 684 | + PassInstrumentationCallbacks PIC; |
| 685 | + StandardInstrumentations SI; |
| 686 | + SI.registerCallbacks(PIC); |
| 687 | + |
| 688 | + Optional<PGOOptions> PGOOpt; |
| 689 | + if (PGOGenPath) { |
| 690 | + assert(!PGOUsePath); |
| 691 | + PGOOpt = PGOOptions(PGOGenPath, "", "", PGOOptions::IRInstr); |
| 692 | + } else if (PGOUsePath) { |
| 693 | + assert(!PGOGenPath); |
| 694 | + PGOOpt = PGOOptions(PGOUsePath, "", "", PGOOptions::IRUse); |
| 695 | + } |
| 696 | + |
| 697 | + PassBuilder PB(TM, PTO, PGOOpt, &PIC); |
| 698 | + |
| 699 | + // FIXME: We may want to expose this as an option. |
| 700 | + bool DebugPassManager = false; |
| 701 | + LoopAnalysisManager LAM(DebugPassManager); |
| 702 | + FunctionAnalysisManager FAM(DebugPassManager); |
| 703 | + CGSCCAnalysisManager CGAM(DebugPassManager); |
| 704 | + ModuleAnalysisManager MAM(DebugPassManager); |
| 705 | + |
| 706 | + FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); }); |
| 707 | + |
| 708 | + Triple TargetTriple(TheModule->getTargetTriple()); |
| 709 | + std::unique_ptr<TargetLibraryInfoImpl> TLII(new TargetLibraryInfoImpl(TargetTriple)); |
| 710 | + if (DisableSimplifyLibCalls) |
| 711 | + TLII->disableAllFunctions(); |
| 712 | + FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); }); |
| 713 | + |
| 714 | + PB.registerModuleAnalyses(MAM); |
| 715 | + PB.registerCGSCCAnalyses(CGAM); |
| 716 | + PB.registerFunctionAnalyses(FAM); |
| 717 | + PB.registerLoopAnalyses(LAM); |
| 718 | + PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); |
| 719 | + |
| 720 | + // We manually collect pipeline callbacks so we can apply them at O0, where the |
| 721 | + // PassBuilder does not create a pipeline. |
| 722 | + std::vector<std::function<void(ModulePassManager &)>> PipelineStartEPCallbacks; |
| 723 | + std::vector<std::function<void(FunctionPassManager &, PassBuilder::OptimizationLevel)>> |
| 724 | + OptimizerLastEPCallbacks; |
| 725 | + |
| 726 | + if (VerifyIR) { |
| 727 | + PipelineStartEPCallbacks.push_back([VerifyIR](ModulePassManager &MPM) { |
| 728 | + MPM.addPass(VerifierPass()); |
| 729 | + }); |
| 730 | + } |
| 731 | + |
| 732 | + if (SanitizerOptions) { |
| 733 | + if (SanitizerOptions->SanitizeMemory) { |
| 734 | + MemorySanitizerOptions Options( |
| 735 | + SanitizerOptions->SanitizeMemoryTrackOrigins, |
| 736 | + SanitizerOptions->SanitizeRecover, |
| 737 | + /*CompileKernel=*/false); |
| 738 | +#if LLVM_VERSION_GE(10, 0) |
| 739 | + PipelineStartEPCallbacks.push_back([Options](ModulePassManager &MPM) { |
| 740 | + MPM.addPass(MemorySanitizerPass(Options)); |
| 741 | + }); |
| 742 | +#endif |
| 743 | + OptimizerLastEPCallbacks.push_back( |
| 744 | + [Options](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) { |
| 745 | + FPM.addPass(MemorySanitizerPass(Options)); |
| 746 | + } |
| 747 | + ); |
| 748 | + } |
| 749 | + |
| 750 | + if (SanitizerOptions->SanitizeThread) { |
| 751 | +#if LLVM_VERSION_GE(10, 0) |
| 752 | + PipelineStartEPCallbacks.push_back([](ModulePassManager &MPM) { |
| 753 | + MPM.addPass(ThreadSanitizerPass()); |
| 754 | + }); |
| 755 | +#endif |
| 756 | + OptimizerLastEPCallbacks.push_back( |
| 757 | + [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) { |
| 758 | + FPM.addPass(ThreadSanitizerPass()); |
| 759 | + } |
| 760 | + ); |
| 761 | + } |
| 762 | + |
| 763 | + if (SanitizerOptions->SanitizeAddress) { |
| 764 | + // FIXME: Rust does not expose the UseAfterScope option. |
| 765 | + PipelineStartEPCallbacks.push_back([&](ModulePassManager &MPM) { |
| 766 | + MPM.addPass(RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>()); |
| 767 | + }); |
| 768 | + OptimizerLastEPCallbacks.push_back( |
| 769 | + [SanitizerOptions](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) { |
| 770 | + FPM.addPass(AddressSanitizerPass( |
| 771 | + /*CompileKernel=*/false, SanitizerOptions->SanitizeRecover)); |
| 772 | + } |
| 773 | + ); |
| 774 | + PipelineStartEPCallbacks.push_back( |
| 775 | + [SanitizerOptions](ModulePassManager &MPM) { |
| 776 | + MPM.addPass(ModuleAddressSanitizerPass( |
| 777 | + /*CompileKernel=*/false, SanitizerOptions->SanitizeRecover)); |
| 778 | + } |
| 779 | + ); |
| 780 | + } |
| 781 | + } |
| 782 | + |
| 783 | + ModulePassManager MPM(DebugPassManager); |
| 784 | + if (!NoPrepopulatePasses) { |
| 785 | + if (OptLevel == PassBuilder::O0) { |
| 786 | + for (const auto &C : PipelineStartEPCallbacks) |
| 787 | + C(MPM); |
| 788 | + |
| 789 | + if (!OptimizerLastEPCallbacks.empty()) { |
| 790 | + FunctionPassManager FPM(DebugPassManager); |
| 791 | + for (const auto &C : OptimizerLastEPCallbacks) |
| 792 | + C(FPM, OptLevel); |
| 793 | + MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 794 | + } |
| 795 | + |
| 796 | + MPM.addPass(AlwaysInlinerPass(/*InsertLifetimeIntrinsics=*/false)); |
| 797 | + |
| 798 | +#if LLVM_VERSION_GE(10, 0) |
| 799 | + if (PGOOpt) { |
| 800 | + PB.addPGOInstrPassesForO0( |
| 801 | + MPM, DebugPassManager, PGOOpt->Action == PGOOptions::IRInstr, |
| 802 | + /*IsCS=*/false, PGOOpt->ProfileFile, PGOOpt->ProfileRemappingFile); |
| 803 | + } |
| 804 | +#endif |
| 805 | + } else { |
| 806 | + for (const auto &C : PipelineStartEPCallbacks) |
| 807 | + PB.registerPipelineStartEPCallback(C); |
| 808 | + for (const auto &C : OptimizerLastEPCallbacks) |
| 809 | + PB.registerOptimizerLastEPCallback(C); |
| 810 | + |
| 811 | + switch (OptStage) { |
| 812 | + case LLVMRustOptStage::PreLinkNoLTO: |
| 813 | + MPM = PB.buildPerModuleDefaultPipeline(OptLevel, DebugPassManager); |
| 814 | + break; |
| 815 | + case LLVMRustOptStage::PreLinkThinLTO: |
| 816 | + MPM = PB.buildThinLTOPreLinkDefaultPipeline(OptLevel, DebugPassManager); |
| 817 | + break; |
| 818 | + case LLVMRustOptStage::PreLinkFatLTO: |
| 819 | + MPM = PB.buildLTOPreLinkDefaultPipeline(OptLevel, DebugPassManager); |
| 820 | + break; |
| 821 | + case LLVMRustOptStage::ThinLTO: |
| 822 | + // FIXME: Does it make sense to pass the ModuleSummaryIndex? |
| 823 | + // It only seems to be needed for C++ specific optimizations. |
| 824 | + MPM = PB.buildThinLTODefaultPipeline(OptLevel, DebugPassManager, nullptr); |
| 825 | + break; |
| 826 | + case LLVMRustOptStage::FatLTO: |
| 827 | + MPM = PB.buildLTODefaultPipeline(OptLevel, DebugPassManager, nullptr); |
| 828 | + break; |
| 829 | + } |
| 830 | + } |
| 831 | + } |
| 832 | + |
| 833 | + if (UseThinLTOBuffers) { |
| 834 | + MPM.addPass(CanonicalizeAliasesPass()); |
| 835 | + MPM.addPass(NameAnonGlobalPass()); |
| 836 | + } |
| 837 | + |
| 838 | + // Upgrade all calls to old intrinsics first. |
| 839 | + for (Module::iterator I = TheModule->begin(), E = TheModule->end(); I != E;) |
| 840 | + UpgradeCallsToIntrinsic(&*I++); // must be post-increment, as we remove |
| 841 | + |
| 842 | + MPM.run(*TheModule, MAM); |
| 843 | +#else |
| 844 | + // The new pass manager has been available for a long time, |
| 845 | + // but we don't bother supporting it on old LLVM versions. |
| 846 | + report_fatal_error("New pass manager only supported since LLVM 9"); |
| 847 | +#endif |
| 848 | +} |
607 | 849 |
|
608 | 850 | // Callback to demangle function name
|
609 | 851 | // Parameters:
|
|
0 commit comments