Clean up uses of the unstable dwarf_version option

- Consolidate calculation of the effective value.
- Check the target `DebuginfoKind` instead of using `is_like_msvc`.
This commit is contained in:
Wesley Wiser 2025-01-19 13:04:28 -06:00
parent 4a5f1cc52b
commit 51eaa0d56a
4 changed files with 34 additions and 27 deletions

View file

@ -919,8 +919,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
.unwrap_or_default(); .unwrap_or_default();
let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo); let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo);
let dwarf_version = let dwarf_version = tcx.sess.dwarf_version();
tcx.sess.opts.unstable_opts.dwarf_version.unwrap_or(tcx.sess.target.default_dwarf_version);
let is_dwarf_kind = let is_dwarf_kind =
matches!(tcx.sess.target.debuginfo_kind, DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym); matches!(tcx.sess.target.debuginfo_kind, DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym);
// Don't emit `.debug_pubnames` and `.debug_pubtypes` on DWARFv4 or lower. // Don't emit `.debug_pubnames` and `.debug_pubtypes` on DWARFv4 or lower.

View file

@ -22,6 +22,7 @@ use rustc_session::config::{self, DebugInfo};
use rustc_span::{ use rustc_span::{
BytePos, Pos, SourceFile, SourceFileAndLine, SourceFileHash, Span, StableSourceFileId, Symbol, BytePos, Pos, SourceFile, SourceFileAndLine, SourceFileHash, Span, StableSourceFileId, Symbol,
}; };
use rustc_target::spec::DebuginfoKind;
use smallvec::SmallVec; use smallvec::SmallVec;
use tracing::debug; use tracing::debug;
@ -93,29 +94,31 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
pub(crate) fn finalize(&self, sess: &Session) { pub(crate) fn finalize(&self, sess: &Session) {
unsafe { llvm::LLVMRustDIBuilderFinalize(self.builder) }; unsafe { llvm::LLVMRustDIBuilderFinalize(self.builder) };
if !sess.target.is_like_msvc {
// Debuginfo generation in LLVM by default uses a higher match sess.target.debuginfo_kind {
// version of dwarf than macOS currently understands. We can DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym => {
// instruct LLVM to emit an older version of dwarf, however, // Debuginfo generation in LLVM by default uses a higher
// for macOS to understand. For more info see #11352 // version of dwarf than macOS currently understands. We can
// This can be overridden using --llvm-opts -dwarf-version,N. // instruct LLVM to emit an older version of dwarf, however,
// Android has the same issue (#22398) // for macOS to understand. For more info see #11352
let dwarf_version = // This can be overridden using --llvm-opts -dwarf-version,N.
sess.opts.unstable_opts.dwarf_version.unwrap_or(sess.target.default_dwarf_version); // Android has the same issue (#22398)
llvm::add_module_flag_u32( llvm::add_module_flag_u32(
self.llmod, self.llmod,
llvm::ModuleFlagMergeBehavior::Warning, llvm::ModuleFlagMergeBehavior::Warning,
"Dwarf Version", "Dwarf Version",
dwarf_version, sess.dwarf_version(),
); );
} else { }
// Indicate that we want CodeView debug information on MSVC DebuginfoKind::Pdb => {
llvm::add_module_flag_u32( // Indicate that we want CodeView debug information
self.llmod, llvm::add_module_flag_u32(
llvm::ModuleFlagMergeBehavior::Warning, self.llmod,
"CodeView", llvm::ModuleFlagMergeBehavior::Warning,
1, "CodeView",
); 1,
);
}
} }
// Prevent bitcode readers from deleting the debug info. // Prevent bitcode readers from deleting the debug info.

View file

@ -1803,6 +1803,7 @@ options! {
"output statistics about monomorphization collection"), "output statistics about monomorphization collection"),
dump_mono_stats_format: DumpMonoStatsFormat = (DumpMonoStatsFormat::Markdown, parse_dump_mono_stats, [UNTRACKED], dump_mono_stats_format: DumpMonoStatsFormat = (DumpMonoStatsFormat::Markdown, parse_dump_mono_stats, [UNTRACKED],
"the format to use for -Z dump-mono-stats (`markdown` (default) or `json`)"), "the format to use for -Z dump-mono-stats (`markdown` (default) or `json`)"),
#[rustc_lint_opt_deny_field_access("use `Session::dwarf_version` instead of this field")]
dwarf_version: Option<u32> = (None, parse_opt_number, [TRACKED], dwarf_version: Option<u32> = (None, parse_opt_number, [TRACKED],
"version of DWARF debug information to emit (default: 2 or 4, depending on platform)"), "version of DWARF debug information to emit (default: 2 or 4, depending on platform)"),
dylib_lto: bool = (false, parse_bool, [UNTRACKED], dylib_lto: bool = (false, parse_bool, [UNTRACKED],

View file

@ -732,6 +732,11 @@ impl Session {
self.opts.cg.split_debuginfo.unwrap_or(self.target.split_debuginfo) self.opts.cg.split_debuginfo.unwrap_or(self.target.split_debuginfo)
} }
/// Returns the DWARF version passed on the CLI or the default for the target.
pub fn dwarf_version(&self) -> u32 {
self.opts.unstable_opts.dwarf_version.unwrap_or(self.target.default_dwarf_version)
}
pub fn stack_protector(&self) -> StackProtector { pub fn stack_protector(&self) -> StackProtector {
if self.target.options.supports_stack_protector { if self.target.options.supports_stack_protector {
self.opts.unstable_opts.stack_protector self.opts.unstable_opts.stack_protector
@ -1263,8 +1268,7 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
} }
if sess.opts.unstable_opts.embed_source { if sess.opts.unstable_opts.embed_source {
let dwarf_version = let dwarf_version = sess.dwarf_version();
sess.opts.unstable_opts.dwarf_version.unwrap_or(sess.target.default_dwarf_version);
if dwarf_version < 5 { if dwarf_version < 5 {
sess.dcx().emit_warn(errors::EmbedSourceInsufficientDwarfVersion { dwarf_version }); sess.dcx().emit_warn(errors::EmbedSourceInsufficientDwarfVersion { dwarf_version });