rustc_codegen_llvm/llvm/
conversions.rs

1//! Conversions from backend-independent data types to/from LLVM FFI types.
2
3use rustc_codegen_ssa::common::{AtomicRmwBinOp, IntPredicate, RealPredicate};
4use rustc_middle::ty::AtomicOrdering;
5use rustc_session::config::DebugInfo;
6use rustc_target::spec::SymbolVisibility;
7
8use crate::llvm;
9
10/// Helper trait for converting backend-independent types to LLVM-specific
11/// types, for FFI purposes.
12pub(crate) trait FromGeneric<T> {
13    fn from_generic(other: T) -> Self;
14}
15
16impl 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
26impl 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
43impl 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
66impl 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
84impl 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
96impl 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}