Skip to main content

rustc_codegen_ssa/
size_of_val.rs

1//! Computing the size and alignment of a value.
2
3use rustc_abi::{Align, WrappingRange};
4use rustc_hir::attrs::lang_items::LangItem;
5use rustc_middle::bug;
6use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
7use rustc_middle::ty::{self, Ty};
8use rustc_span::{DUMMY_SP, Span};
9use tracing::{debug, trace};
10
11use crate::common::IntPredicate;
12use crate::traits::*;
13use crate::{common, meth};
14
15pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
16    bx: &mut Bx,
17    t: Ty<'tcx>,
18    info: Option<Bx::Value>,
19    span: Span,
20) -> (Bx::Value, Bx::Value) {
21    let layout = bx.spanned_layout_of(t, span);
22    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/size_of_val.rs:22",
                        "rustc_codegen_ssa::size_of_val", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/size_of_val.rs"),
                        ::tracing_core::__macro_support::Option::Some(22u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::size_of_val"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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!("size_and_align_of_dst(ty={0}, info={1:?}): layout: {2:?}",
                                                    t, info, layout) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};trace!("size_and_align_of_dst(ty={}, info={:?}): layout: {:?}", t, info, layout);
23    if layout.is_sized() {
24        let size = bx.const_usize(layout.size.bytes());
25        let align = bx.const_usize(layout.align.bytes());
26        return (size, align);
27    }
28    match t.kind() {
29        ty::Dynamic(..) => {
30            // Load size/align from vtable.
31            let vtable = info.unwrap();
32            let size = meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_SIZE)
33                .get_usize(bx, vtable, t);
34            let align = meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_ALIGN)
35                .get_usize(bx, vtable, t);
36
37            // Size is always <= isize::MAX.
38            let size_bound = bx.data_layout().ptr_sized_integer().signed_max() as u128;
39            bx.range_metadata(size, WrappingRange { start: 0, end: size_bound });
40            // Alignment is always a power of two, thus 1..=0x800…000,
41            // but also bounded by the maximum we support in type layout.
42            let align_bound = Align::max_for_target(bx.data_layout()).bytes().into();
43            bx.range_metadata(align, WrappingRange { start: 1, end: align_bound });
44
45            (size, align)
46        }
47        ty::Slice(_) | ty::Str => {
48            let unit = layout.field(bx, 0);
49            // The info in this case is the length of the str, so the size is that
50            // times the unit size.
51            (
52                // All slice sizes must fit into `isize`, so this multiplication cannot
53                // wrap -- neither signed nor unsigned.
54                bx.unchecked_sumul(info.unwrap(), bx.const_usize(unit.size.bytes())),
55                bx.const_usize(unit.align.bytes()),
56            )
57        }
58        ty::Foreign(_) => {
59            // `extern` type. We cannot compute the size, so panic.
60            let msg_str = {
    let _guard = NoVisibleGuard::new();
    {
        {
            let _guard = NoTrimmedGuard::new();
            {
                ::alloc::__export::must_use({
                        ::alloc::fmt::format(format_args!("attempted to compute the size or alignment of extern type `{0}`",
                                t))
                    })
            }
        }
    }
}with_no_visible_paths!({
61                with_no_trimmed_paths!({
62                    format!("attempted to compute the size or alignment of extern type `{t}`")
63                })
64            });
65            let msg = bx.const_str(&msg_str);
66
67            // Obtain the panic entry point.
68            let (fn_abi, llfn, _instance) =
69                common::build_langcall(bx, DUMMY_SP, LangItem::PanicNounwind);
70
71            // Generate the call. Cannot use `do_call` since we don't have a MIR terminator so we
72            // can't create a `TerminationCodegenHelper`. (But we are in good company, this code is
73            // duplicated plenty of times.)
74            let fn_ty = bx.fn_decl_backend_type(fn_abi);
75
76            bx.call(
77                fn_ty,
78                /* fn_attrs */ None,
79                Some(fn_abi),
80                llfn,
81                &[msg.0, msg.1],
82                None,
83                None,
84            );
85
86            // This function does not return so we can now return whatever we want.
87            let size = bx.const_usize(layout.size.bytes());
88            let align = bx.const_usize(layout.align.bytes());
89            (size, align)
90        }
91        ty::Adt(..) | ty::Tuple(..) => {
92            // First get the size of all statically known fields.
93            // Don't use size_of because it also rounds up to alignment, which we
94            // want to avoid, as the unsized field's alignment could be smaller.
95            if !!t.is_simd() {
    ::core::panicking::panic("assertion failed: !t.is_simd()")
};assert!(!t.is_simd());
96            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/size_of_val.rs:96",
                        "rustc_codegen_ssa::size_of_val", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/size_of_val.rs"),
                        ::tracing_core::__macro_support::Option::Some(96u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::size_of_val"),
                        ::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!("DST {0} layout: {1:?}",
                                                    t, layout) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("DST {} layout: {:?}", t, layout);
97
98            let i = layout.fields.count() - 1;
99            let unsized_offset_unadjusted = layout.fields.offset(i).bytes();
100            let sized_align = layout.align.bytes();
101            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/size_of_val.rs:101",
                        "rustc_codegen_ssa::size_of_val", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/size_of_val.rs"),
                        ::tracing_core::__macro_support::Option::Some(101u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::size_of_val"),
                        ::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!("DST {0} offset of dyn field: {1}, statically sized align: {2}",
                                                    t, unsized_offset_unadjusted, sized_align) as
                                            &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
102                "DST {} offset of dyn field: {}, statically sized align: {}",
103                t, unsized_offset_unadjusted, sized_align
104            );
105            let unsized_offset_unadjusted = bx.const_usize(unsized_offset_unadjusted);
106            let sized_align = bx.const_usize(sized_align);
107
108            // Recurse to get the size of the dynamically sized field (must be
109            // the last field).
110            let field_ty = layout.field(bx, i).ty;
111            let (unsized_size, mut unsized_align) = size_and_align_of_dst(bx, field_ty, info, span);
112
113            // # First compute the dynamic alignment
114
115            // For packed types, we need to cap the alignment.
116            if let ty::Adt(def, _) = t.kind()
117                && let Some(packed) = def.repr().pack
118            {
119                if packed.bytes() == 1 {
120                    // We know this will be capped to 1.
121                    unsized_align = bx.const_usize(1);
122                } else {
123                    // We have to dynamically compute `min(unsized_align, packed)`.
124                    let packed = bx.const_usize(packed.bytes());
125                    let cmp = bx.icmp(IntPredicate::IntULT, unsized_align, packed);
126                    unsized_align = bx.select(cmp, unsized_align, packed);
127                }
128            }
129
130            // Choose max of two known alignments (combined value must
131            // be aligned according to more restrictive of the two).
132            let full_align = match (
133                bx.const_to_opt_u128(sized_align, false),
134                bx.const_to_opt_u128(unsized_align, false),
135            ) {
136                (Some(sized_align), Some(unsized_align)) => {
137                    // If both alignments are constant, (the sized_align should always be), then
138                    // pick the correct alignment statically.
139                    bx.const_usize(std::cmp::max(sized_align, unsized_align) as u64)
140                }
141                _ => {
142                    let cmp = bx.icmp(IntPredicate::IntUGT, sized_align, unsized_align);
143                    bx.select(cmp, sized_align, unsized_align)
144                }
145            };
146
147            // # Then compute the dynamic size
148
149            // The full formula for the size would be:
150            // let unsized_offset_adjusted = unsized_offset_unadjusted.align_to(unsized_align);
151            // let full_size = (unsized_offset_adjusted + unsized_size).align_to(full_align);
152            // However, `unsized_size` is a multiple of `unsized_align`. Therefore, we can
153            // equivalently do the `align_to(unsized_align)` *after* adding `unsized_size`:
154            //
155            // let full_size =
156            //     (unsized_offset_unadjusted + unsized_size)
157            //     .align_to(unsized_align)
158            //     .align_to(full_align);
159            //
160            // Furthermore, `align >= unsized_align`, and therefore we only need to do:
161            // let full_size = (unsized_offset_unadjusted + unsized_size).align_to(full_align);
162
163            let full_size = bx.add(unsized_offset_unadjusted, unsized_size);
164
165            // Issue #27023: must add any necessary padding to `size`
166            // (to make it a multiple of `align`) before returning it.
167            //
168            // Namely, the returned size should be, in C notation:
169            //
170            //   `size + ((size & (align-1)) ? align : 0)`
171            //
172            // emulated via the semi-standard fast bit trick:
173            //
174            //   `(size + (align-1)) & -align`
175            let one = bx.const_usize(1);
176            let addend = bx.sub(full_align, one);
177            let add = bx.add(full_size, addend);
178            let neg = bx.neg(full_align);
179            let full_size = bx.and(add, neg);
180
181            (full_size, full_align)
182        }
183        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("size_and_align_of_dst: {0} not supported",
        t))bug!("size_and_align_of_dst: {t} not supported"),
184    }
185}