1
Fork 0

Auto merge of #120080 - cuviper:128-align-packed, r=nikic

Pack u128 in the compiler to mitigate new alignment

This is based on #116672, adding a new `#[repr(packed(8))]` wrapper on `u128` to avoid changing any of the compiler's size assertions. This is needed in two places:

* `SwitchTargets`, otherwise its `SmallVec<[u128; 1]>` gets padded up to 32 bytes.
* `LitKind::Int`, so that entire `enum` can stay 24 bytes.
  * This change definitely has far-reaching effects though, since it's public.
This commit is contained in:
bors 2024-01-22 13:08:19 +00:00
commit 3066253050
43 changed files with 173 additions and 86 deletions

View file

@ -1672,19 +1672,13 @@ mod size_asserts {
use super::*;
use rustc_data_structures::static_assert_size;
// tidy-alphabetical-start
// This can be removed after i128:128 is in the bootstrap compiler's target.
#[cfg(not(bootstrap))]
static_assert_size!(BasicBlockData<'_>, 144);
static_assert_size!(BasicBlockData<'_>, 136);
static_assert_size!(LocalDecl<'_>, 40);
static_assert_size!(SourceScopeData<'_>, 72);
static_assert_size!(Statement<'_>, 32);
static_assert_size!(StatementKind<'_>, 16);
// This can be removed after i128:128 is in the bootstrap compiler's target.
#[cfg(not(bootstrap))]
static_assert_size!(Terminator<'_>, 112);
// This can be removed after i128:128 is in the bootstrap compiler's target.
#[cfg(not(bootstrap))]
static_assert_size!(TerminatorKind<'_>, 96);
static_assert_size!(Terminator<'_>, 104);
static_assert_size!(TerminatorKind<'_>, 88);
static_assert_size!(VarDebugInfo<'_>, 88);
// tidy-alphabetical-end
}

View file

@ -13,6 +13,7 @@ use crate::ty::{self, List, Ty};
use crate::ty::{Region, UserTypeAnnotationIndex};
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
use rustc_data_structures::packed::Pu128;
use rustc_hir::def_id::DefId;
use rustc_hir::{self, CoroutineKind};
use rustc_index::IndexVec;
@ -829,7 +830,7 @@ impl TerminatorKind<'_> {
pub struct SwitchTargets {
/// Possible values. The locations to branch to in each case
/// are found in the corresponding indices from the `targets` vector.
pub(super) values: SmallVec<[u128; 1]>,
pub(super) values: SmallVec<[Pu128; 1]>,
/// Possible branch sites. The last element of this vector is used
/// for the otherwise branch, so targets.len() == values.len() + 1

View file

@ -3,6 +3,7 @@ use rustc_hir::LangItem;
use smallvec::SmallVec;
use super::TerminatorKind;
use rustc_data_structures::packed::Pu128;
use rustc_macros::HashStable;
use std::slice;
@ -14,7 +15,8 @@ impl SwitchTargets {
/// The iterator may be empty, in which case the `SwitchInt` instruction is equivalent to
/// `goto otherwise;`.
pub fn new(targets: impl Iterator<Item = (u128, BasicBlock)>, otherwise: BasicBlock) -> Self {
let (values, mut targets): (SmallVec<_>, SmallVec<_>) = targets.unzip();
let (values, mut targets): (SmallVec<_>, SmallVec<_>) =
targets.map(|(v, t)| (Pu128(v), t)).unzip();
targets.push(otherwise);
Self { values, targets }
}
@ -22,7 +24,7 @@ impl SwitchTargets {
/// Builds a switch targets definition that jumps to `then` if the tested value equals `value`,
/// and to `else_` if not.
pub fn static_if(value: u128, then: BasicBlock, else_: BasicBlock) -> Self {
Self { values: smallvec![value], targets: smallvec![then, else_] }
Self { values: smallvec![Pu128(value)], targets: smallvec![then, else_] }
}
/// Inverse of `SwitchTargets::static_if`.
@ -31,7 +33,7 @@ impl SwitchTargets {
if let &[value] = &self.values[..]
&& let &[then, else_] = &self.targets[..]
{
Some((value, then, else_))
Some((value.get(), then, else_))
} else {
None
}
@ -75,7 +77,7 @@ impl SwitchTargets {
}
pub struct SwitchTargetsIter<'a> {
inner: iter::Zip<slice::Iter<'a, u128>, slice::Iter<'a, BasicBlock>>,
inner: iter::Zip<slice::Iter<'a, Pu128>, slice::Iter<'a, BasicBlock>>,
}
impl<'a> Iterator for SwitchTargetsIter<'a> {
@ -83,7 +85,7 @@ impl<'a> Iterator for SwitchTargetsIter<'a> {
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(val, bb)| (*val, *bb))
self.inner.next().map(|(val, bb)| (val.get(), *bb))
}
#[inline]

View file

@ -745,7 +745,7 @@ impl<'tcx> TyCtxt<'tcx> {
],
) = attr.meta_item_list().as_deref()
{
Bound::Included(a)
Bound::Included(a.get())
} else {
self.dcx().span_delayed_bug(
attr.span,