Skip to content

Commit 341aa97

Browse files
committed
llvm: update ffi bindings for split dwarf
This commit modifies the FFI bindings to LLVM required for Split DWARF support in rustc. In particular: - `addPassesToEmitFile`'s wrapper, `LLVMRustWriteOutputFile` now takes a `DwoPath` `const char*`. When disabled, `nullptr` should be provided which will preserve existing behaviour. When enabled, the path to the `.dwo` file should be provided. - `createCompileUnit`'s wrapper, `LLVMRustDIBuilderCreateCompileUnit` now has two additional arguments, for the `DWOId` and to enable `SplitDebugInlining`. `DWOId` should always be zero. - `createTargetMachine`'s wrapper, `LLVMRustCreateTargetMachine` has an additional argument which should be provided the path to the `.dwo` when enabled. Signed-off-by: David Wood <[email protected]>
1 parent ddbc617 commit 341aa97

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ pub fn write_output_file(
5353
) -> Result<(), FatalError> {
5454
unsafe {
5555
let output_c = path_to_c_string(output);
56-
let result = llvm::LLVMRustWriteOutputFile(target, pm, m, output_c.as_ptr(), file_type);
56+
let result = llvm::LLVMRustWriteOutputFile(
57+
target,
58+
pm,
59+
m,
60+
output_c.as_ptr(),
61+
std::ptr::null(),
62+
file_type,
63+
);
5764
result.into_result().map_err(|()| {
5865
let msg = format!("could not write output to {}", output.display());
5966
llvm_err(handler, &msg)
@@ -164,6 +171,7 @@ pub fn target_machine_factory(
164171
!sess.opts.debugging_opts.use_ctors_section.unwrap_or(sess.target.use_ctors_section);
165172

166173
Arc::new(move || {
174+
let split_dwarf_file = std::ptr::null();
167175
let tm = unsafe {
168176
llvm::LLVMRustCreateTargetMachine(
169177
triple.as_ptr(),
@@ -182,6 +190,7 @@ pub fn target_machine_factory(
182190
emit_stack_size_section,
183191
relax_elf_relocations,
184192
use_init_array,
193+
split_dwarf_file,
185194
)
186195
};
187196

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,8 @@ pub fn compile_unit_metadata(
10391039
split_name.as_ptr().cast(),
10401040
split_name.len(),
10411041
kind,
1042+
0,
1043+
true,
10421044
);
10431045

10441046
if tcx.sess.opts.debugging_opts.profile {

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,8 @@ extern "C" {
18301830
SplitName: *const c_char,
18311831
SplitNameLen: size_t,
18321832
kind: DebugEmissionKind,
1833+
DWOId: u64,
1834+
SplitDebugInlining: bool,
18331835
) -> &'a DIDescriptor;
18341836

18351837
pub fn LLVMRustDIBuilderCreateFile(
@@ -2151,6 +2153,7 @@ extern "C" {
21512153
EmitStackSizeSection: bool,
21522154
RelaxELFRelocations: bool,
21532155
UseInitArray: bool,
2156+
SplitDwarfFile: *const c_char,
21542157
) -> Option<&'static mut TargetMachine>;
21552158
pub fn LLVMRustDisposeTargetMachine(T: &'static mut TargetMachine);
21562159
pub fn LLVMRustAddBuilderLibraryInfo(
@@ -2179,6 +2182,7 @@ extern "C" {
21792182
PM: &PassManager<'a>,
21802183
M: &'a Module,
21812184
Output: *const c_char,
2185+
DwoOutput: *const c_char,
21822186
FileType: FileType,
21832187
) -> LLVMRustResult;
21842188
pub fn LLVMRustOptimizeWithNewPassManager(

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,8 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
450450
bool AsmComments,
451451
bool EmitStackSizeSection,
452452
bool RelaxELFRelocations,
453-
bool UseInitArray) {
453+
bool UseInitArray,
454+
const char *SplitDwarfFile) {
454455

455456
auto OptLevel = fromRust(RustOptLevel);
456457
auto RM = fromRust(RustReloc);
@@ -476,6 +477,9 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
476477
Options.MCOptions.AsmVerbose = AsmComments;
477478
Options.MCOptions.PreserveAsmComments = AsmComments;
478479
Options.MCOptions.ABIName = ABIStr;
480+
if (SplitDwarfFile) {
481+
Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
482+
}
479483
Options.RelaxELFRelocations = RelaxELFRelocations;
480484
Options.UseInitArray = UseInitArray;
481485

@@ -610,7 +614,7 @@ static TargetMachine::CodeGenFileType fromRust(LLVMRustFileType Type) {
610614

611615
extern "C" LLVMRustResult
612616
LLVMRustWriteOutputFile(LLVMTargetMachineRef Target, LLVMPassManagerRef PMR,
613-
LLVMModuleRef M, const char *Path,
617+
LLVMModuleRef M, const char *Path, const char *DwoPath,
614618
LLVMRustFileType RustFileType) {
615619
llvm::legacy::PassManager *PM = unwrap<llvm::legacy::PassManager>(PMR);
616620
auto FileType = fromRust(RustFileType);
@@ -626,8 +630,22 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target, LLVMPassManagerRef PMR,
626630
}
627631

628632
buffer_ostream BOS(OS);
629-
unwrap(Target)->addPassesToEmitFile(*PM, BOS, nullptr, FileType, false);
630-
PM->run(*unwrap(M));
633+
if (DwoPath) {
634+
raw_fd_ostream DOS(DwoPath, EC, sys::fs::F_None);
635+
EC.clear();
636+
if (EC)
637+
ErrorInfo = EC.message();
638+
if (ErrorInfo != "") {
639+
LLVMRustSetLastError(ErrorInfo.c_str());
640+
return LLVMRustResult::Failure;
641+
}
642+
buffer_ostream DBOS(DOS);
643+
unwrap(Target)->addPassesToEmitFile(*PM, BOS, &DBOS, FileType, false);
644+
PM->run(*unwrap(M));
645+
} else {
646+
unwrap(Target)->addPassesToEmitFile(*PM, BOS, nullptr, FileType, false);
647+
PM->run(*unwrap(M));
648+
}
631649

632650
// Apparently `addPassesToEmitFile` adds a pointer to our on-the-stack output
633651
// stream (OS), so the only real safe place to delete this is here? Don't we

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,13 +690,14 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateCompileUnit(
690690
const char *Producer, size_t ProducerLen, bool isOptimized,
691691
const char *Flags, unsigned RuntimeVer,
692692
const char *SplitName, size_t SplitNameLen,
693-
LLVMRustDebugEmissionKind Kind) {
693+
LLVMRustDebugEmissionKind Kind,
694+
uint64_t DWOId, bool SplitDebugInlining) {
694695
auto *File = unwrapDI<DIFile>(FileRef);
695696

696697
return wrap(Builder->createCompileUnit(Lang, File, StringRef(Producer, ProducerLen),
697698
isOptimized, Flags, RuntimeVer,
698699
StringRef(SplitName, SplitNameLen),
699-
fromRust(Kind)));
700+
fromRust(Kind), DWOId, SplitDebugInlining));
700701
}
701702

702703
extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateFile(

0 commit comments

Comments
 (0)