Skip to content

Commit

Permalink
Auto merge of rust-lang#117962 - weihanglo:debug-name-table, r=<try>
Browse files Browse the repository at this point in the history
fix: stop `emitting .debug_pubnames` and `.debug_pubtypes`

A continuation of rust-lang#94181.
Fixes rust-lang#48762
MCP can be found in <rust-lang/compiler-team#688>.

`.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.

Some other no-really-useful personal notes:

<details><summary>Details</summary>
<p>

## Pepole saying they are not useful

* rust-lang#48762
* https://rust-lang.zulipchat.com/#narrow/stream/317568-t-compiler.2Fwg-debugging/topic/investigating.20debuginfo.20size/near/342713604
* `DwarfCompileUnit::hasDwarfPubSections()` — https://github.com/llvm/llvm-project/blob/f633f325a1b808d33ca9653ed373353549ddcde6/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp#L1477-L1494
* clang default to no debug name table when no option provided — https://github.com/llvm/llvm-project/blob/f633f325a1b808d33ca9653ed373353549ddcde6/clang/lib/Frontend/CompilerInvocation.cpp#L1819-L1824
* GCC explicitly says GDB doesn't use pub sections (`TARGET_WANT_DEBUG_PUB_SECTIONS` only be true on Darwin) — https://github.com/gcc-mirror/gcc/blob/5d2a360f0a541646abb11efdbabc33c6a04de7ee/gcc/target.def#L6985-L6990 and https://github.com/bminor/binutils-gdb/blob/319b460545dc79280e2904dcc280057cf71fb753/gold/dwarf_reader.h#L424-L427
* Probably the only place that makes use of pub section in lldb — https://github.com/llvm/llvm-project/blob/725115d7bba2faf3d0c21442f4661dea77b8a77c/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp#L2117-L2135

* "The -gsplit-dwarf option requires -ggnu-pubnames." — https://github.com/gcc-mirror/gcc/blob/5d2a360f0a541646abb11efdbabc33c6a04de7ee/gcc/opts.cc#L1205

* LLVM: Always emit `.debug_names` with dwarf 5 for Apple platforms — https://reviews.llvm.org/D118754

</p>
</details>
  • Loading branch information
bors committed Dec 11, 2023
2 parents 5701093 + 1667f3d commit 7db8714
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
8 changes: 8 additions & 0 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
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
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 @@ -794,6 +794,15 @@ pub mod debuginfo {
}
}
}

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

use bitflags::bitflags;
Expand Down Expand Up @@ -1812,6 +1821,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
Expand Up @@ -697,6 +697,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 @@ -765,13 +784,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
24 changes: 24 additions & 0 deletions tests/assembly/dwarf4.rs
@@ -0,0 +1,24 @@
// Makes sure that `-Z dwarf-version=4` causes `rustc` to emit DWARF version 4.
// assembly-output: emit-asm
// compile-flags: -g --target x86_64-unknown-linux-gnu -Z dwarf-version=4 -Copt-level=0
// needs-llvm-components: x86

#![feature(no_core, lang_items)]
#![crate_type = "rlib"]
#![no_core]

#[lang = "sized"]
trait Sized {}
#[lang = "copy"]
trait Copy {}

pub fn wibble() {}

pub struct X;

// CHECK: .section .debug_info
// CHECK-NOT: .short 2
// CHECK-NOT: .short 5
// CHECK: .short 4
// CHECK-NOT: .section .debug_pubnames
// CHECK-NOT: .section .debug_pubtypes
1 change: 1 addition & 0 deletions tests/assembly/dwarf5.rs
Expand Up @@ -18,3 +18,4 @@ pub fn wibble() {}
// CHECK-NOT: .short 2
// CHECK-NOT: .short 4
// CHECK: .short 5
// CHECK: .section .debug_names

0 comments on commit 7db8714

Please sign in to comment.