Skip to content

Commit

Permalink
Auto merge of #86266 - LeSeulArtichaut:box-thir-adt, r=davidtwco
Browse files Browse the repository at this point in the history
Box `thir::ExprKind::Adt` for performance

`Adt` is the biggest variant in the enum and probably isn't used very often compared to the other expr kinds, so boxing it should be beneficial for performance. We need a perf test to be sure.
  • Loading branch information
bors committed Jun 16, 2021
2 parents 9fef8d9 + 5e802e5 commit a85f584
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
29 changes: 16 additions & 13 deletions compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ pub struct Block {
pub safety_mode: BlockSafety,
}

#[derive(Debug, HashStable)]
pub struct Adt<'tcx> {
pub adt_def: &'tcx AdtDef,
pub variant_index: VariantIdx,
pub substs: SubstsRef<'tcx>,

/// Optional user-given substs: for something like `let x =
/// Bar::<T> { ... }`.
pub user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,

pub fields: Box<[FieldExpr]>,
pub base: Option<FruInfo<'tcx>>,
}

#[derive(Copy, Clone, Debug, HashStable)]
pub enum BlockSafety {
Safe,
Expand Down Expand Up @@ -145,7 +159,7 @@ pub enum StmtKind<'tcx> {

// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
rustc_data_structures::static_assert_size!(Expr<'_>, 144);
rustc_data_structures::static_assert_size!(Expr<'_>, 104);

/// The Thir trait implementor lowers their expressions (`&'tcx H::Expr`)
/// into instances of this `Expr` enum. This lowering can be done
Expand Down Expand Up @@ -304,18 +318,7 @@ pub enum ExprKind<'tcx> {
Tuple {
fields: Box<[ExprId]>,
},
Adt {
adt_def: &'tcx AdtDef,
variant_index: VariantIdx,
substs: SubstsRef<'tcx>,

/// Optional user-given substs: for something like `let x =
/// Bar::<T> { ... }`.
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,

fields: Box<[FieldExpr]>,
base: Option<FruInfo<'tcx>>,
},
Adt(Box<Adt<'tcx>>),
PlaceTypeAscription {
source: ExprId,
/// Type that the user gave to this expression
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_mir_build/src/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
this.cfg.push_assign(block, source_info, destination, address_of);
block.unit()
}
ExprKind::Adt { adt_def, variant_index, substs, user_ty, ref fields, ref base } => {
ExprKind::Adt(box Adt {
adt_def,
variant_index,
substs,
user_ty,
ref fields,
ref base,
}) => {
// See the notes for `ExprKind::Array` in `as_rvalue` and for
// `ExprKind::Borrow` above.
let is_union = adt_def.is_union();
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => {
self.requires_unsafe(expr.span, UseOfInlineAssembly);
}
ExprKind::Adt {
ExprKind::Adt(box Adt {
adt_def,
variant_index: _,
substs: _,
user_ty: _,
fields: _,
base: _,
} => match self.tcx.layout_scalar_valid_range(adt_def.did) {
}) => match self.tcx.layout_scalar_valid_range(adt_def.did) {
(Bound::Unbounded, Bound::Unbounded) => {}
_ => self.requires_unsafe(expr.span, InitializingTypeWith),
},
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@ impl<'tcx> Cx<'tcx> {
expr: self.mirror_expr(e),
})
.collect();
ExprKind::Adt {
ExprKind::Adt(Box::new(Adt {
adt_def,
substs,
variant_index: index,
fields: field_refs,
user_ty,
base: None,
}
}))
} else {
ExprKind::Call {
ty: self.typeck_results().node_type(fun.hir_id),
Expand Down Expand Up @@ -362,7 +362,7 @@ impl<'tcx> Cx<'tcx> {
let user_provided_types = self.typeck_results().user_provided_types();
let user_ty = user_provided_types.get(expr.hir_id).copied();
debug!("make_mirror_unadjusted: (struct/union) user_ty={:?}", user_ty);
ExprKind::Adt {
ExprKind::Adt(Box::new(Adt {
adt_def: adt,
variant_index: VariantIdx::new(0),
substs,
Expand All @@ -375,7 +375,7 @@ impl<'tcx> Cx<'tcx> {
.copied()
.collect(),
}),
}
}))
}
AdtKind::Enum => {
let res = self.typeck_results().qpath_res(qpath, expr.hir_id);
Expand All @@ -388,14 +388,14 @@ impl<'tcx> Cx<'tcx> {
self.typeck_results().user_provided_types();
let user_ty = user_provided_types.get(expr.hir_id).copied();
debug!("make_mirror_unadjusted: (variant) user_ty={:?}", user_ty);
ExprKind::Adt {
ExprKind::Adt(Box::new(Adt {
adt_def: adt,
variant_index: index,
substs,
user_ty,
fields: self.field_refs(fields),
base: None,
}
}))
}
_ => {
span_bug!(expr.span, "unexpected res: {:?}", res);
Expand Down Expand Up @@ -906,14 +906,14 @@ impl<'tcx> Cx<'tcx> {
match ty.kind() {
// A unit struct/variant which is used as a value.
// We return a completely different ExprKind here to account for this special case.
ty::Adt(adt_def, substs) => ExprKind::Adt {
ty::Adt(adt_def, substs) => ExprKind::Adt(Box::new(Adt {
adt_def,
variant_index: adt_def.variant_index_with_ctor_id(def_id),
substs,
user_ty: user_provided_type,
fields: box [],
base: None,
},
})),
_ => bug!("unexpected ty: {:?}", ty),
}
}
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_mir_build/src/thir/visit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_middle::thir::*;
use rustc_middle::thir::{self, *};
use rustc_middle::ty::Const;

pub trait Visitor<'a, 'tcx: 'a>: Sized {
Expand Down Expand Up @@ -94,7 +94,14 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
visitor.visit_expr(&visitor.thir()[field]);
}
}
Adt { ref fields, ref base, adt_def: _, variant_index: _, substs: _, user_ty: _ } => {
Adt(box thir::Adt {
ref fields,
ref base,
adt_def: _,
variant_index: _,
substs: _,
user_ty: _,
}) => {
for field in &**fields {
visitor.visit_expr(&visitor.thir()[field.expr]);
}
Expand Down

0 comments on commit a85f584

Please sign in to comment.