Skip to content

Commit 5b4747d

Browse files
committed
rustc_target: avoid using AbiAndPrefAlign where possible.
1 parent 3ce8d44 commit 5b4747d

38 files changed

+311
-334
lines changed

src/librustc/mir/interpret/allocation.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use super::{Pointer, EvalResult, AllocId};
1414

15-
use ty::layout::{Size, Align, AbiAndPrefAlign};
15+
use ty::layout::{Size, Align};
1616
use syntax::ast::Mutability;
1717
use std::iter;
1818
use mir;
@@ -40,7 +40,7 @@ pub struct Allocation<Tag=(),Extra=()> {
4040
/// Denotes undefined memory. Reading from undefined memory is forbidden in miri
4141
pub undef_mask: UndefMask,
4242
/// The alignment of the allocation to detect unaligned reads.
43-
pub align: AbiAndPrefAlign,
43+
pub align: Align,
4444
/// Whether the allocation is mutable.
4545
/// Also used by codegen to determine if a static should be put into mutable memory,
4646
/// which happens for `static mut` and `static` with interior mutability.
@@ -90,7 +90,7 @@ impl AllocationExtra<()> for () {}
9090

9191
impl<Tag, Extra: Default> Allocation<Tag, Extra> {
9292
/// Creates a read-only allocation initialized by the given bytes
93-
pub fn from_bytes(slice: &[u8], align: AbiAndPrefAlign) -> Self {
93+
pub fn from_bytes(slice: &[u8], align: Align) -> Self {
9494
let mut undef_mask = UndefMask::new(Size::ZERO);
9595
undef_mask.grow(Size::from_bytes(slice.len() as u64), true);
9696
Self {
@@ -104,10 +104,10 @@ impl<Tag, Extra: Default> Allocation<Tag, Extra> {
104104
}
105105

106106
pub fn from_byte_aligned_bytes(slice: &[u8]) -> Self {
107-
Allocation::from_bytes(slice, AbiAndPrefAlign::new(Align::from_bytes(1).unwrap()))
107+
Allocation::from_bytes(slice, Align::from_bytes(1).unwrap())
108108
}
109109

110-
pub fn undef(size: Size, align: AbiAndPrefAlign) -> Self {
110+
pub fn undef(size: Size, align: Align) -> Self {
111111
assert_eq!(size.bytes() as usize as u64, size.bytes());
112112
Allocation {
113113
bytes: vec![0; size.bytes() as usize],

src/librustc/mir/interpret/error.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{fmt, env};
1313
use hir::map::definitions::DefPathData;
1414
use mir;
1515
use ty::{self, Ty, layout};
16-
use ty::layout::{Size, AbiAndPrefAlign, LayoutError};
16+
use ty::layout::{Size, Align, LayoutError};
1717
use rustc_target::spec::abi::Abi;
1818

1919
use super::{RawConst, Pointer, InboundsCheck, ScalarMaybeUndef};
@@ -301,8 +301,8 @@ pub enum EvalErrorKind<'tcx, O> {
301301
TlsOutOfBounds,
302302
AbiViolation(String),
303303
AlignmentCheckFailed {
304-
required: AbiAndPrefAlign,
305-
has: AbiAndPrefAlign,
304+
required: Align,
305+
has: Align,
306306
},
307307
ValidationFailure(String),
308308
CalledClosureAsFunction,
@@ -315,7 +315,7 @@ pub enum EvalErrorKind<'tcx, O> {
315315
DeallocatedWrongMemoryKind(String, String),
316316
ReallocateNonBasePtr,
317317
DeallocateNonBasePtr,
318-
IncorrectAllocationInformation(Size, Size, AbiAndPrefAlign, AbiAndPrefAlign),
318+
IncorrectAllocationInformation(Size, Size, Align, Align),
319319
Layout(layout::LayoutError<'tcx>),
320320
HeapAllocZeroBytes,
321321
HeapAllocNonPowerOfTwoAlignment(u64),
@@ -527,7 +527,7 @@ impl<'tcx, O: fmt::Debug> fmt::Debug for EvalErrorKind<'tcx, O> {
527527
write!(f, "tried to interpret an invalid 32-bit value as a char: {}", c),
528528
AlignmentCheckFailed { required, has } =>
529529
write!(f, "tried to access memory with alignment {}, but alignment {} is required",
530-
has.abi.bytes(), required.abi.bytes()),
530+
has.bytes(), required.bytes()),
531531
TypeNotPrimitive(ty) =>
532532
write!(f, "expected primitive type, got {}", ty),
533533
Layout(ref err) =>
@@ -539,7 +539,7 @@ impl<'tcx, O: fmt::Debug> fmt::Debug for EvalErrorKind<'tcx, O> {
539539
IncorrectAllocationInformation(size, size2, align, align2) =>
540540
write!(f, "incorrect alloc info: expected size {} and align {}, \
541541
got size {} and align {}",
542-
size.bytes(), align.abi.bytes(), size2.bytes(), align2.abi.bytes()),
542+
size.bytes(), align.bytes(), size2.bytes(), align2.bytes()),
543543
Panic { ref msg, line, col, ref file } =>
544544
write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col),
545545
InvalidDiscriminant(val) =>

src/librustc/session/code_stats.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use rustc_target::abi::{AbiAndPrefAlign, Size};
11+
use rustc_target::abi::{Align, Size};
1212
use rustc_data_structures::fx::{FxHashSet};
1313
use std::cmp::{self, Ordering};
1414

@@ -63,15 +63,15 @@ impl CodeStats {
6363
pub fn record_type_size<S: ToString>(&mut self,
6464
kind: DataTypeKind,
6565
type_desc: S,
66-
align: AbiAndPrefAlign,
66+
align: Align,
6767
overall_size: Size,
6868
packed: bool,
6969
opt_discr_size: Option<Size>,
7070
variants: Vec<VariantInfo>) {
7171
let info = TypeSizeInfo {
7272
kind,
7373
type_description: type_desc.to_string(),
74-
align: align.abi.bytes(),
74+
align: align.bytes(),
7575
overall_size: overall_size.bytes(),
7676
packed: packed,
7777
opt_discr_size: opt_discr_size.map(|s| s.bytes()),

src/librustc/ty/layout.rs

+41-46
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,10 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
226226
tcx.intern_layout(LayoutDetails::scalar(self, scalar_unit(value)))
227227
};
228228
let scalar_pair = |a: Scalar, b: Scalar| {
229-
let align = a.value.align(dl).max(b.value.align(dl)).max(dl.aggregate_align);
230-
let b_offset = a.value.size(dl).abi_align(b.value.align(dl));
231-
let size = (b_offset + b.value.size(dl)).abi_align(align);
229+
let b_align = b.value.align(dl);
230+
let align = a.value.align(dl).max(b_align).max(dl.aggregate_align);
231+
let b_offset = a.value.size(dl).align_to(b_align.abi);
232+
let size = (b_offset + b.value.size(dl)).align_to(align.abi);
232233
LayoutDetails {
233234
variants: Variants::Single { index: VariantIdx::new(0) },
234235
fields: FieldPlacement::Arbitrary {
@@ -248,7 +249,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
248249
/// A univariant, the last field of which may be coerced to unsized.
249250
MaybeUnsized,
250251
/// A univariant, but with a prefix of an arbitrary size & alignment (e.g. enum tag).
251-
Prefixed(Size, AbiAndPrefAlign),
252+
Prefixed(Size, Align),
252253
}
253254

254255
let univariant_uninterned = |fields: &[TyLayout<'_>], repr: &ReprOptions, kind| {
@@ -257,10 +258,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
257258
bug!("struct cannot be packed and aligned");
258259
}
259260

260-
let pack = {
261-
let pack = repr.pack as u64;
262-
AbiAndPrefAlign::new(Align::from_bytes(pack).unwrap())
263-
};
261+
let pack = Align::from_bytes(repr.pack as u64).unwrap();
264262

265263
let mut align = if packed {
266264
dl.i8_align
@@ -274,7 +272,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
274272

275273
let mut optimize = !repr.inhibit_struct_field_reordering_opt();
276274
if let StructKind::Prefixed(_, align) = kind {
277-
optimize &= align.abi.bytes() == 1;
275+
optimize &= align.bytes() == 1;
278276
}
279277

280278
if optimize {
@@ -285,7 +283,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
285283
};
286284
let optimizing = &mut inverse_memory_index[..end];
287285
let field_align = |f: &TyLayout<'_>| {
288-
if packed { f.align.min(pack).abi } else { f.align.abi }
286+
if packed { f.align.abi.min(pack) } else { f.align.abi }
289287
};
290288
match kind {
291289
StructKind::AlwaysSized |
@@ -312,13 +310,13 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
312310
let mut offset = Size::ZERO;
313311

314312
if let StructKind::Prefixed(prefix_size, prefix_align) = kind {
315-
if packed {
316-
let prefix_align = prefix_align.min(pack);
317-
align = align.max(prefix_align);
313+
let prefix_align = if packed {
314+
prefix_align.min(pack)
318315
} else {
319-
align = align.max(prefix_align);
320-
}
321-
offset = prefix_size.abi_align(prefix_align);
316+
prefix_align
317+
};
318+
align = align.max(AbiAndPrefAlign::new(prefix_align));
319+
offset = prefix_size.align_to(prefix_align);
322320
}
323321

324322
for &i in &inverse_memory_index {
@@ -333,15 +331,13 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
333331
}
334332

335333
// Invariant: offset < dl.obj_size_bound() <= 1<<61
336-
if packed {
337-
let field_pack = field.align.min(pack);
338-
offset = offset.abi_align(field_pack);
339-
align = align.max(field_pack);
340-
}
341-
else {
342-
offset = offset.abi_align(field.align);
343-
align = align.max(field.align);
344-
}
334+
let field_align = if packed {
335+
field.align.min(AbiAndPrefAlign::new(pack))
336+
} else {
337+
field.align
338+
};
339+
offset = offset.align_to(field_align.abi);
340+
align = align.max(field_align);
345341

346342
debug!("univariant offset: {:?} field: {:#?}", offset, field);
347343
offsets[i as usize] = offset;
@@ -377,7 +373,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
377373
memory_index = inverse_memory_index;
378374
}
379375

380-
let size = min_size.abi_align(align);
376+
let size = min_size.align_to(align.abi);
381377
let mut abi = Abi::Aggregate { sized };
382378

383379
// Unpack newtype ABIs and find scalar pairs.
@@ -648,7 +644,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
648644
let size = element.size.checked_mul(count, dl)
649645
.ok_or(LayoutError::SizeOverflow(ty))?;
650646
let align = dl.vector_align(size);
651-
let size = size.abi_align(align);
647+
let size = size.align_to(align.abi);
652648

653649
tcx.intern_layout(LayoutDetails {
654650
variants: Variants::Single { index: VariantIdx::new(0) },
@@ -680,10 +676,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
680676
bug!("Union cannot be packed and aligned");
681677
}
682678

683-
let pack = {
684-
let pack = def.repr.pack as u64;
685-
AbiAndPrefAlign::new(Align::from_bytes(pack).unwrap())
686-
};
679+
let pack = Align::from_bytes(def.repr.pack as u64).unwrap();
687680

688681
let mut align = if packed {
689682
dl.i8_align
@@ -704,12 +697,12 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
704697
for field in &variants[index] {
705698
assert!(!field.is_unsized());
706699

707-
if packed {
708-
let field_pack = field.align.min(pack);
709-
align = align.max(field_pack);
700+
let field_align = if packed {
701+
field.align.min(AbiAndPrefAlign::new(pack))
710702
} else {
711-
align = align.max(field.align);
712-
}
703+
field.align
704+
};
705+
align = align.max(field_align);
713706

714707
// If all non-ZST fields have the same ABI, forward this ABI
715708
if optimize && !field.is_zst() {
@@ -749,7 +742,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
749742
fields: FieldPlacement::Union(variants[index].len()),
750743
abi,
751744
align,
752-
size: size.abi_align(align)
745+
size: size.align_to(align.abi)
753746
}));
754747
}
755748

@@ -964,19 +957,19 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
964957
let mut size = Size::ZERO;
965958

966959
// We're interested in the smallest alignment, so start large.
967-
let mut start_align = AbiAndPrefAlign::new(Align::from_bytes(256).unwrap());
968-
assert_eq!(Integer::for_abi_align(dl, start_align), None);
960+
let mut start_align = Align::from_bytes(256).unwrap();
961+
assert_eq!(Integer::for_align(dl, start_align), None);
969962

970963
// repr(C) on an enum tells us to make a (tag, union) layout,
971964
// so we need to grow the prefix alignment to be at least
972965
// the alignment of the union. (This value is used both for
973966
// determining the alignment of the overall enum, and the
974967
// determining the alignment of the payload after the tag.)
975-
let mut prefix_align = min_ity.align(dl);
968+
let mut prefix_align = min_ity.align(dl).abi;
976969
if def.repr.c() {
977970
for fields in &variants {
978971
for field in fields {
979-
prefix_align = prefix_align.max(field.align);
972+
prefix_align = prefix_align.max(field.align.abi);
980973
}
981974
}
982975
}
@@ -990,7 +983,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
990983
// to make room for a larger discriminant.
991984
for field in st.fields.index_by_increasing_offset().map(|j| field_layouts[j]) {
992985
if !field.is_zst() || field.align.abi.bytes() != 1 {
993-
start_align = start_align.min(field.align);
986+
start_align = start_align.min(field.align.abi);
994987
break;
995988
}
996989
}
@@ -1000,7 +993,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
1000993
}).collect::<Result<IndexVec<VariantIdx, _>, _>>()?;
1001994

1002995
// Align the maximum variant size to the largest alignment.
1003-
size = size.abi_align(align);
996+
size = size.align_to(align.abi);
1004997

1005998
if size.bytes() >= dl.obj_size_bound() {
1006999
return Err(LayoutError::SizeOverflow(ty));
@@ -1036,7 +1029,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
10361029
let mut ity = if def.repr.c() || def.repr.int.is_some() {
10371030
min_ity
10381031
} else {
1039-
Integer::for_abi_align(dl, start_align).unwrap_or(min_ity)
1032+
Integer::for_align(dl, start_align).unwrap_or(min_ity)
10401033
};
10411034

10421035
// If the alignment is not larger than the chosen discriminant size,
@@ -1204,7 +1197,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
12041197
let type_desc = format!("{:?}", layout.ty);
12051198
self.tcx.sess.code_stats.borrow_mut().record_type_size(kind,
12061199
type_desc,
1207-
layout.align,
1200+
layout.align.abi,
12081201
layout.size,
12091202
packed,
12101203
opt_discr_size,
@@ -1823,7 +1816,9 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
18231816
Abi::ScalarPair(ref a, ref b) => {
18241817
// HACK(nox): We iter on `b` and then `a` because `max_by_key`
18251818
// returns the last maximum.
1826-
let niche = iter::once((b, a.value.size(self).abi_align(b.value.align(self))))
1819+
let niche = iter::once(
1820+
(b, a.value.size(self).align_to(b.value.align(self).abi))
1821+
)
18271822
.chain(iter::once((a, Size::ZERO)))
18281823
.filter_map(|(scalar, offset)| scalar_niche(scalar, offset))
18291824
.max_by_key(|niche| niche.available);

0 commit comments

Comments
 (0)