Skip to main content

rustc_abi/
callconv.rs

1#[cfg(feature = "nightly")]
2use crate::{BackendRepr, FieldsShape, Primitive, Size, TyAbiInterface, TyAndLayout, Variants};
3
4mod reg;
5
6pub use reg::{Reg, RegKind};
7
8/// Return value from the `homogeneous_aggregate` test function.
9#[derive(#[automatically_derived]
impl ::core::marker::Copy for HomogeneousAggregate { }Copy, #[automatically_derived]
impl ::core::clone::Clone for HomogeneousAggregate {
    #[inline]
    fn clone(&self) -> HomogeneousAggregate {
        let _: ::core::clone::AssertParamIsClone<Reg>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for HomogeneousAggregate {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            HomogeneousAggregate::Homogeneous(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "Homogeneous", &__self_0),
            HomogeneousAggregate::NoData =>
                ::core::fmt::Formatter::write_str(f, "NoData"),
        }
    }
}Debug)]
10pub enum HomogeneousAggregate {
11    /// Yes, all the "leaf fields" of this struct are passed in the
12    /// same way (specified in the `Reg` value).
13    Homogeneous(Reg),
14
15    /// There are no leaf fields at all.
16    NoData,
17}
18
19/// Error from the `homogeneous_aggregate` test function, indicating
20/// there are distinct leaf fields passed in different ways,
21/// or this is uninhabited.
22#[derive(#[automatically_derived]
impl ::core::marker::Copy for Heterogeneous { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Heterogeneous {
    #[inline]
    fn clone(&self) -> Heterogeneous { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Heterogeneous {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "Heterogeneous")
    }
}Debug)]
23pub struct Heterogeneous;
24
25impl HomogeneousAggregate {
26    /// If this is a homogeneous aggregate, returns the homogeneous
27    /// unit, else `None`.
28    pub fn unit(self) -> Option<Reg> {
29        match self {
30            HomogeneousAggregate::Homogeneous(reg) => Some(reg),
31            HomogeneousAggregate::NoData => None,
32        }
33    }
34
35    /// Try to combine two `HomogeneousAggregate`s, e.g. from two fields in
36    /// the same `struct`. Only succeeds if only one of them has any data,
37    /// or both units are identical.
38    fn merge(self, other: HomogeneousAggregate) -> Result<HomogeneousAggregate, Heterogeneous> {
39        match (self, other) {
40            (x, HomogeneousAggregate::NoData) | (HomogeneousAggregate::NoData, x) => Ok(x),
41
42            (HomogeneousAggregate::Homogeneous(a), HomogeneousAggregate::Homogeneous(b)) => {
43                if a != b {
44                    return Err(Heterogeneous);
45                }
46                Ok(self)
47            }
48        }
49    }
50}
51
52#[cfg(feature = "nightly")]
53impl<'a, Ty> TyAndLayout<'a, Ty> {
54    /// Returns `Homogeneous` if this layout is an aggregate containing fields of
55    /// only a single type (e.g., `(u32, u32)`). Such aggregates are often
56    /// special-cased in ABIs.
57    ///
58    /// Note: We generally ignore 1-ZST fields when computing this value (see #56877).
59    ///
60    /// This is public so that it can be used in unit tests, but
61    /// should generally only be relevant to the ABI details of
62    /// specific targets.
63    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("homogeneous_aggregate",
                                    "rustc_abi::callconv", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_abi/src/callconv.rs"),
                                    ::tracing_core::__macro_support::Option::Some(63u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_abi::callconv"),
                                    ::tracing_core::field::FieldSet::new(&["self"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Result<HomogeneousAggregate, Heterogeneous> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            match self.backend_repr {
                BackendRepr::Scalar(scalar) => {
                    let kind =
                        match scalar.primitive() {
                            Primitive::Int(..) | Primitive::Pointer(_) =>
                                RegKind::Integer,
                            Primitive::Float(_) => RegKind::Float,
                        };
                    Ok(HomogeneousAggregate::Homogeneous(Reg {
                                kind,
                                size: self.size,
                            }))
                }
                BackendRepr::SimdVector { element, count: _ } => {
                    if !!self.is_zst() {
                        ::core::panicking::panic("assertion failed: !self.is_zst()")
                    };
                    Ok(HomogeneousAggregate::Homogeneous(Reg {
                                kind: RegKind::Vector {
                                    hint_vector_elem: element.primitive(),
                                },
                                size: self.size,
                            }))
                }
                BackendRepr::SimdScalableVector { .. } => {
                    {
                        ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
                                format_args!("`homogeneous_aggregate` should not be called for scalable vectors")));
                    }
                }
                BackendRepr::ScalarPair(..) | BackendRepr::Memory {
                    sized: true } => {
                    let from_fields_at =
                        |layout: Self, start: Size|
                            -> Result<(HomogeneousAggregate, Size), Heterogeneous>
                            {
                                let is_union =
                                    match layout.fields {
                                        FieldsShape::Primitive => {
                                            {
                                                ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
                                                        format_args!("aggregates can\'t have `FieldsShape::Primitive`")));
                                            }
                                        }
                                        FieldsShape::Array { count, .. } => {
                                            match (&start, &Size::ZERO) {
                                                (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);
                                                    }
                                                }
                                            };
                                            let result =
                                                if count > 0 {
                                                    layout.field(cx, 0).homogeneous_aggregate(cx)?
                                                } else { HomogeneousAggregate::NoData };
                                            return Ok((result, layout.size));
                                        }
                                        FieldsShape::Union(_) => true,
                                        FieldsShape::Arbitrary { .. } => false,
                                    };
                                let mut result = HomogeneousAggregate::NoData;
                                let mut total = start;
                                for i in 0..layout.fields.count() {
                                    let field = layout.field(cx, i);
                                    if field.is_1zst() { continue; }
                                    if !is_union && total != layout.fields.offset(i) {
                                        return Err(Heterogeneous);
                                    }
                                    result = result.merge(field.homogeneous_aggregate(cx)?)?;
                                    let size = field.size;
                                    if is_union {
                                        total = total.max(size);
                                    } else { total += size; }
                                }
                                Ok((result, total))
                            };
                    let (mut result, mut total) =
                        from_fields_at(*self, Size::ZERO)?;
                    match &self.variants {
                        Variants::Single { .. } | Variants::Empty => {}
                        Variants::Multiple { variants, .. } => {
                            let variant_start = total;
                            for variant_idx in variants.indices() {
                                let (variant_result, variant_total) =
                                    from_fields_at(self.for_variant(cx, variant_idx),
                                            variant_start)?;
                                result = result.merge(variant_result)?;
                                total = total.max(variant_total);
                            }
                        }
                    }
                    if total != self.size {
                        Err(Heterogeneous)
                    } else {
                        match result {
                            HomogeneousAggregate::Homogeneous(_) => {
                                match (&total, &Size::ZERO) {
                                    (left_val, right_val) => {
                                        if *left_val == *right_val {
                                            let kind = ::core::panicking::AssertKind::Ne;
                                            ::core::panicking::assert_failed(kind, &*left_val,
                                                &*right_val, ::core::option::Option::None);
                                        }
                                    }
                                };
                            }
                            HomogeneousAggregate::NoData => {
                                match (&total, &Size::ZERO) {
                                    (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);
                                        }
                                    }
                                };
                            }
                        }
                        Ok(result)
                    }
                }
                BackendRepr::Memory { sized: false } => Err(Heterogeneous),
            }
        }
    }
}#[tracing::instrument(skip(cx), level = "debug")]
64    pub fn homogeneous_aggregate<C>(&self, cx: &C) -> Result<HomogeneousAggregate, Heterogeneous>
65    where
66        Ty: TyAbiInterface<'a, C> + Copy,
67    {
68        match self.backend_repr {
69            // The primitive for this algorithm.
70            BackendRepr::Scalar(scalar) => {
71                let kind = match scalar.primitive() {
72                    Primitive::Int(..) | Primitive::Pointer(_) => RegKind::Integer,
73                    Primitive::Float(_) => RegKind::Float,
74                };
75                Ok(HomogeneousAggregate::Homogeneous(Reg { kind, size: self.size }))
76            }
77
78            BackendRepr::SimdVector { element, count: _ } => {
79                assert!(!self.is_zst());
80
81                Ok(HomogeneousAggregate::Homogeneous(Reg {
82                    kind: RegKind::Vector { hint_vector_elem: element.primitive() },
83                    size: self.size,
84                }))
85            }
86
87            BackendRepr::SimdScalableVector { .. } => {
88                unreachable!("`homogeneous_aggregate` should not be called for scalable vectors")
89            }
90
91            BackendRepr::ScalarPair(..) | BackendRepr::Memory { sized: true } => {
92                // Helper for computing `homogeneous_aggregate`, allowing a custom
93                // starting offset (used below for handling variants).
94                let from_fields_at =
95                    |layout: Self,
96                     start: Size|
97                     -> Result<(HomogeneousAggregate, Size), Heterogeneous> {
98                        let is_union = match layout.fields {
99                            FieldsShape::Primitive => {
100                                unreachable!("aggregates can't have `FieldsShape::Primitive`")
101                            }
102                            FieldsShape::Array { count, .. } => {
103                                assert_eq!(start, Size::ZERO);
104
105                                let result = if count > 0 {
106                                    layout.field(cx, 0).homogeneous_aggregate(cx)?
107                                } else {
108                                    HomogeneousAggregate::NoData
109                                };
110                                return Ok((result, layout.size));
111                            }
112                            FieldsShape::Union(_) => true,
113                            FieldsShape::Arbitrary { .. } => false,
114                        };
115
116                        let mut result = HomogeneousAggregate::NoData;
117                        let mut total = start;
118
119                        for i in 0..layout.fields.count() {
120                            let field = layout.field(cx, i);
121                            if field.is_1zst() {
122                                // No data here and no impact on layout, can be ignored.
123                                // (We might be able to also ignore all aligned ZST but that's less clear.)
124                                continue;
125                            }
126
127                            if !is_union && total != layout.fields.offset(i) {
128                                // This field isn't just after the previous one we considered, abort.
129                                return Err(Heterogeneous);
130                            }
131
132                            result = result.merge(field.homogeneous_aggregate(cx)?)?;
133
134                            // Keep track of the offset (without padding).
135                            let size = field.size;
136                            if is_union {
137                                total = total.max(size);
138                            } else {
139                                total += size;
140                            }
141                        }
142
143                        Ok((result, total))
144                    };
145
146                let (mut result, mut total) = from_fields_at(*self, Size::ZERO)?;
147
148                match &self.variants {
149                    Variants::Single { .. } | Variants::Empty => {}
150                    Variants::Multiple { variants, .. } => {
151                        // Treat enum variants like union members.
152                        // HACK(eddyb) pretend the `enum` field (discriminant)
153                        // is at the start of every variant (otherwise the gap
154                        // at the start of all variants would disqualify them).
155                        //
156                        // NB: for all tagged `enum`s (which include all non-C-like
157                        // `enum`s with defined FFI representation), this will
158                        // match the homogeneous computation on the equivalent
159                        // `struct { tag; union { variant1; ... } }` and/or
160                        // `union { struct { tag; variant1; } ... }`
161                        // (the offsets of variant fields should be identical
162                        // between the two for either to be a homogeneous aggregate).
163                        let variant_start = total;
164                        for variant_idx in variants.indices() {
165                            let (variant_result, variant_total) =
166                                from_fields_at(self.for_variant(cx, variant_idx), variant_start)?;
167
168                            result = result.merge(variant_result)?;
169                            total = total.max(variant_total);
170                        }
171                    }
172                }
173
174                // There needs to be no padding.
175                if total != self.size {
176                    Err(Heterogeneous)
177                } else {
178                    match result {
179                        HomogeneousAggregate::Homogeneous(_) => {
180                            assert_ne!(total, Size::ZERO);
181                        }
182                        HomogeneousAggregate::NoData => {
183                            assert_eq!(total, Size::ZERO);
184                        }
185                    }
186                    Ok(result)
187                }
188            }
189            BackendRepr::Memory { sized: false } => Err(Heterogeneous),
190        }
191    }
192}