Skip to content

Commit 45ec25e

Browse files
authored
Rollup merge of #73525 - cuviper:llvm11, r=nikic
Prepare for LLVM 11 These are just the code changes needed to build with the current LLVM master (version 11). r? @nikic
2 parents 6a944c1 + 49f6166 commit 45ec25e

File tree

8 files changed

+167
-57
lines changed

8 files changed

+167
-57
lines changed

src/libprofiler_builtins/build.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ fn main() {
2424
"InstrProfilingUtil.c",
2525
"InstrProfilingValue.c",
2626
"InstrProfilingWriter.c",
27+
// This file was renamed in LLVM 10.
28+
"InstrProfilingRuntime.cc",
29+
"InstrProfilingRuntime.cpp",
30+
// These files were added in LLVM 11.
31+
"InstrProfilingInternal.c",
32+
"InstrProfilingBiasVar.c",
2733
];
2834

2935
if target.contains("msvc") {
@@ -69,14 +75,12 @@ fn main() {
6975

7076
let src_root = root.join("lib").join("profile");
7177
for src in profile_sources {
72-
cfg.file(src_root.join(src));
78+
let path = src_root.join(src);
79+
if path.exists() {
80+
cfg.file(path);
81+
}
7382
}
7483

75-
// The file was renamed in LLVM 10.
76-
let old_runtime_path = src_root.join("InstrProfilingRuntime.cc");
77-
let new_runtime_path = src_root.join("InstrProfilingRuntime.cpp");
78-
cfg.file(if old_runtime_path.exists() { old_runtime_path } else { new_runtime_path });
79-
8084
cfg.include(root.join("include"));
8185
cfg.warnings(false);
8286
cfg.compile("profiler-rt");

src/librustc_codegen_llvm/back/lto.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ pub unsafe fn optimize_thin_module(
797797
kind: ModuleKind::Regular,
798798
};
799799
{
800+
let target = &*module.module_llvm.tm;
800801
let llmod = module.module_llvm.llmod();
801802
save_temp_bitcode(&cgcx, &module, "thin-lto-input");
802803

@@ -833,7 +834,7 @@ pub unsafe fn optimize_thin_module(
833834
{
834835
let _timer =
835836
cgcx.prof.generic_activity_with_arg("LLVM_thin_lto_rename", thin_module.name());
836-
if !llvm::LLVMRustPrepareThinLTORename(thin_module.shared.data.0, llmod) {
837+
if !llvm::LLVMRustPrepareThinLTORename(thin_module.shared.data.0, llmod, target) {
837838
let msg = "failed to prepare thin LTO module";
838839
return Err(write::llvm_err(&diag_handler, msg));
839840
}
@@ -865,7 +866,7 @@ pub unsafe fn optimize_thin_module(
865866
{
866867
let _timer =
867868
cgcx.prof.generic_activity_with_arg("LLVM_thin_lto_import", thin_module.name());
868-
if !llvm::LLVMRustPrepareThinLTOImport(thin_module.shared.data.0, llmod) {
869+
if !llvm::LLVMRustPrepareThinLTOImport(thin_module.shared.data.0, llmod, target) {
869870
let msg = "failed to prepare thin LTO module";
870871
return Err(write::llvm_err(&diag_handler, msg));
871872
}

src/librustc_codegen_llvm/llvm/ffi.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ pub enum TypeKind {
233233
Metadata = 14,
234234
X86_MMX = 15,
235235
Token = 16,
236+
ScalableVector = 17,
237+
BFloat = 18,
236238
}
237239

238240
impl TypeKind {
@@ -255,6 +257,8 @@ impl TypeKind {
255257
TypeKind::Metadata => rustc_codegen_ssa::common::TypeKind::Metadata,
256258
TypeKind::X86_MMX => rustc_codegen_ssa::common::TypeKind::X86_MMX,
257259
TypeKind::Token => rustc_codegen_ssa::common::TypeKind::Token,
260+
TypeKind::ScalableVector => rustc_codegen_ssa::common::TypeKind::ScalableVector,
261+
TypeKind::BFloat => rustc_codegen_ssa::common::TypeKind::BFloat,
258262
}
259263
}
260264
}
@@ -2141,10 +2145,18 @@ extern "C" {
21412145
PreservedSymbols: *const *const c_char,
21422146
PreservedSymbolsLen: c_uint,
21432147
) -> Option<&'static mut ThinLTOData>;
2144-
pub fn LLVMRustPrepareThinLTORename(Data: &ThinLTOData, Module: &Module) -> bool;
2148+
pub fn LLVMRustPrepareThinLTORename(
2149+
Data: &ThinLTOData,
2150+
Module: &Module,
2151+
Target: &TargetMachine,
2152+
) -> bool;
21452153
pub fn LLVMRustPrepareThinLTOResolveWeak(Data: &ThinLTOData, Module: &Module) -> bool;
21462154
pub fn LLVMRustPrepareThinLTOInternalize(Data: &ThinLTOData, Module: &Module) -> bool;
2147-
pub fn LLVMRustPrepareThinLTOImport(Data: &ThinLTOData, Module: &Module) -> bool;
2155+
pub fn LLVMRustPrepareThinLTOImport(
2156+
Data: &ThinLTOData,
2157+
Module: &Module,
2158+
Target: &TargetMachine,
2159+
) -> bool;
21482160
pub fn LLVMRustGetThinLTOModuleImports(
21492161
Data: *const ThinLTOData,
21502162
ModuleNameCallback: ThinLTOModuleNameCallback,

src/librustc_codegen_ssa/common.rs

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ pub enum TypeKind {
9898
Metadata,
9999
X86_MMX,
100100
Token,
101+
ScalableVector,
102+
BFloat,
101103
}
102104

103105
// FIXME(mw): Anything that is produced via DepGraph::with_task() must implement

src/rustllvm/PassWrapper.cpp

+93-26
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
4949

5050
DEFINE_STDCXX_CONVERSION_FUNCTIONS(Pass, LLVMPassRef)
5151
DEFINE_STDCXX_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
52+
#if LLVM_VERSION_LT(11, 0)
5253
DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBuilder,
5354
LLVMPassManagerBuilderRef)
55+
#endif
5456

5557
extern "C" void LLVMInitializePasses() {
5658
PassRegistry &Registry = *PassRegistry::getPassRegistry();
@@ -343,17 +345,17 @@ enum class LLVMRustPassBuilderOptLevel {
343345
static 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+
13381399
extern "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

13641435
extern "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

Comments
 (0)