Skip to content

Commit b0cf4c2

Browse files
committed
turns out Layout has some more things to worry about -- move ABI comparison into helper function
like is_bool, and some special magic extra fields
1 parent 28d1529 commit b0cf4c2

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

compiler/rustc_abi/src/lib.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1300,12 +1300,18 @@ impl Abi {
13001300
matches!(*self, Abi::Uninhabited)
13011301
}
13021302

1303-
/// Returns `true` is this is a scalar type
1303+
/// Returns `true` if this is a scalar type
13041304
#[inline]
13051305
pub fn is_scalar(&self) -> bool {
13061306
matches!(*self, Abi::Scalar(_))
13071307
}
13081308

1309+
/// Returns `true` if this is a bool
1310+
#[inline]
1311+
pub fn is_bool(&self) -> bool {
1312+
matches!(*self, Abi::Scalar(s) if s.is_bool())
1313+
}
1314+
13091315
/// Returns the fixed alignment of this ABI, if any is mandated.
13101316
pub fn inherent_align<C: HasDataLayout>(&self, cx: &C) -> Option<AbiAndPrefAlign> {
13111317
Some(match *self {
@@ -1703,6 +1709,22 @@ impl LayoutS {
17031709
Abi::Aggregate { sized } => sized && self.size.bytes() == 0,
17041710
}
17051711
}
1712+
1713+
/// Checks if these two `Layout` are equal enough to be considered "the same for all function
1714+
/// call ABIs". Note however that real ABIs depend on more details that are not reflected in the
1715+
/// `Layout`; the `PassMode` need to be compared as well.
1716+
pub fn eq_abi(&self, other: &Self) -> bool {
1717+
// The one thing that we are not capturing here is that for unsized types, the metadata must
1718+
// also have the same ABI, and moreover that the same metadata leads to the same size. The
1719+
// 2nd point is quite hard to check though.
1720+
self.size == other.size
1721+
&& self.is_sized() == other.is_sized()
1722+
&& self.abi.eq_up_to_validity(&other.abi)
1723+
&& self.abi.is_bool() == other.abi.is_bool()
1724+
&& self.align.abi == other.align.abi
1725+
&& self.max_repr_align == other.max_repr_align
1726+
&& self.unadjusted_abi_align == other.unadjusted_abi_align
1727+
}
17061728
}
17071729

17081730
#[derive(Copy, Clone, Debug)]

compiler/rustc_const_eval/src/interpret/terminator.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
332332
if self.layout_compat(caller_abi.layout, callee_abi.layout)
333333
&& caller_abi.mode.eq_abi(&callee_abi.mode)
334334
{
335-
// Something went very wrong if our checks don't even imply that the layout is the same.
336-
assert!(
337-
caller_abi.layout.size == callee_abi.layout.size
338-
&& caller_abi.layout.align.abi == callee_abi.layout.align.abi
339-
&& caller_abi.layout.is_sized() == callee_abi.layout.is_sized()
340-
);
335+
// Something went very wrong if our checks don't imply layout ABI compatibility.
336+
assert!(caller_abi.layout.eq_abi(&callee_abi.layout));
341337
return true;
342338
} else {
343339
trace!(

compiler/rustc_passes/src/abi_test.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_middle::ty::layout::{FnAbiError, LayoutError};
55
use rustc_middle::ty::{self, GenericArgs, Instance, Ty, TyCtxt};
66
use rustc_span::source_map::Spanned;
77
use rustc_span::symbol::sym;
8-
use rustc_target::abi::call::{ArgAbi, FnAbi};
8+
use rustc_target::abi::call::FnAbi;
99

1010
use crate::errors::{AbiInvalidAttribute, AbiNe, AbiOf, UnrecognizedField};
1111

@@ -114,20 +114,6 @@ fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) {
114114
}
115115
}
116116

117-
fn test_arg_abi_eq<'tcx>(
118-
abi1: &'tcx ArgAbi<'tcx, Ty<'tcx>>,
119-
abi2: &'tcx ArgAbi<'tcx, Ty<'tcx>>,
120-
) -> bool {
121-
// Ideally we'd just compare the `mode`, but that is not enough -- for some modes LLVM will look
122-
// at the type. Comparing the `mode` and `layout.abi` as well as size and alignment should catch
123-
// basically everything though (except for tricky cases around unized types).
124-
abi1.mode.eq_abi(&abi2.mode)
125-
&& abi1.layout.abi.eq_up_to_validity(&abi2.layout.abi)
126-
&& abi1.layout.size == abi2.layout.size
127-
&& abi1.layout.align.abi == abi2.layout.align.abi
128-
&& abi1.layout.is_sized() == abi2.layout.is_sized()
129-
}
130-
131117
fn test_abi_eq<'tcx>(abi1: &'tcx FnAbi<'tcx, Ty<'tcx>>, abi2: &'tcx FnAbi<'tcx, Ty<'tcx>>) -> bool {
132118
if abi1.conv != abi2.conv
133119
|| abi1.args.len() != abi2.args.len()
@@ -138,8 +124,8 @@ fn test_abi_eq<'tcx>(abi1: &'tcx FnAbi<'tcx, Ty<'tcx>>, abi2: &'tcx FnAbi<'tcx,
138124
return false;
139125
}
140126

141-
test_arg_abi_eq(&abi1.ret, &abi2.ret)
142-
&& abi1.args.iter().zip(abi2.args.iter()).all(|(arg1, arg2)| test_arg_abi_eq(arg1, arg2))
127+
abi1.ret.eq_abi(&abi2.ret)
128+
&& abi1.args.iter().zip(abi2.args.iter()).all(|(arg1, arg2)| arg1.eq_abi(arg2))
143129
}
144130

145131
fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) {

compiler/rustc_target/src/abi/call/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ pub enum PassMode {
6060

6161
impl PassMode {
6262
/// Checks if these two `PassMode` are equal enough to be considered "the same for all
63-
/// function call ABIs".
63+
/// function call ABIs". However, the `Layout` can also impact ABI decisions,
64+
/// so that needs to be compared as well!
6465
pub fn eq_abi(&self, other: &Self) -> bool {
6566
match (self, other) {
6667
(PassMode::Ignore, PassMode::Ignore) => true, // can still be reached for the return type
@@ -623,6 +624,14 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
623624
pub fn is_ignore(&self) -> bool {
624625
matches!(self.mode, PassMode::Ignore)
625626
}
627+
628+
/// Checks if these two `ArgAbi` are equal enough to be considered "the same for all
629+
/// function call ABIs".
630+
pub fn eq_abi(&self, other: &Self) -> bool {
631+
// Ideally we'd just compare the `mode`, but that is not enough -- for some modes LLVM will look
632+
// at the type.
633+
self.layout.eq_abi(&other.layout) && self.mode.eq_abi(&other.mode)
634+
}
626635
}
627636

628637
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]

0 commit comments

Comments
 (0)