Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
31 changes: 31 additions & 0 deletions compiler/rustc_codegen_llvm/src/debuginfo/di_builder.rs
Original file line number Diff line number Diff line change
@@ -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<DIBuilder<'ll>>,
}

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) };
}
}
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_llvm/src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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;
Expand Down
40 changes: 39 additions & 1 deletion compiler/rustc_codegen_llvm/src/llvm/conversions.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<T> {
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<T> {
fn to_generic(&self) -> T;
}

impl FromGeneric<SymbolVisibility> for llvm::Visibility {
fn from_generic(visibility: SymbolVisibility) -> Self {
match visibility {
Expand Down Expand Up @@ -113,3 +125,29 @@ impl FromGeneric<DebugInfo> for llvm::debuginfo::DebugEmissionKind {
}
}
}

impl ToGeneric<TypeKind> 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,
}
}
}
90 changes: 17 additions & 73 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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.
///
Expand All @@ -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<DIBuilder<'ll>>,
}

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;
Expand Down Expand Up @@ -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<TypeKind>;
Expand Down Expand Up @@ -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>(
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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,
);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
Loading