diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index bcadb6f0de929..fde183db42067 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -250,7 +250,7 @@ pub(crate) fn target_machine_factory( let use_emulated_tls = matches!(sess.tls_model(), TlsModel::Emulated); - let debuginfo_compression = match sess.opts.unstable_opts.debuginfo_compression { + let debuginfo_compression = match sess.debuginfo_compression() { config::DebugInfoCompression::None => llvm::CompressionKind::None, config::DebugInfoCompression::Zlib => { if llvm::LLVMRustLLVMHasZlibCompression() { diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index c73e950bed408..f8104b4ec743c 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -756,7 +756,7 @@ impl<'a> Linker for GccLinker<'a> { self.link_arg("--strip-all"); } } - match self.sess.opts.unstable_opts.debuginfo_compression { + match self.sess.debuginfo_compression() { config::DebugInfoCompression::None => {} config::DebugInfoCompression::Zlib => { self.link_arg("--compress-debug-sections=zlib"); diff --git a/compiler/rustc_codegen_ssa/src/traits/backend.rs b/compiler/rustc_codegen_ssa/src/traits/backend.rs index cb74e2e46d650..2984f546ecd5e 100644 --- a/compiler/rustc_codegen_ssa/src/traits/backend.rs +++ b/compiler/rustc_codegen_ssa/src/traits/backend.rs @@ -81,7 +81,7 @@ pub trait CodegenBackend { /// Value printed by `--print=backend-has-zstd`. /// /// Used by compiletest to determine whether tests involving zstd compression - /// (e.g. `-Zdebuginfo-compression=zstd`) should be executed or skipped. + /// (e.g. `-Cdebuginfo-compression=zstd`) should be executed or skipped. fn has_zstd(&self) -> bool { false } diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index f11ad12fb9ddf..1fb32ca83ac6b 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1357,15 +1357,15 @@ pub mod parse { } pub(crate) fn parse_debuginfo_compression( - slot: &mut DebugInfoCompression, + slot: &mut Option, v: Option<&str>, ) -> bool { - match v { - Some("none") => *slot = DebugInfoCompression::None, - Some("zlib") => *slot = DebugInfoCompression::Zlib, - Some("zstd") => *slot = DebugInfoCompression::Zstd, + *slot = Some(match v { + Some("none") => DebugInfoCompression::None, + Some("zlib") => DebugInfoCompression::Zlib, + Some("zstd") => DebugInfoCompression::Zstd, _ => return false, - }; + }); true } @@ -2101,6 +2101,9 @@ options! { debuginfo: DebugInfo = (DebugInfo::None, parse_debuginfo, [TRACKED], "debug info emission level (0-2, none, line-directives-only, \ line-tables-only, limited, or full; default: 0)"), + #[rustc_lint_opt_deny_field_access("use `Session::debuginfo_compression` instead of this field")] + debuginfo_compression: Option = (None, parse_debuginfo_compression, [TRACKED], + "compress debug info sections (none, zlib, zstd, default: none)"), default_linker_libraries: bool = (false, parse_bool, [UNTRACKED], "allow the linker to link its default libraries (default: no)"), dlltool: Option = (None, parse_opt_pathbuf, [UNTRACKED], @@ -2291,7 +2294,8 @@ options! { "emit discriminators and other data necessary for AutoFDO"), debug_info_type_line_numbers: bool = (false, parse_bool, [TRACKED], "emit type and line information for additional data types (default: no)"), - debuginfo_compression: DebugInfoCompression = (DebugInfoCompression::None, parse_debuginfo_compression, [TRACKED], + #[rustc_lint_opt_deny_field_access("use `Session::debuginfo_compression` instead of this field")] + debuginfo_compression: Option = (None, parse_debuginfo_compression, [TRACKED], "compress debug info sections (none, zlib, zstd, default: none)"), deduplicate_diagnostics: bool = (true, parse_bool, [UNTRACKED], "deduplicate identical diagnostics (default: yes)"), diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 1a0ec600af47d..57b61174ed998 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -37,8 +37,9 @@ use rustc_target::spec::{ use crate::code_stats::CodeStats; pub use crate::code_stats::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo}; use crate::config::{ - self, CoverageLevel, CoverageOptions, CrateType, DebugInfo, ErrorOutputType, FunctionReturn, - Input, InstrumentCoverage, OptLevel, OutFileName, OutputType, SwitchWithOptPath, + self, CoverageLevel, CoverageOptions, CrateType, DebugInfo, DebugInfoCompression, + ErrorOutputType, FunctionReturn, Input, InstrumentCoverage, OptLevel, OutFileName, OutputType, + SwitchWithOptPath, }; use crate::filesearch::FileSearch; use crate::lint::LintId; @@ -713,6 +714,14 @@ impl Session { .unwrap_or(self.target.default_dwarf_version) } + pub fn debuginfo_compression(&self) -> DebugInfoCompression { + self.opts + .cg + .debuginfo_compression + .or(self.opts.unstable_opts.debuginfo_compression) + .unwrap_or(DebugInfoCompression::None) + } + pub fn stack_protector(&self) -> StackProtector { if self.target.options.supports_stack_protector { self.opts.unstable_opts.stack_protector diff --git a/src/doc/rustc/src/codegen-options/index.md b/src/doc/rustc/src/codegen-options/index.md index f0f991ed0c909..c4400db6caf4b 100644 --- a/src/doc/rustc/src/codegen-options/index.md +++ b/src/doc/rustc/src/codegen-options/index.md @@ -91,6 +91,26 @@ following values: Note: The [`-g` flag][option-g-debug] is an alias for `-C debuginfo=2`. +## debuginfo-compression + +This flag controls how debug information is compressed. It takes one of the +following values: + +* `none`: debug info is not compressed (the default). +* `zlib`: debug info is compressed using zlib. +* `zstd`: debug info is compressed using zstd. + +Tools which read debug info such as debuggers and profilers must support the +compression algorithm or they will be unable to process the debug info. `zlib` +compression tends to be well supported by Linux tools but `zstd` may require +very recent versions of such tools. + +Compressing debug info can improve binary size but will increase time and memory +used during compilation. + +This flag is ignored by the compiler if the target does not support the specified +compression algorithm. + ## default-linker-libraries This flag controls whether or not the linker includes its default libraries. diff --git a/tests/run-make/compressed-debuginfo-zstd/rmake.rs b/tests/run-make/compressed-debuginfo-zstd/rmake.rs index 8d7e5c089daa1..0e94fe6c11cc8 100644 --- a/tests/run-make/compressed-debuginfo-zstd/rmake.rs +++ b/tests/run-make/compressed-debuginfo-zstd/rmake.rs @@ -13,7 +13,7 @@ use run_make_support::{Rustc, llvm_readobj, run_in_tmpdir}; fn check_compression(compression: &str, to_find: &str) { // check compressed debug sections via rustc flag prepare_and_check(to_find, |rustc| { - rustc.arg(&format!("-Zdebuginfo-compression={compression}")) + rustc.arg(&format!("-Cdebuginfo-compression={compression}")) }); // check compressed debug sections via rust-lld flag diff --git a/tests/run-make/compressed-debuginfo/rmake.rs b/tests/run-make/compressed-debuginfo/rmake.rs index 5ba1a1852d51b..19e915af6bc58 100644 --- a/tests/run-make/compressed-debuginfo/rmake.rs +++ b/tests/run-make/compressed-debuginfo/rmake.rs @@ -14,7 +14,7 @@ fn check_compression(compression: &str, to_find: &str) { .crate_type("lib") .emit("obj") .arg("-Cdebuginfo=full") - .arg(&format!("-Zdebuginfo-compression={compression}")) + .arg(&format!("-Cdebuginfo-compression={compression}")) .input("foo.rs") .run(); let stderr = out.stderr_utf8();