Skip to main content

rustc_codegen_llvm/
type_of.rs

1use std::fmt::Write;
2
3use rustc_abi::Primitive::{Float, Int, Pointer};
4use rustc_abi::{Align, BackendRepr, FieldsShape, Scalar, Size, Variants};
5use rustc_codegen_ssa::traits::*;
6use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
7use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
8use rustc_middle::ty::{self, CoroutineArgsExt, Ty, TypeVisitableExt};
9use rustc_span::{DUMMY_SP, Span, bug};
10use tracing::debug;
11
12use crate::common::*;
13use crate::llvm::Type;
14
15fn uncached_llvm_type<'a, 'tcx>(
16    cx: &CodegenCx<'a, 'tcx>,
17    layout: TyAndLayout<'tcx>,
18    defer: &mut Option<(&'a Type, TyAndLayout<'tcx>)>,
19) -> &'a Type {
20    match layout.backend_repr {
21        BackendRepr::Scalar(_) => ::rustc_span::macros::bug_impl(None, format_args!("handled elsewhere"),
    Location::caller())bug!("handled elsewhere"),
22        BackendRepr::SimdVector { element, count } => {
23            let element = layout.scalar_llvm_type_at(cx, element);
24            return cx.type_vector(element, count.as_u64());
25        }
26        BackendRepr::SimdScalableVector { ref element, count, number_of_vectors } => {
27            let element = if element.is_bool() {
28                cx.type_i1()
29            } else {
30                layout.scalar_llvm_type_at(cx, *element)
31            };
32
33            let vector_type = cx.type_scalable_vector(element, count.as_u64());
34            return match number_of_vectors.0 {
35                1 => vector_type,
36                2 => cx.type_struct(&[vector_type; 2]),
37                3 => cx.type_struct(&[vector_type; 3]),
38                4 => cx.type_struct(&[vector_type; 4]),
39                5 => cx.type_struct(&[vector_type; 5]),
40                6 => cx.type_struct(&[vector_type; 6]),
41                7 => cx.type_struct(&[vector_type; 7]),
42                8 => cx.type_struct(&[vector_type; 8]),
43                _ => ::rustc_span::macros::bug_impl(None,
    format_args!("`#[rustc_scalable_vector]` tuple struct with too many fields"),
    Location::caller())bug!("`#[rustc_scalable_vector]` tuple struct with too many fields"),
44            };
45        }
46        BackendRepr::Memory { .. } | BackendRepr::ScalarPair { .. } => {}
47    }
48
49    let name = match layout.ty.kind() {
50        // FIXME(eddyb) producing readable type names for trait objects can result
51        // in problematically distinct types due to HRTB and subtyping (see #47638).
52        // ty::Dynamic(..) |
53        ty::Adt(..) | ty::Closure(..) | ty::CoroutineClosure(..) | ty::Foreign(..) | ty::Coroutine(..) | ty::Str
54            // For performance reasons we use names only when emitting LLVM IR.
55            if !cx.sess().fewer_names() =>
56        {
57            let mut name = {
    let _guard = NoVisibleGuard::new();
    { let _guard = NoTrimmedGuard::new(); layout.ty.to_string() }
}with_no_visible_paths!(with_no_trimmed_paths!(layout.ty.to_string()));
58            if let (&ty::Adt(def, _), &Variants::Single { index }) =
59                (layout.ty.kind(), &layout.variants)
60            {
61                if def.is_enum() {
62                    (&mut name).write_fmt(format_args!("::{0}", def.variant(index).name))write!(&mut name, "::{}", def.variant(index).name).unwrap();
63                }
64            }
65            if let (&ty::Coroutine(_, _), &Variants::Single { index }) =
66                (layout.ty.kind(), &layout.variants)
67            {
68                (&mut name).write_fmt(format_args!("::{0}",
        ty::CoroutineArgs::variant_name(index)))write!(&mut name, "::{}", ty::CoroutineArgs::variant_name(index)).unwrap();
69            }
70            Some(name)
71        }
72        _ => None,
73    };
74
75    match layout.fields {
76        FieldsShape::Primitive | FieldsShape::Union(_) => {
77            let fill = cx.type_padding_filler(layout.size, layout.align);
78            match name {
79                None => cx.type_struct(&[fill]),
80                Some(ref name) => {
81                    let llty = cx.type_named_struct(name);
82                    let packed = false;
83                    cx.set_struct_body(llty, &[fill], packed);
84                    llty
85                }
86            }
87        }
88        FieldsShape::Array { count, .. } => cx.type_array(layout.field(cx, 0).llvm_type(cx), count),
89        FieldsShape::Arbitrary { .. } => match name {
90            None => {
91                let (llfields, packed) = struct_llfields(cx, layout);
92                if packed { cx.type_packed_struct(&llfields) } else { cx.type_struct(&llfields) }
93            }
94            Some(ref name) => {
95                let llty = cx.type_named_struct(name);
96                *defer = Some((llty, layout));
97                llty
98            }
99        },
100    }
101}
102
103fn struct_llfields<'a, 'tcx>(
104    cx: &CodegenCx<'a, 'tcx>,
105    layout: TyAndLayout<'tcx>,
106) -> (Vec<&'a Type>, bool) {
107    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs:107",
                        "rustc_codegen_llvm::type_of", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs"),
                        ::tracing_core::__macro_support::Option::Some(107u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::type_of"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("struct_llfields: {0:#?}",
                                                    layout) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("struct_llfields: {:#?}", layout);
108    let field_count = layout.fields.count();
109
110    let mut packed = false;
111    let mut offset = Size::ZERO;
112    let mut prev_effective_align = layout.align;
113    let mut result: Vec<_> = Vec::with_capacity(1 + field_count * 2);
114    for i in layout.fields.index_by_increasing_offset() {
115        let target_offset = layout.fields.offset(i as usize);
116        let field = layout.field(cx, i);
117        let effective_field_align =
118            layout.align.min(field.align).restrict_for_offset(target_offset);
119        packed |= effective_field_align < field.align;
120
121        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs:121",
                        "rustc_codegen_llvm::type_of", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs"),
                        ::tracing_core::__macro_support::Option::Some(121u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::type_of"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("struct_llfields: {0}: {1:?} offset: {2:?} target_offset: {3:?} effective_field_align: {4}",
                                                    i, field, offset, target_offset,
                                                    effective_field_align.bytes()) as
                                            &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
122            "struct_llfields: {}: {:?} offset: {:?} target_offset: {:?} \
123                effective_field_align: {}",
124            i,
125            field,
126            offset,
127            target_offset,
128            effective_field_align.bytes()
129        );
130        if !(target_offset >= offset) {
    ::core::panicking::panic("assertion failed: target_offset >= offset")
};assert!(target_offset >= offset);
131        let padding = target_offset - offset;
132        if padding != Size::ZERO {
133            let padding_align = prev_effective_align.min(effective_field_align);
134            {
    match (&(offset.align_to(padding_align) + padding), &target_offset) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    }
};assert_eq!(offset.align_to(padding_align) + padding, target_offset);
135            result.push(cx.type_padding_filler(padding, padding_align));
136            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs:136",
                        "rustc_codegen_llvm::type_of", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs"),
                        ::tracing_core::__macro_support::Option::Some(136u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::type_of"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("    padding before: {0:?}",
                                                    padding) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("    padding before: {:?}", padding);
137        }
138        result.push(field.llvm_type(cx));
139        offset = target_offset + field.size;
140        prev_effective_align = effective_field_align;
141    }
142    if layout.is_sized() && field_count > 0 {
143        if offset > layout.size {
144            ::rustc_span::macros::bug_impl(None,
    format_args!("layout: {0:#?} stride: {1:?} offset: {2:?}", layout,
        layout.size, offset), Location::caller());bug!("layout: {:#?} stride: {:?} offset: {:?}", layout, layout.size, offset);
145        }
146        let padding = layout.size - offset;
147        if padding != Size::ZERO {
148            let padding_align = prev_effective_align;
149            {
    match (&(offset.align_to(padding_align) + padding), &layout.size) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    }
};assert_eq!(offset.align_to(padding_align) + padding, layout.size);
150            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs:150",
                        "rustc_codegen_llvm::type_of", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs"),
                        ::tracing_core::__macro_support::Option::Some(150u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::type_of"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("struct_llfields: pad_bytes: {0:?} offset: {1:?} stride: {2:?}",
                                                    padding, offset, layout.size) as
                                            &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
151                "struct_llfields: pad_bytes: {:?} offset: {:?} stride: {:?}",
152                padding, offset, layout.size
153            );
154            result.push(cx.type_padding_filler(padding, padding_align));
155        }
156    } else {
157        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs:157",
                        "rustc_codegen_llvm::type_of", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs"),
                        ::tracing_core::__macro_support::Option::Some(157u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::type_of"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("struct_llfields: offset: {0:?} stride: {1:?}",
                                                    offset, layout.size) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("struct_llfields: offset: {:?} stride: {:?}", offset, layout.size);
158    }
159    (result, packed)
160}
161
162impl<'a, 'tcx> CodegenCx<'a, 'tcx> {
163    pub(crate) fn align_of(&self, ty: Ty<'tcx>) -> Align {
164        self.layout_of(ty).align
165    }
166
167    pub(crate) fn size_of(&self, ty: Ty<'tcx>) -> Size {
168        self.layout_of(ty).size
169    }
170
171    pub(crate) fn size_and_align_of(&self, ty: Ty<'tcx>) -> (Size, Align) {
172        self.spanned_size_and_align_of(ty, DUMMY_SP)
173    }
174
175    pub(crate) fn spanned_size_and_align_of(&self, ty: Ty<'tcx>, span: Span) -> (Size, Align) {
176        let layout = self.spanned_layout_of(ty, span);
177        (layout.size, layout.align)
178    }
179}
180
181pub(crate) trait LayoutLlvmExt<'tcx> {
182    fn llvm_type<'a>(&self, cx: &CodegenCx<'a, 'tcx>) -> &'a Type;
183    fn immediate_llvm_type<'a>(&self, cx: &CodegenCx<'a, 'tcx>) -> &'a Type;
184    fn scalar_llvm_type_at<'a>(&self, cx: &CodegenCx<'a, 'tcx>, scalar: Scalar) -> &'a Type;
185    fn scalar_pair_element_llvm_type<'a>(
186        &self,
187        cx: &CodegenCx<'a, 'tcx>,
188        index: usize,
189        immediate: bool,
190    ) -> &'a Type;
191}
192
193impl<'tcx> LayoutLlvmExt<'tcx> for TyAndLayout<'tcx> {
194    /// Gets the LLVM type corresponding to a Rust type, i.e., `rustc_middle::ty::Ty`.
195    /// The pointee type of the pointer in `PlaceRef` is always this type.
196    /// For sized types, it is also the right LLVM type for an `alloca`
197    /// containing a value of that type, and most immediates (except `bool`).
198    /// Unsized types, however, are represented by a "minimal unit", e.g.
199    /// `[T]` becomes `T`, while `str` and `Trait` turn into `i8` - this
200    /// is useful for indexing slices, as `&[T]`'s data pointer is `T*`.
201    /// If the type is an unsized struct, the regular layout is generated,
202    /// with the innermost trailing unsized field using the "minimal unit"
203    /// of that field's type - this is useful for taking the address of
204    /// that field and ensuring the struct has the right alignment.
205    fn llvm_type<'a>(&self, cx: &CodegenCx<'a, 'tcx>) -> &'a Type {
206        // This must produce the same result for `repr(transparent)` wrappers as for the inner type!
207        // In other words, this should generally not look at the type at all, but only at the
208        // layout.
209        if let BackendRepr::Scalar(scalar) = self.backend_repr {
210            // Use a different cache for scalars because pointers to DSTs
211            // can be either wide or thin (data pointers of wide pointers).
212            if let Some(&llty) = cx.scalar_lltypes.borrow().get(&self.ty) {
213                return llty;
214            }
215            let llty = self.scalar_llvm_type_at(cx, scalar);
216            cx.scalar_lltypes.borrow_mut().insert(self.ty, llty);
217            return llty;
218        }
219
220        // Check the cache.
221        let variant_index = match self.variants {
222            Variants::Single { index } => Some(index),
223            _ => None,
224        };
225        if let Some(llty) = cx.type_lowering.borrow().get(&(self.ty, variant_index)) {
226            return llty;
227        }
228
229        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs:229",
                        "rustc_codegen_llvm::type_of", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs"),
                        ::tracing_core::__macro_support::Option::Some(229u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::type_of"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("llvm_type({0:#?})",
                                                    self) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("llvm_type({:#?})", self);
230
231        if !!self.ty.has_escaping_bound_vars() {
    {
        ::core::panicking::panic_fmt(format_args!("{0:?} has escaping bound vars",
                self.ty));
    }
};assert!(!self.ty.has_escaping_bound_vars(), "{:?} has escaping bound vars", self.ty);
232
233        // Make sure lifetimes are erased, to avoid generating distinct LLVM
234        // types for Rust types that only differ in the choice of lifetimes.
235        let normal_ty = cx.tcx.erase_and_anonymize_regions(self.ty);
236
237        let mut defer = None;
238        let llty = if self.ty != normal_ty {
239            let mut layout = cx.layout_of(normal_ty);
240            if let Some(v) = variant_index {
241                layout = layout.for_variant(cx, v);
242            }
243            layout.llvm_type(cx)
244        } else {
245            uncached_llvm_type(cx, *self, &mut defer)
246        };
247        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs:247",
                        "rustc_codegen_llvm::type_of", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/63931fb6b6bc05c38a5111ed7b32fe7648d22b1c/compiler/rustc_codegen_llvm/src/type_of.rs"),
                        ::tracing_core::__macro_support::Option::Some(247u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::type_of"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("--> mapped {0:#?} to llty={1:?}",
                                                    self, llty) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("--> mapped {:#?} to llty={:?}", self, llty);
248
249        cx.type_lowering.borrow_mut().insert((self.ty, variant_index), llty);
250
251        if let Some((llty, layout)) = defer {
252            let (llfields, packed) = struct_llfields(cx, layout);
253            cx.set_struct_body(llty, &llfields, packed);
254        }
255        llty
256    }
257
258    fn immediate_llvm_type<'a>(&self, cx: &CodegenCx<'a, 'tcx>) -> &'a Type {
259        match self.backend_repr {
260            BackendRepr::Scalar(scalar) => {
261                if scalar.is_bool() {
262                    return cx.type_i1();
263                }
264            }
265            BackendRepr::ScalarPair { .. } => {
266                // An immediate pair always contains just the two elements, without any padding
267                // filler, as it should never be stored to memory.
268                return cx.type_struct(&[
269                    self.scalar_pair_element_llvm_type(cx, 0, true),
270                    self.scalar_pair_element_llvm_type(cx, 1, true),
271                ]);
272            }
273            _ => {}
274        };
275        self.llvm_type(cx)
276    }
277
278    fn scalar_llvm_type_at<'a>(&self, cx: &CodegenCx<'a, 'tcx>, scalar: Scalar) -> &'a Type {
279        match scalar.primitive() {
280            Int(i, _) => cx.type_from_integer(i),
281            Float(f) => cx.type_from_float(f),
282            Pointer(address_space) => cx.type_ptr_ext(address_space),
283        }
284    }
285
286    fn scalar_pair_element_llvm_type<'a>(
287        &self,
288        cx: &CodegenCx<'a, 'tcx>,
289        index: usize,
290        immediate: bool,
291    ) -> &'a Type {
292        // This must produce the same result for `repr(transparent)` wrappers as for the inner type!
293        // In other words, this should generally not look at the type at all, but only at the
294        // layout.
295        let BackendRepr::ScalarPair { a, b, b_offset: _ } = self.backend_repr else {
296            ::rustc_span::macros::bug_impl(None,
    format_args!("TyAndLayout::scalar_pair_element_llty({0:?}): not applicable",
        self), Location::caller());bug!("TyAndLayout::scalar_pair_element_llty({:?}): not applicable", self);
297        };
298        let scalar = [a, b][index];
299
300        // Make sure to return the same type `immediate_llvm_type` would when
301        // dealing with an immediate pair. This means that `(bool, bool)` is
302        // effectively represented as `{i8, i8}` in memory and two `i1`s as an
303        // immediate, just like `bool` is typically `i8` in memory and only `i1`
304        // when immediate. We need to load/store `bool` as `i8` to avoid
305        // crippling LLVM optimizations or triggering other LLVM bugs with `i1`.
306        if immediate && scalar.is_bool() {
307            return cx.type_i1();
308        }
309
310        self.scalar_llvm_type_at(cx, scalar)
311    }
312}