@@ -49,8 +49,10 @@ typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
4949
5050DEFINE_STDCXX_CONVERSION_FUNCTIONS (Pass, LLVMPassRef)
5151DEFINE_STDCXX_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
52+ #if LLVM_VERSION_LT(11, 0)
5253DEFINE_STDCXX_CONVERSION_FUNCTIONS (PassManagerBuilder,
5354 LLVMPassManagerBuilderRef)
55+ #endif
5456
5557extern " C" void LLVMInitializePasses () {
5658 PassRegistry &Registry = *PassRegistry::getPassRegistry ();
@@ -343,17 +345,17 @@ enum class LLVMRustPassBuilderOptLevel {
343345static PassBuilder::OptimizationLevel fromRust (LLVMRustPassBuilderOptLevel Level) {
344346 switch (Level) {
345347 case LLVMRustPassBuilderOptLevel::O0:
346- return PassBuilder::O0;
348+ return PassBuilder::OptimizationLevel:: O0;
347349 case LLVMRustPassBuilderOptLevel::O1:
348- return PassBuilder::O1;
350+ return PassBuilder::OptimizationLevel:: O1;
349351 case LLVMRustPassBuilderOptLevel::O2:
350- return PassBuilder::O2;
352+ return PassBuilder::OptimizationLevel:: O2;
351353 case LLVMRustPassBuilderOptLevel::O3:
352- return PassBuilder::O3;
354+ return PassBuilder::OptimizationLevel:: O3;
353355 case LLVMRustPassBuilderOptLevel::Os:
354- return PassBuilder::Os;
356+ return PassBuilder::OptimizationLevel:: Os;
355357 case LLVMRustPassBuilderOptLevel::Oz:
356- return PassBuilder::Oz;
358+ return PassBuilder::OptimizationLevel:: Oz;
357359 default :
358360 report_fatal_error (" Bad PassBuilderOptLevel." );
359361 }
@@ -796,8 +798,13 @@ LLVMRustOptimizeWithNewPassManager(
796798 // We manually collect pipeline callbacks so we can apply them at O0, where the
797799 // PassBuilder does not create a pipeline.
798800 std::vector<std::function<void (ModulePassManager &)>> PipelineStartEPCallbacks;
801+ #if LLVM_VERSION_GE(11, 0)
802+ std::vector<std::function<void (ModulePassManager &, PassBuilder::OptimizationLevel)>>
803+ OptimizerLastEPCallbacks;
804+ #else
799805 std::vector<std::function<void (FunctionPassManager &, PassBuilder::OptimizationLevel)>>
800806 OptimizerLastEPCallbacks;
807+ #endif
801808
802809 if (VerifyIR) {
803810 PipelineStartEPCallbacks.push_back ([VerifyIR](ModulePassManager &MPM) {
@@ -811,6 +818,14 @@ LLVMRustOptimizeWithNewPassManager(
811818 SanitizerOptions->SanitizeMemoryTrackOrigins ,
812819 SanitizerOptions->SanitizeMemoryRecover ,
813820 /* CompileKernel=*/ false );
821+ #if LLVM_VERSION_GE(11, 0)
822+ OptimizerLastEPCallbacks.push_back (
823+ [Options](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
824+ MPM.addPass (MemorySanitizerPass (Options));
825+ MPM.addPass (createModuleToFunctionPassAdaptor (MemorySanitizerPass (Options)));
826+ }
827+ );
828+ #else
814829#if LLVM_VERSION_GE(10, 0)
815830 PipelineStartEPCallbacks.push_back ([Options](ModulePassManager &MPM) {
816831 MPM.addPass (MemorySanitizerPass (Options));
@@ -821,9 +836,18 @@ LLVMRustOptimizeWithNewPassManager(
821836 FPM.addPass (MemorySanitizerPass (Options));
822837 }
823838 );
839+ #endif
824840 }
825841
826842 if (SanitizerOptions->SanitizeThread ) {
843+ #if LLVM_VERSION_GE(11, 0)
844+ OptimizerLastEPCallbacks.push_back (
845+ [](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
846+ MPM.addPass (ThreadSanitizerPass ());
847+ MPM.addPass (createModuleToFunctionPassAdaptor (ThreadSanitizerPass ()));
848+ }
849+ );
850+ #else
827851#if LLVM_VERSION_GE(10, 0)
828852 PipelineStartEPCallbacks.push_back ([](ModulePassManager &MPM) {
829853 MPM.addPass (ThreadSanitizerPass ());
@@ -834,9 +858,22 @@ LLVMRustOptimizeWithNewPassManager(
834858 FPM.addPass (ThreadSanitizerPass ());
835859 }
836860 );
861+ #endif
837862 }
838863
839864 if (SanitizerOptions->SanitizeAddress ) {
865+ #if LLVM_VERSION_GE(11, 0)
866+ OptimizerLastEPCallbacks.push_back (
867+ [SanitizerOptions](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
868+ MPM.addPass (RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
869+ MPM.addPass (ModuleAddressSanitizerPass (
870+ /* CompileKernel=*/ false , SanitizerOptions->SanitizeAddressRecover ));
871+ MPM.addPass (createModuleToFunctionPassAdaptor (AddressSanitizerPass (
872+ /* CompileKernel=*/ false , SanitizerOptions->SanitizeAddressRecover ,
873+ /* UseAfterScope=*/ true )));
874+ }
875+ );
876+ #else
840877 PipelineStartEPCallbacks.push_back ([&](ModulePassManager &MPM) {
841878 MPM.addPass (RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
842879 });
@@ -853,21 +890,27 @@ LLVMRustOptimizeWithNewPassManager(
853890 /* CompileKernel=*/ false , SanitizerOptions->SanitizeAddressRecover ));
854891 }
855892 );
893+ #endif
856894 }
857895 }
858896
859897 ModulePassManager MPM (DebugPassManager);
860898 if (!NoPrepopulatePasses) {
861- if (OptLevel == PassBuilder::O0) {
899+ if (OptLevel == PassBuilder::OptimizationLevel:: O0) {
862900 for (const auto &C : PipelineStartEPCallbacks)
863901 C (MPM);
864902
903+ #if LLVM_VERSION_GE(11, 0)
904+ for (const auto &C : OptimizerLastEPCallbacks)
905+ C (MPM, OptLevel);
906+ #else
865907 if (!OptimizerLastEPCallbacks.empty ()) {
866908 FunctionPassManager FPM (DebugPassManager);
867909 for (const auto &C : OptimizerLastEPCallbacks)
868910 C (FPM, OptLevel);
869911 MPM.addPass (createModuleToFunctionPassAdaptor (std::move (FPM)));
870912 }
913+ #endif
871914
872915 MPM.addPass (AlwaysInlinerPass (EmitLifetimeMarkers));
873916
@@ -892,12 +935,17 @@ LLVMRustOptimizeWithNewPassManager(
892935 break ;
893936 case LLVMRustOptStage::PreLinkThinLTO:
894937 MPM = PB.buildThinLTOPreLinkDefaultPipeline (OptLevel, DebugPassManager);
938+ #if LLVM_VERSION_GE(11, 0)
939+ for (const auto &C : OptimizerLastEPCallbacks)
940+ C (MPM, OptLevel);
941+ #else
895942 if (!OptimizerLastEPCallbacks.empty ()) {
896943 FunctionPassManager FPM (DebugPassManager);
897944 for (const auto &C : OptimizerLastEPCallbacks)
898945 C (FPM, OptLevel);
899946 MPM.addPass (createModuleToFunctionPassAdaptor (std::move (FPM)));
900947 }
948+ #endif
901949 break ;
902950 case LLVMRustOptStage::PreLinkFatLTO:
903951 MPM = PB.buildLTOPreLinkDefaultPipeline (OptLevel, DebugPassManager);
@@ -994,10 +1042,10 @@ class RustAssemblyAnnotationWriter : public AssemblyAnnotationWriter {
9941042 const Value *Value;
9951043 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
9961044 Name = " call" ;
997- Value = CI->getCalledValue ();
1045+ Value = CI->getCalledOperand ();
9981046 } else if (const InvokeInst* II = dyn_cast<InvokeInst>(I)) {
9991047 Name = " invoke" ;
1000- Value = II->getCalledValue ();
1048+ Value = II->getCalledOperand ();
10011049 } else {
10021050 // Could demangle more operations, e. g.
10031051 // `store %place, @function`.
@@ -1335,10 +1383,33 @@ LLVMRustFreeThinLTOData(LLVMRustThinLTOData *Data) {
13351383// `ProcessThinLTOModule` function. Here they're split up into separate steps
13361384// so rustc can save off the intermediate bytecode between each step.
13371385
1386+ #if LLVM_VERSION_GE(11, 0)
1387+ static bool
1388+ clearDSOLocalOnDeclarations (Module &Mod, TargetMachine &TM) {
1389+ // When linking an ELF shared object, dso_local should be dropped. We
1390+ // conservatively do this for -fpic.
1391+ bool ClearDSOLocalOnDeclarations =
1392+ TM.getTargetTriple ().isOSBinFormatELF () &&
1393+ TM.getRelocationModel () != Reloc::Static &&
1394+ Mod.getPIELevel () == PIELevel::Default;
1395+ return ClearDSOLocalOnDeclarations;
1396+ }
1397+ #endif
1398+
13381399extern " C" bool
1339- LLVMRustPrepareThinLTORename (const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1400+ LLVMRustPrepareThinLTORename (const LLVMRustThinLTOData *Data, LLVMModuleRef M,
1401+ LLVMTargetMachineRef TM) {
13401402 Module &Mod = *unwrap (M);
1341- if (renameModuleForThinLTO (Mod, Data->Index )) {
1403+ TargetMachine &Target = *unwrap (TM);
1404+
1405+ #if LLVM_VERSION_GE(11, 0)
1406+ bool ClearDSOLocal = clearDSOLocalOnDeclarations (Mod, Target);
1407+ bool error = renameModuleForThinLTO (Mod, Data->Index , ClearDSOLocal);
1408+ #else
1409+ bool error = renameModuleForThinLTO (Mod, Data->Index );
1410+ #endif
1411+
1412+ if (error) {
13421413 LLVMRustSetLastError (" renameModuleForThinLTO failed" );
13431414 return false ;
13441415 }
@@ -1362,8 +1433,10 @@ LLVMRustPrepareThinLTOInternalize(const LLVMRustThinLTOData *Data, LLVMModuleRef
13621433}
13631434
13641435extern " C" bool
1365- LLVMRustPrepareThinLTOImport (const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1436+ LLVMRustPrepareThinLTOImport (const LLVMRustThinLTOData *Data, LLVMModuleRef M,
1437+ LLVMTargetMachineRef TM) {
13661438 Module &Mod = *unwrap (M);
1439+ TargetMachine &Target = *unwrap (TM);
13671440
13681441 const auto &ImportList = Data->ImportLists .lookup (Mod.getModuleIdentifier ());
13691442 auto Loader = [&](StringRef Identifier) {
@@ -1399,7 +1472,12 @@ LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
13991472
14001473 return MOrErr;
14011474 };
1475+ #if LLVM_VERSION_GE(11, 0)
1476+ bool ClearDSOLocal = clearDSOLocalOnDeclarations (Mod, Target);
1477+ FunctionImporter Importer (Data->Index , Loader, ClearDSOLocal);
1478+ #else
14021479 FunctionImporter Importer (Data->Index , Loader);
1480+ #endif
14031481 Expected<bool > Result = Importer.importFunctions (Mod, ImportList);
14041482 if (!Result) {
14051483 LLVMRustSetLastError (toString (Result.takeError ()).c_str ());
@@ -1558,22 +1636,11 @@ LLVMRustThinLTOPatchDICompileUnit(LLVMModuleRef Mod, DICompileUnit *Unit) {
15581636 }
15591637
15601638 // Use LLVM's built-in `DebugInfoFinder` to find a bunch of debuginfo and
1561- // process it recursively. Note that we specifically iterate over instructions
1562- // to ensure we feed everything into it.
1639+ // process it recursively. Note that we used to specifically iterate over
1640+ // instructions to ensure we feed everything into it, but `processModule`
1641+ // started doing this the same way in LLVM 7 (commit d769eb36ab2b8).
15631642 DebugInfoFinder Finder;
15641643 Finder.processModule (*M);
1565- for (Function &F : M->functions ()) {
1566- for (auto &FI : F) {
1567- for (Instruction &BI : FI) {
1568- if (auto Loc = BI.getDebugLoc ())
1569- Finder.processLocation (*M, Loc);
1570- if (auto DVI = dyn_cast<DbgValueInst>(&BI))
1571- Finder.processValue (*M, DVI);
1572- if (auto DDI = dyn_cast<DbgDeclareInst>(&BI))
1573- Finder.processDeclare (*M, DDI);
1574- }
1575- }
1576- }
15771644
15781645 // After we've found all our debuginfo, rewrite all subprograms to point to
15791646 // the same `DICompileUnit`.
0 commit comments