Skip to content

Commit 7c6f242

Browse files
committed
rustc: don't track whether layouts are "packed".
1 parent 53a6d14 commit 7c6f242

File tree

10 files changed

+116
-184
lines changed

10 files changed

+116
-184
lines changed

src/librustc/ty/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ pub struct InterpretInterner<'tcx> {
895895
/// Allows checking whether a constant already has an allocation
896896
///
897897
/// The pointers are to the beginning of an `alloc_by_id` allocation
898-
alloc_cache: FxHashMap<interpret::GlobalId<'tcx>, interpret::PtrAndAlign>,
898+
alloc_cache: FxHashMap<interpret::GlobalId<'tcx>, interpret::Pointer>,
899899

900900
/// A cache for basic byte allocations keyed by their contents. This is used to deduplicate
901901
/// allocations for string and bytestring literals.
@@ -931,14 +931,14 @@ impl<'tcx> InterpretInterner<'tcx> {
931931
pub fn get_cached(
932932
&self,
933933
global_id: interpret::GlobalId<'tcx>,
934-
) -> Option<interpret::PtrAndAlign> {
934+
) -> Option<interpret::Pointer> {
935935
self.alloc_cache.get(&global_id).cloned()
936936
}
937937

938938
pub fn cache(
939939
&mut self,
940940
global_id: interpret::GlobalId<'tcx>,
941-
ptr: interpret::PtrAndAlign,
941+
ptr: interpret::Pointer,
942942
) {
943943
if let Some(old) = self.alloc_cache.insert(global_id, ptr) {
944944
bug!("tried to cache {:?}, but was already existing as {:#?}", global_id, old);

src/librustc/ty/layout.rs

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,6 @@ pub enum Abi {
778778
Aggregate {
779779
/// If true, the size is exact, otherwise it's only a lower bound.
780780
sized: bool,
781-
packed: bool
782781
}
783782
}
784783

@@ -790,18 +789,7 @@ impl Abi {
790789
Abi::Scalar(_) |
791790
Abi::ScalarPair(..) |
792791
Abi::Vector { .. } => false,
793-
Abi::Aggregate { sized, .. } => !sized
794-
}
795-
}
796-
797-
/// Returns true if the fields of the layout are packed.
798-
pub fn is_packed(&self) -> bool {
799-
match *self {
800-
Abi::Uninhabited |
801-
Abi::Scalar(_) |
802-
Abi::ScalarPair(..) |
803-
Abi::Vector { .. } => false,
804-
Abi::Aggregate { packed, .. } => packed
792+
Abi::Aggregate { sized } => !sized
805793
}
806794
}
807795
}
@@ -1077,10 +1065,7 @@ impl<'a, 'tcx> LayoutDetails {
10771065
}
10781066

10791067
let size = min_size.abi_align(align);
1080-
let mut abi = Abi::Aggregate {
1081-
sized,
1082-
packed
1083-
};
1068+
let mut abi = Abi::Aggregate { sized };
10841069

10851070
// Unpack newtype ABIs and find scalar pairs.
10861071
if sized && size.bytes() > 0 {
@@ -1254,10 +1239,7 @@ impl<'a, 'tcx> LayoutDetails {
12541239
stride: element.size,
12551240
count
12561241
},
1257-
abi: Abi::Aggregate {
1258-
sized: true,
1259-
packed: false
1260-
},
1242+
abi: Abi::Aggregate { sized: true },
12611243
align: element.align,
12621244
size
12631245
})
@@ -1270,10 +1252,7 @@ impl<'a, 'tcx> LayoutDetails {
12701252
stride: element.size,
12711253
count: 0
12721254
},
1273-
abi: Abi::Aggregate {
1274-
sized: false,
1275-
packed: false
1276-
},
1255+
abi: Abi::Aggregate { sized: false },
12771256
align: element.align,
12781257
size: Size::from_bytes(0)
12791258
})
@@ -1285,10 +1264,7 @@ impl<'a, 'tcx> LayoutDetails {
12851264
stride: Size::from_bytes(1),
12861265
count: 0
12871266
},
1288-
abi: Abi::Aggregate {
1289-
sized: false,
1290-
packed: false
1291-
},
1267+
abi: Abi::Aggregate { sized: false },
12921268
align: dl.i8_align,
12931269
size: Size::from_bytes(0)
12941270
})
@@ -1302,7 +1278,7 @@ impl<'a, 'tcx> LayoutDetails {
13021278
let mut unit = univariant_uninterned(&[], &ReprOptions::default(),
13031279
StructKind::AlwaysSized)?;
13041280
match unit.abi {
1305-
Abi::Aggregate { ref mut sized, .. } => *sized = false,
1281+
Abi::Aggregate { ref mut sized } => *sized = false,
13061282
_ => bug!()
13071283
}
13081284
tcx.intern_layout(unit)
@@ -1418,10 +1394,7 @@ impl<'a, 'tcx> LayoutDetails {
14181394
return Ok(tcx.intern_layout(LayoutDetails {
14191395
variants: Variants::Single { index: 0 },
14201396
fields: FieldPlacement::Union(variants[0].len()),
1421-
abi: Abi::Aggregate {
1422-
sized: true,
1423-
packed
1424-
},
1397+
abi: Abi::Aggregate { sized: true },
14251398
align,
14261399
size: size.abi_align(align)
14271400
}));
@@ -1525,15 +1498,10 @@ impl<'a, 'tcx> LayoutDetails {
15251498
let abi = if offset.bytes() == 0 && niche.value.size(dl) == size {
15261499
Abi::Scalar(niche.clone())
15271500
} else {
1528-
let mut packed = st[i].abi.is_packed();
15291501
if offset.abi_align(niche_align) != offset {
1530-
packed = true;
15311502
niche_align = dl.i8_align;
15321503
}
1533-
Abi::Aggregate {
1534-
sized: true,
1535-
packed
1536-
}
1504+
Abi::Aggregate { sized: true }
15371505
};
15381506
align = align.max(niche_align);
15391507

@@ -1681,10 +1649,7 @@ impl<'a, 'tcx> LayoutDetails {
16811649
let abi = if discr.value.size(dl) == size {
16821650
Abi::Scalar(discr.clone())
16831651
} else {
1684-
Abi::Aggregate {
1685-
sized: true,
1686-
packed: false
1687-
}
1652+
Abi::Aggregate { sized: true }
16881653
};
16891654
tcx.intern_layout(LayoutDetails {
16901655
variants: Variants::Tagged {
@@ -2277,19 +2242,14 @@ impl<'a, 'tcx> TyLayout<'tcx> {
22772242
self.abi.is_unsized()
22782243
}
22792244

2280-
/// Returns true if the fields of the layout are packed.
2281-
pub fn is_packed(&self) -> bool {
2282-
self.abi.is_packed()
2283-
}
2284-
22852245
/// Returns true if the type is a ZST and not unsized.
22862246
pub fn is_zst(&self) -> bool {
22872247
match self.abi {
22882248
Abi::Uninhabited => true,
22892249
Abi::Scalar(_) |
22902250
Abi::ScalarPair(..) |
22912251
Abi::Vector { .. } => false,
2292-
Abi::Aggregate { sized, .. } => sized && self.size.bytes() == 0
2252+
Abi::Aggregate { sized } => sized && self.size.bytes() == 0
22932253
}
22942254
}
22952255

@@ -2452,8 +2412,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for Abi {
24522412
element.hash_stable(hcx, hasher);
24532413
count.hash_stable(hcx, hasher);
24542414
}
2455-
Aggregate { packed, sized } => {
2456-
packed.hash_stable(hcx, hasher);
2415+
Aggregate { sized } => {
24572416
sized.hash_stable(hcx, hasher);
24582417
}
24592418
}

src/librustc_mir/interpret/const_eval.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_data_structures::indexed_vec::Idx;
1212
use syntax::ast::Mutability;
1313
use syntax::codemap::Span;
1414

15-
use rustc::mir::interpret::{EvalResult, EvalError, EvalErrorKind, GlobalId, Value, PrimVal, PtrAndAlign};
15+
use rustc::mir::interpret::{EvalResult, EvalError, EvalErrorKind, GlobalId, Value, Pointer, PrimVal, PtrAndAlign};
1616
use super::{Place, PlaceExtra, EvalContext, StackPopCleanup, ValTy, HasMemory};
1717

1818
use rustc_const_math::ConstInt;
@@ -45,7 +45,7 @@ pub fn eval_body<'a, 'tcx>(
4545
tcx: TyCtxt<'a, 'tcx, 'tcx>,
4646
instance: Instance<'tcx>,
4747
param_env: ty::ParamEnv<'tcx>,
48-
) -> EvalResult<'tcx, (PtrAndAlign, Ty<'tcx>)> {
48+
) -> EvalResult<'tcx, (Pointer, Ty<'tcx>)> {
4949
debug!("eval_body: {:?}, {:?}", instance, param_env);
5050
let limits = super::ResourceLimits::default();
5151
let mut ecx = EvalContext::new(tcx, param_env, limits, CompileTimeEvaluator, ());
@@ -69,13 +69,7 @@ pub fn eval_body<'a, 'tcx>(
6969
layout.align.abi(),
7070
None,
7171
)?;
72-
tcx.interpret_interner.borrow_mut().cache(
73-
cid,
74-
PtrAndAlign {
75-
ptr: ptr.into(),
76-
aligned: !layout.is_packed(),
77-
},
78-
);
72+
tcx.interpret_interner.borrow_mut().cache(cid, ptr.into());
7973
let cleanup = StackPopCleanup::MarkStatic(Mutability::Immutable);
8074
let name = ty::tls::with(|tcx| tcx.item_path_str(instance.def_id()));
8175
trace!("const_eval: pushing stack frame for global: {}", name);
@@ -101,7 +95,7 @@ pub fn eval_body_as_integer<'a, 'tcx>(
10195
let ptr_ty = eval_body(tcx, instance, param_env);
10296
let (ptr, ty) = ptr_ty?;
10397
let ecx = mk_eval_cx(tcx, instance, param_env)?;
104-
let prim = match ecx.read_maybe_aligned(ptr.aligned, |ectx| ectx.try_read_value(ptr.ptr, ty))? {
98+
let prim = match ecx.try_read_value(ptr, ty)? {
10599
Some(Value::ByVal(prim)) => prim.to_bytes()?,
106100
_ => return err!(TypeNotPrimitive(ty)),
107101
};
@@ -363,7 +357,10 @@ pub fn const_eval_provider<'a, 'tcx>(
363357
(_, Err(err)) => Err(err),
364358
(Ok((miri_val, miri_ty)), Ok(ctfe)) => {
365359
let mut ecx = mk_eval_cx(tcx, instance, key.param_env).unwrap();
366-
check_ctfe_against_miri(&mut ecx, miri_val, miri_ty, ctfe.val);
360+
check_ctfe_against_miri(&mut ecx, PtrAndAlign {
361+
ptr: miri_val,
362+
aligned: true
363+
}, miri_ty, ctfe.val);
367364
Ok(ctfe)
368365
}
369366
}

src/librustc_mir/interpret/eval_context.rs

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,10 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
261261

262262
Unevaluated(def_id, substs) => {
263263
let instance = self.resolve(def_id, substs)?;
264-
let cid = GlobalId {
264+
return Ok(self.read_global_as_value(GlobalId {
265265
instance,
266266
promoted: None,
267-
};
268-
return Ok(Value::ByRef(self.tcx.interpret_interner.borrow().get_cached(cid).expect("static/const not cached")));
267+
}));
269268
}
270269

271270
Aggregate(..) |
@@ -834,11 +833,10 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
834833
Literal::Value { ref value } => self.const_to_value(&value.val)?,
835834

836835
Literal::Promoted { index } => {
837-
let cid = GlobalId {
836+
self.read_global_as_value(GlobalId {
838837
instance: self.frame().instance,
839838
promoted: Some(index),
840-
};
841-
Value::ByRef(self.tcx.interpret_interner.borrow().get_cached(cid).expect("promoted not cached"))
839+
})
842840
}
843841
};
844842

@@ -951,7 +949,10 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
951949
}
952950

953951
pub fn read_global_as_value(&self, gid: GlobalId) -> Value {
954-
Value::ByRef(self.tcx.interpret_interner.borrow().get_cached(gid).expect("global not cached"))
952+
Value::ByRef(PtrAndAlign {
953+
ptr: self.tcx.interpret_interner.borrow().get_cached(gid).expect("global not cached"),
954+
aligned: true
955+
})
955956
}
956957

957958
fn copy(&mut self, src: Pointer, dest: Pointer, ty: Ty<'tcx>) -> EvalResult<'tcx> {
@@ -1149,51 +1150,29 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
11491150
}
11501151
Value::ByVal(primval) => {
11511152
let layout = self.layout_of(dest_ty)?;
1152-
if layout.is_zst() {
1153-
assert!(primval.is_undef());
1154-
Ok(())
1155-
} else {
1156-
// TODO: Do we need signedness?
1157-
self.memory.write_maybe_aligned_mut(!layout.is_packed(), |mem| {
1158-
mem.write_primval(dest.to_ptr()?, primval, layout.size.bytes(), false)
1159-
})
1153+
match layout.abi {
1154+
layout::Abi::Scalar(_) => {}
1155+
_ if primval.is_undef() => {}
1156+
_ => bug!("write_value_to_ptr: invalid ByVal layout: {:#?}", layout)
11601157
}
1158+
// TODO: Do we need signedness?
1159+
self.memory.write_primval(dest.to_ptr()?, primval, layout.size.bytes(), false)
11611160
}
1162-
Value::ByValPair(a, b) => {
1161+
Value::ByValPair(a_val, b_val) => {
11631162
let ptr = dest.to_ptr()?;
11641163
let mut layout = self.layout_of(dest_ty)?;
11651164
trace!("write_value_to_ptr valpair: {:#?}", layout);
1166-
let mut packed = layout.is_packed();
1167-
'outer: loop {
1168-
for i in 0..layout.fields.count() {
1169-
let field = layout.field(&self, i)?;
1170-
if layout.fields.offset(i).bytes() == 0 && layout.size == field.size {
1171-
layout = field;
1172-
packed |= layout.is_packed();
1173-
continue 'outer;
1174-
}
1175-
}
1176-
break;
1177-
}
1178-
trace!("write_value_to_ptr valpair: {:#?}", layout);
1179-
assert_eq!(layout.fields.count(), 2);
1180-
let field_0 = layout.field(&self, 0)?;
1181-
let field_1 = layout.field(&self, 1)?;
1182-
trace!("write_value_to_ptr field 0: {:#?}", field_0);
1183-
trace!("write_value_to_ptr field 1: {:#?}", field_1);
1184-
assert_eq!(
1185-
field_0.is_packed(),
1186-
field_1.is_packed(),
1187-
"the two fields must agree on being packed"
1188-
);
1189-
packed |= field_0.is_packed();
1190-
let field_0_ptr = ptr.offset(layout.fields.offset(0).bytes(), &self)?.into();
1191-
let field_1_ptr = ptr.offset(layout.fields.offset(1).bytes(), &self)?.into();
1165+
let (a, b) = match layout.abi {
1166+
layout::Abi::ScalarPair(ref a, ref b) => (&a.value, &b.value),
1167+
_ => bug!("write_value_to_ptr: invalid ByValPair layout: {:#?}", layout)
1168+
};
1169+
let (a_size, b_size) = (a.size(&self), b.size(&self));
1170+
let a_ptr = ptr;
1171+
let b_offset = a_size.abi_align(b.align(&self));
1172+
let b_ptr = ptr.offset(b_offset.bytes(), &self)?.into();
11921173
// TODO: What about signedess?
1193-
self.memory.write_maybe_aligned_mut(!packed, |mem| {
1194-
mem.write_primval(field_0_ptr, a, field_0.size.bytes(), false)?;
1195-
mem.write_primval(field_1_ptr, b, field_1.size.bytes(), false)
1196-
})?;
1174+
self.memory.write_primval(a_ptr, a_val, a_size.bytes(), false)?;
1175+
self.memory.write_primval(b_ptr, b_val, b_size.bytes(), false)?;
11971176
Ok(())
11981177
}
11991178
}

0 commit comments

Comments
 (0)