From 2bee8d2d281670c66cd033e4a41df92ffaf3fda8 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 6 Oct 2025 13:38:00 +1100 Subject: [PATCH 1/3] Remove inherent methods from `llvm::TypeKind` --- .../src/llvm/conversions.rs | 40 ++++++++++++++++++- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 27 ------------- compiler/rustc_codegen_llvm/src/type_.rs | 2 +- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/llvm/conversions.rs b/compiler/rustc_codegen_llvm/src/llvm/conversions.rs index 9e9f9339ade85..3bc790ca7cff4 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/conversions.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/conversions.rs @@ -1,6 +1,6 @@ //! Conversions from backend-independent data types to/from LLVM FFI types. -use rustc_codegen_ssa::common::{AtomicRmwBinOp, IntPredicate, RealPredicate}; +use rustc_codegen_ssa::common::{AtomicRmwBinOp, IntPredicate, RealPredicate, TypeKind}; use rustc_middle::ty::AtomicOrdering; use rustc_session::config::DebugInfo; use rustc_target::spec::SymbolVisibility; @@ -9,10 +9,22 @@ use crate::llvm; /// Helper trait for converting backend-independent types to LLVM-specific /// types, for FFI purposes. +/// +/// FIXME(#147327): These trait/method names were chosen to avoid churn in +/// existing code, but are not great and could probably be made clearer. pub(crate) trait FromGeneric { fn from_generic(other: T) -> Self; } +/// Helper trait for converting LLVM-specific types to backend-independent +/// types, for FFI purposes. +/// +/// FIXME(#147327): These trait/method names were chosen to avoid churn in +/// existing code, but are not great and could probably be made clearer. +pub(crate) trait ToGeneric { + fn to_generic(&self) -> T; +} + impl FromGeneric for llvm::Visibility { fn from_generic(visibility: SymbolVisibility) -> Self { match visibility { @@ -113,3 +125,29 @@ impl FromGeneric for llvm::debuginfo::DebugEmissionKind { } } } + +impl ToGeneric for llvm::TypeKind { + fn to_generic(&self) -> TypeKind { + match self { + Self::Void => TypeKind::Void, + Self::Half => TypeKind::Half, + Self::Float => TypeKind::Float, + Self::Double => TypeKind::Double, + Self::X86_FP80 => TypeKind::X86_FP80, + Self::FP128 => TypeKind::FP128, + Self::PPC_FP128 => TypeKind::PPC_FP128, + Self::Label => TypeKind::Label, + Self::Integer => TypeKind::Integer, + Self::Function => TypeKind::Function, + Self::Struct => TypeKind::Struct, + Self::Array => TypeKind::Array, + Self::Pointer => TypeKind::Pointer, + Self::Vector => TypeKind::Vector, + Self::Metadata => TypeKind::Metadata, + Self::Token => TypeKind::Token, + Self::ScalableVector => TypeKind::ScalableVector, + Self::BFloat => TypeKind::BFloat, + Self::X86_AMX => TypeKind::X86_AMX, + } + } +} diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index c4b5cf413a725..d3665b229b083 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -360,33 +360,6 @@ pub(crate) enum TypeKind { X86_AMX = 19, } -impl TypeKind { - pub(crate) fn to_generic(self) -> rustc_codegen_ssa::common::TypeKind { - use rustc_codegen_ssa::common::TypeKind as Common; - match self { - Self::Void => Common::Void, - Self::Half => Common::Half, - Self::Float => Common::Float, - Self::Double => Common::Double, - Self::X86_FP80 => Common::X86_FP80, - Self::FP128 => Common::FP128, - Self::PPC_FP128 => Common::PPC_FP128, - Self::Label => Common::Label, - Self::Integer => Common::Integer, - Self::Function => Common::Function, - Self::Struct => Common::Struct, - Self::Array => Common::Array, - Self::Pointer => Common::Pointer, - Self::Vector => Common::Vector, - Self::Metadata => Common::Metadata, - Self::Token => Common::Token, - Self::ScalableVector => Common::ScalableVector, - Self::BFloat => Common::BFloat, - Self::X86_AMX => Common::X86_AMX, - } - } -} - /// LLVMAtomicRmwBinOp #[derive(Copy, Clone)] #[repr(C)] diff --git a/compiler/rustc_codegen_llvm/src/type_.rs b/compiler/rustc_codegen_llvm/src/type_.rs index 81bb70c958790..59caf1a507f81 100644 --- a/compiler/rustc_codegen_llvm/src/type_.rs +++ b/compiler/rustc_codegen_llvm/src/type_.rs @@ -15,7 +15,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, Metadata, TRUE, ToLlvmBool, Type, Value}; +use crate::llvm::{self, FALSE, Metadata, TRUE, ToGeneric, ToLlvmBool, Type, Value}; use crate::type_of::LayoutLlvmExt; impl PartialEq for Type { From f577d00431fb919a7dbd00f7af0e7827519be22a Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 6 Oct 2025 13:42:39 +1100 Subject: [PATCH 2/3] Move `DIBuilderBox` out of `ffi.rs` --- .../src/debuginfo/di_builder.rs | 31 +++++++++++++++++++ .../rustc_codegen_llvm/src/debuginfo/mod.rs | 4 ++- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 30 ------------------ 3 files changed, 34 insertions(+), 31 deletions(-) create mode 100644 compiler/rustc_codegen_llvm/src/debuginfo/di_builder.rs diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/di_builder.rs b/compiler/rustc_codegen_llvm/src/debuginfo/di_builder.rs new file mode 100644 index 0000000000000..0a1194a33eef3 --- /dev/null +++ b/compiler/rustc_codegen_llvm/src/debuginfo/di_builder.rs @@ -0,0 +1,31 @@ +use std::ptr; + +use crate::llvm::debuginfo::DIBuilder; +use crate::llvm::{self, Module}; + +/// Owning pointer to a `DIBuilder<'ll>` that will dispose of the builder +/// when dropped. Use `.as_ref()` to get the underlying `&DIBuilder` +/// needed for debuginfo FFI calls. +pub(crate) struct DIBuilderBox<'ll> { + raw: ptr::NonNull>, +} + +impl<'ll> DIBuilderBox<'ll> { + pub(crate) fn new(llmod: &'ll Module) -> Self { + let raw = unsafe { llvm::LLVMCreateDIBuilder(llmod) }; + let raw = ptr::NonNull::new(raw).unwrap(); + Self { raw } + } + + pub(crate) fn as_ref(&self) -> &DIBuilder<'ll> { + // SAFETY: This is an owning pointer, so `&DIBuilder` is valid + // for as long as `&self` is. + unsafe { self.raw.as_ref() } + } +} + +impl<'ll> Drop for DIBuilderBox<'ll> { + fn drop(&mut self) { + unsafe { llvm::LLVMDisposeDIBuilder(self.raw) }; + } +} diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs index f61c25bccfad3..efc2c99cd0a5c 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs @@ -28,6 +28,7 @@ use rustc_target::spec::DebuginfoKind; use smallvec::SmallVec; use tracing::debug; +use self::di_builder::DIBuilderBox; use self::metadata::{ UNKNOWN_COLUMN_NUMBER, UNKNOWN_LINE_NUMBER, file_metadata, spanned_type_di_node, type_di_node, }; @@ -36,12 +37,13 @@ use self::utils::{DIB, create_DIArray, is_node_local_to_unit}; use crate::builder::Builder; use crate::common::{AsCCharPtr, CodegenCx}; use crate::llvm::debuginfo::{ - DIArray, DIBuilderBox, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope, + DIArray, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope, DITemplateTypeParameter, DIType, DIVariable, }; use crate::llvm::{self, Value}; mod create_scope_map; +mod di_builder; mod dwarf_const; mod gdb; pub(crate) mod metadata; diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index d3665b229b083..43faa05943d82 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -698,12 +698,9 @@ unsafe extern "C" { pub(crate) type DiagnosticHandlerTy = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void); pub(crate) mod debuginfo { - use std::ptr; - use bitflags::bitflags; use super::{InvariantOpaque, Metadata}; - use crate::llvm::{self, Module}; /// Opaque target type for references to an LLVM debuginfo builder. /// @@ -716,33 +713,6 @@ pub(crate) mod debuginfo { #[repr(C)] pub(crate) struct DIBuilder<'ll>(InvariantOpaque<'ll>); - /// Owning pointer to a `DIBuilder<'ll>` that will dispose of the builder - /// when dropped. Use `.as_ref()` to get the underlying `&DIBuilder` - /// needed for debuginfo FFI calls. - pub(crate) struct DIBuilderBox<'ll> { - raw: ptr::NonNull>, - } - - impl<'ll> DIBuilderBox<'ll> { - pub(crate) fn new(llmod: &'ll Module) -> Self { - let raw = unsafe { llvm::LLVMCreateDIBuilder(llmod) }; - let raw = ptr::NonNull::new(raw).unwrap(); - Self { raw } - } - - pub(crate) fn as_ref(&self) -> &DIBuilder<'ll> { - // SAFETY: This is an owning pointer, so `&DIBuilder` is valid - // for as long as `&self` is. - unsafe { self.raw.as_ref() } - } - } - - impl<'ll> Drop for DIBuilderBox<'ll> { - fn drop(&mut self) { - unsafe { llvm::LLVMDisposeDIBuilder(self.raw) }; - } - } - pub(crate) type DIDescriptor = Metadata; pub(crate) type DILocation = Metadata; pub(crate) type DIScope = DIDescriptor; From e738c5b9ef7fa92503cf3e8b774815cf8f298a46 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 6 Oct 2025 14:04:33 +1100 Subject: [PATCH 3/3] Tweak imports to make the LLVM FFI bindings easier to move --- .../coverageinfo/{ffi.rs => coverage_ffi.rs} | 0 .../src/coverageinfo/llvm_cov.rs | 2 +- .../src/coverageinfo/mapgen/covfun.rs | 2 +- .../src/coverageinfo/mapgen/spans.rs | 2 +- .../src/coverageinfo/mod.rs | 2 +- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 33 ++++++++++--------- 6 files changed, 21 insertions(+), 20 deletions(-) rename compiler/rustc_codegen_llvm/src/coverageinfo/{ffi.rs => coverage_ffi.rs} (100%) diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/coverage_ffi.rs similarity index 100% rename from compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs rename to compiler/rustc_codegen_llvm/src/coverageinfo/coverage_ffi.rs diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs index a58202834cfa5..8eac9bab8363d 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs @@ -2,7 +2,7 @@ use std::ffi::CString; -use crate::coverageinfo::ffi; +use crate::coverageinfo::coverage_ffi as ffi; use crate::llvm; pub(crate) fn covmap_var_name() -> CString { diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs index 7835d18046860..0f54830ae6ed7 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs @@ -20,7 +20,7 @@ use tracing::debug; use crate::common::CodegenCx; use crate::coverageinfo::mapgen::{GlobalFileTable, VirtualFileMapping, spans}; -use crate::coverageinfo::{ffi, llvm_cov}; +use crate::coverageinfo::{coverage_ffi as ffi, llvm_cov}; use crate::llvm; /// Intermediate coverage metadata for a single function, used to help build diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/spans.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/spans.rs index 574463be7ffe0..baec5c71fdb7b 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/spans.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/spans.rs @@ -2,7 +2,7 @@ use rustc_span::source_map::SourceMap; use rustc_span::{BytePos, Pos, SourceFile, Span}; use tracing::debug; -use crate::coverageinfo::ffi; +use crate::coverageinfo::coverage_ffi as ffi; use crate::coverageinfo::mapgen::LocalFileId; /// Line and byte-column coordinates of a source code span within some file. diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs index 6a58f495c9d8f..aa5675c4620d3 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs @@ -13,7 +13,7 @@ use crate::builder::Builder; use crate::common::CodegenCx; use crate::llvm; -pub(crate) mod ffi; +pub(crate) mod coverage_ffi; mod llvm_cov; mod mapgen; diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 43faa05943d82..b3731689f47b5 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -19,15 +19,16 @@ use std::ptr; use bitflags::bitflags; use libc::{c_char, c_int, c_uchar, c_uint, c_ulonglong, c_void, size_t}; +use rustc_llvm::RustString; -use super::RustString; -use super::debuginfo::{ +use self::debuginfo::{ DIArray, DIBuilder, DIDerivedType, DIDescriptor, DIEnumerator, DIFile, DIFlags, DIGlobalVariableExpression, DILocation, DISPFlags, DIScope, DISubprogram, DITemplateTypeParameter, DIType, DebugEmissionKind, DebugNameTableKind, }; +use crate::TryFromU32; +use crate::coverageinfo::coverage_ffi; use crate::llvm::MetadataKindId; -use crate::{TryFromU32, llvm}; /// In the LLVM-C API, boolean values are passed as `typedef int LLVMBool`, /// which has a different ABI from Rust or C++ `bool`. @@ -75,13 +76,13 @@ impl Debug for Bool { /// Being able to write `b.to_llvm_bool()` is less noisy than `llvm::Bool::from(b)`, /// while being more explicit and less mistake-prone than something like `b.into()`. pub(crate) trait ToLlvmBool: Copy { - fn to_llvm_bool(self) -> llvm::Bool; + fn to_llvm_bool(self) -> Bool; } impl ToLlvmBool for bool { #[inline(always)] - fn to_llvm_bool(self) -> llvm::Bool { - llvm::Bool::from_bool(self) + fn to_llvm_bool(self) -> Bool { + Bool::from_bool(self) } } @@ -882,10 +883,10 @@ unsafe extern "C" { AsmStringSize: size_t, Constraints: *const c_uchar, // See "PTR_LEN_STR". ConstraintsSize: size_t, - HasSideEffects: llvm::Bool, - IsAlignStack: llvm::Bool, + HasSideEffects: Bool, + IsAlignStack: Bool, Dialect: AsmDialect, - CanThrow: llvm::Bool, + CanThrow: Bool, ) -> &'ll Value; pub(crate) safe fn LLVMGetTypeKind(Ty: &Type) -> RawEnum; @@ -1655,7 +1656,7 @@ unsafe extern "C" { ParentScope: Option<&'ll Metadata>, Name: *const c_uchar, // See "PTR_LEN_STR". NameLen: size_t, - ExportSymbols: llvm::Bool, + ExportSymbols: Bool, ) -> &'ll Metadata; pub(crate) fn LLVMDIBuilderCreateLexicalBlock<'ll>( @@ -1843,7 +1844,7 @@ unsafe extern "C" { File: &'ll Metadata, LineNo: c_uint, Ty: &'ll Metadata, - AlwaysPreserve: llvm::Bool, // "If true, this descriptor will survive optimizations." + AlwaysPreserve: Bool, // "If true, this descriptor will survive optimizations." Flags: DIFlags, AlignInBits: u32, ) -> &'ll Metadata; @@ -1857,7 +1858,7 @@ unsafe extern "C" { File: &'ll Metadata, LineNo: c_uint, Ty: &'ll Metadata, - AlwaysPreserve: llvm::Bool, // "If true, this descriptor will survive optimizations." + AlwaysPreserve: Bool, // "If true, this descriptor will survive optimizations." Flags: DIFlags, ) -> &'ll Metadata; } @@ -2084,13 +2085,13 @@ unsafe extern "C" { pub(crate) fn LLVMRustCoverageWriteFunctionMappingsToBuffer( VirtualFileMappingIDs: *const c_uint, NumVirtualFileMappingIDs: size_t, - Expressions: *const crate::coverageinfo::ffi::CounterExpression, + Expressions: *const coverage_ffi::CounterExpression, NumExpressions: size_t, - CodeRegions: *const crate::coverageinfo::ffi::CodeRegion, + CodeRegions: *const coverage_ffi::CodeRegion, NumCodeRegions: size_t, - ExpansionRegions: *const crate::coverageinfo::ffi::ExpansionRegion, + ExpansionRegions: *const coverage_ffi::ExpansionRegion, NumExpansionRegions: size_t, - BranchRegions: *const crate::coverageinfo::ffi::BranchRegion, + BranchRegions: *const coverage_ffi::BranchRegion, NumBranchRegions: size_t, BufferOut: &RustString, );