Skip to main content

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.
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.
15pub(crate) trait FromGeneric<T> {
16    fn from_generic(other: T) -> Self;
17}
18
19impl FromGeneric<SymbolVisibility> for llvm::Visibility {
20    fn from_generic(visibility: SymbolVisibility) -> Self {
21        match visibility {
22            SymbolVisibility::Hidden => Self::Hidden,
23            SymbolVisibility::Protected => Self::Protected,
24            SymbolVisibility::Interposable => Self::Default,
25        }
26    }
27}
28
29impl FromGeneric<IntPredicate> for llvm::IntPredicate {
30    fn from_generic(int_pred: IntPredicate) -> Self {
31        match int_pred {
32            IntPredicate::IntEQ => Self::IntEQ,
33            IntPredicate::IntNE => Self::IntNE,
34            IntPredicate::IntUGT => Self::IntUGT,
35            IntPredicate::IntUGE => Self::IntUGE,
36            IntPredicate::IntULT => Self::IntULT,
37            IntPredicate::IntULE => Self::IntULE,
38            IntPredicate::IntSGT => Self::IntSGT,
39            IntPredicate::IntSGE => Self::IntSGE,
40            IntPredicate::IntSLT => Self::IntSLT,
41            IntPredicate::IntSLE => Self::IntSLE,
42        }
43    }
44}
45
46impl FromGeneric<RealPredicate> for llvm::RealPredicate {
47    fn from_generic(real_pred: RealPredicate) -> Self {
48        match real_pred {
49            RealPredicate::RealPredicateFalse => Self::RealPredicateFalse,
50            RealPredicate::RealOEQ => Self::RealOEQ,
51            RealPredicate::RealOGT => Self::RealOGT,
52            RealPredicate::RealOGE => Self::RealOGE,
53            RealPredicate::RealOLT => Self::RealOLT,
54            RealPredicate::RealOLE => Self::RealOLE,
55            RealPredicate::RealONE => Self::RealONE,
56            RealPredicate::RealORD => Self::RealORD,
57            RealPredicate::RealUNO => Self::RealUNO,
58            RealPredicate::RealUEQ => Self::RealUEQ,
59            RealPredicate::RealUGT => Self::RealUGT,
60            RealPredicate::RealUGE => Self::RealUGE,
61            RealPredicate::RealULT => Self::RealULT,
62            RealPredicate::RealULE => Self::RealULE,
63            RealPredicate::RealUNE => Self::RealUNE,
64            RealPredicate::RealPredicateTrue => Self::RealPredicateTrue,
65        }
66    }
67}
68
69impl FromGeneric<AtomicRmwBinOp> for llvm::AtomicRmwBinOp {
70    fn from_generic(op: AtomicRmwBinOp) -> Self {
71        match op {
72            AtomicRmwBinOp::AtomicXchg => Self::AtomicXchg,
73            AtomicRmwBinOp::AtomicAdd => Self::AtomicAdd,
74            AtomicRmwBinOp::AtomicSub => Self::AtomicSub,
75            AtomicRmwBinOp::AtomicAnd => Self::AtomicAnd,
76            AtomicRmwBinOp::AtomicNand => Self::AtomicNand,
77            AtomicRmwBinOp::AtomicOr => Self::AtomicOr,
78            AtomicRmwBinOp::AtomicXor => Self::AtomicXor,
79            AtomicRmwBinOp::AtomicMax => Self::AtomicMax,
80            AtomicRmwBinOp::AtomicMin => Self::AtomicMin,
81            AtomicRmwBinOp::AtomicUMax => Self::AtomicUMax,
82            AtomicRmwBinOp::AtomicUMin => Self::AtomicUMin,
83        }
84    }
85}
86
87impl FromGeneric<AtomicOrdering> for llvm::AtomicOrdering {
88    fn from_generic(ordering: AtomicOrdering) -> Self {
89        match ordering {
90            AtomicOrdering::Relaxed => Self::Monotonic,
91            AtomicOrdering::Acquire => Self::Acquire,
92            AtomicOrdering::Release => Self::Release,
93            AtomicOrdering::AcqRel => Self::AcquireRelease,
94            AtomicOrdering::SeqCst => Self::SequentiallyConsistent,
95        }
96    }
97}
98
99impl FromGeneric<DebugInfo> for llvm::debuginfo::DebugEmissionKind {
100    fn from_generic(kind: DebugInfo) -> Self {
101        // We should be setting LLVM's emission kind to `LineTablesOnly` if
102        // we are compiling with "limited" debuginfo. However, some of the
103        // existing tools relied on slightly more debuginfo being generated than
104        // would be the case with `LineTablesOnly`, and we did not want to break
105        // these tools in a "drive-by fix", without a good idea or plan about
106        // what limited debuginfo should exactly look like. So for now we are
107        // instead adding a new debuginfo option "line-tables-only" so as to
108        // not break anything and to allow users to have 'limited' debug info.
109        //
110        // See https://github.com/rust-lang/rust/issues/60020 for details.
111        match kind {
112            DebugInfo::None => Self::NoDebug,
113            DebugInfo::LineDirectivesOnly => Self::DebugDirectivesOnly,
114            DebugInfo::LineTablesOnly => Self::LineTablesOnly,
115            DebugInfo::Limited | DebugInfo::Full => Self::FullDebug,
116        }
117    }
118}