Rollup merge of #148103 - Zalathar:compression, r=wesleywiser

cg_llvm: Pass `debuginfo_compression` through FFI as an enum

There are only three possible values, making an enum more appropriate.

This avoids string allocation on the Rust side, and avoids ad-hoc `!strcmp` to convert back to an enum on the C++ side.
This commit is contained in:
Matthias Krüger 2025-10-31 18:41:51 +01:00 committed by GitHub
commit 3d671c0d54
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 60 additions and 42 deletions

View file

@ -36,7 +36,7 @@ impl OwnedTargetMachine {
use_init_array: bool,
split_dwarf_file: &CStr,
output_obj_file: &CStr,
debug_info_compression: &CStr,
debug_info_compression: llvm::CompressionKind,
use_emulated_tls: bool,
use_wasm_eh: bool,
) -> Result<Self, LlvmError<'static>> {
@ -62,7 +62,7 @@ impl OwnedTargetMachine {
use_init_array,
split_dwarf_file.as_ptr(),
output_obj_file.as_ptr(),
debug_info_compression.as_ptr(),
debug_info_compression,
use_emulated_tls,
use_wasm_eh,
)

View file

@ -6,9 +6,6 @@ use std::sync::Arc;
use std::{fs, slice, str};
use libc::{c_char, c_int, c_void, size_t};
use llvm::{
LLVMRustLLVMHasZlibCompressionForDebugSymbols, LLVMRustLLVMHasZstdCompressionForDebugSymbols,
};
use rustc_codegen_ssa::back::link::ensure_removed;
use rustc_codegen_ssa::back::versioned_llvm_target;
use rustc_codegen_ssa::back::write::{
@ -252,21 +249,25 @@ pub(crate) fn target_machine_factory(
let use_emulated_tls = matches!(sess.tls_model(), TlsModel::Emulated);
let debuginfo_compression = sess.opts.debuginfo_compression.to_string();
match sess.opts.debuginfo_compression {
rustc_session::config::DebugInfoCompression::Zlib => {
if !unsafe { LLVMRustLLVMHasZlibCompressionForDebugSymbols() } {
let debuginfo_compression = match sess.opts.debuginfo_compression {
config::DebugInfoCompression::None => llvm::CompressionKind::None,
config::DebugInfoCompression::Zlib => {
if llvm::LLVMRustLLVMHasZlibCompression() {
llvm::CompressionKind::Zlib
} else {
sess.dcx().emit_warn(UnknownCompression { algorithm: "zlib" });
llvm::CompressionKind::None
}
}
rustc_session::config::DebugInfoCompression::Zstd => {
if !unsafe { LLVMRustLLVMHasZstdCompressionForDebugSymbols() } {
config::DebugInfoCompression::Zstd => {
if llvm::LLVMRustLLVMHasZstdCompression() {
llvm::CompressionKind::Zstd
} else {
sess.dcx().emit_warn(UnknownCompression { algorithm: "zstd" });
llvm::CompressionKind::None
}
}
rustc_session::config::DebugInfoCompression::None => {}
};
let debuginfo_compression = SmallCStr::new(&debuginfo_compression);
let file_name_display_preference =
sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
@ -310,7 +311,7 @@ pub(crate) fn target_machine_factory(
use_init_array,
&split_dwarf_file,
&output_obj_file,
&debuginfo_compression,
debuginfo_compression,
use_emulated_tls,
use_wasm_eh,
)

View file

@ -678,6 +678,15 @@ pub(crate) enum Opcode {
CatchSwitch = 65,
}
/// Must match the layout of `LLVMRustCompressionKind`.
#[derive(Copy, Clone)]
#[repr(C)]
pub(crate) enum CompressionKind {
None = 0,
Zlib = 1,
Zstd = 2,
}
unsafe extern "C" {
type Opaque;
}
@ -2329,7 +2338,7 @@ unsafe extern "C" {
UseInitArray: bool,
SplitDwarfFile: *const c_char,
OutputObjFile: *const c_char,
DebugInfoCompression: *const c_char,
DebugInfoCompression: CompressionKind,
UseEmulatedTls: bool,
UseWasmEH: bool,
) -> *mut TargetMachine;
@ -2517,9 +2526,8 @@ unsafe extern "C" {
pub(crate) fn LLVMRustGetElementTypeArgIndex(CallSite: &Value) -> i32;
pub(crate) fn LLVMRustLLVMHasZlibCompressionForDebugSymbols() -> bool;
pub(crate) fn LLVMRustLLVMHasZstdCompressionForDebugSymbols() -> bool;
pub(crate) safe fn LLVMRustLLVMHasZlibCompression() -> bool;
pub(crate) safe fn LLVMRustLLVMHasZstdCompression() -> bool;
pub(crate) fn LLVMRustGetSymbols(
buf_ptr: *const u8,

View file

@ -224,6 +224,31 @@ static FloatABI::ABIType fromRust(LLVMRustFloatABI RustFloatAbi) {
report_fatal_error("Bad FloatABI.");
}
// Must match the layout of `rustc_codegen_llvm::llvm::ffi::CompressionKind`.
enum class LLVMRustCompressionKind {
None = 0,
Zlib = 1,
Zstd = 2,
};
static llvm::DebugCompressionType fromRust(LLVMRustCompressionKind Kind) {
switch (Kind) {
case LLVMRustCompressionKind::None:
return llvm::DebugCompressionType::None;
case LLVMRustCompressionKind::Zlib:
if (!llvm::compression::zlib::isAvailable()) {
report_fatal_error("LLVMRustCompressionKind::Zlib not available");
}
return llvm::DebugCompressionType::Zlib;
case LLVMRustCompressionKind::Zstd:
if (!llvm::compression::zstd::isAvailable()) {
report_fatal_error("LLVMRustCompressionKind::Zstd not available");
}
return llvm::DebugCompressionType::Zstd;
}
report_fatal_error("bad LLVMRustCompressionKind");
}
extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM,
RustStringRef OutStr) {
ArrayRef<SubtargetSubTypeKV> CPUTable =
@ -271,7 +296,8 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
bool TrapUnreachable, bool Singlethread, bool VerboseAsm,
bool EmitStackSizeSection, bool RelaxELFRelocations, bool UseInitArray,
const char *SplitDwarfFile, const char *OutputObjFile,
const char *DebugInfoCompression, bool UseEmulatedTls, bool UseWasmEH) {
LLVMRustCompressionKind DebugInfoCompression, bool UseEmulatedTls,
bool UseWasmEH) {
auto OptLevel = fromRust(RustOptLevel);
auto RM = fromRust(RustReloc);
@ -307,16 +333,10 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
if (OutputObjFile) {
Options.ObjectFilenameForDebug = OutputObjFile;
}
if (!strcmp("zlib", DebugInfoCompression) &&
llvm::compression::zlib::isAvailable()) {
Options.MCOptions.CompressDebugSections = DebugCompressionType::Zlib;
} else if (!strcmp("zstd", DebugInfoCompression) &&
llvm::compression::zstd::isAvailable()) {
Options.MCOptions.CompressDebugSections = DebugCompressionType::Zstd;
} else if (!strcmp("none", DebugInfoCompression)) {
Options.MCOptions.CompressDebugSections = DebugCompressionType::None;
}
// To avoid fatal errors, make sure the Rust-side code only passes a
// compression kind that is known to be supported by this build of LLVM, via
// `LLVMRustLLVMHasZlibCompression` and `LLVMRustLLVMHasZstdCompression`.
Options.MCOptions.CompressDebugSections = fromRust(DebugInfoCompression);
Options.MCOptions.X86RelaxRelocations = RelaxELFRelocations;
Options.UseInitArray = UseInitArray;
Options.EmulatedTLS = UseEmulatedTls;

View file

@ -1645,11 +1645,11 @@ extern "C" bool LLVMRustIsNonGVFunctionPointerTy(LLVMValueRef V) {
return false;
}
extern "C" bool LLVMRustLLVMHasZlibCompressionForDebugSymbols() {
extern "C" bool LLVMRustLLVMHasZlibCompression() {
return llvm::compression::zlib::isAvailable();
}
extern "C" bool LLVMRustLLVMHasZstdCompressionForDebugSymbols() {
extern "C" bool LLVMRustLLVMHasZstdCompression() {
return llvm::compression::zstd::isAvailable();
}

View file

@ -583,17 +583,6 @@ pub enum DebugInfoCompression {
Zstd,
}
impl ToString for DebugInfoCompression {
fn to_string(&self) -> String {
match self {
DebugInfoCompression::None => "none",
DebugInfoCompression::Zlib => "zlib",
DebugInfoCompression::Zstd => "zstd",
}
.to_owned()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
pub enum MirStripDebugInfo {
None,