Skip to content

Commit f72022d

Browse files
committed
Auto merge of #120080 - cuviper:128-align-packed, r=<try>
[WIP] 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. r? ghost
2 parents 2457c02 + 477f5f5 commit f72022d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+175
-88
lines changed

compiler/rustc_ast/src/ast.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -3300,9 +3300,13 @@ mod size_asserts {
33003300
static_assert_size!(Impl, 136);
33013301
static_assert_size!(Item, 136);
33023302
static_assert_size!(ItemKind, 64);
3303-
static_assert_size!(LitKind, 24);
3303+
// This can be removed after i128:128 is in the bootstrap compiler's target.
3304+
#[cfg(not(bootstrap))]
3305+
static_assert_size!(LitKind, 32);
33043306
static_assert_size!(Local, 72);
3305-
static_assert_size!(MetaItemLit, 40);
3307+
// This can be removed after i128:128 is in the bootstrap compiler's target.
3308+
#[cfg(not(bootstrap))]
3309+
static_assert_size!(MetaItemLit, 48);
33063310
static_assert_size!(Param, 40);
33073311
static_assert_size!(Pat, 72);
33083312
static_assert_size!(Path, 24);

compiler/rustc_codegen_llvm/src/context.rs

+8
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ pub unsafe fn create_module<'ll>(
145145
.replace("-Fi64", "");
146146
}
147147
}
148+
if llvm_version < (18, 0, 0) {
149+
if sess.target.arch == "x86" || sess.target.arch == "x86_64" {
150+
// LLVM 18 adjusts i128 to be 128-bit aligned on x86 variants.
151+
// Earlier LLVMs leave this as default alignment, so remove it.
152+
// See https://reviews.llvm.org/D86310
153+
target_data_layout = target_data_layout.replace("-i128:128", "");
154+
}
155+
}
148156

149157
// Ensure the data-layout values hardcoded remain the defaults.
150158
if sess.target.is_builtin {

compiler/rustc_data_structures/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pub mod aligned;
9393
pub mod frozen;
9494
mod hashes;
9595
pub mod owned_slice;
96+
pub mod packed;
9697
pub mod sso;
9798
pub mod steal;
9899
pub mod tagged_ptr;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use crate::stable_hasher::{HashStable, StableHasher};
2+
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
3+
use std::cmp::Ordering;
4+
use std::fmt;
5+
6+
#[repr(packed(8))]
7+
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
8+
pub struct Pu128(pub u128);
9+
10+
impl Pu128 {
11+
#[inline]
12+
pub fn get(self) -> u128 {
13+
self.0
14+
}
15+
}
16+
17+
impl From<u128> for Pu128 {
18+
#[inline]
19+
fn from(value: u128) -> Self {
20+
Self(value)
21+
}
22+
}
23+
24+
impl PartialEq<u128> for Pu128 {
25+
#[inline]
26+
fn eq(&self, other: &u128) -> bool {
27+
({ self.0 }) == *other
28+
}
29+
}
30+
31+
impl PartialOrd<u128> for Pu128 {
32+
#[inline]
33+
fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
34+
{ self.0 }.partial_cmp(other)
35+
}
36+
}
37+
38+
impl fmt::Display for Pu128 {
39+
#[inline]
40+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41+
{ self.0 }.fmt(f)
42+
}
43+
}
44+
45+
impl fmt::UpperHex for Pu128 {
46+
#[inline]
47+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48+
{ self.0 }.fmt(f)
49+
}
50+
}
51+
52+
impl<CTX> HashStable<CTX> for Pu128 {
53+
#[inline]
54+
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
55+
{ self.0 }.hash_stable(ctx, hasher)
56+
}
57+
}
58+
59+
impl<S: Encoder> Encodable<S> for Pu128 {
60+
#[inline]
61+
fn encode(&self, s: &mut S) {
62+
{ self.0 }.encode(s);
63+
}
64+
}
65+
66+
impl<D: Decoder> Decodable<D> for Pu128 {
67+
#[inline]
68+
fn decode(d: &mut D) -> Self {
69+
Self(u128::decode(d))
70+
}
71+
}

compiler/rustc_middle/src/mir/syntax.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::ty::{self, List, Ty};
1313
use crate::ty::{Region, UserTypeAnnotationIndex};
1414

1515
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
16+
use rustc_data_structures::packed::Pu128;
1617
use rustc_hir::def_id::DefId;
1718
use rustc_hir::{self, CoroutineKind};
1819
use rustc_index::IndexVec;
@@ -829,7 +830,7 @@ impl TerminatorKind<'_> {
829830
pub struct SwitchTargets {
830831
/// Possible values. The locations to branch to in each case
831832
/// are found in the corresponding indices from the `targets` vector.
832-
pub(super) values: SmallVec<[u128; 1]>,
833+
pub(super) values: SmallVec<[Pu128; 1]>,
833834

834835
/// Possible branch sites. The last element of this vector is used
835836
/// for the otherwise branch, so targets.len() == values.len() + 1

compiler/rustc_middle/src/mir/terminator.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_hir::LangItem;
33
use smallvec::SmallVec;
44

55
use super::TerminatorKind;
6+
use rustc_data_structures::packed::Pu128;
67
use rustc_macros::HashStable;
78
use std::slice;
89

@@ -14,15 +15,16 @@ impl SwitchTargets {
1415
/// The iterator may be empty, in which case the `SwitchInt` instruction is equivalent to
1516
/// `goto otherwise;`.
1617
pub fn new(targets: impl Iterator<Item = (u128, BasicBlock)>, otherwise: BasicBlock) -> Self {
17-
let (values, mut targets): (SmallVec<_>, SmallVec<_>) = targets.unzip();
18+
let (values, mut targets): (SmallVec<_>, SmallVec<_>) =
19+
targets.map(|(v, t)| (Pu128(v), t)).unzip();
1820
targets.push(otherwise);
1921
Self { values, targets }
2022
}
2123

2224
/// Builds a switch targets definition that jumps to `then` if the tested value equals `value`,
2325
/// and to `else_` if not.
2426
pub fn static_if(value: u128, then: BasicBlock, else_: BasicBlock) -> Self {
25-
Self { values: smallvec![value], targets: smallvec![then, else_] }
27+
Self { values: smallvec![Pu128(value)], targets: smallvec![then, else_] }
2628
}
2729

2830
/// Inverse of `SwitchTargets::static_if`.
@@ -31,7 +33,7 @@ impl SwitchTargets {
3133
if let &[value] = &self.values[..]
3234
&& let &[then, else_] = &self.targets[..]
3335
{
34-
Some((value, then, else_))
36+
Some((value.get(), then, else_))
3537
} else {
3638
None
3739
}
@@ -75,15 +77,15 @@ impl SwitchTargets {
7577
}
7678

7779
pub struct SwitchTargetsIter<'a> {
78-
inner: iter::Zip<slice::Iter<'a, u128>, slice::Iter<'a, BasicBlock>>,
80+
inner: iter::Zip<slice::Iter<'a, Pu128>, slice::Iter<'a, BasicBlock>>,
7981
}
8082

8183
impl<'a> Iterator for SwitchTargetsIter<'a> {
8284
type Item = (u128, BasicBlock);
8385

8486
#[inline]
8587
fn next(&mut self) -> Option<Self::Item> {
86-
self.inner.next().map(|(val, bb)| (*val, *bb))
88+
self.inner.next().map(|(val, bb)| (val.get(), *bb))
8789
}
8890

8991
#[inline]

compiler/rustc_target/src/spec/targets/i386_apple_ios.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn target() -> Target {
1111
llvm_target: ios_sim_llvm_target(arch).into(),
1212
pointer_width: 32,
1313
data_layout: "e-m:o-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
14-
f64:32:64-f80:128-n8:16:32-S128"
14+
i128:128-f64:32:64-f80:128-n8:16:32-S128"
1515
.into(),
1616
arch: arch.target_arch(),
1717
options: TargetOptions { max_atomic_width: Some(64), ..opts("ios", arch) },

compiler/rustc_target/src/spec/targets/i686_apple_darwin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn target() -> Target {
1818
llvm_target: macos_llvm_target(Arch::I686).into(),
1919
pointer_width: 32,
2020
data_layout: "e-m:o-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
21-
f64:32:64-f80:128-n8:16:32-S128"
21+
i128:128-f64:32:64-f80:128-n8:16:32-S128"
2222
.into(),
2323
arch: arch.target_arch(),
2424
options: TargetOptions { mcount: "\u{1}mcount".into(), ..base },

compiler/rustc_target/src/spec/targets/i686_linux_android.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn target() -> Target {
1717
llvm_target: "i686-linux-android".into(),
1818
pointer_width: 32,
1919
data_layout: "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
20-
f64:32:64-f80:32-n8:16:32-S128"
20+
i128:128-f64:32:64-f80:32-n8:16:32-S128"
2121
.into(),
2222
arch: "x86".into(),
2323
options: TargetOptions { supported_sanitizers: SanitizerSet::ADDRESS, ..base },

compiler/rustc_target/src/spec/targets/i686_pc_windows_gnu.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn target() -> Target {
1919
llvm_target: "i686-pc-windows-gnu".into(),
2020
pointer_width: 32,
2121
data_layout: "e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
22-
i64:64-f80:32-n8:16:32-a:0:32-S32"
22+
i64:64-i128:128-f80:32-n8:16:32-a:0:32-S32"
2323
.into(),
2424
arch: "x86".into(),
2525
options: base,

compiler/rustc_target/src/spec/targets/i686_pc_windows_gnullvm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn target() -> Target {
1818
llvm_target: "i686-pc-windows-gnu".into(),
1919
pointer_width: 32,
2020
data_layout: "e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
21-
i64:64-f80:32-n8:16:32-a:0:32-S32"
21+
i64:64-i128:128-f80:32-n8:16:32-a:0:32-S32"
2222
.into(),
2323
arch: "x86".into(),
2424
options: base,

compiler/rustc_target/src/spec/targets/i686_pc_windows_msvc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn target() -> Target {
2525
llvm_target: "i686-pc-windows-msvc".into(),
2626
pointer_width: 32,
2727
data_layout: "e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
28-
i64:64-f80:128-n8:16:32-a:0:32-S32"
28+
i64:64-i128:128-f80:128-n8:16:32-a:0:32-S32"
2929
.into(),
3030
arch: "x86".into(),
3131
options: base,

compiler/rustc_target/src/spec/targets/i686_unknown_freebsd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn target() -> Target {
1111
llvm_target: "i686-unknown-freebsd".into(),
1212
pointer_width: 32,
1313
data_layout: "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
14-
f64:32:64-f80:32-n8:16:32-S128"
14+
i128:128-f64:32:64-f80:32-n8:16:32-S128"
1515
.into(),
1616
arch: "x86".into(),
1717
options: base,

compiler/rustc_target/src/spec/targets/i686_unknown_haiku.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn target() -> Target {
1111
llvm_target: "i686-unknown-haiku".into(),
1212
pointer_width: 32,
1313
data_layout: "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
14-
f64:32:64-f80:32-n8:16:32-S128"
14+
i128:128-f64:32:64-f80:32-n8:16:32-S128"
1515
.into(),
1616
arch: "x86".into(),
1717
options: base,

compiler/rustc_target/src/spec/targets/i686_unknown_hurd_gnu.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn target() -> Target {
1111
llvm_target: "i686-unknown-hurd-gnu".into(),
1212
pointer_width: 32,
1313
data_layout: "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
14-
f64:32:64-f80:32-n8:16:32-S128"
14+
i128:128-f64:32:64-f80:32-n8:16:32-S128"
1515
.into(),
1616
arch: "x86".into(),
1717
options: base,

compiler/rustc_target/src/spec/targets/i686_unknown_linux_gnu.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn target() -> Target {
1212
llvm_target: "i686-unknown-linux-gnu".into(),
1313
pointer_width: 32,
1414
data_layout: "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
15-
f64:32:64-f80:32-n8:16:32-S128"
15+
i128:128-f64:32:64-f80:32-n8:16:32-S128"
1616
.into(),
1717
arch: "x86".into(),
1818
options: base,

compiler/rustc_target/src/spec/targets/i686_unknown_linux_musl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn target() -> Target {
2525
llvm_target: "i686-unknown-linux-musl".into(),
2626
pointer_width: 32,
2727
data_layout: "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
28-
f64:32:64-f80:32-n8:16:32-S128"
28+
i128:128-f64:32:64-f80:32-n8:16:32-S128"
2929
.into(),
3030
arch: "x86".into(),
3131
options: base,

compiler/rustc_target/src/spec/targets/i686_unknown_netbsd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn target() -> Target {
1111
llvm_target: "i686-unknown-netbsdelf".into(),
1212
pointer_width: 32,
1313
data_layout: "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
14-
f64:32:64-f80:32-n8:16:32-S128"
14+
i128:128-f64:32:64-f80:32-n8:16:32-S128"
1515
.into(),
1616
arch: "x86".into(),
1717
options: TargetOptions { mcount: "__mcount".into(), ..base },

compiler/rustc_target/src/spec/targets/i686_unknown_openbsd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn target() -> Target {
1111
llvm_target: "i686-unknown-openbsd".into(),
1212
pointer_width: 32,
1313
data_layout: "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
14-
f64:32:64-f80:32-n8:16:32-S128"
14+
i128:128-f64:32:64-f80:32-n8:16:32-S128"
1515
.into(),
1616
arch: "x86".into(),
1717
options: base,

compiler/rustc_target/src/spec/targets/i686_uwp_windows_gnu.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn target() -> Target {
1818
llvm_target: "i686-pc-windows-gnu".into(),
1919
pointer_width: 32,
2020
data_layout: "e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
21-
i64:64-f80:32-n8:16:32-a:0:32-S32"
21+
i64:64-i128:128-f80:32-n8:16:32-a:0:32-S32"
2222
.into(),
2323
arch: "x86".into(),
2424
options: base,

compiler/rustc_target/src/spec/targets/i686_uwp_windows_msvc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn target() -> Target {
99
llvm_target: "i686-pc-windows-msvc".into(),
1010
pointer_width: 32,
1111
data_layout: "e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
12-
i64:64-f80:128-n8:16:32-a:0:32-S32"
12+
i64:64-i128:128-f80:128-n8:16:32-a:0:32-S32"
1313
.into(),
1414
arch: "x86".into(),
1515
options: base,

compiler/rustc_target/src/spec/targets/i686_wrs_vxworks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn target() -> Target {
1111
llvm_target: "i686-unknown-linux-gnu".into(),
1212
pointer_width: 32,
1313
data_layout: "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-\
14-
f64:32:64-f80:32-n8:16:32-S128"
14+
i128:128-f64:32:64-f80:32-n8:16:32-S128"
1515
.into(),
1616
arch: "x86".into(),
1717
options: base,

compiler/rustc_target/src/spec/targets/x86_64_apple_darwin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ pub fn target() -> Target {
1717
// correctly, we do too.
1818
llvm_target: macos_llvm_target(arch).into(),
1919
pointer_width: 64,
20-
data_layout: "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
21-
.into(),
20+
data_layout:
21+
"e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128".into(),
2222
arch: arch.target_arch(),
2323
options: TargetOptions { mcount: "\u{1}mcount".into(), ..base },
2424
}

compiler/rustc_target/src/spec/targets/x86_64_apple_ios.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ pub fn target() -> Target {
99
Target {
1010
llvm_target: ios_sim_llvm_target(arch).into(),
1111
pointer_width: 64,
12-
data_layout: "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
13-
.into(),
12+
data_layout:
13+
"e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128".into(),
1414
arch: arch.target_arch(),
1515
options: TargetOptions { max_atomic_width: Some(128), ..base },
1616
}

compiler/rustc_target/src/spec/targets/x86_64_apple_ios_macabi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub fn target() -> Target {
1212
Target {
1313
llvm_target: llvm_target.into(),
1414
pointer_width: 64,
15-
data_layout: "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
16-
.into(),
15+
data_layout:
16+
"e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128".into(),
1717
arch: arch.target_arch(),
1818
options: TargetOptions { max_atomic_width: Some(128), ..base },
1919
}

compiler/rustc_target/src/spec/targets/x86_64_apple_tvos.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ pub fn target() -> Target {
66
Target {
77
llvm_target: tvos_sim_llvm_target(arch).into(),
88
pointer_width: 64,
9-
data_layout: "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
10-
.into(),
9+
data_layout:
10+
"e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128".into(),
1111
arch: arch.target_arch(),
1212
options: TargetOptions { max_atomic_width: Some(128), ..opts("tvos", arch) },
1313
}

compiler/rustc_target/src/spec/targets/x86_64_apple_watchos_sim.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ pub fn target() -> Target {
66
Target {
77
llvm_target: watchos_sim_llvm_target(arch).into(),
88
pointer_width: 64,
9-
data_layout: "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
10-
.into(),
9+
data_layout:
10+
"e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128".into(),
1111
arch: arch.target_arch(),
1212
options: TargetOptions { max_atomic_width: Some(128), ..opts("watchos", arch) },
1313
}

compiler/rustc_target/src/spec/targets/x86_64_fortanix_unknown_sgx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ pub fn target() -> Target {
7575
Target {
7676
llvm_target: "x86_64-elf".into(),
7777
pointer_width: 64,
78-
data_layout: "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
79-
.into(),
78+
data_layout:
79+
"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128".into(),
8080
arch: "x86_64".into(),
8181
options: opts,
8282
}

0 commit comments

Comments
 (0)