Skip to content

Commit fe50688

Browse files
authored
Unrolled build for #147430
Rollup merge of #147430 - Zalathar:inherent, r=chenyukang cg_llvm: More preparation for moving FFI bindings to `rustc_llvm` - Sequel to #147327 - Motivated by #142897 --- This is another batch of adjustments to code that would otherwise interfere with moving FFI bindings out of `rustc_codegen_llvm` and into `rustc_llvm`.
2 parents 07a5b02 + 735a980 commit fe50688

File tree

5 files changed

+72
-61
lines changed

5 files changed

+72
-61
lines changed

‎compiler/rustc_codegen_llvm/src/debuginfo/di_builder.rs‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
1+
use std::ptr;
2+
13
use libc::c_uint;
24
use rustc_abi::Align;
35

46
use crate::llvm::debuginfo::DIBuilder;
5-
use crate::llvm::{self, ToLlvmBool};
7+
use crate::llvm::{self, Module, ToLlvmBool};
8+
9+
/// Owning pointer to a `DIBuilder<'ll>` that will dispose of the builder
10+
/// when dropped. Use `.as_ref()` to get the underlying `&DIBuilder`
11+
/// needed for debuginfo FFI calls.
12+
pub(crate) struct DIBuilderBox<'ll> {
13+
raw: ptr::NonNull<DIBuilder<'ll>>,
14+
}
15+
16+
impl<'ll> DIBuilderBox<'ll> {
17+
pub(crate) fn new(llmod: &'ll Module) -> Self {
18+
let raw = unsafe { llvm::LLVMCreateDIBuilder(llmod) };
19+
let raw = ptr::NonNull::new(raw).unwrap();
20+
Self { raw }
21+
}
22+
23+
pub(crate) fn as_ref(&self) -> &DIBuilder<'ll> {
24+
// SAFETY: This is an owning pointer, so `&DIBuilder` is valid
25+
// for as long as `&self` is.
26+
unsafe { self.raw.as_ref() }
27+
}
28+
}
29+
30+
impl<'ll> Drop for DIBuilderBox<'ll> {
31+
fn drop(&mut self) {
32+
unsafe { llvm::LLVMDisposeDIBuilder(self.raw) };
33+
}
34+
}
635

736
/// Extension trait for defining safe wrappers and helper methods on
837
/// `&DIBuilder<'ll>`, without requiring it to be defined in the same crate.

‎compiler/rustc_codegen_llvm/src/debuginfo/mod.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ use self::namespace::mangled_name_of_instance;
3838
use self::utils::{DIB, create_DIArray, is_node_local_to_unit};
3939
use crate::builder::Builder;
4040
use crate::common::{AsCCharPtr, CodegenCx};
41+
use crate::debuginfo::di_builder::DIBuilderBox;
4142
use crate::llvm::debuginfo::{
42-
DIArray, DIBuilderBox, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope,
43+
DIArray, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope,
4344
DITemplateTypeParameter, DIType, DIVariable,
4445
};
4546
use crate::llvm::{self, Value};

‎compiler/rustc_codegen_llvm/src/llvm/conversions.rs‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Conversions from backend-independent data types to/from LLVM FFI types.
22
3-
use rustc_codegen_ssa::common::{AtomicRmwBinOp, IntPredicate, RealPredicate};
3+
use rustc_codegen_ssa::common::{AtomicRmwBinOp, IntPredicate, RealPredicate, TypeKind};
44
use rustc_middle::ty::AtomicOrdering;
55
use rustc_session::config::DebugInfo;
66
use rustc_target::spec::SymbolVisibility;
@@ -9,10 +9,22 @@ use crate::llvm;
99

1010
/// Helper trait for converting backend-independent types to LLVM-specific
1111
/// types, for FFI purposes.
12+
///
13+
/// FIXME(#147327): These trait/method names were chosen to avoid churn in
14+
/// existing code, but are not great and could probably be made clearer.
1215
pub(crate) trait FromGeneric<T> {
1316
fn from_generic(other: T) -> Self;
1417
}
1518

19+
/// Helper trait for converting LLVM-specific types to backend-independent
20+
/// types, for FFI purposes.
21+
///
22+
/// FIXME(#147327): These trait/method names were chosen to avoid churn in
23+
/// existing code, but are not great and could probably be made clearer.
24+
pub(crate) trait ToGeneric<T> {
25+
fn to_generic(&self) -> T;
26+
}
27+
1628
impl FromGeneric<SymbolVisibility> for llvm::Visibility {
1729
fn from_generic(visibility: SymbolVisibility) -> Self {
1830
match visibility {
@@ -113,3 +125,29 @@ impl FromGeneric<DebugInfo> for llvm::debuginfo::DebugEmissionKind {
113125
}
114126
}
115127
}
128+
129+
impl ToGeneric<TypeKind> for llvm::TypeKind {
130+
fn to_generic(&self) -> TypeKind {
131+
match self {
132+
Self::Void => TypeKind::Void,
133+
Self::Half => TypeKind::Half,
134+
Self::Float => TypeKind::Float,
135+
Self::Double => TypeKind::Double,
136+
Self::X86_FP80 => TypeKind::X86_FP80,
137+
Self::FP128 => TypeKind::FP128,
138+
Self::PPC_FP128 => TypeKind::PPC_FP128,
139+
Self::Label => TypeKind::Label,
140+
Self::Integer => TypeKind::Integer,
141+
Self::Function => TypeKind::Function,
142+
Self::Struct => TypeKind::Struct,
143+
Self::Array => TypeKind::Array,
144+
Self::Pointer => TypeKind::Pointer,
145+
Self::Vector => TypeKind::Vector,
146+
Self::Metadata => TypeKind::Metadata,
147+
Self::Token => TypeKind::Token,
148+
Self::ScalableVector => TypeKind::ScalableVector,
149+
Self::BFloat => TypeKind::BFloat,
150+
Self::X86_AMX => TypeKind::X86_AMX,
151+
}
152+
}
153+
}

‎compiler/rustc_codegen_llvm/src/llvm/ffi.rs‎

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -363,33 +363,6 @@ pub(crate) enum TypeKind {
363363
X86_AMX = 19,
364364
}
365365

366-
impl TypeKind {
367-
pub(crate) fn to_generic(self) -> rustc_codegen_ssa::common::TypeKind {
368-
use rustc_codegen_ssa::common::TypeKind as Common;
369-
match self {
370-
Self::Void => Common::Void,
371-
Self::Half => Common::Half,
372-
Self::Float => Common::Float,
373-
Self::Double => Common::Double,
374-
Self::X86_FP80 => Common::X86_FP80,
375-
Self::FP128 => Common::FP128,
376-
Self::PPC_FP128 => Common::PPC_FP128,
377-
Self::Label => Common::Label,
378-
Self::Integer => Common::Integer,
379-
Self::Function => Common::Function,
380-
Self::Struct => Common::Struct,
381-
Self::Array => Common::Array,
382-
Self::Pointer => Common::Pointer,
383-
Self::Vector => Common::Vector,
384-
Self::Metadata => Common::Metadata,
385-
Self::Token => Common::Token,
386-
Self::ScalableVector => Common::ScalableVector,
387-
Self::BFloat => Common::BFloat,
388-
Self::X86_AMX => Common::X86_AMX,
389-
}
390-
}
391-
}
392-
393366
/// LLVMAtomicRmwBinOp
394367
#[derive(Copy, Clone)]
395368
#[repr(C)]
@@ -738,12 +711,9 @@ unsafe extern "C" {
738711
pub(crate) type DiagnosticHandlerTy = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void);
739712

740713
pub(crate) mod debuginfo {
741-
use std::ptr;
742-
743714
use bitflags::bitflags;
744715

745716
use super::{InvariantOpaque, Metadata};
746-
use crate::llvm::{self, Module};
747717

748718
/// Opaque target type for references to an LLVM debuginfo builder.
749719
///
@@ -756,33 +726,6 @@ pub(crate) mod debuginfo {
756726
#[repr(C)]
757727
pub(crate) struct DIBuilder<'ll>(InvariantOpaque<'ll>);
758728

759-
/// Owning pointer to a `DIBuilder<'ll>` that will dispose of the builder
760-
/// when dropped. Use `.as_ref()` to get the underlying `&DIBuilder`
761-
/// needed for debuginfo FFI calls.
762-
pub(crate) struct DIBuilderBox<'ll> {
763-
raw: ptr::NonNull<DIBuilder<'ll>>,
764-
}
765-
766-
impl<'ll> DIBuilderBox<'ll> {
767-
pub(crate) fn new(llmod: &'ll Module) -> Self {
768-
let raw = unsafe { llvm::LLVMCreateDIBuilder(llmod) };
769-
let raw = ptr::NonNull::new(raw).unwrap();
770-
Self { raw }
771-
}
772-
773-
pub(crate) fn as_ref(&self) -> &DIBuilder<'ll> {
774-
// SAFETY: This is an owning pointer, so `&DIBuilder` is valid
775-
// for as long as `&self` is.
776-
unsafe { self.raw.as_ref() }
777-
}
778-
}
779-
780-
impl<'ll> Drop for DIBuilderBox<'ll> {
781-
fn drop(&mut self) {
782-
unsafe { llvm::LLVMDisposeDIBuilder(self.raw) };
783-
}
784-
}
785-
786729
pub(crate) type DIDescriptor = Metadata;
787730
pub(crate) type DILocation = Metadata;
788731
pub(crate) type DIScope = DIDescriptor;

‎compiler/rustc_codegen_llvm/src/type_.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_target::callconv::{CastTarget, FnAbi};
1515
use crate::abi::{FnAbiLlvmExt, LlvmType};
1616
use crate::common;
1717
use crate::context::{CodegenCx, GenericCx, SCx};
18-
use crate::llvm::{self, FALSE, Metadata, TRUE, ToLlvmBool, Type, Value};
18+
use crate::llvm::{self, FALSE, Metadata, TRUE, ToGeneric, ToLlvmBool, Type, Value};
1919
use crate::type_of::LayoutLlvmExt;
2020

2121
impl PartialEq for Type {

0 commit comments

Comments
 (0)