Skip to content

Commit 8159f38

Browse files
committed
Auto merge of #59710 - alexcrichton:llvm-9-compat, r=sanxiyn
rustc: Start implementing compat with LLVM 9 This commit doesn't actually migrate to LLVM 9, but it brings our own C++ bindings in line with LLVM 9 and able to compile against tip of tree. The changes made were: * The `MainSubprogram` flag for debuginfo moved between flag types. * Iteration of archive members was tweaked slightly and we have to construct the two iterators before constructing the returned `RustArchiveIterator` value. * The `getOrInsertFunction` binding now returns a wrapper which we use `getCallee()` on to get the value we're interested in.
2 parents 209b0b4 + d5985bc commit 8159f38

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

src/librustc_codegen_llvm/debuginfo/mod.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,6 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
296296

297297
let mut flags = DIFlags::FlagPrototyped;
298298

299-
if let Some((id, _)) = self.tcx.entry_fn(LOCAL_CRATE) {
300-
if id == def_id {
301-
flags |= DIFlags::FlagMainSubprogram;
302-
}
303-
}
304-
305299
if self.layout_of(sig.output()).abi.is_uninhabited() {
306300
flags |= DIFlags::FlagNoReturn;
307301
}
@@ -313,6 +307,11 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
313307
if self.sess().opts.optimize != config::OptLevel::No {
314308
spflags |= DISPFlags::SPFlagOptimized;
315309
}
310+
if let Some((id, _)) = self.tcx.entry_fn(LOCAL_CRATE) {
311+
if id == def_id {
312+
spflags |= DISPFlags::SPFlagMainSubprogram;
313+
}
314+
}
316315

317316
let fn_metadata = unsafe {
318317
llvm::LLVMRustDIBuilderCreateFunction(

src/librustc_codegen_llvm/llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,6 @@ pub mod debuginfo {
588588
const FlagIntroducedVirtual = (1 << 18);
589589
const FlagBitField = (1 << 19);
590590
const FlagNoReturn = (1 << 20);
591-
const FlagMainSubprogram = (1 << 21);
592591
}
593592
}
594593

@@ -603,6 +602,7 @@ pub mod debuginfo {
603602
const SPFlagLocalToUnit = (1 << 2);
604603
const SPFlagDefinition = (1 << 3);
605604
const SPFlagOptimized = (1 << 4);
605+
const SPFlagMainSubprogram = (1 << 5);
606606
}
607607
}
608608

src/rustllvm/ArchiveWrapper.cpp

+16-12
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ struct RustArchiveIterator {
2424
bool First;
2525
Archive::child_iterator Cur;
2626
Archive::child_iterator End;
27-
Error Err;
28-
29-
RustArchiveIterator() : First(true), Err(Error::success()) {}
27+
std::unique_ptr<Error> Err;
28+
29+
RustArchiveIterator(Archive::child_iterator Cur, Archive::child_iterator End,
30+
std::unique_ptr<Error> Err)
31+
: First(true),
32+
Cur(Cur),
33+
End(End),
34+
Err(std::move(Err)) {}
3035
};
3136

3237
enum class LLVMRustArchiveKind {
@@ -84,15 +89,14 @@ extern "C" void LLVMRustDestroyArchive(LLVMRustArchiveRef RustArchive) {
8489
extern "C" LLVMRustArchiveIteratorRef
8590
LLVMRustArchiveIteratorNew(LLVMRustArchiveRef RustArchive) {
8691
Archive *Archive = RustArchive->getBinary();
87-
RustArchiveIterator *RAI = new RustArchiveIterator();
88-
RAI->Cur = Archive->child_begin(RAI->Err);
89-
if (RAI->Err) {
90-
LLVMRustSetLastError(toString(std::move(RAI->Err)).c_str());
91-
delete RAI;
92+
std::unique_ptr<Error> Err = llvm::make_unique<Error>(Error::success());
93+
auto Cur = Archive->child_begin(*Err);
94+
if (*Err) {
95+
LLVMRustSetLastError(toString(std::move(*Err)).c_str());
9296
return nullptr;
9397
}
94-
RAI->End = Archive->child_end();
95-
return RAI;
98+
auto End = Archive->child_end();
99+
return new RustArchiveIterator(Cur, End, std::move(Err));
96100
}
97101

98102
extern "C" LLVMRustArchiveChildConstRef
@@ -108,8 +112,8 @@ LLVMRustArchiveIteratorNext(LLVMRustArchiveIteratorRef RAI) {
108112
// but instead advance it *before* fetching the child in all later calls.
109113
if (!RAI->First) {
110114
++RAI->Cur;
111-
if (RAI->Err) {
112-
LLVMRustSetLastError(toString(std::move(RAI->Err)).c_str());
115+
if (*RAI->Err) {
116+
LLVMRustSetLastError(toString(std::move(*RAI->Err)).c_str());
113117
return nullptr;
114118
}
115119
} else {

src/rustllvm/RustWrapper.cpp

+23-8
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ extern "C" LLVMValueRef LLVMRustGetOrInsertFunction(LLVMModuleRef M,
117117
const char *Name,
118118
LLVMTypeRef FunctionTy) {
119119
return wrap(
120-
unwrap(M)->getOrInsertFunction(Name, unwrap<FunctionType>(FunctionTy)));
120+
unwrap(M)->getOrInsertFunction(Name, unwrap<FunctionType>(FunctionTy))
121+
#if LLVM_VERSION_GE(9, 0)
122+
.getCallee()
123+
#endif
124+
);
121125
}
122126

123127
extern "C" LLVMValueRef
@@ -417,7 +421,6 @@ enum class LLVMRustDIFlags : uint32_t {
417421
FlagIntroducedVirtual = (1 << 18),
418422
FlagBitField = (1 << 19),
419423
FlagNoReturn = (1 << 20),
420-
FlagMainSubprogram = (1 << 21),
421424
// Do not add values that are not supported by the minimum LLVM
422425
// version we support! see llvm/include/llvm/IR/DebugInfoFlags.def
423426
};
@@ -508,9 +511,6 @@ static DINode::DIFlags fromRust(LLVMRustDIFlags Flags) {
508511
if (isSet(Flags & LLVMRustDIFlags::FlagNoReturn)) {
509512
Result |= DINode::DIFlags::FlagNoReturn;
510513
}
511-
if (isSet(Flags & LLVMRustDIFlags::FlagMainSubprogram)) {
512-
Result |= DINode::DIFlags::FlagMainSubprogram;
513-
}
514514

515515
return Result;
516516
}
@@ -525,6 +525,7 @@ enum class LLVMRustDISPFlags : uint32_t {
525525
SPFlagLocalToUnit = (1 << 2),
526526
SPFlagDefinition = (1 << 3),
527527
SPFlagOptimized = (1 << 4),
528+
SPFlagMainSubprogram = (1 << 5),
528529
// Do not add values that are not supported by the minimum LLVM
529530
// version we support! see llvm/include/llvm/IR/DebugInfoFlags.def
530531
// (In LLVM < 8, createFunction supported these as separate bool arguments.)
@@ -575,6 +576,11 @@ static DISubprogram::DISPFlags fromRust(LLVMRustDISPFlags SPFlags) {
575576
if (isSet(SPFlags & LLVMRustDISPFlags::SPFlagOptimized)) {
576577
Result |= DISubprogram::DISPFlags::SPFlagOptimized;
577578
}
579+
#if LLVM_VERSION_GE(9, 0)
580+
if (isSet(SPFlags & LLVMRustDISPFlags::SPFlagMainSubprogram)) {
581+
Result |= DISubprogram::DISPFlags::SPFlagMainSubprogram;
582+
}
583+
#endif
578584

579585
return Result;
580586
}
@@ -671,18 +677,27 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateFunction(
671677
DITemplateParameterArray TParams =
672678
DITemplateParameterArray(unwrap<MDTuple>(TParam));
673679
#if LLVM_VERSION_GE(8, 0)
680+
DISubprogram::DISPFlags llvmSPFlags = fromRust(SPFlags);
681+
DINode::DIFlags llvmFlags = fromRust(Flags);
682+
#if LLVM_VERSION_LT(9, 0)
683+
if (isSet(SPFlags & LLVMRustDISPFlags::SPFlagMainSubprogram))
684+
llvmFlags |= DINode::DIFlags::FlagMainSubprogram;
685+
#endif
674686
DISubprogram *Sub = Builder->createFunction(
675687
unwrapDI<DIScope>(Scope), Name, LinkageName, unwrapDI<DIFile>(File),
676-
LineNo, unwrapDI<DISubroutineType>(Ty), ScopeLine, fromRust(Flags),
677-
fromRust(SPFlags), TParams, unwrapDIPtr<DISubprogram>(Decl));
688+
LineNo, unwrapDI<DISubroutineType>(Ty), ScopeLine, llvmFlags,
689+
llvmSPFlags, TParams, unwrapDIPtr<DISubprogram>(Decl));
678690
#else
679691
bool IsLocalToUnit = isSet(SPFlags & LLVMRustDISPFlags::SPFlagLocalToUnit);
680692
bool IsDefinition = isSet(SPFlags & LLVMRustDISPFlags::SPFlagDefinition);
681693
bool IsOptimized = isSet(SPFlags & LLVMRustDISPFlags::SPFlagOptimized);
694+
DINode::DIFlags llvmFlags = fromRust(Flags);
695+
if (isSet(SPFlags & LLVMRustDISPFlags::SPFlagMainSubprogram))
696+
llvmFlags |= DINode::DIFlags::FlagMainSubprogram;
682697
DISubprogram *Sub = Builder->createFunction(
683698
unwrapDI<DIScope>(Scope), Name, LinkageName, unwrapDI<DIFile>(File),
684699
LineNo, unwrapDI<DISubroutineType>(Ty), IsLocalToUnit, IsDefinition,
685-
ScopeLine, fromRust(Flags), IsOptimized, TParams,
700+
ScopeLine, llvmFlags, IsOptimized, TParams,
686701
unwrapDIPtr<DISubprogram>(Decl));
687702
#endif
688703
unwrap<Function>(Fn)->setSubprogram(Sub);

0 commit comments

Comments
 (0)