Skip to content

Commit 891a4da

Browse files
committed
stricter alignment enforcement for ScalarPair and Vector
1 parent 58dd62a commit 891a4da

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

compiler/rustc_ty_utils/src/layout_sanity_check.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,24 @@ pub(super) fn sanity_check_layout<'tcx>(
135135
}
136136
}
137137
Abi::ScalarPair(scalar1, scalar2) => {
138-
// Sanity-check scalar pairs. These are a bit more flexible and support
139-
// padding, but we can at least ensure both fields actually fit into the layout
140-
// and the alignment requirement has not been weakened.
138+
// Sanity-check scalar pairs. Computing the expected size and alignment is a bit of work.
141139
let size1 = scalar1.size(cx);
142140
let align1 = scalar1.align(cx).abi;
143141
let size2 = scalar2.size(cx);
144142
let align2 = scalar2.align(cx).abi;
145-
assert!(
146-
layout.layout.align().abi >= cmp::max(align1, align2),
147-
"alignment mismatch between ABI and layout in {layout:#?}",
148-
);
143+
let align = cmp::max(align1, align2);
149144
let field2_offset = size1.align_to(align2);
150-
assert!(
151-
layout.layout.size() >= field2_offset + size2,
145+
let size = (field2_offset + size2).align_to(align);
146+
assert_eq!(
147+
layout.layout.size(),
148+
size,
152149
"size mismatch between ABI and layout in {layout:#?}"
153150
);
151+
assert_eq!(
152+
layout.layout.align().abi,
153+
align,
154+
"alignment mismatch between ABI and layout in {layout:#?}",
155+
);
154156
// Check that the underlying pair of fields matches.
155157
let inner = skip_newtypes(cx, layout);
156158
assert!(
@@ -233,17 +235,22 @@ pub(super) fn sanity_check_layout<'tcx>(
233235
);
234236
}
235237
Abi::Vector { count, element } => {
236-
// No padding in vectors. Alignment can be strengthened, though.
237-
assert!(
238-
layout.layout.align().abi >= element.align(cx).abi,
239-
"alignment mismatch between ABI and layout in {layout:#?}"
240-
);
238+
// No padding in vectors, except possibly for trailing padding to make the size a multiple of align.
241239
let size = element.size(cx) * count;
240+
let align = cx.data_layout().vector_align(size).abi;
241+
let size = size.align_to(align); // needed e.g. for vectors of size 3
242+
assert!(align >= element.align(cx).abi); // just sanity-checking `vector_align`.
242243
assert_eq!(
243244
layout.layout.size(),
244-
size.align_to(cx.data_layout().vector_align(size).abi),
245+
size,
245246
"size mismatch between ABI and layout in {layout:#?}"
246247
);
248+
assert_eq!(
249+
layout.layout.align().abi,
250+
align,
251+
"alignment mismatch between ABI and layout in {layout:#?}"
252+
);
253+
// FIXME: Do some kind of check of the inner type, like for Scalar and ScalarPair.
247254
}
248255
Abi::Uninhabited | Abi::Aggregate { .. } => {} // Nothing to check.
249256
}

0 commit comments

Comments
 (0)