Skip to content

Commit b1094f6

Browse files
committed
Add a safe wrapper for LLVMAppendModuleInlineAsm
This patch also changes the Rust-side declaration to take `*const c_uchar` instead of `*const c_char`, to avoid the need for `AsCCharPtr`.
1 parent d1bb310 commit b1094f6

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

compiler/rustc_codegen_llvm/src/asm.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,7 @@ impl<'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
435435
template_str.push_str("\n.att_syntax\n");
436436
}
437437

438-
unsafe {
439-
llvm::LLVMAppendModuleInlineAsm(
440-
self.llmod,
441-
template_str.as_c_char_ptr(),
442-
template_str.len(),
443-
);
444-
}
438+
llvm::append_module_inline_asm(self.llmod, template_str.as_bytes());
445439
}
446440

447441
fn mangled_name(&self, instance: Instance<'tcx>) -> String {

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,9 +1148,9 @@ unsafe fn embed_bitcode(
11481148
// We need custom section flags, so emit module-level inline assembly.
11491149
let section_flags = if cgcx.is_pe_coff { "n" } else { "e" };
11501150
let asm = create_section_with_flags_asm(".llvmbc", section_flags, bitcode);
1151-
llvm::LLVMAppendModuleInlineAsm(llmod, asm.as_c_char_ptr(), asm.len());
1151+
llvm::append_module_inline_asm(llmod, &asm);
11521152
let asm = create_section_with_flags_asm(".llvmcmd", section_flags, cmdline.as_bytes());
1153-
llvm::LLVMAppendModuleInlineAsm(llmod, asm.as_c_char_ptr(), asm.len());
1153+
llvm::append_module_inline_asm(llmod, &asm);
11541154
}
11551155
}
11561156
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,8 +1014,12 @@ unsafe extern "C" {
10141014
pub(crate) fn LLVMGetDataLayoutStr(M: &Module) -> *const c_char;
10151015
pub(crate) fn LLVMSetDataLayout(M: &Module, Triple: *const c_char);
10161016

1017-
/// See Module::setModuleInlineAsm.
1018-
pub(crate) fn LLVMAppendModuleInlineAsm(M: &Module, Asm: *const c_char, Len: size_t);
1017+
/// Append inline assembly to a module. See `Module::appendModuleInlineAsm`.
1018+
pub(crate) fn LLVMAppendModuleInlineAsm(
1019+
M: &Module,
1020+
Asm: *const c_uchar, // See "PTR_LEN_STR".
1021+
Len: size_t,
1022+
);
10191023

10201024
/// Create the specified uniqued inline asm string. See `InlineAsm::get()`.
10211025
pub(crate) fn LLVMGetInlineAsm<'ll>(

compiler/rustc_codegen_llvm/src/llvm/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,11 @@ pub(crate) fn set_dso_local<'ll>(v: &'ll Value) {
440440
LLVMRustSetDSOLocal(v, true);
441441
}
442442
}
443+
444+
/// Safe wrapper for `LLVMAppendModuleInlineAsm`, which delegates to
445+
/// `Module::appendModuleInlineAsm`.
446+
pub(crate) fn append_module_inline_asm<'ll>(llmod: &'ll Module, asm: &[u8]) {
447+
unsafe {
448+
LLVMAppendModuleInlineAsm(llmod, asm.as_ptr(), asm.len());
449+
}
450+
}

0 commit comments

Comments
 (0)