Skip to content

Commit

Permalink
fix: stop emitting .debug_pubnames and .debug_pubtypes
Browse files Browse the repository at this point in the history
`.debug_pubnames` and `.debug_pubtypes` are poorly designed and people
seldom use them. However, they take a considerable portion of size in
the final binary. This tells LLVM stop emitting those sections on
DWARFv4 or lower. DWARFv5 use `.debug_names` which is more concise
in size and performant for name lookup.
  • Loading branch information
weihanglo committed Nov 17, 2023
1 parent 9144d51 commit 30cd8d4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
8 changes: 8 additions & 0 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::debuginfo::utils::FatPtrKind;
use crate::llvm;
use crate::llvm::debuginfo::{
DIDescriptor, DIFile, DIFlags, DILexicalBlock, DIScope, DIType, DebugEmissionKind,
DebugNameTableKind,
};
use crate::value::Value;

Expand Down Expand Up @@ -878,6 +879,12 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
let split_name = split_name.to_str().unwrap();
let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo);

let dwarf_version =
tcx.sess.opts.unstable_opts.dwarf_version.unwrap_or(tcx.sess.target.default_dwarf_version);
// Don't emit `.debug_pubnames` and `.debug_pubtypes` on DWARFv4 or lower.
let debug_name_table_kind =
if dwarf_version > 4 { DebugNameTableKind::Default } else { DebugNameTableKind::None };

unsafe {
let compile_unit_file = llvm::LLVMRustDIBuilderCreateFile(
debug_context.builder,
Expand Down Expand Up @@ -907,6 +914,7 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
kind,
0,
tcx.sess.opts.unstable_opts.split_dwarf_inlining,
debug_name_table_kind,
);

if tcx.sess.opts.unstable_opts.profile {
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::debuginfo::{
DIArray, DIBasicType, DIBuilder, DICompositeType, DIDerivedType, DIDescriptor, DIEnumerator,
DIFile, DIFlags, DIGlobalVariableExpression, DILexicalBlock, DILocation, DINameSpace,
DISPFlags, DIScope, DISubprogram, DISubrange, DITemplateTypeParameter, DIType, DIVariable,
DebugEmissionKind,
DebugEmissionKind, DebugNameTableKind,
};

use libc::{c_char, c_int, c_uint, size_t};
Expand Down Expand Up @@ -793,6 +793,15 @@ pub mod debuginfo {
}
}
}

/// LLVMRustDebugNameTableKind
#[derive(Clone, Copy)]
#[repr(C)]
pub enum DebugNameTableKind {
Default,
Gnu,
None,
}
}

use bitflags::bitflags;
Expand Down Expand Up @@ -1783,6 +1792,7 @@ extern "C" {
kind: DebugEmissionKind,
DWOId: u64,
SplitDebugInlining: bool,
DebugNameTableKind: DebugNameTableKind,
) -> &'a DIDescriptor;

pub fn LLVMRustDIBuilderCreateFile<'a>(
Expand Down
25 changes: 23 additions & 2 deletions compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,25 @@ static DICompileUnit::DebugEmissionKind fromRust(LLVMRustDebugEmissionKind Kind)
}
}

enum class LLVMRustDebugNameTableKind {
Default,
GNU,
None,
};

static DICompileUnit::DebugNameTableKind fromRust(LLVMRustDebugNameTableKind Kind) {
switch (Kind) {
case LLVMRustDebugNameTableKind::Default:
return DICompileUnit::DebugNameTableKind::Default;
case LLVMRustDebugNameTableKind::GNU:
return DICompileUnit::DebugNameTableKind::GNU;
case LLVMRustDebugNameTableKind::None:
return DICompileUnit::DebugNameTableKind::None;
default:
report_fatal_error("bad DebugNameTableKind.");
}
}

enum class LLVMRustChecksumKind {
None,
MD5,
Expand Down Expand Up @@ -795,13 +814,15 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateCompileUnit(
const char *Flags, unsigned RuntimeVer,
const char *SplitName, size_t SplitNameLen,
LLVMRustDebugEmissionKind Kind,
uint64_t DWOId, bool SplitDebugInlining) {
uint64_t DWOId, bool SplitDebugInlining,
LLVMRustDebugNameTableKind TableKind) {
auto *File = unwrapDI<DIFile>(FileRef);

return wrap(Builder->createCompileUnit(Lang, File, StringRef(Producer, ProducerLen),
isOptimized, Flags, RuntimeVer,
StringRef(SplitName, SplitNameLen),
fromRust(Kind), DWOId, SplitDebugInlining));
fromRust(Kind), DWOId, SplitDebugInlining,
false, fromRust(TableKind)));
}

extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateFile(
Expand Down

0 comments on commit 30cd8d4

Please sign in to comment.