From 5c91f2c4d3635192cbed539c0c3ef88e4bfdd2a5 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 8 Dec 2025 22:41:13 +1100 Subject: [PATCH] Make `--print=backend-has-zstd` work by default on any backend Using a defaulted `CodegenBackend` method that querying for zstd support should automatically print a safe value of `false` on any backend that doesn't specifically indicate the presence or absence of zstd. --- compiler/rustc_codegen_cranelift/src/lib.rs | 12 +----------- compiler/rustc_codegen_llvm/src/lib.rs | 8 ++++---- compiler/rustc_codegen_ssa/src/traits/backend.rs | 8 ++++++++ compiler/rustc_driver_impl/src/lib.rs | 5 ++++- src/tools/compiletest/src/directives/needs.rs | 4 ++++ 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/lib.rs b/compiler/rustc_codegen_cranelift/src/lib.rs index b47b9afa4f073..5fdecd014ac05 100644 --- a/compiler/rustc_codegen_cranelift/src/lib.rs +++ b/compiler/rustc_codegen_cranelift/src/lib.rs @@ -47,7 +47,7 @@ use rustc_codegen_ssa::{CodegenResults, TargetConfig}; use rustc_log::tracing::info; use rustc_middle::dep_graph::{WorkProduct, WorkProductId}; use rustc_session::Session; -use rustc_session::config::{OutputFilenames, PrintKind, PrintRequest}; +use rustc_session::config::OutputFilenames; use rustc_span::{Symbol, sym}; use rustc_target::spec::{Abi, Arch, Env, Os}; @@ -160,16 +160,6 @@ impl CodegenBackend for CraneliftCodegenBackend { } } - fn print(&self, req: &PrintRequest, out: &mut String, _sess: &Session) { - match req.kind { - // FIXME have a default impl that returns false - PrintKind::BackendHasZstd => { - out.push_str("false\n"); - } - _ => {} - } - } - fn target_config(&self, sess: &Session) -> TargetConfig { // FIXME return the actually used target features. this is necessary for #[cfg(target_feature)] let target_features = match sess.target.arch { diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index 8c0c0afcc1dd7..0da5810518c5c 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -257,10 +257,6 @@ impl CodegenBackend for LlvmCodegenBackend { } writeln!(out).unwrap(); } - PrintKind::BackendHasZstd => { - let has_zstd = llvm::LLVMRustLLVMHasZstdCompression(); - writeln!(out, "{has_zstd}").unwrap(); - } PrintKind::CodeModels => { writeln!(out, "Available code models:").unwrap(); for name in &["tiny", "small", "kernel", "medium", "large"] { @@ -314,6 +310,10 @@ impl CodegenBackend for LlvmCodegenBackend { llvm_util::print_version(); } + fn has_zstd(&self) -> bool { + llvm::LLVMRustLLVMHasZstdCompression() + } + fn target_config(&self, sess: &Session) -> TargetConfig { target_config(sess) } diff --git a/compiler/rustc_codegen_ssa/src/traits/backend.rs b/compiler/rustc_codegen_ssa/src/traits/backend.rs index 85bff45408140..cb74e2e46d650 100644 --- a/compiler/rustc_codegen_ssa/src/traits/backend.rs +++ b/compiler/rustc_codegen_ssa/src/traits/backend.rs @@ -78,6 +78,14 @@ pub trait CodegenBackend { fn print_version(&self) {} + /// 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. + fn has_zstd(&self) -> bool { + false + } + /// The metadata loader used to load rlib and dylib metadata. /// /// Alternative codegen backends may want to use different rlib or dylib formats than the diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 0853f638509fd..63fc9c96f450d 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -798,8 +798,11 @@ fn print_crate_info( let calling_conventions = rustc_abi::all_names(); println_info!("{}", calling_conventions.join("\n")); } + BackendHasZstd => { + let has_zstd: bool = codegen_backend.has_zstd(); + println_info!("{has_zstd}"); + } RelocationModels - | BackendHasZstd | CodeModels | TlsModels | TargetCPUs diff --git a/src/tools/compiletest/src/directives/needs.rs b/src/tools/compiletest/src/directives/needs.rs index 208e96166021e..92b596c70ae91 100644 --- a/src/tools/compiletest/src/directives/needs.rs +++ b/src/tools/compiletest/src/directives/needs.rs @@ -432,6 +432,10 @@ fn has_symlinks() -> bool { } fn llvm_has_zstd(config: &Config) -> bool { + // FIXME(#149764): This actually queries the compiler's _default_ backend, + // which is usually LLVM, but can be another backend depending on the value + // of `rust.codegen-backends` in bootstrap.toml. + // The compiler already knows whether LLVM was built with zstd or not, // so compiletest can just ask the compiler. let output = query_rustc_output(