Skip to content

Commit f50fd07

Browse files
committed
Auto merge of #45225 - eddyb:trans-abi, r=arielb1
Refactor type memory layouts and ABIs, to be more general and easier to optimize. To combat combinatorial explosion, type layouts are now described through 3 orthogonal properties: * `Variants` describes the plurality of sum types (where applicable) * `Single` is for one inhabited/active variant, including all C `struct`s and `union`s * `Tagged` has its variants discriminated by an integer tag, including C `enum`s * `NicheFilling` uses otherwise-invalid values ("niches") for all but one of its inhabited variants * `FieldPlacement` describes the number and memory offsets of fields (if any) * `Union` has all its fields at offset `0` * `Array` has offsets that are a multiple of its `stride`; guarantees all fields have one type * `Arbitrary` records all the field offsets, which can be out-of-order * `Abi` describes how values of the type should be passed around, including for FFI * `Uninhabited` corresponds to no values, associated with unreachable control-flow * `Scalar` is ABI-identical to its only integer/floating-point/pointer "scalar component" * `ScalarPair` has two "scalar components", but only applies to the Rust ABI * `Vector` is for SIMD vectors, typically `#[repr(simd)]` `struct`s in Rust * `Aggregate` has arbitrary contents, including all non-transparent C `struct`s and `union`s Size optimizations implemented so far: * ignoring uninhabited variants (i.e. containing uninhabited fields), e.g.: * `Option<!>` is 0 bytes * `Result<T, !>` has the same size as `T` * using arbitrary niches, not just `0`, to represent a data-less variant, e.g.: * `Option<bool>`, `Option<Option<bool>>`, `Option<Ordering>` are all 1 byte * `Option<char>` is 4 bytes * using a range of niches to represent *multiple* data-less variants, e.g.: * `enum E { A(bool), B, C, D }` is 1 byte Code generation now takes advantage of `Scalar` and `ScalarPair` to, in more cases, pass around scalar components as immediates instead of indirectly, through pointers into temporary memory, while avoiding LLVM's "first-class aggregates", and there's more untapped potential here. Closes #44426, fixes #5977, fixes #14540, fixes #43278.
2 parents 5041b3b + f9f5ab9 commit f50fd07

Some content is hidden

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

93 files changed

+4986
-5624
lines changed

src/liballoc/boxed.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<T> Place<T> for IntermediateBox<T> {
151151
unsafe fn finalize<T>(b: IntermediateBox<T>) -> Box<T> {
152152
let p = b.ptr as *mut T;
153153
mem::forget(b);
154-
mem::transmute(p)
154+
Box::from_raw(p)
155155
}
156156

157157
fn make_place<T>() -> IntermediateBox<T> {
@@ -300,7 +300,10 @@ impl<T: ?Sized> Box<T> {
300300
issue = "27730")]
301301
#[inline]
302302
pub unsafe fn from_unique(u: Unique<T>) -> Self {
303-
mem::transmute(u)
303+
#[cfg(stage0)]
304+
return mem::transmute(u);
305+
#[cfg(not(stage0))]
306+
return Box(u);
304307
}
305308

306309
/// Consumes the `Box`, returning the wrapped raw pointer.
@@ -362,7 +365,14 @@ impl<T: ?Sized> Box<T> {
362365
issue = "27730")]
363366
#[inline]
364367
pub fn into_unique(b: Box<T>) -> Unique<T> {
365-
unsafe { mem::transmute(b) }
368+
#[cfg(stage0)]
369+
return unsafe { mem::transmute(b) };
370+
#[cfg(not(stage0))]
371+
return {
372+
let unique = b.0;
373+
mem::forget(b);
374+
unique
375+
};
366376
}
367377
}
368378

@@ -627,7 +637,7 @@ impl Box<Any + Send> {
627637
pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any + Send>> {
628638
<Box<Any>>::downcast(self).map_err(|s| unsafe {
629639
// reapply the Send marker
630-
mem::transmute::<Box<Any>, Box<Any + Send>>(s)
640+
Box::from_raw(Box::into_raw(s) as *mut (Any + Send))
631641
})
632642
}
633643
}

src/librustc/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@
4646
#![feature(const_fn)]
4747
#![feature(core_intrinsics)]
4848
#![feature(drain_filter)]
49+
#![feature(i128)]
4950
#![feature(i128_type)]
50-
#![feature(match_default_bindings)]
51+
#![feature(inclusive_range)]
5152
#![feature(inclusive_range_syntax)]
5253
#![cfg_attr(windows, feature(libc))]
5354
#![feature(macro_vis_matcher)]
55+
#![feature(match_default_bindings)]
5456
#![feature(never_type)]
5557
#![feature(nonzero)]
5658
#![feature(quote)]

src/librustc/lint/context.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ use middle::privacy::AccessLevels;
3434
use rustc_serialize::{Decoder, Decodable, Encoder, Encodable};
3535
use session::{config, early_error, Session};
3636
use traits::Reveal;
37-
use ty::{self, TyCtxt};
37+
use ty::{self, TyCtxt, Ty};
38+
use ty::layout::{LayoutError, LayoutOf, TyLayout};
3839
use util::nodemap::FxHashMap;
3940

4041
use std::default::Default as StdDefault;
@@ -626,6 +627,14 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
626627
}
627628
}
628629

630+
impl<'a, 'tcx> LayoutOf<Ty<'tcx>> for &'a LateContext<'a, 'tcx> {
631+
type TyLayout = Result<TyLayout<'tcx>, LayoutError<'tcx>>;
632+
633+
fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
634+
(self.tcx, self.param_env.reveal_all()).layout_of(ty)
635+
}
636+
}
637+
629638
impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
630639
/// Because lints are scoped lexically, we want to walk nested
631640
/// items in the context of the outer item, so enable

src/librustc/middle/mem_categorization.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl<'tcx> cmt_<'tcx> {
210210
adt_def.variant_with_id(variant_did)
211211
}
212212
_ => {
213-
assert!(adt_def.is_univariant());
213+
assert_eq!(adt_def.variants.len(), 1);
214214
&adt_def.variants[0]
215215
}
216216
};
@@ -1096,7 +1096,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
10961096
-> cmt<'tcx> {
10971097
// univariant enums do not need downcasts
10981098
let base_did = self.tcx.parent_def_id(variant_did).unwrap();
1099-
if !self.tcx.adt_def(base_did).is_univariant() {
1099+
if self.tcx.adt_def(base_did).variants.len() != 1 {
11001100
let base_ty = base_cmt.ty;
11011101
let ret = Rc::new(cmt_ {
11021102
id: node.id(),

src/librustc/ty/context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use ty::{PolyFnSig, InferTy, ParamTy, ProjectionTy, ExistentialPredicate, Predic
4141
use ty::RegionKind;
4242
use ty::{TyVar, TyVid, IntVar, IntVid, FloatVar, FloatVid};
4343
use ty::TypeVariants::*;
44-
use ty::layout::{Layout, TargetDataLayout};
44+
use ty::layout::{LayoutDetails, TargetDataLayout};
4545
use ty::maps;
4646
use ty::steal::Steal;
4747
use ty::BindingMode;
@@ -78,7 +78,7 @@ use hir;
7878
/// Internal storage
7979
pub struct GlobalArenas<'tcx> {
8080
// internings
81-
layout: TypedArena<Layout>,
81+
layout: TypedArena<LayoutDetails>,
8282

8383
// references
8484
generics: TypedArena<ty::Generics>,
@@ -918,7 +918,7 @@ pub struct GlobalCtxt<'tcx> {
918918

919919
stability_interner: RefCell<FxHashSet<&'tcx attr::Stability>>,
920920

921-
layout_interner: RefCell<FxHashSet<&'tcx Layout>>,
921+
layout_interner: RefCell<FxHashSet<&'tcx LayoutDetails>>,
922922

923923
/// A vector of every trait accessible in the whole crate
924924
/// (i.e. including those from subcrates). This is used only for
@@ -1016,7 +1016,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10161016
interned
10171017
}
10181018

1019-
pub fn intern_layout(self, layout: Layout) -> &'gcx Layout {
1019+
pub fn intern_layout(self, layout: LayoutDetails) -> &'gcx LayoutDetails {
10201020
if let Some(layout) = self.layout_interner.borrow().get(&layout) {
10211021
return layout;
10221022
}

0 commit comments

Comments
 (0)