From 68dd9e543658e428d58e6518fe5d97f6073f0b07 Mon Sep 17 00:00:00 2001 From: malezjaa Date: Sun, 8 Nov 2026 09:54:38 +0100 Subject: [PATCH 1/6] 1/n: split set_struct_body into set_struct_body and set_packed_struct_body --- .../rustc_codegen_llvm/src/builder/gpu_offload.rs | 4 ++-- compiler/rustc_codegen_llvm/src/consts.rs | 6 ++---- compiler/rustc_codegen_llvm/src/type_.rs | 13 ++++++++----- compiler/rustc_codegen_llvm/src/type_of.rs | 9 ++++++--- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs b/compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs index 46526b6bce421..71935d8a998c1 100644 --- a/compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs +++ b/compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs @@ -171,7 +171,7 @@ pub(crate) fn generate_at_one<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll llvm::Value ]; let struct_elems_ty: Vec<_> = struct_elems.iter().map(|&x| cx.val_ty(x)).collect(); let initializer = crate::common::named_struct(struct_ident_ty, &struct_elems); - cx.set_struct_body(struct_ident_ty, &struct_elems_ty, false); + cx.set_struct_body(struct_ident_ty, &struct_elems_ty); let at_one = add_unnamed_global(&cx, &"", initializer, PrivateLinkage); llvm::set_alignment(at_one, Align::EIGHT); at_one @@ -199,7 +199,7 @@ impl TgtOffloadEntry { // For each kernel to run on the gpu, we will later generate one entry of this type. // copied from LLVM let entry_elements = vec![ti64, ti16, ti16, ti32, tptr, tptr, ti64, ti64, tptr]; - cx.set_struct_body(offload_entry_ty, &entry_elements, false); + cx.set_struct_body(offload_entry_ty, &entry_elements); offload_entry_ty } diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs index 33d48642812e6..d739b20a64339 100644 --- a/compiler/rustc_codegen_llvm/src/consts.rs +++ b/compiler/rustc_codegen_llvm/src/consts.rs @@ -731,8 +731,7 @@ impl<'ll> CodegenCx<'ll, '_> { let class_t = self.type_named_struct("struct._class_t"); let els = [self.type_ptr(); 5]; - let packed = false; - self.set_struct_body(class_t, &els, packed); + self.set_struct_body(class_t, &els); self.objc_class_t.set(Some(class_t)); class_t @@ -884,8 +883,7 @@ impl<'ll> CodegenCx<'ll, '_> { let llty = self.type_named_struct("struct._objc_module"); let i32_llty = self.type_i32(); let ptr_llty = self.type_ptr(); - let packed = false; - self.set_struct_body(llty, &[i32_llty, i32_llty, ptr_llty, ptr_llty], packed); + self.set_struct_body(llty, &[i32_llty, i32_llty, ptr_llty, ptr_llty]); let version = self.const_uint(i32_llty, 7); let size = self.const_uint(i32_llty, 16); diff --git a/compiler/rustc_codegen_llvm/src/type_.rs b/compiler/rustc_codegen_llvm/src/type_.rs index dc36ef2b1536a..a16297be586c5 100644 --- a/compiler/rustc_codegen_llvm/src/type_.rs +++ b/compiler/rustc_codegen_llvm/src/type_.rs @@ -13,7 +13,7 @@ use rustc_target::callconv::{CastTarget, FnAbi}; use crate::abi::{FnAbiLlvmExt, LlvmType}; use crate::common; use crate::context::{CodegenCx, GenericCx, SCx}; -use crate::llvm::{self, FALSE, TRUE, ToLlvmBool, Type, TypeKind, Value}; +use crate::llvm::{self, FALSE, TRUE, Type, TypeKind, Value}; use crate::type_of::LayoutLlvmExt; impl<'ll> fmt::Debug for Type<'ll> { @@ -34,11 +34,14 @@ impl<'ll, CX: Borrow>> GenericCx<'ll, CX> { unsafe { llvm::LLVMStructCreateNamed(self.llcx(), name.as_ptr()) } } - pub(crate) fn set_struct_body(&self, ty: Type<'ll>, els: &[Type<'ll>], packed: bool) { - unsafe { - llvm::LLVMStructSetBody(ty, els.as_ptr(), els.len() as c_uint, packed.to_llvm_bool()) - } + pub(crate) fn set_struct_body(&self, ty: Type<'ll>, els: &[Type<'ll>]) { + unsafe { llvm::LLVMStructSetBody(ty, els.as_ptr(), els.len() as c_uint, FALSE) } } + + pub(crate) fn set_struct_body_packed(&self, ty: Type<'ll>, els: &[Type<'ll>]) { + unsafe { llvm::LLVMStructSetBody(ty, els.as_ptr(), els.len() as c_uint, TRUE) } + } + pub(crate) fn type_void(&self) -> Type<'ll> { unsafe { llvm::LLVMVoidTypeInContext(self.llcx()) } } diff --git a/compiler/rustc_codegen_llvm/src/type_of.rs b/compiler/rustc_codegen_llvm/src/type_of.rs index b003c322640dd..f8d2d57312666 100644 --- a/compiler/rustc_codegen_llvm/src/type_of.rs +++ b/compiler/rustc_codegen_llvm/src/type_of.rs @@ -79,8 +79,7 @@ fn uncached_llvm_type<'ll, 'tcx>( None => cx.type_struct(&[fill]), Some(ref name) => { let llty = cx.type_named_struct(name); - let packed = false; - cx.set_struct_body(llty, &[fill], packed); + cx.set_struct_body(llty, &[fill]); llty } } @@ -250,7 +249,11 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyAndLayout<'tcx> { if let Some((llty, layout)) = defer { let (llfields, packed) = struct_llfields(cx, layout); - cx.set_struct_body(llty, &llfields, packed); + if packed { + cx.set_struct_body_packed(llty, &llfields) + } else { + cx.set_struct_body(llty, &llfields) + } } llty } From fbe40d1dca4d464dd6aaa9dd52c0ed146387a759 Mon Sep 17 00:00:00 2001 From: malezjaa Date: Sun, 8 Nov 2026 10:03:31 +0100 Subject: [PATCH 2/6] 2/n: remove pointless casts --- compiler/rustc_codegen_llvm/src/back/write.rs | 10 ++++------ compiler/rustc_codegen_llvm/src/builder/autodiff.rs | 2 +- compiler/rustc_codegen_llvm/src/common.rs | 2 +- compiler/rustc_codegen_llvm/src/debuginfo/mod.rs | 8 ++++---- compiler/rustc_codegen_llvm/src/intrinsic.rs | 8 ++++---- compiler/rustc_codegen_llvm/src/type_of.rs | 2 +- 6 files changed, 15 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index e43b9ba6359ed..38127c1c3141b 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -669,7 +669,7 @@ pub(crate) unsafe fn llvm_optimize<'ll>( } // Create the new parameter list, with ptr as the first argument - let mut new_param_types = Vec::with_capacity(old_param_count as usize + 1); + let mut new_param_types = Vec::with_capacity(old_param_count + 1); new_param_types.push(cx.type_ptr()); // This relies on undocumented LLVM knowledge that scalars must be passed as i64 @@ -1043,14 +1043,12 @@ pub(crate) fn codegen( output_ptr: *mut c_char, output_len: size_t, ) -> size_t { - let input = - unsafe { slice::from_raw_parts(input_ptr as *const u8, input_len as usize) }; + let input = unsafe { slice::from_raw_parts(input_ptr as *const u8, input_len) }; let Ok(input) = str::from_utf8(input) else { return 0 }; - let output = unsafe { - slice::from_raw_parts_mut(output_ptr as *mut u8, output_len as usize) - }; + let output = + unsafe { slice::from_raw_parts_mut(output_ptr as *mut u8, output_len) }; let mut cursor = io::Cursor::new(output); let Ok(demangled) = rustc_demangle::try_demangle(input) else { return 0 }; diff --git a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs index 4c83f0a64248f..2bc3dc2903995 100644 --- a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs +++ b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs @@ -163,7 +163,7 @@ fn match_args_from_caller_to_enzyme<'ll, 'tcx>( let global_dupnoneedv = cx.declare_global("enzyme_dupnoneedv", cx.type_ptr()); while activity_pos < inputs.len() { - let diff_activity = inputs[activity_pos as usize]; + let diff_activity = inputs[activity_pos]; // Duplicated arguments received a shadow argument, into which enzyme will write the // gradient. let (activity, duplicated): (&Value, bool) = match diff_activity { diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs index b2ce873154bcc..dc7a096ff19fe 100644 --- a/compiler/rustc_codegen_llvm/src/common.rs +++ b/compiler/rustc_codegen_llvm/src/common.rs @@ -291,7 +291,7 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> { } fn const_i64(&self, i: i64) -> &'ll Value { - self.const_int(self.type_i64(), i as i64) + self.const_int(self.type_i64(), i) } fn const_int(&self, t: Type<'ll>, i: i64) -> &'ll Value { diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs index 5fe5ee3e92fea..f89726910c41c 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs @@ -540,22 +540,22 @@ impl<'ll, 'tcx> DebugInfoBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> { if direct_offset.bytes() > 0 { addr_ops.push(DW_OP_plus_uconst); - addr_ops.push(direct_offset.bytes() as u64); + addr_ops.push(direct_offset.bytes()); addr_ops.push(DW_OP_stack_value); } for &offset in indirect_offsets { addr_ops.push(DW_OP_deref); if offset.bytes() > 0 { addr_ops.push(DW_OP_plus_uconst); - addr_ops.push(offset.bytes() as u64); + addr_ops.push(offset.bytes()); } } if let Some(fragment) = fragment { // `DW_OP_LLVM_fragment` takes as arguments the fragment's // offset and size, both of them in bits. addr_ops.push(DW_OP_LLVM_fragment); - addr_ops.push(fragment.start.bits() as u64); - addr_ops.push((fragment.end - fragment.start).bits() as u64); + addr_ops.push(fragment.start.bits()); + addr_ops.push((fragment.end - fragment.start).bits()); } let di_builder = DIB(self.cx()); diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index 23065419f9f1d..9c702a70dd291 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -2294,7 +2294,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>( // Check that the indices are in-bounds. let indices = args[2].immediate(); for i in 0..n { - let val = bx.const_get_elt(indices, i as u64); + let val = bx.const_get_elt(indices, i); let idx = bx .const_to_opt_u128(val, true) .unwrap_or_else(|| bug!("typeck should have already ensured that these are const")); @@ -2386,7 +2386,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>( }), }; let i1 = bx.type_i1(); - let i1xn = bx.type_scalable_vector(i1, m_len as u64); + let i1xn = bx.type_scalable_vector(i1, m_len); bx.trunc(args[0].immediate(), i1xn) } else { let in_elem_bitwidth = require_int_or_uint_ty!( @@ -3336,7 +3336,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>( in_elem }), }, - in_len as u64, + in_len, ); let llvm_intrinsic = match name { sym::simd_bswap => "llvm.bswap", @@ -3423,7 +3423,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>( if signed { 's' } else { 'u' }, if is_add { "add" } else { "sub" }, ); - let vec_ty = bx.cx.type_vector(elem_ty, in_len as u64); + let vec_ty = bx.cx.type_vector(elem_ty, in_len); return Ok(bx.call_intrinsic(llvm_intrinsic, &[vec_ty], &[lhs, rhs])); } diff --git a/compiler/rustc_codegen_llvm/src/type_of.rs b/compiler/rustc_codegen_llvm/src/type_of.rs index f8d2d57312666..cab8f187f5f11 100644 --- a/compiler/rustc_codegen_llvm/src/type_of.rs +++ b/compiler/rustc_codegen_llvm/src/type_of.rs @@ -111,7 +111,7 @@ fn struct_llfields<'ll, 'tcx>( let mut prev_effective_align = layout.align; let mut result: Vec<_> = Vec::with_capacity(1 + field_count * 2); for i in layout.fields.index_by_increasing_offset() { - let target_offset = layout.fields.offset(i as usize); + let target_offset = layout.fields.offset(i); let field = layout.field(cx, i); let effective_field_align = layout.align.min(field.align).restrict_for_offset(target_offset); From 08fd5c4a5528feb742a29a885182e516a227bc8c Mon Sep 17 00:00:00 2001 From: malezjaa Date: Sun, 8 Nov 2026 10:10:27 +0100 Subject: [PATCH 3/6] 3/n: returns --- compiler/rustc_codegen_llvm/src/builder.rs | 5 ++- compiler/rustc_codegen_llvm/src/consts.rs | 5 ++- .../src/debuginfo/metadata.rs | 8 ++--- .../src/debuginfo/metadata/enums/cpp_like.rs | 6 ++-- compiler/rustc_codegen_llvm/src/intrinsic.rs | 36 ++----------------- compiler/rustc_codegen_llvm/src/va_arg.rs | 9 ++--- 6 files changed, 15 insertions(+), 54 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index ed5da8e69246f..dd8acea132d72 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -74,7 +74,7 @@ impl<'a, 'll> SBuilder<'a, 'll> { bundles.push(funclet_bundle); } - let call = unsafe { + unsafe { llvm::LLVMBuildCallWithOperandBundles( self.llbuilder(), llty, @@ -85,8 +85,7 @@ impl<'a, 'll> SBuilder<'a, 'll> { bundles.len() as c_uint, c"".as_ptr(), ) - }; - call + } } } diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs index d739b20a64339..047763ecc4d68 100644 --- a/compiler/rustc_codegen_llvm/src/consts.rs +++ b/compiler/rustc_codegen_llvm/src/consts.rs @@ -302,10 +302,9 @@ impl<'ll> CodegenCx<'ll, '_> { let gv = match kind { Some(kind) if !self.tcx.sess.fewer_names() => { let name = self.generate_local_symbol_name(kind); - let gv = self.define_global(&name, self.val_ty(cv)).unwrap_or_else(|| { + self.define_global(&name, self.val_ty(cv)).unwrap_or_else(|| { bug!("symbol `{}` is already defined", name); - }); - gv + }) } _ => self.define_global("", self.val_ty(cv)).unwrap_or_else(|| { bug!("anonymous global symbol is already defined"); diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 752cbc67d5637..9d1ded35db75a 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -990,7 +990,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>( ); let flags = c""; - let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit( + llvm::LLVMRustDIBuilderCreateCompileUnit( debug_context.builder.as_ref(), dwarf_const::DW_LANG_Rust, compile_unit_file, @@ -1008,10 +1008,8 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>( 0, tcx.sess.opts.unstable_opts.split_dwarf_inlining, debug_name_table_kind, - ); - - return unit_metadata; - }; + ) + } } /// Creates a `DW_TAG_member` entry inside the DIE represented by the given `type_di_node`. diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs index fe7f38fb195bc..37ad5223735c5 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs @@ -213,10 +213,8 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>( ), |cx, enum_type_di_node| { match enum_type_and_layout.variants { - Variants::Empty => { - // We don't generate any members for uninhabited types. - return smallvec![]; - } + // We don't generate any members for uninhabited types. + Variants::Empty => smallvec![], Variants::Single { index: variant_index } => build_single_variant_union_fields( cx, enum_adt_def, diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index 9c702a70dd291..7ca050c0a9b79 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -1495,17 +1495,7 @@ fn codegen_msvc_try<'ll, 'tcx>( // Note that no invoke is used here because by definition this function // can't panic (that's what it's catching). - let ret = bx.call( - llty, - None, - None, - llfn, - ReturnSlot::Direct, - &[try_func, data, catch_func], - None, - None, - ); - ret + bx.call(llty, None, None, llfn, ReturnSlot::Direct, &[try_func, data, catch_func], None, None) } // WASM's definition of the `rust_try` function. @@ -1596,17 +1586,7 @@ fn codegen_wasm_try<'ll, 'tcx>( // Note that no invoke is used here because by definition this function // can't panic (that's what it's catching). - let ret = bx.call( - llty, - None, - None, - llfn, - ReturnSlot::Direct, - &[try_func, data, catch_func], - None, - None, - ); - ret + bx.call(llty, None, None, llfn, ReturnSlot::Direct, &[try_func, data, catch_func], None, None) } // Definition of the standard `try` function for Rust using the GNU-like model @@ -1681,17 +1661,7 @@ fn codegen_gnu_try<'ll, 'tcx>( // Note that no invoke is used here because by definition this function // can't panic (that's what it's catching). - let ret = bx.call( - llty, - None, - None, - llfn, - ReturnSlot::Direct, - &[try_func, data, catch_func], - None, - None, - ); - ret + bx.call(llty, None, None, llfn, ReturnSlot::Direct, &[try_func, data, catch_func], None, None) } // Helper function to give a Block to a closure to codegen a shim function. diff --git a/compiler/rustc_codegen_llvm/src/va_arg.rs b/compiler/rustc_codegen_llvm/src/va_arg.rs index bef94359fa2e5..4c9ad7ddd7a7e 100644 --- a/compiler/rustc_codegen_llvm/src/va_arg.rs +++ b/compiler/rustc_codegen_llvm/src/va_arg.rs @@ -21,7 +21,7 @@ fn round_up_to_alignment<'ll>( align: Align, ) -> &'ll Value { value = bx.add(value, bx.cx().const_i32(align.bytes() as i32 - 1)); - return bx.and(value, bx.cx().const_i32(-(align.bytes() as i32))); + bx.and(value, bx.cx().const_i32(-(align.bytes() as i32))) } fn round_pointer_up_to_alignment<'ll>( @@ -294,10 +294,7 @@ fn emit_aapcs_va_arg<'ll, 'tcx>( bx.br(end); bx.switch_to_block(end); - let val = - bx.phi(layout.immediate_llvm_type(bx), &[reg_value, stack_value], &[in_reg, on_stack]); - - val + bx.phi(layout.immediate_llvm_type(bx), &[reg_value, stack_value], &[in_reg, on_stack]) } fn is_powerpc_softfloat_abi(target: &Target) -> bool { @@ -1060,7 +1057,7 @@ fn emit_xtensa_va_arg<'ll, 'tcx>( assert!(bx.tcx().sess.target.endian == Endian::Little); let value_ptr = bx.phi(bx.type_ptr(), &[regsave_value_ptr, stack_value_ptr], &[from_regsave, from_stack]); - return bx.load(layout.llvm_type(bx), value_ptr, layout.align); + bx.load(layout.llvm_type(bx), value_ptr, layout.align) } /// Determine the va_arg implementation to use. The LLVM va_arg instruction From af93603cf92b85b8f3b5ce55128a0753bc71a7e3 Mon Sep 17 00:00:00 2001 From: malezjaa Date: Sun, 8 Nov 2026 10:15:26 +0100 Subject: [PATCH 4/6] 4/n: smaller cleanups --- compiler/rustc_codegen_llvm/src/attributes.rs | 2 +- compiler/rustc_codegen_llvm/src/back/archive.rs | 8 ++++---- compiler/rustc_codegen_llvm/src/back/write.rs | 6 +++--- compiler/rustc_codegen_llvm/src/builder.rs | 6 +++--- compiler/rustc_codegen_llvm/src/builder/autodiff.rs | 2 +- compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs | 2 +- compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs | 2 +- .../src/debuginfo/metadata/enums/cpp_like.rs | 2 +- .../rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs | 3 +-- compiler/rustc_codegen_llvm/src/intrinsic.rs | 5 +---- compiler/rustc_codegen_llvm/src/llvm_util.rs | 4 ++-- compiler/rustc_codegen_llvm/src/mono_item.rs | 5 ++--- 12 files changed, 21 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs index d74c6dd68564b..b0113686a7b9b 100644 --- a/compiler/rustc_codegen_llvm/src/attributes.rs +++ b/compiler/rustc_codegen_llvm/src/attributes.rs @@ -410,7 +410,7 @@ fn stackprotector_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll A fn packed_stack_attr<'ll>( cx: &SimpleCx<'ll>, sess: &Session, - function_attributes: &Vec, + function_attributes: &[TargetFeature], ) -> Option<&'ll Attribute> { if sess.target.arch != Arch::S390x { return None; diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index f9dc48e3aba3f..80a465d4c88b4 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -82,10 +82,10 @@ fn get_llvm_object_symbols( unsafe extern "C" fn error_callback(error: *const c_char) -> *mut c_void { let error = unsafe { CStr::from_ptr(error) }; - Box::into_raw(Box::new(io::Error::new( - io::ErrorKind::Other, - format!("LLVM error: {}", error.to_string_lossy()), - )) as Box) as *mut c_void + Box::into_raw(Box::new(io::Error::other(format!( + "LLVM error: {}", + error.to_string_lossy() + )))) as *mut c_void } } diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index 38127c1c3141b..45a15dfcda7bf 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -1181,15 +1181,15 @@ fn create_section_with_flags_asm(section_name: &str, section_flags: &str, data: if byte == b'\\' || byte == b'"' { asm.push(b'\\'); asm.push(byte); - } else if byte < 0x20 || byte >= 0x80 { + } else if (0x20..0x80).contains(&byte) { + asm.push(byte); + } else { // Avoid non UTF-8 inline assembly. Use octal escape sequence, because it is fixed // width, while hex escapes will consume following characters. asm.push(b'\\'); asm.push(b'0' + ((byte >> 6) & 0x7)); asm.push(b'0' + ((byte >> 3) & 0x7)); asm.push(b'0' + ((byte >> 0) & 0x7)); - } else { - asm.push(byte); } } asm.extend_from_slice(b"\"\n"); diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index dd8acea132d72..ec7062d74e361 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -79,7 +79,7 @@ impl<'a, 'll> SBuilder<'a, 'll> { self.llbuilder(), llty, llfn, - args.as_ptr() as *const &llvm::Value, + args.as_ptr(), args.len() as c_uint, bundles.as_ptr(), bundles.len() as c_uint, @@ -153,7 +153,7 @@ impl<'a, 'll, CX: Borrow>> GenericBuilder<'a, 'll, CX> { // Cast to default addrspace if necessary llvm::LLVMBuildPointerCast(self.llbuilder(), alloca, self.cx.type_ptr(), UNNAMED) }; - if name != "" { + if !name.is_empty() { llvm::set_value_name(val, &name.as_bytes()); } val @@ -1479,7 +1479,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { self.llbuilder(), llty, llfn, - args.as_ptr() as *const &llvm::Value, + args.as_ptr(), args.len() as c_uint, bundles.as_ptr(), bundles.len() as c_uint, diff --git a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs index 2bc3dc2903995..b30bccabccbca 100644 --- a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs +++ b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs @@ -110,7 +110,7 @@ pub(crate) fn adjust_activity_to_abi<'tcx>( if let rustc_abi::BackendRepr::ScalarPair { a: _, b: _, b_offset: _ } = layout.backend_repr() { - new_activities.push(da[i].clone()); + new_activities.push(da[i]); new_positions.push(i + 1 - del_activities); } } diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs index 92abe16eb4109..562e47365b3cb 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs @@ -41,7 +41,7 @@ pub(crate) fn create_pgo_func_name_var<'ll>( pub(crate) fn write_filenames_to_buffer(filenames: &[impl AsRef]) -> Vec { let (pointers, lengths) = filenames - .into_iter() + .iter() .map(AsRef::as_ref) .map(|s: &str| (s.as_ptr(), s.len())) .unzip::<_, _, Vec<_>, Vec<_>>(); diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 9d1ded35db75a..a3a1780ddb219 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -1855,7 +1855,7 @@ pub(crate) fn extend_scope_to_file<'ll>( } fn tuple_field_name(field_index: usize) -> Cow<'static, str> { - const TUPLE_FIELD_NAMES: [&'static str; 16] = [ + const TUPLE_FIELD_NAMES: [&str; 16] = [ "__0", "__1", "__2", "__3", "__4", "__5", "__6", "__7", "__8", "__9", "__10", "__11", "__12", "__13", "__14", "__15", ]; diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs index 37ad5223735c5..360d7ac49dfdf 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs @@ -809,7 +809,7 @@ fn build_union_fields_for_direct_tag_enum_or_coroutine<'ll, 'tcx>( let mut unions_fields = SmallVec::with_capacity(variant_field_infos.len() + 1); // We create a field in the union for each variant ... - unions_fields.extend(variant_field_infos.into_iter().map(|variant_member_info| { + unions_fields.extend(variant_field_infos.iter().map(|variant_member_info| { let (file_di_node, line_number) = variant_member_info .source_info .unwrap_or_else(|| (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)); diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs index c9039bf46e416..e3ae84e6f496a 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs @@ -321,8 +321,7 @@ pub(super) fn build_type_with_children<'ll, 'tcx>( debug_context(cx).type_map.insert(stub_info.unique_type_id, stub_info.metadata); - let members: SmallVec<_> = - members(cx, stub_info.metadata).into_iter().map(|node| Some(node)).collect(); + let members: SmallVec<_> = members(cx, stub_info.metadata).into_iter().map(Some).collect(); let generics = generics(cx); if !(members.is_empty() && generics.is_empty()) { diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index 7ca050c0a9b79..381dede0c1c75 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -2139,10 +2139,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>( return Ok(splat); } - let supports_scalable = match name { - sym::simd_cast | sym::simd_select => true, - _ => false, - }; + let supports_scalable = matches!(name, sym::simd_cast | sym::simd_select); // Every intrinsic below takes a SIMD vector as its first argument. Some intrinsics also accept // scalable vectors. `require_simd_or_scalable` is used regardless as it'll do the right thing diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 09305eba07569..b9e976f662958 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -95,7 +95,7 @@ unsafe fn configure_llvm(sess: &EarlySession) { let sess_args = tg_opts.chain(cg_opts); let user_specified_args: FxHashSet<_> = - sess_args.clone().map(|s| llvm_arg_to_arg_name(s)).filter(|s| !s.is_empty()).collect(); + sess_args.clone().map(llvm_arg_to_arg_name).filter(|s| !s.is_empty()).collect(); { // This adds the given argument to LLVM. Unless `force` is true @@ -337,7 +337,7 @@ pub(crate) fn target_config(sess: &EarlySession) -> TargetConfig { sess, |feature| { to_llvm_features(&sess.target, feature) - .map(|f| SmallVec::<[&str; 2]>::from_iter(f.into_iter())) + .map(SmallVec::<[&str; 2]>::from_iter) .unwrap_or_default() }, |feature| { diff --git a/compiler/rustc_codegen_llvm/src/mono_item.rs b/compiler/rustc_codegen_llvm/src/mono_item.rs index 82bd5584211c0..68f9dd62d679d 100644 --- a/compiler/rustc_codegen_llvm/src/mono_item.rs +++ b/compiler/rustc_codegen_llvm/src/mono_item.rs @@ -1,4 +1,3 @@ -use std::borrow::Cow; use std::ffi::CString; use rustc_abi::AddressSpace; @@ -78,7 +77,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { fn predefine_without_aliases( &self, instance: Instance<'tcx>, - attrs: &Cow<'_, CodegenFnAttrs>, + attrs: &CodegenFnAttrs, linkage: Linkage, visibility: Visibility, symbol_name: &str, @@ -167,7 +166,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> { &self, aliasee_instance: Instance<'tcx>, aliasee: &'ll llvm::Value, - attrs: &Cow<'_, CodegenFnAttrs>, + attrs: &CodegenFnAttrs, aliases: &[(DefId, Linkage, Visibility)], ) { for (alias, linkage, visibility) in aliases { From ae398250740d7329ab2822a92c3d09519737ed57 Mon Sep 17 00:00:00 2001 From: malezjaa Date: Sun, 8 Nov 2026 10:32:07 +0100 Subject: [PATCH 5/6] 5/n: needless borrows/derefs --- compiler/rustc_codegen_llvm/src/abi.rs | 2 +- compiler/rustc_codegen_llvm/src/allocator.rs | 6 ++-- .../src/back/llvm_backend.rs | 6 ++-- compiler/rustc_codegen_llvm/src/back/lto.rs | 8 ++--- compiler/rustc_codegen_llvm/src/back/write.rs | 6 ++-- compiler/rustc_codegen_llvm/src/builder.rs | 2 +- .../src/builder/autodiff.rs | 4 +-- .../src/builder/gpu_offload.rs | 30 +++++++++---------- compiler/rustc_codegen_llvm/src/consts.rs | 4 +-- compiler/rustc_codegen_llvm/src/context.rs | 2 +- .../src/debuginfo/metadata.rs | 2 +- .../src/debuginfo/metadata/enums/native.rs | 2 +- compiler/rustc_codegen_llvm/src/intrinsic.rs | 15 +++++----- compiler/rustc_codegen_llvm/src/llvm_util.rs | 6 ++-- compiler/rustc_codegen_llvm/src/typetree.rs | 10 +++---- 15 files changed, 52 insertions(+), 53 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/abi.rs b/compiler/rustc_codegen_llvm/src/abi.rs index 6270ce19a69b5..9a1bd59daae3e 100644 --- a/compiler/rustc_codegen_llvm/src/abi.rs +++ b/compiler/rustc_codegen_llvm/src/abi.rs @@ -728,7 +728,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> { } } - let cconv = self.llvm_cconv(&bx.cx); + let cconv = self.llvm_cconv(bx.cx); if cconv != llvm::CCallConv { llvm::SetInstructionCallConv(callsite, cconv); } diff --git a/compiler/rustc_codegen_llvm/src/allocator.rs b/compiler/rustc_codegen_llvm/src/allocator.rs index afddb875ec60c..9a0cdf5f77ff0 100644 --- a/compiler/rustc_codegen_llvm/src/allocator.rs +++ b/compiler/rustc_codegen_llvm/src/allocator.rs @@ -118,7 +118,7 @@ fn create_wrapper_function<'ll>( ) { let ty = cx.type_func(args, output.unwrap_or_else(|| cx.type_void())); let llfn = declare_simple_fn( - &cx, + cx, from_name, llvm::CallConv::CCallConv, llvm::UnnamedAddr::Global, @@ -149,11 +149,11 @@ fn create_wrapper_function<'ll>( } let llbb = unsafe { llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, c"entry".as_ptr()) }; - let mut bx = SBuilder::build(&cx, llbb); + let mut bx = SBuilder::build(cx, llbb); if let Some(to_name) = to_name { let callee = declare_simple_fn( - &cx, + cx, to_name, llvm::CallConv::CCallConv, llvm::UnnamedAddr::Global, diff --git a/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs b/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs index 69d63c52e4b88..43a9d6b971c73 100644 --- a/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs +++ b/compiler/rustc_codegen_llvm/src/back/llvm_backend.rs @@ -105,7 +105,7 @@ impl WriteBackendMethods for LlvmCodegenBackend { each_linked_rlib_for_lto: &[PathBuf], modules: Vec>, ) -> CompiledModule { - let mut module = back::lto::run_fat( + let module = back::lto::run_fat( cgcx, &sess.prof, shared_emitter, @@ -117,7 +117,7 @@ impl WriteBackendMethods for LlvmCodegenBackend { let dcx = DiagCtxt::new(Box::new(shared_emitter.clone())); let dcx = dcx.handle(); - back::lto::run_pass_manager(cgcx, &sess.prof, dcx, &mut module, false); + back::lto::run_pass_manager(cgcx, &sess.prof, dcx, &module, false); back::write::codegen(cgcx, &sess.prof, shared_emitter, module, &cgcx.module_config) } @@ -401,7 +401,7 @@ impl CodegenBackend for LlvmCodegenBackend { llvm::build_string(|s| unsafe { llvm::LLVMRustPrintStatisticsJSON(s) }).unwrap(); if !llvm_stats_json.is_empty() { - if let Err(e) = std::fs::write(&out_path, llvm_stats_json) { + if let Err(e) = std::fs::write(out_path, llvm_stats_json) { sess.dcx().err(format!("failed to write stats to {out_path}: {e}")); } } else { diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index 8258c1f7c8d20..d1ce0a3cbcf94 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -94,12 +94,12 @@ fn prepare_lto( let mut upstream_modules = Vec::new(); for path in each_linked_rlib_for_lto { let archive_data = unsafe { - Mmap::map(std::fs::File::open(&path).expect("couldn't open rlib")) + Mmap::map(std::fs::File::open(path).expect("couldn't open rlib")) .expect("couldn't map rlib") }; let archive = ArchiveFile::parse(&*archive_data).expect("wanted an rlib"); let metadata_link = - rmeta_link::read(&archive, &archive_data, &path, cgcx.target_is_like_aix).unwrap(); + rmeta_link::read(&archive, &archive_data, path, cgcx.target_is_like_aix).unwrap(); let obj_files = archive .members() .filter_map(|child| { @@ -465,7 +465,7 @@ fn thin_lto( incr_comp_session.new_session_directory.join(THIN_LTO_KEYS_INCR_COMP_FILE_NAME) }); - let prev_key_map = if let Some(ref old_incr_comp_session_dir) = incr_comp_session + let prev_key_map = if let Some(old_incr_comp_session_dir) = incr_comp_session .and_then(|incr_comp_session| incr_comp_session.old_session_directory.as_deref()) { let old_path = old_incr_comp_session_dir.join(THIN_LTO_KEYS_INCR_COMP_FILE_NAME); @@ -548,7 +548,7 @@ pub(crate) fn enable_autodiff_settings(ad: &[config::AutoDiff]) { } config::AutoDiff::PrintTAFn(fun) => { enzyme.set_print_type(true); // Enable general type printing - enzyme.set_print_type_fun(&fun); // Set specific function to analyze + enzyme.set_print_type_fun(fun); // Set specific function to analyze } config::AutoDiff::Inline => { enzyme.set_inline(true); diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index 45a15dfcda7bf..6f694241a3a4a 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -301,7 +301,7 @@ pub(crate) fn save_temp_bitcode( } fn write_bitcode_to_file(module: &ModuleLlvm, path: &Path) { - let path = path_to_c_string(&path); + let path = path_to_c_string(path); module.with_module_refs(|ModuleRefs { llmod, .. }| { unsafe { llvm::LLVMWriteBitcodeToFile(llmod, path.as_ptr()) }; }); @@ -689,7 +689,7 @@ pub(crate) unsafe fn llvm_optimize<'ll>( // Create the new function, with a temporary .offload name to avoid a name collision. let old_fn_name = String::from_utf8(llvm::get_value_name(old_fn)).unwrap(); - let new_fn_name = format!("{}.offload", &old_fn_name); + let new_fn_name = format!("{old_fn_name}.offload"); let new_fn = cx.add_func(&new_fn_name, new_fn_ty); let a0 = llvm::get_param(new_fn, 0); llvm::set_value_name(a0, CString::new("dyn_ptr").unwrap().as_bytes()); @@ -1026,7 +1026,7 @@ pub(crate) fn codegen( .generic_activity_with_arg("LLVM_module_codegen_embed_bitcode", &*module.name); let thin_bc = module.module_llvm.thin_lto_buffer.get().expect("cannot find embedded bitcode"); - embed_bitcode(cgcx, llcx, llmod, &thin_bc); + embed_bitcode(cgcx, llcx, llmod, thin_bc); } } diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index ec7062d74e361..033336a9823c1 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -154,7 +154,7 @@ impl<'a, 'll, CX: Borrow>> GenericBuilder<'a, 'll, CX> { llvm::LLVMBuildPointerCast(self.llbuilder(), alloca, self.cx.type_ptr(), UNNAMED) }; if !name.is_empty() { - llvm::set_value_name(val, &name.as_bytes()); + llvm::set_value_name(val, name.as_bytes()); } val } diff --git a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs index b30bccabccbca..6592ce7816955 100644 --- a/compiler/rustc_codegen_llvm/src/builder/autodiff.rs +++ b/compiler/rustc_codegen_llvm/src/builder/autodiff.rs @@ -346,7 +346,7 @@ pub(crate) fn generate_enzyme_call<'ll, 'tcx>( enzyme_ty, ); - let num_args = llvm::LLVMCountParams(&fn_to_diff); + let num_args = llvm::LLVMCountParams(fn_to_diff); let mut args = Vec::with_capacity(num_args as usize + 1); args.push(fn_to_diff); @@ -363,7 +363,7 @@ pub(crate) fn generate_enzyme_call<'ll, 'tcx>( match_args_from_caller_to_enzyme(bx, attrs.width, &mut args, &attrs.input_activity, fn_args); if !fnc_tree.args.is_empty() || !fnc_tree.ret.0.is_empty() { - crate::typetree::add_tt(&bx, fn_to_diff, fnc_tree); + crate::typetree::add_tt(bx, fn_to_diff, fnc_tree); } let call = bx.call(enzyme_ty, None, None, ad_fn, ReturnSlot::Direct, &args, None, None); diff --git a/compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs b/compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs index 71935d8a998c1..2faa29e662766 100644 --- a/compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs +++ b/compiler/rustc_codegen_llvm/src/builder/gpu_offload.rs @@ -127,7 +127,7 @@ fn generate_launcher<'ll>(cx: &CodegenCx<'ll, '_>) -> (&'ll llvm::Value, llvm::T let args = vec![tptr, ti64, ti32, ti32, tptr, tptr]; let tgt_fn_ty = cx.type_func(&args, ti32); let name = "__tgt_target_kernel"; - let tgt_decl = declare_offload_fn(&cx, name, tgt_fn_ty); + let tgt_decl = declare_offload_fn(cx, name, tgt_fn_ty); let nounwind = llvm::AttributeKind::NoUnwind.create_attr(cx.llcx); attributes::apply_to_llfn(tgt_decl, Function, &[nounwind]); (tgt_decl, tgt_fn_ty) @@ -141,7 +141,7 @@ pub(crate) fn declare_omp_get_num_devices<'ll>( let ti32 = cx.type_i32(); let tgt_fn_ty = cx.type_func(&[], ti32); let name = "omp_get_num_devices"; - let tgt_decl = declare_offload_fn(&cx, name, tgt_fn_ty); + let tgt_decl = declare_offload_fn(cx, name, tgt_fn_ty); let nounwind = llvm::AttributeKind::NoUnwind.create_attr(cx.llcx); attributes::apply_to_llfn(tgt_decl, Function, &[nounwind]); (tgt_decl, tgt_fn_ty) @@ -157,7 +157,7 @@ pub(crate) fn generate_at_one<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll llvm::Value let c_entry_name = CString::new(unknown_txt).unwrap(); let c_val = c_entry_name.as_bytes_with_nul(); let initializer = crate::common::bytes_in_context(cx.llcx, c_val); - let at_zero = add_unnamed_global(&cx, &"", initializer, PrivateLinkage); + let at_zero = add_unnamed_global(cx, "", initializer, PrivateLinkage); llvm::set_alignment(at_zero, Align::ONE); // @1 = private unnamed_addr constant %struct.ident_t { i32 0, i32 2, i32 0, i32 22, ptr @0 }, align 8 @@ -172,7 +172,7 @@ pub(crate) fn generate_at_one<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll llvm::Value let struct_elems_ty: Vec<_> = struct_elems.iter().map(|&x| cx.val_ty(x)).collect(); let initializer = crate::common::named_struct(struct_ident_ty, &struct_elems); cx.set_struct_body(struct_ident_ty, &struct_elems_ty); - let at_one = add_unnamed_global(&cx, &"", initializer, PrivateLinkage); + let at_one = add_unnamed_global(cx, "", initializer, PrivateLinkage); llvm::set_alignment(at_one, Align::EIGHT); at_one } @@ -355,9 +355,9 @@ fn gen_tgt_data_mappers<'ll>( let mapper_begin = "__tgt_target_data_begin_mapper"; let mapper_update = "__tgt_target_data_update_mapper"; let mapper_end = "__tgt_target_data_end_mapper"; - let begin_mapper_decl = declare_offload_fn(&cx, mapper_begin, mapper_fn_ty); - let update_mapper_decl = declare_offload_fn(&cx, mapper_update, mapper_fn_ty); - let end_mapper_decl = declare_offload_fn(&cx, mapper_end, mapper_fn_ty); + let begin_mapper_decl = declare_offload_fn(cx, mapper_begin, mapper_fn_ty); + let update_mapper_decl = declare_offload_fn(cx, mapper_update, mapper_fn_ty); + let end_mapper_decl = declare_offload_fn(cx, mapper_end, mapper_fn_ty); let nounwind = llvm::AttributeKind::NoUnwind.create_attr(cx.llcx); attributes::apply_to_llfn(begin_mapper_decl, Function, &[nounwind]); @@ -455,34 +455,34 @@ pub(crate) fn gen_define_handling<'ll>( }) .collect::>(); let offload_sizes = - add_priv_unnamed_arr(&cx, &format!(".offload_sizes.{symbol}"), &actual_sizes); + add_priv_unnamed_arr(cx, &format!(".offload_sizes.{symbol}"), &actual_sizes); let memtransfer_begin = - add_priv_unnamed_arr(&cx, &format!(".offload_maptypes.{symbol}.begin"), &transfer_to); + add_priv_unnamed_arr(cx, &format!(".offload_maptypes.{symbol}.begin"), &transfer_to); let memtransfer_kernel = - add_priv_unnamed_arr(&cx, &format!(".offload_maptypes.{symbol}.kernel"), &transfer_kernel); + add_priv_unnamed_arr(cx, &format!(".offload_maptypes.{symbol}.kernel"), &transfer_kernel); let memtransfer_end = - add_priv_unnamed_arr(&cx, &format!(".offload_maptypes.{symbol}.end"), &transfer_from); + add_priv_unnamed_arr(cx, &format!(".offload_maptypes.{symbol}.end"), &transfer_from); // Next: For each function, generate these three entries. A weak constant, // the llvm.rodata entry name, and the llvm_offload_entries value let name = format!(".{symbol}.region_id"); let initializer = cx.get_const_i8(0); - let region_id = add_global(&cx, &name, initializer, WeakAnyLinkage); + let region_id = add_global(cx, &name, initializer, WeakAnyLinkage); let c_entry_name = CString::new(symbol.clone()).unwrap(); let c_val = c_entry_name.as_bytes_with_nul(); let offload_entry_name = format!(".offloading.entry_name.{symbol}"); let initializer = crate::common::bytes_in_context(cx.llcx, c_val); - let llglobal = add_unnamed_global(&cx, &offload_entry_name, initializer, InternalLinkage); + let llglobal = add_unnamed_global(cx, &offload_entry_name, initializer, InternalLinkage); llvm::set_alignment(llglobal, Align::ONE); llvm::set_section(llglobal, c".llvm.rodata.offloading"); let name = format!(".offloading.entry.{symbol}"); // See the __tgt_offload_entry documentation above. - let elems = TgtOffloadEntry::new(&cx, region_id, llglobal); + let elems = TgtOffloadEntry::new(cx, region_id, llglobal); let initializer = crate::common::named_struct(offload_entry_ty, &elems); let c_name = CString::new(name).unwrap(); @@ -735,7 +735,7 @@ pub(crate) fn gen_call_handling<'ll, 'tcx>( s_ident_t, ); let values = KernelArgsTy::new( - &cx, + cx, num_args, memtransfer_kernel, geps, diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs index 047763ecc4d68..338f7a6d1ff08 100644 --- a/compiler/rustc_codegen_llvm/src/consts.rs +++ b/compiler/rustc_codegen_llvm/src/consts.rs @@ -216,7 +216,7 @@ fn check_and_apply_linkage<'ll, 'tcx>( { should_sign = true; } - cx.declare_fn(sym, &fn_abi, None) + cx.declare_fn(sym, fn_abi, None) } else { cx.declare_global(sym, cx.type_i8()) } @@ -891,7 +891,7 @@ impl<'ll> CodegenCx<'ll, '_> { let llval = crate::common::named_struct(llty, &[version, size, name, symtab]); let sym = "OBJC_MODULES"; - let g = self.define_global(&sym, llty).unwrap_or_else(|| { + let g = self.define_global(sym, llty).unwrap_or_else(|| { bug!("symbol `{}` is already defined", sym); }); set_global_alignment(self, g, self.tcx.data_layout.pointer_align()); diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 2156ac783758a..028b2e7d34038 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -1160,7 +1160,7 @@ impl<'ll> CodegenCx<'ll, '_> { _ => { let intrinsic = llvm::Intrinsic::lookup(base_name.as_bytes()) .unwrap_or_else(|| bug!("Unknown intrinsic: `{base_name}`")); - let f = intrinsic.get_declaration(self.llmod, &type_params); + let f = intrinsic.get_declaration(self.llmod, type_params); (self.get_type_of_global(f), f) } } diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index a3a1780ddb219..847cf4f31736c 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -531,7 +531,7 @@ pub(crate) fn spanned_type_di_node<'ll, 'tcx>( // FIXME(mw): Cache this via a regular UniqueTypeId instead of an extra field in the debug context. fn recursion_marker_type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> &'ll DIType { - *debug_context(cx).recursion_marker_type.get_or_init(move || { + debug_context(cx).recursion_marker_type.get_or_init(move || { // The choice of type here is pretty arbitrary - // anything reading the debuginfo for a recursive // type is going to see *something* weird - the only diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs index c9cac61a8bbe1..2809e588b7635 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs @@ -369,7 +369,7 @@ fn build_discr_member_di_node<'ll, 'tcx>( Some(create_member_type( cx, containing_scope, - &tag_name, + tag_name, file, UNKNOWN_LINE_NUMBER, layout, diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index 381dede0c1c75..25ba4884e9db9 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -209,7 +209,7 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> { assert_eq!(args[1].layout, args[2].layout); let select = |bx: &mut Self, true_val, false_val| { let result = bx.select(cond, true_val, false_val); - bx.set_unpredictable(&result); + bx.set_unpredictable(result); result }; match (args[1].val, args[2].val) { @@ -251,9 +251,8 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> { if let CVariadicStatus::Unstable { feature } = stability && !self.tcx.features().enabled(feature) { - let msg = - format!("C-variadic function definitions on this target are unstable"); - feature_err(&*self.sess(), feature, span, msg).emit(); + let msg = "C-variadic function definitions on this target are unstable"; + feature_err(self.sess(), feature, span, msg).emit(); } let BackendRepr::Scalar(scalar) = result_layout.backend_repr else { @@ -1804,7 +1803,7 @@ fn codegen_autodiff<'ll, 'tcx>( }; let val_arr = get_args_from_tuple(bx, args[2], fn_diff); - let diff_symbol = symbol_name_for_instance_in_crate(tcx, fn_diff.clone(), LOCAL_CRATE); + let diff_symbol = symbol_name_for_instance_in_crate(tcx, fn_diff, LOCAL_CRATE); let Some(Some(mut diff_attrs)) = find_attr!(tcx, fn_diff.def_id(), RustcAutodiff(attr) => attr.clone()) @@ -1906,7 +1905,7 @@ fn codegen_offload<'ll, 'tcx>( } }; let offload_data = - gpu_offload::gen_define_handling(&cx, &metadata, target_symbol, offload_globals); + gpu_offload::gen_define_handling(cx, &metadata, target_symbol, offload_globals); gpu_offload::gen_call_handling( bx, &offload_data, @@ -1915,8 +1914,8 @@ fn codegen_offload<'ll, 'tcx>( &metadata, offload_globals, &offload_dims, - &dyn_cache, - &device_id, + dyn_cache, + device_id, ); } diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index b9e976f662958..e054eacce5143 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -144,7 +144,7 @@ unsafe fn configure_llvm(sess: &EarlySession) { } for arg in sess_args { - add(&(*arg), true); + add(arg, true); } match ( @@ -528,7 +528,7 @@ pub(crate) fn print(req: &PrintRequest, out: &mut String, sess: &Session) { fn print_target_cpus(sess: &Session, tm: &llvm::TargetMachine, out: &mut String) { let cpu_names = llvm::build_string(|s| unsafe { - llvm::LLVMRustPrintTargetCPUs(&tm, s); + llvm::LLVMRustPrintTargetCPUs(tm, s); }) .unwrap(); @@ -594,7 +594,7 @@ fn print_target_features(sess: &Session, tm: &llvm::TargetMachine, out: &mut Str } // LLVM asserts that these are sorted. LLVM and Rust both use byte comparison for these // strings. - let llvm_feature = to_llvm_features(&sess.target, *feature)?.llvm_feature_name; + let llvm_feature = to_llvm_features(&sess.target, feature)?.llvm_feature_name; let desc = match llvm_target_features.binary_search_by_key(&llvm_feature, |(f, _d)| f).ok() { Some(index) => { diff --git a/compiler/rustc_codegen_llvm/src/typetree.rs b/compiler/rustc_codegen_llvm/src/typetree.rs index efda11f399ba4..53505e10e7bcd 100644 --- a/compiler/rustc_codegen_llvm/src/typetree.rs +++ b/compiler/rustc_codegen_llvm/src/typetree.rs @@ -12,7 +12,7 @@ fn to_enzyme_typetree<'ll>( llcx: llvm::Context<'ll>, ) -> (llvm::TypeTree, Vec) { let mut enzyme_tt = llvm::TypeTree::new(); - let extra_ints = process_typetree_recursive(&mut enzyme_tt, &rust_typetree, &[], llcx); + let extra_ints = process_typetree_recursive(&mut enzyme_tt, rust_typetree, &[], llcx); let mut int_vec = vec![]; for _ in 0..extra_ints { @@ -105,7 +105,7 @@ pub(crate) fn add_tt<'tcx, 'll>(cx: &FullCx<'ll, 'tcx>, fn_def: &'ll Value, tt: let mut offset = 0; for (i, input) in inputs.iter().enumerate() { - let (enzyme_tt, extra_ints) = to_enzyme_typetree(&input, llvm_data_layout, llcx); + let (enzyme_tt, extra_ints) = to_enzyme_typetree(input, llvm_data_layout, llcx); // This scope is just a visual reminder that we *must* drop the enzyme_wrapper before // we drop any typetrees (mainly enzyme_tt and extra_ints). Drop calls can not accept @@ -116,7 +116,7 @@ pub(crate) fn add_tt<'tcx, 'll>(cx: &FullCx<'ll, 'tcx>, fn_def: &'ll Value, tt: let enzyme_wrapper = EnzymeWrapper::get_instance(); let c_str = enzyme_wrapper.tree_to_cstr(enzyme_tt.inner); - let attr = llvm::CreateAttrStringValueFromCStr(llcx, &c_attr_name, &c_str); + let attr = llvm::CreateAttrStringValueFromCStr(llcx, &c_attr_name, c_str); let arg_pos = llvm::AttributePlace::Argument(i as u32 + offset); // FIXME(autodiff): We currently know that this is correct for all the cases in which we // call this function. But we should make it more robust for the future. @@ -132,7 +132,7 @@ pub(crate) fn add_tt<'tcx, 'll>(cx: &FullCx<'ll, 'tcx>, fn_def: &'ll Value, tt: for v in &extra_ints { offset += 1; let c_str = enzyme_wrapper.tree_to_cstr(v.inner); - let int_attr = llvm::CreateAttrStringValueFromCStr(llcx, &c_attr_name, &c_str); + let int_attr = llvm::CreateAttrStringValueFromCStr(llcx, &c_attr_name, c_str); let arg_pos = llvm::AttributePlace::Argument(i as u32 + offset); match tt_location { TTLocation::Definition => { @@ -165,7 +165,7 @@ pub(crate) fn add_tt<'tcx, 'll>(cx: &FullCx<'ll, 'tcx>, fn_def: &'ll Value, tt: if ret_tt != RustTypeTree::new() { let enzyme_wrapper = EnzymeWrapper::get_instance(); let c_str = enzyme_wrapper.tree_to_cstr(enzyme_tt.inner); - let ret_attr = llvm::CreateAttrStringValueFromCStr(llcx, &c_attr_name, &c_str); + let ret_attr = llvm::CreateAttrStringValueFromCStr(llcx, &c_attr_name, c_str); let arg_pos = llvm::AttributePlace::ReturnValue; match tt_location { TTLocation::Definition => { From 3ba6ed9dec0b2eb85c1b006e06d1190dc09ceb36 Mon Sep 17 00:00:00 2001 From: malezjaa Date: Sun, 8 Nov 2026 10:46:17 +0100 Subject: [PATCH 6/6] 1/n: split set_struct_body into set_struct_body and set_struct_body_packed --- compiler/rustc_codegen_llvm/src/context.rs | 25 +++++++++-------- compiler/rustc_codegen_llvm/src/llvm_util.rs | 28 +++++++------------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 028b2e7d34038..75eb0062c50f2 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -516,20 +516,19 @@ fn init_module<'ll, 'tcx>(tcx: TyCtxt<'tcx>, llcx: llvm::Context<'ll>, llmod: ll ); } - match (sess.opts.unstable_opts.small_data_threshold, sess.target.small_data_threshold_support()) + // Set up the small-data optimization limit for architectures that use + // an LLVM module flag to control this. + if let Some(threshold) = sess.opts.unstable_opts.small_data_threshold + && let SmallDataThresholdSupport::LlvmModuleFlag(flag) = + sess.target.small_data_threshold_support() { - // Set up the small-data optimization limit for architectures that use - // an LLVM module flag to control this. - (Some(threshold), SmallDataThresholdSupport::LlvmModuleFlag(flag)) => { - llvm::add_module_flag_u32( - llmod, - llvm::ModuleFlagMergeBehavior::Error, - &flag, - threshold as u32, - ); - } - _ => (), - }; + llvm::add_module_flag_u32( + llmod, + llvm::ModuleFlagMergeBehavior::Error, + &flag, + threshold as u32, + ); + } // Insert `llvm.ident` metadata. // diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index e054eacce5143..ac0d31fb78b49 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -147,17 +147,14 @@ unsafe fn configure_llvm(sess: &EarlySession) { add(arg, true); } - match ( - sess.opts.unstable_opts.small_data_threshold, - sess.target.small_data_threshold_support(), - ) { - // Set up the small-data optimization limit for architectures that use - // an LLVM argument to control this. - (Some(threshold), SmallDataThresholdSupport::LlvmArg(arg)) => { - add(&format!("--{arg}={threshold}"), false) - } - _ => (), - }; + // Set up the small-data optimization limit for architectures that use + // an LLVM argument to control this. + if let Some(threshold) = sess.opts.unstable_opts.small_data_threshold + && let SmallDataThresholdSupport::LlvmArg(arg) = + sess.target.small_data_threshold_support() + { + add(&format!("--{arg}={threshold}"), false); + } } if sess.opts.unstable_opts.llvm_time_trace { @@ -345,13 +342,8 @@ pub(crate) fn target_config(sess: &EarlySession) -> TargetConfig { // do *not* consider the `-Ctarget-feature`s here (that's why we passed `for_cfg: true` // to `global_llvm_features` above) because that will be handled later in // `internal_target_features`. - if let Some(feat) = to_llvm_features(&sess.target, feature) - && mc_subtarget_info.has_features(feat) - { - true - } else { - false - } + to_llvm_features(&sess.target, feature) + .is_some_and(|feat| mc_subtarget_info.has_features(feat)) }, );