Skip to content

Commit f955c76

Browse files
committed
Remove inherent methods from several LLVM FFI types
Using a helper trait allows the conversions to remain in `rustc_codegen_llvm`, even if the FFI types are moved to a different crate.
1 parent 7950f24 commit f955c76

File tree

7 files changed

+122
-111
lines changed

7 files changed

+122
-111
lines changed

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_symbol_mangling::mangle_internal_symbol;
1414
use crate::attributes::llfn_attrs_from_instance;
1515
use crate::builder::SBuilder;
1616
use crate::declare::declare_simple_fn;
17-
use crate::llvm::{self, FALSE, TRUE, Type, Value};
17+
use crate::llvm::{self, FALSE, FromGeneric, TRUE, Type, Value};
1818
use crate::{SimpleCx, attributes, debuginfo};
1919

2020
pub(crate) unsafe fn codegen(

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ use crate::attributes;
3636
use crate::common::Funclet;
3737
use crate::context::{CodegenCx, FullCx, GenericCx, SCx};
3838
use crate::llvm::{
39-
self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, GEPNoWrapFlags, Metadata, TRUE, ToLlvmBool,
39+
self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, FromGeneric, GEPNoWrapFlags, Metadata, TRUE,
40+
ToLlvmBool,
4041
};
4142
use crate::type_::Type;
4243
use crate::type_of::LayoutLlvmExt;

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ use crate::common::{AsCCharPtr, CodegenCx};
3737
use crate::debuginfo::dwarf_const;
3838
use crate::debuginfo::metadata::type_map::build_type_with_children;
3939
use crate::debuginfo::utils::{WidePtrKind, wide_pointer_kind};
40-
use crate::llvm;
4140
use crate::llvm::debuginfo::{
4241
DIBasicType, DIBuilder, DICompositeType, DIDescriptor, DIFile, DIFlags, DILexicalBlock,
4342
DIScope, DIType, DebugEmissionKind, DebugNameTableKind,
4443
};
44+
use crate::llvm::{self, FromGeneric};
4545
use crate::value::Value;
4646

4747
impl PartialEq for llvm::Metadata {

compiler/rustc_codegen_llvm/src/declare.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::abi::FnAbiLlvmExt;
2626
use crate::common::AsCCharPtr;
2727
use crate::context::{CodegenCx, GenericCx, SCx, SimpleCx};
2828
use crate::llvm::AttributePlace::Function;
29-
use crate::llvm::Visibility;
29+
use crate::llvm::{FromGeneric, Visibility};
3030
use crate::type_::Type;
3131
use crate::value::Value;
3232
use crate::{attributes, llvm};
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//! Conversions from backend-independent data types to/from LLVM FFI types.
2+
3+
use rustc_codegen_ssa::common::{AtomicRmwBinOp, IntPredicate, RealPredicate};
4+
use rustc_middle::ty::AtomicOrdering;
5+
use rustc_session::config::DebugInfo;
6+
use rustc_target::spec::SymbolVisibility;
7+
8+
use crate::llvm;
9+
10+
/// Helper trait for converting backend-independent types to LLVM-specific
11+
/// types, for FFI purposes.
12+
pub(crate) trait FromGeneric<T> {
13+
fn from_generic(other: T) -> Self;
14+
}
15+
16+
impl FromGeneric<SymbolVisibility> for llvm::Visibility {
17+
fn from_generic(visibility: SymbolVisibility) -> Self {
18+
match visibility {
19+
SymbolVisibility::Hidden => Self::Hidden,
20+
SymbolVisibility::Protected => Self::Protected,
21+
SymbolVisibility::Interposable => Self::Default,
22+
}
23+
}
24+
}
25+
26+
impl FromGeneric<IntPredicate> for llvm::IntPredicate {
27+
fn from_generic(int_pred: IntPredicate) -> Self {
28+
match int_pred {
29+
IntPredicate::IntEQ => Self::IntEQ,
30+
IntPredicate::IntNE => Self::IntNE,
31+
IntPredicate::IntUGT => Self::IntUGT,
32+
IntPredicate::IntUGE => Self::IntUGE,
33+
IntPredicate::IntULT => Self::IntULT,
34+
IntPredicate::IntULE => Self::IntULE,
35+
IntPredicate::IntSGT => Self::IntSGT,
36+
IntPredicate::IntSGE => Self::IntSGE,
37+
IntPredicate::IntSLT => Self::IntSLT,
38+
IntPredicate::IntSLE => Self::IntSLE,
39+
}
40+
}
41+
}
42+
43+
impl FromGeneric<RealPredicate> for llvm::RealPredicate {
44+
fn from_generic(real_pred: RealPredicate) -> Self {
45+
match real_pred {
46+
RealPredicate::RealPredicateFalse => Self::RealPredicateFalse,
47+
RealPredicate::RealOEQ => Self::RealOEQ,
48+
RealPredicate::RealOGT => Self::RealOGT,
49+
RealPredicate::RealOGE => Self::RealOGE,
50+
RealPredicate::RealOLT => Self::RealOLT,
51+
RealPredicate::RealOLE => Self::RealOLE,
52+
RealPredicate::RealONE => Self::RealONE,
53+
RealPredicate::RealORD => Self::RealORD,
54+
RealPredicate::RealUNO => Self::RealUNO,
55+
RealPredicate::RealUEQ => Self::RealUEQ,
56+
RealPredicate::RealUGT => Self::RealUGT,
57+
RealPredicate::RealUGE => Self::RealUGE,
58+
RealPredicate::RealULT => Self::RealULT,
59+
RealPredicate::RealULE => Self::RealULE,
60+
RealPredicate::RealUNE => Self::RealUNE,
61+
RealPredicate::RealPredicateTrue => Self::RealPredicateTrue,
62+
}
63+
}
64+
}
65+
66+
impl FromGeneric<AtomicRmwBinOp> for llvm::AtomicRmwBinOp {
67+
fn from_generic(op: AtomicRmwBinOp) -> Self {
68+
match op {
69+
AtomicRmwBinOp::AtomicXchg => Self::AtomicXchg,
70+
AtomicRmwBinOp::AtomicAdd => Self::AtomicAdd,
71+
AtomicRmwBinOp::AtomicSub => Self::AtomicSub,
72+
AtomicRmwBinOp::AtomicAnd => Self::AtomicAnd,
73+
AtomicRmwBinOp::AtomicNand => Self::AtomicNand,
74+
AtomicRmwBinOp::AtomicOr => Self::AtomicOr,
75+
AtomicRmwBinOp::AtomicXor => Self::AtomicXor,
76+
AtomicRmwBinOp::AtomicMax => Self::AtomicMax,
77+
AtomicRmwBinOp::AtomicMin => Self::AtomicMin,
78+
AtomicRmwBinOp::AtomicUMax => Self::AtomicUMax,
79+
AtomicRmwBinOp::AtomicUMin => Self::AtomicUMin,
80+
}
81+
}
82+
}
83+
84+
impl FromGeneric<AtomicOrdering> for llvm::AtomicOrdering {
85+
fn from_generic(ordering: AtomicOrdering) -> Self {
86+
match ordering {
87+
AtomicOrdering::Relaxed => Self::Monotonic,
88+
AtomicOrdering::Acquire => Self::Acquire,
89+
AtomicOrdering::Release => Self::Release,
90+
AtomicOrdering::AcqRel => Self::AcquireRelease,
91+
AtomicOrdering::SeqCst => Self::SequentiallyConsistent,
92+
}
93+
}
94+
}
95+
96+
impl FromGeneric<DebugInfo> for llvm::debuginfo::DebugEmissionKind {
97+
fn from_generic(kind: DebugInfo) -> Self {
98+
// We should be setting LLVM's emission kind to `LineTablesOnly` if
99+
// we are compiling with "limited" debuginfo. However, some of the
100+
// existing tools relied on slightly more debuginfo being generated than
101+
// would be the case with `LineTablesOnly`, and we did not want to break
102+
// these tools in a "drive-by fix", without a good idea or plan about
103+
// what limited debuginfo should exactly look like. So for now we are
104+
// instead adding a new debuginfo option "line-tables-only" so as to
105+
// not break anything and to allow users to have 'limited' debug info.
106+
//
107+
// See https://github.com/rust-lang/rust/issues/60020 for details.
108+
match kind {
109+
DebugInfo::None => Self::NoDebug,
110+
DebugInfo::LineDirectivesOnly => Self::DebugDirectivesOnly,
111+
DebugInfo::LineTablesOnly => Self::LineTablesOnly,
112+
DebugInfo::Limited | DebugInfo::Full => Self::FullDebug,
113+
}
114+
}
115+
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use std::ptr;
2020
use bitflags::bitflags;
2121
use libc::{c_char, c_int, c_uchar, c_uint, c_ulonglong, c_void, size_t};
2222
use rustc_macros::TryFromU32;
23-
use rustc_target::spec::SymbolVisibility;
2423

2524
use super::RustString;
2625
use super::debuginfo::{
@@ -220,16 +219,6 @@ pub(crate) enum Visibility {
220219
Protected = 2,
221220
}
222221

223-
impl Visibility {
224-
pub(crate) fn from_generic(visibility: SymbolVisibility) -> Self {
225-
match visibility {
226-
SymbolVisibility::Hidden => Visibility::Hidden,
227-
SymbolVisibility::Protected => Visibility::Protected,
228-
SymbolVisibility::Interposable => Visibility::Default,
229-
}
230-
}
231-
}
232-
233222
/// LLVMUnnamedAddr
234223
#[repr(C)]
235224
pub(crate) enum UnnamedAddr {
@@ -319,24 +308,6 @@ pub(crate) enum IntPredicate {
319308
IntSLE = 41,
320309
}
321310

322-
impl IntPredicate {
323-
pub(crate) fn from_generic(intpre: rustc_codegen_ssa::common::IntPredicate) -> Self {
324-
use rustc_codegen_ssa::common::IntPredicate as Common;
325-
match intpre {
326-
Common::IntEQ => Self::IntEQ,
327-
Common::IntNE => Self::IntNE,
328-
Common::IntUGT => Self::IntUGT,
329-
Common::IntUGE => Self::IntUGE,
330-
Common::IntULT => Self::IntULT,
331-
Common::IntULE => Self::IntULE,
332-
Common::IntSGT => Self::IntSGT,
333-
Common::IntSGE => Self::IntSGE,
334-
Common::IntSLT => Self::IntSLT,
335-
Common::IntSLE => Self::IntSLE,
336-
}
337-
}
338-
}
339-
340311
/// LLVMRealPredicate
341312
#[derive(Copy, Clone)]
342313
#[repr(C)]
@@ -359,30 +330,6 @@ pub(crate) enum RealPredicate {
359330
RealPredicateTrue = 15,
360331
}
361332

362-
impl RealPredicate {
363-
pub(crate) fn from_generic(realp: rustc_codegen_ssa::common::RealPredicate) -> Self {
364-
use rustc_codegen_ssa::common::RealPredicate as Common;
365-
match realp {
366-
Common::RealPredicateFalse => Self::RealPredicateFalse,
367-
Common::RealOEQ => Self::RealOEQ,
368-
Common::RealOGT => Self::RealOGT,
369-
Common::RealOGE => Self::RealOGE,
370-
Common::RealOLT => Self::RealOLT,
371-
Common::RealOLE => Self::RealOLE,
372-
Common::RealONE => Self::RealONE,
373-
Common::RealORD => Self::RealORD,
374-
Common::RealUNO => Self::RealUNO,
375-
Common::RealUEQ => Self::RealUEQ,
376-
Common::RealUGT => Self::RealUGT,
377-
Common::RealUGE => Self::RealUGE,
378-
Common::RealULT => Self::RealULT,
379-
Common::RealULE => Self::RealULE,
380-
Common::RealUNE => Self::RealUNE,
381-
Common::RealPredicateTrue => Self::RealPredicateTrue,
382-
}
383-
}
384-
}
385-
386333
/// Must match the layout of `LLVMTypeKind`.
387334
///
388335
/// Use [`RawEnum<TypeKind>`] for values of `LLVMTypeKind` returned from LLVM,
@@ -458,25 +405,6 @@ pub(crate) enum AtomicRmwBinOp {
458405
AtomicUMin = 10,
459406
}
460407

461-
impl AtomicRmwBinOp {
462-
pub(crate) fn from_generic(op: rustc_codegen_ssa::common::AtomicRmwBinOp) -> Self {
463-
use rustc_codegen_ssa::common::AtomicRmwBinOp as Common;
464-
match op {
465-
Common::AtomicXchg => Self::AtomicXchg,
466-
Common::AtomicAdd => Self::AtomicAdd,
467-
Common::AtomicSub => Self::AtomicSub,
468-
Common::AtomicAnd => Self::AtomicAnd,
469-
Common::AtomicNand => Self::AtomicNand,
470-
Common::AtomicOr => Self::AtomicOr,
471-
Common::AtomicXor => Self::AtomicXor,
472-
Common::AtomicMax => Self::AtomicMax,
473-
Common::AtomicMin => Self::AtomicMin,
474-
Common::AtomicUMax => Self::AtomicUMax,
475-
Common::AtomicUMin => Self::AtomicUMin,
476-
}
477-
}
478-
}
479-
480408
/// LLVMAtomicOrdering
481409
#[derive(Copy, Clone)]
482410
#[repr(C)]
@@ -493,19 +421,6 @@ pub(crate) enum AtomicOrdering {
493421
SequentiallyConsistent = 7,
494422
}
495423

496-
impl AtomicOrdering {
497-
pub(crate) fn from_generic(ao: rustc_middle::ty::AtomicOrdering) -> Self {
498-
use rustc_middle::ty::AtomicOrdering as Common;
499-
match ao {
500-
Common::Relaxed => Self::Monotonic,
501-
Common::Acquire => Self::Acquire,
502-
Common::Release => Self::Release,
503-
Common::AcqRel => Self::AcquireRelease,
504-
Common::SeqCst => Self::SequentiallyConsistent,
505-
}
506-
}
507-
}
508-
509424
/// LLVMRustFileType
510425
#[derive(Copy, Clone)]
511426
#[repr(C)]
@@ -940,28 +855,6 @@ pub(crate) mod debuginfo {
940855
DebugDirectivesOnly,
941856
}
942857

943-
impl DebugEmissionKind {
944-
pub(crate) fn from_generic(kind: rustc_session::config::DebugInfo) -> Self {
945-
// We should be setting LLVM's emission kind to `LineTablesOnly` if
946-
// we are compiling with "limited" debuginfo. However, some of the
947-
// existing tools relied on slightly more debuginfo being generated than
948-
// would be the case with `LineTablesOnly`, and we did not want to break
949-
// these tools in a "drive-by fix", without a good idea or plan about
950-
// what limited debuginfo should exactly look like. So for now we are
951-
// instead adding a new debuginfo option "line-tables-only" so as to
952-
// not break anything and to allow users to have 'limited' debug info.
953-
//
954-
// See https://github.com/rust-lang/rust/issues/60020 for details.
955-
use rustc_session::config::DebugInfo;
956-
match kind {
957-
DebugInfo::None => DebugEmissionKind::NoDebug,
958-
DebugInfo::LineDirectivesOnly => DebugEmissionKind::DebugDirectivesOnly,
959-
DebugInfo::LineTablesOnly => DebugEmissionKind::LineTablesOnly,
960-
DebugInfo::Limited | DebugInfo::Full => DebugEmissionKind::FullDebug,
961-
}
962-
}
963-
}
964-
965858
/// LLVMRustDebugNameTableKind
966859
#[derive(Clone, Copy)]
967860
#[repr(C)]

compiler/rustc_codegen_llvm/src/llvm/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ use rustc_llvm::RustString;
1111

1212
pub(crate) use self::CallConv::*;
1313
pub(crate) use self::CodeGenOptSize::*;
14+
pub(crate) use self::conversions::*;
1415
pub(crate) use self::ffi::*;
1516
pub(crate) use self::metadata_kind::*;
1617
use crate::common::AsCCharPtr;
1718

19+
mod conversions;
1820
pub(crate) mod diagnostic;
1921
pub(crate) mod enzyme_ffi;
2022
mod ffi;

0 commit comments

Comments
 (0)