@@ -49,8 +49,10 @@ typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
49
49
50
50
DEFINE_STDCXX_CONVERSION_FUNCTIONS (Pass, LLVMPassRef)
51
51
DEFINE_STDCXX_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
52
+ #if LLVM_VERSION_LT(11, 0)
52
53
DEFINE_STDCXX_CONVERSION_FUNCTIONS (PassManagerBuilder,
53
54
LLVMPassManagerBuilderRef)
55
+ #endif
54
56
55
57
extern " C" void LLVMInitializePasses () {
56
58
PassRegistry &Registry = *PassRegistry::getPassRegistry ();
@@ -343,17 +345,17 @@ enum class LLVMRustPassBuilderOptLevel {
343
345
static PassBuilder::OptimizationLevel fromRust (LLVMRustPassBuilderOptLevel Level) {
344
346
switch (Level) {
345
347
case LLVMRustPassBuilderOptLevel::O0:
346
- return PassBuilder::O0;
348
+ return PassBuilder::OptimizationLevel:: O0;
347
349
case LLVMRustPassBuilderOptLevel::O1:
348
- return PassBuilder::O1;
350
+ return PassBuilder::OptimizationLevel:: O1;
349
351
case LLVMRustPassBuilderOptLevel::O2:
350
- return PassBuilder::O2;
352
+ return PassBuilder::OptimizationLevel:: O2;
351
353
case LLVMRustPassBuilderOptLevel::O3:
352
- return PassBuilder::O3;
354
+ return PassBuilder::OptimizationLevel:: O3;
353
355
case LLVMRustPassBuilderOptLevel::Os:
354
- return PassBuilder::Os;
356
+ return PassBuilder::OptimizationLevel:: Os;
355
357
case LLVMRustPassBuilderOptLevel::Oz:
356
- return PassBuilder::Oz;
358
+ return PassBuilder::OptimizationLevel:: Oz;
357
359
default :
358
360
report_fatal_error (" Bad PassBuilderOptLevel." );
359
361
}
@@ -796,8 +798,13 @@ LLVMRustOptimizeWithNewPassManager(
796
798
// We manually collect pipeline callbacks so we can apply them at O0, where the
797
799
// PassBuilder does not create a pipeline.
798
800
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
799
805
std::vector<std::function<void (FunctionPassManager &, PassBuilder::OptimizationLevel)>>
800
806
OptimizerLastEPCallbacks;
807
+ #endif
801
808
802
809
if (VerifyIR) {
803
810
PipelineStartEPCallbacks.push_back ([VerifyIR](ModulePassManager &MPM) {
@@ -811,6 +818,14 @@ LLVMRustOptimizeWithNewPassManager(
811
818
SanitizerOptions->SanitizeMemoryTrackOrigins ,
812
819
SanitizerOptions->SanitizeMemoryRecover ,
813
820
/* 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
814
829
#if LLVM_VERSION_GE(10, 0)
815
830
PipelineStartEPCallbacks.push_back ([Options](ModulePassManager &MPM) {
816
831
MPM.addPass (MemorySanitizerPass (Options));
@@ -821,9 +836,18 @@ LLVMRustOptimizeWithNewPassManager(
821
836
FPM.addPass (MemorySanitizerPass (Options));
822
837
}
823
838
);
839
+ #endif
824
840
}
825
841
826
842
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
827
851
#if LLVM_VERSION_GE(10, 0)
828
852
PipelineStartEPCallbacks.push_back ([](ModulePassManager &MPM) {
829
853
MPM.addPass (ThreadSanitizerPass ());
@@ -834,9 +858,22 @@ LLVMRustOptimizeWithNewPassManager(
834
858
FPM.addPass (ThreadSanitizerPass ());
835
859
}
836
860
);
861
+ #endif
837
862
}
838
863
839
864
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
840
877
PipelineStartEPCallbacks.push_back ([&](ModulePassManager &MPM) {
841
878
MPM.addPass (RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
842
879
});
@@ -853,21 +890,27 @@ LLVMRustOptimizeWithNewPassManager(
853
890
/* CompileKernel=*/ false , SanitizerOptions->SanitizeAddressRecover ));
854
891
}
855
892
);
893
+ #endif
856
894
}
857
895
}
858
896
859
897
ModulePassManager MPM (DebugPassManager);
860
898
if (!NoPrepopulatePasses) {
861
- if (OptLevel == PassBuilder::O0) {
899
+ if (OptLevel == PassBuilder::OptimizationLevel:: O0) {
862
900
for (const auto &C : PipelineStartEPCallbacks)
863
901
C (MPM);
864
902
903
+ #if LLVM_VERSION_GE(11, 0)
904
+ for (const auto &C : OptimizerLastEPCallbacks)
905
+ C (MPM, OptLevel);
906
+ #else
865
907
if (!OptimizerLastEPCallbacks.empty ()) {
866
908
FunctionPassManager FPM (DebugPassManager);
867
909
for (const auto &C : OptimizerLastEPCallbacks)
868
910
C (FPM, OptLevel);
869
911
MPM.addPass (createModuleToFunctionPassAdaptor (std::move (FPM)));
870
912
}
913
+ #endif
871
914
872
915
MPM.addPass (AlwaysInlinerPass (EmitLifetimeMarkers));
873
916
@@ -892,12 +935,17 @@ LLVMRustOptimizeWithNewPassManager(
892
935
break ;
893
936
case LLVMRustOptStage::PreLinkThinLTO:
894
937
MPM = PB.buildThinLTOPreLinkDefaultPipeline (OptLevel, DebugPassManager);
938
+ #if LLVM_VERSION_GE(11, 0)
939
+ for (const auto &C : OptimizerLastEPCallbacks)
940
+ C (MPM, OptLevel);
941
+ #else
895
942
if (!OptimizerLastEPCallbacks.empty ()) {
896
943
FunctionPassManager FPM (DebugPassManager);
897
944
for (const auto &C : OptimizerLastEPCallbacks)
898
945
C (FPM, OptLevel);
899
946
MPM.addPass (createModuleToFunctionPassAdaptor (std::move (FPM)));
900
947
}
948
+ #endif
901
949
break ;
902
950
case LLVMRustOptStage::PreLinkFatLTO:
903
951
MPM = PB.buildLTOPreLinkDefaultPipeline (OptLevel, DebugPassManager);
@@ -994,10 +1042,10 @@ class RustAssemblyAnnotationWriter : public AssemblyAnnotationWriter {
994
1042
const Value *Value;
995
1043
if (const CallInst *CI = dyn_cast<CallInst>(I)) {
996
1044
Name = " call" ;
997
- Value = CI->getCalledValue ();
1045
+ Value = CI->getCalledOperand ();
998
1046
} else if (const InvokeInst* II = dyn_cast<InvokeInst>(I)) {
999
1047
Name = " invoke" ;
1000
- Value = II->getCalledValue ();
1048
+ Value = II->getCalledOperand ();
1001
1049
} else {
1002
1050
// Could demangle more operations, e. g.
1003
1051
// `store %place, @function`.
@@ -1335,10 +1383,33 @@ LLVMRustFreeThinLTOData(LLVMRustThinLTOData *Data) {
1335
1383
// `ProcessThinLTOModule` function. Here they're split up into separate steps
1336
1384
// so rustc can save off the intermediate bytecode between each step.
1337
1385
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
+
1338
1399
extern " C" bool
1339
- LLVMRustPrepareThinLTORename (const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1400
+ LLVMRustPrepareThinLTORename (const LLVMRustThinLTOData *Data, LLVMModuleRef M,
1401
+ LLVMTargetMachineRef TM) {
1340
1402
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) {
1342
1413
LLVMRustSetLastError (" renameModuleForThinLTO failed" );
1343
1414
return false ;
1344
1415
}
@@ -1362,8 +1433,10 @@ LLVMRustPrepareThinLTOInternalize(const LLVMRustThinLTOData *Data, LLVMModuleRef
1362
1433
}
1363
1434
1364
1435
extern " C" bool
1365
- LLVMRustPrepareThinLTOImport (const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1436
+ LLVMRustPrepareThinLTOImport (const LLVMRustThinLTOData *Data, LLVMModuleRef M,
1437
+ LLVMTargetMachineRef TM) {
1366
1438
Module &Mod = *unwrap (M);
1439
+ TargetMachine &Target = *unwrap (TM);
1367
1440
1368
1441
const auto &ImportList = Data->ImportLists .lookup (Mod.getModuleIdentifier ());
1369
1442
auto Loader = [&](StringRef Identifier) {
@@ -1399,7 +1472,12 @@ LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
1399
1472
1400
1473
return MOrErr;
1401
1474
};
1475
+ #if LLVM_VERSION_GE(11, 0)
1476
+ bool ClearDSOLocal = clearDSOLocalOnDeclarations (Mod, Target);
1477
+ FunctionImporter Importer (Data->Index , Loader, ClearDSOLocal);
1478
+ #else
1402
1479
FunctionImporter Importer (Data->Index , Loader);
1480
+ #endif
1403
1481
Expected<bool > Result = Importer.importFunctions (Mod, ImportList);
1404
1482
if (!Result) {
1405
1483
LLVMRustSetLastError (toString (Result.takeError ()).c_str ());
@@ -1558,22 +1636,11 @@ LLVMRustThinLTOPatchDICompileUnit(LLVMModuleRef Mod, DICompileUnit *Unit) {
1558
1636
}
1559
1637
1560
1638
// 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).
1563
1642
DebugInfoFinder Finder;
1564
1643
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
- }
1577
1644
1578
1645
// After we've found all our debuginfo, rewrite all subprograms to point to
1579
1646
// the same `DICompileUnit`.
0 commit comments