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/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 c6ad1c2e18ec7..6fd116d29f5a8 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, }; @@ -37,12 +38,13 @@ use crate::builder::Builder; use crate::common::{AsCCharPtr, CodegenCx}; use crate::llvm; use crate::llvm::debuginfo::{ - DIArray, DIBuilderBox, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope, + DIArray, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope, DITemplateTypeParameter, DIType, DIVariable, }; use crate::value::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/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..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) } } @@ -360,33 +361,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)] @@ -725,12 +699,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. /// @@ -743,33 +714,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; @@ -939,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; @@ -1712,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>( @@ -1900,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; @@ -1914,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; } @@ -2141,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, ); diff --git a/compiler/rustc_codegen_llvm/src/type_.rs b/compiler/rustc_codegen_llvm/src/type_.rs index 9de9e0ec44c67..1caf519771997 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::context::{CodegenCx, GenericCx, SCx}; pub(crate) use crate::llvm::Type; -use crate::llvm::{FALSE, Metadata, TRUE, ToLlvmBool}; +use crate::llvm::{FALSE, Metadata, TRUE, ToGeneric, ToLlvmBool}; use crate::type_of::LayoutLlvmExt; use crate::value::Value; use crate::{common, llvm};