Skip to content

Commit 73b734b

Browse files
committed
Pass debuginfo_compression through FFI as an enum
1 parent 04ff05c commit 73b734b

File tree

6 files changed

+60
-42
lines changed

6 files changed

+60
-42
lines changed

compiler/rustc_codegen_llvm/src/back/owned_target_machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl OwnedTargetMachine {
3636
use_init_array: bool,
3737
split_dwarf_file: &CStr,
3838
output_obj_file: &CStr,
39-
debug_info_compression: &CStr,
39+
debug_info_compression: llvm::CompressionKind,
4040
use_emulated_tls: bool,
4141
use_wasm_eh: bool,
4242
) -> Result<Self, LlvmError<'static>> {
@@ -62,7 +62,7 @@ impl OwnedTargetMachine {
6262
use_init_array,
6363
split_dwarf_file.as_ptr(),
6464
output_obj_file.as_ptr(),
65-
debug_info_compression.as_ptr(),
65+
debug_info_compression,
6666
use_emulated_tls,
6767
use_wasm_eh,
6868
)

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ use std::sync::Arc;
66
use std::{fs, slice, str};
77

88
use libc::{c_char, c_int, c_void, size_t};
9-
use llvm::{
10-
LLVMRustLLVMHasZlibCompressionForDebugSymbols, LLVMRustLLVMHasZstdCompressionForDebugSymbols,
11-
};
129
use rustc_codegen_ssa::back::link::ensure_removed;
1310
use rustc_codegen_ssa::back::versioned_llvm_target;
1411
use rustc_codegen_ssa::back::write::{
@@ -252,21 +249,25 @@ pub(crate) fn target_machine_factory(
252249

253250
let use_emulated_tls = matches!(sess.tls_model(), TlsModel::Emulated);
254251

255-
let debuginfo_compression = sess.opts.debuginfo_compression.to_string();
256-
match sess.opts.debuginfo_compression {
257-
rustc_session::config::DebugInfoCompression::Zlib => {
258-
if !unsafe { LLVMRustLLVMHasZlibCompressionForDebugSymbols() } {
252+
let debuginfo_compression = match sess.opts.debuginfo_compression {
253+
config::DebugInfoCompression::None => llvm::CompressionKind::None,
254+
config::DebugInfoCompression::Zlib => {
255+
if llvm::LLVMRustLLVMHasZlibCompression() {
256+
llvm::CompressionKind::Zlib
257+
} else {
259258
sess.dcx().emit_warn(UnknownCompression { algorithm: "zlib" });
259+
llvm::CompressionKind::None
260260
}
261261
}
262-
rustc_session::config::DebugInfoCompression::Zstd => {
263-
if !unsafe { LLVMRustLLVMHasZstdCompressionForDebugSymbols() } {
262+
config::DebugInfoCompression::Zstd => {
263+
if llvm::LLVMRustLLVMHasZstdCompression() {
264+
llvm::CompressionKind::Zstd
265+
} else {
264266
sess.dcx().emit_warn(UnknownCompression { algorithm: "zstd" });
267+
llvm::CompressionKind::None
265268
}
266269
}
267-
rustc_session::config::DebugInfoCompression::None => {}
268270
};
269-
let debuginfo_compression = SmallCStr::new(&debuginfo_compression);
270271

271272
let file_name_display_preference =
272273
sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
@@ -310,7 +311,7 @@ pub(crate) fn target_machine_factory(
310311
use_init_array,
311312
&split_dwarf_file,
312313
&output_obj_file,
313-
&debuginfo_compression,
314+
debuginfo_compression,
314315
use_emulated_tls,
315316
use_wasm_eh,
316317
)

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,15 @@ pub(crate) enum Opcode {
677677
CatchSwitch = 65,
678678
}
679679

680+
/// Must match the layout of `LLVMRustCompressionKind`.
681+
#[derive(Copy, Clone)]
682+
#[repr(C)]
683+
pub(crate) enum CompressionKind {
684+
None = 0,
685+
Zlib = 1,
686+
Zstd = 2,
687+
}
688+
680689
unsafe extern "C" {
681690
type Opaque;
682691
}
@@ -2328,7 +2337,7 @@ unsafe extern "C" {
23282337
UseInitArray: bool,
23292338
SplitDwarfFile: *const c_char,
23302339
OutputObjFile: *const c_char,
2331-
DebugInfoCompression: *const c_char,
2340+
DebugInfoCompression: CompressionKind,
23322341
UseEmulatedTls: bool,
23332342
UseWasmEH: bool,
23342343
) -> *mut TargetMachine;
@@ -2516,9 +2525,8 @@ unsafe extern "C" {
25162525

25172526
pub(crate) fn LLVMRustGetElementTypeArgIndex(CallSite: &Value) -> i32;
25182527

2519-
pub(crate) fn LLVMRustLLVMHasZlibCompressionForDebugSymbols() -> bool;
2520-
2521-
pub(crate) fn LLVMRustLLVMHasZstdCompressionForDebugSymbols() -> bool;
2528+
pub(crate) safe fn LLVMRustLLVMHasZlibCompression() -> bool;
2529+
pub(crate) safe fn LLVMRustLLVMHasZstdCompression() -> bool;
25222530

25232531
pub(crate) fn LLVMRustGetSymbols(
25242532
buf_ptr: *const u8,

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,31 @@ static FloatABI::ABIType fromRust(LLVMRustFloatABI RustFloatAbi) {
224224
report_fatal_error("Bad FloatABI.");
225225
}
226226

227+
// Must match the layout of `rustc_codegen_llvm::llvm::ffi::CompressionKind`.
228+
enum class LLVMRustCompressionKind {
229+
None = 0,
230+
Zlib = 1,
231+
Zstd = 2,
232+
};
233+
234+
static llvm::DebugCompressionType fromRust(LLVMRustCompressionKind Kind) {
235+
switch (Kind) {
236+
case LLVMRustCompressionKind::None:
237+
return llvm::DebugCompressionType::None;
238+
case LLVMRustCompressionKind::Zlib:
239+
if (!llvm::compression::zlib::isAvailable()) {
240+
report_fatal_error("LLVMRustCompressionKind::Zlib not available");
241+
}
242+
return llvm::DebugCompressionType::Zlib;
243+
case LLVMRustCompressionKind::Zstd:
244+
if (!llvm::compression::zstd::isAvailable()) {
245+
report_fatal_error("LLVMRustCompressionKind::Zstd not available");
246+
}
247+
return llvm::DebugCompressionType::Zstd;
248+
}
249+
report_fatal_error("bad LLVMRustCompressionKind");
250+
}
251+
227252
extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM,
228253
RustStringRef OutStr) {
229254
ArrayRef<SubtargetSubTypeKV> CPUTable =
@@ -271,7 +296,8 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
271296
bool TrapUnreachable, bool Singlethread, bool VerboseAsm,
272297
bool EmitStackSizeSection, bool RelaxELFRelocations, bool UseInitArray,
273298
const char *SplitDwarfFile, const char *OutputObjFile,
274-
const char *DebugInfoCompression, bool UseEmulatedTls, bool UseWasmEH) {
299+
LLVMRustCompressionKind DebugInfoCompression, bool UseEmulatedTls,
300+
bool UseWasmEH) {
275301

276302
auto OptLevel = fromRust(RustOptLevel);
277303
auto RM = fromRust(RustReloc);
@@ -307,16 +333,10 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
307333
if (OutputObjFile) {
308334
Options.ObjectFilenameForDebug = OutputObjFile;
309335
}
310-
if (!strcmp("zlib", DebugInfoCompression) &&
311-
llvm::compression::zlib::isAvailable()) {
312-
Options.MCOptions.CompressDebugSections = DebugCompressionType::Zlib;
313-
} else if (!strcmp("zstd", DebugInfoCompression) &&
314-
llvm::compression::zstd::isAvailable()) {
315-
Options.MCOptions.CompressDebugSections = DebugCompressionType::Zstd;
316-
} else if (!strcmp("none", DebugInfoCompression)) {
317-
Options.MCOptions.CompressDebugSections = DebugCompressionType::None;
318-
}
319-
336+
// To avoid fatal errors, make sure the Rust-side code only passes a
337+
// compression kind that is known to be supported by this build of LLVM, via
338+
// `LLVMRustLLVMHasZlibCompression` and `LLVMRustLLVMHasZstdCompression`.
339+
Options.MCOptions.CompressDebugSections = fromRust(DebugInfoCompression);
320340
Options.MCOptions.X86RelaxRelocations = RelaxELFRelocations;
321341
Options.UseInitArray = UseInitArray;
322342
Options.EmulatedTLS = UseEmulatedTls;

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,11 +1640,11 @@ extern "C" bool LLVMRustIsNonGVFunctionPointerTy(LLVMValueRef V) {
16401640
return false;
16411641
}
16421642

1643-
extern "C" bool LLVMRustLLVMHasZlibCompressionForDebugSymbols() {
1643+
extern "C" bool LLVMRustLLVMHasZlibCompression() {
16441644
return llvm::compression::zlib::isAvailable();
16451645
}
16461646

1647-
extern "C" bool LLVMRustLLVMHasZstdCompressionForDebugSymbols() {
1647+
extern "C" bool LLVMRustLLVMHasZstdCompression() {
16481648
return llvm::compression::zstd::isAvailable();
16491649
}
16501650

compiler/rustc_session/src/config.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -583,17 +583,6 @@ pub enum DebugInfoCompression {
583583
Zstd,
584584
}
585585

586-
impl ToString for DebugInfoCompression {
587-
fn to_string(&self) -> String {
588-
match self {
589-
DebugInfoCompression::None => "none",
590-
DebugInfoCompression::Zlib => "zlib",
591-
DebugInfoCompression::Zstd => "zstd",
592-
}
593-
.to_owned()
594-
}
595-
}
596-
597586
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
598587
pub enum MirStripDebugInfo {
599588
None,

0 commit comments

Comments
 (0)