From 7b22d008df7fa15a24ba021e72e53a3e08d5d88f Mon Sep 17 00:00:00 2001 From: Zalathar Date: Fri, 9 Oct 2026 17:03:40 +1100 Subject: [PATCH 1/2] Avoid some explicit casts to `*const c_char` --- compiler/rustc_codegen_llvm/src/back/lto.rs | 6 +----- compiler/rustc_codegen_llvm/src/common.rs | 10 ++-------- compiler/rustc_codegen_llvm/src/context.rs | 14 +++----------- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 8 ++++---- 4 files changed, 10 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index d52e34d30fb7d..d9f6f399906df 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -312,11 +312,7 @@ fn fat_lto( let data = bc_decoded.data(); unsafe { - if !llvm::LLVMRustLinkerAdd( - linker, - data.as_ptr() as *const libc::c_char, - data.len(), - ) { + if !llvm::LLVMRustLinkerAdd(linker, data.as_ptr(), data.len()) { llvm::LLVMRustLinkerFree(linker); write::llvm_err(dcx, LlvmError::LoadBitcode { name }) } diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs index a5a4f0b3ddb25..651082cee5d43 100644 --- a/compiler/rustc_codegen_llvm/src/common.rs +++ b/compiler/rustc_codegen_llvm/src/common.rs @@ -470,20 +470,14 @@ pub(crate) fn val_ty(v: &Value) -> &Type { } pub(crate) fn bytes_in_context<'ll>(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value { - unsafe { - let ptr = bytes.as_ptr() as *const c_char; - llvm::LLVMConstStringInContext2(llcx, ptr, bytes.len(), TRUE) - } + unsafe { llvm::LLVMConstStringInContext2(llcx, bytes.as_ptr(), bytes.len(), TRUE) } } pub(crate) fn null_terminate_bytes_in_context<'ll>( llcx: &'ll llvm::Context, bytes: &[u8], ) -> &'ll Value { - unsafe { - let ptr = bytes.as_ptr() as *const c_char; - llvm::LLVMConstStringInContext2(llcx, ptr, bytes.len(), FALSE) - } + unsafe { llvm::LLVMConstStringInContext2(llcx, bytes.as_ptr(), bytes.len(), FALSE) } } pub(crate) fn named_struct<'ll>(ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value { diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 1059de3c0aec0..46196ab7d360f 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -1,6 +1,6 @@ use std::borrow::{Borrow, Cow}; use std::cell::{Cell, RefCell}; -use std::ffi::{CStr, c_char, c_uint}; +use std::ffi::{CStr, c_uint}; use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; use std::str; @@ -911,19 +911,11 @@ impl<'ll, CX: Borrow>> GenericCx<'ll, CX> { } pub(crate) fn get_md_kind_id(&self, name: &str) -> llvm::MetadataKindId { - unsafe { - llvm::LLVMGetMDKindIDInContext( - self.llcx(), - name.as_ptr() as *const c_char, - name.len() as c_uint, - ) - } + unsafe { llvm::LLVMGetMDKindIDInContext(self.llcx(), name.as_ptr(), name.len() as c_uint) } } pub(crate) fn create_metadata(&self, name: &[u8]) -> &'ll Metadata { - unsafe { - llvm::LLVMMDStringInContext2(self.llcx(), name.as_ptr() as *const c_char, name.len()) - } + unsafe { llvm::LLVMMDStringInContext2(self.llcx(), name.as_ptr(), name.len()) } } pub(crate) fn get_functions(&self) -> Vec<&'ll Value> { diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 7e3f2699fff0b..749ef8120da6f 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -890,7 +890,7 @@ unsafe extern "C" { pub(crate) fn LLVMContextSetDiscardValueNames(C: &Context, Discard: Bool); pub(crate) fn LLVMGetMDKindIDInContext( C: &Context, - Name: *const c_char, + Name: *const c_uchar, // See "PTR_LEN_STR". SLen: c_uint, ) -> MetadataKindId; @@ -1009,7 +1009,7 @@ unsafe extern "C" { // Operations on metadata pub(crate) fn LLVMMDStringInContext2( C: &Context, - Str: *const c_char, + Str: *const c_uchar, // See "PTR_LEN_STR". SLen: size_t, ) -> &Metadata; pub(crate) fn LLVMMDNodeInContext2<'a>( @@ -1042,7 +1042,7 @@ unsafe extern "C" { pub(crate) fn LLVMArrayType2(ElementType: &Type, ElementCount: u64) -> &Type; pub(crate) fn LLVMConstStringInContext2( C: &Context, - Str: *const c_char, + Str: *const c_uchar, // See "PTR_LEN_STR". Length: size_t, DontNullTerminate: Bool, ) -> &Value; @@ -2561,7 +2561,7 @@ unsafe extern "C" { pub(crate) fn LLVMRustLinkerNew(M: &Module) -> &mut Linker<'_>; pub(crate) fn LLVMRustLinkerAdd( linker: &Linker<'_>, - bytecode: *const c_char, + bytecode: *const c_uchar, // See "PTR_LEN_STR". bytecode_len: usize, ) -> bool; pub(crate) fn LLVMRustLinkerFree<'a>(linker: &'a mut Linker<'a>); From de49a781749b3b4870b99cdcae1d46e631996dde Mon Sep 17 00:00:00 2001 From: Zalathar Date: Fri, 9 Oct 2026 17:19:25 +1100 Subject: [PATCH 2/2] Remove some unnecessary pointer casts --- compiler/rustc_codegen_llvm/src/back/lto.rs | 3 +-- compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs | 5 +---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index d9f6f399906df..b171424b46245 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -323,10 +323,9 @@ fn fat_lto( // Internalize everything below threshold to help strip out more modules and such. unsafe { - let ptr = symbols_below_threshold.as_ptr(); llvm::LLVMRustRunRestrictionPass( llmod, - ptr as *const *const libc::c_char, + symbols_below_threshold.as_ptr(), symbols_below_threshold.len() as libc::size_t, ); } diff --git a/compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs index 68e5ed3d37741..2e76223c68c12 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs @@ -329,10 +329,7 @@ pub(crate) mod Enzyme_AD { let c_fun_name = std::ffi::CString::new(fun_name) .unwrap_or_else(|err| bug!("failed to set_print_type_fun: {err}")); unsafe { - (self.EnzymeSetCLString)( - self.EnzymeFunctionToAnalyze, - c_fun_name.as_ptr() as *const c_char, - ); + (self.EnzymeSetCLString)(self.EnzymeFunctionToAnalyze, c_fun_name.as_ptr()); } }