Skip to content

layout: Factor out a utility function. #63899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 20 additions & 40 deletions src/librustc/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,24 +267,27 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
}
}

fn align_and_pack(&self, repr: &ReprOptions) -> (AbiAndPrefAlign, Option<Align>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you added a pack: Option<Align> field to ReprOptions?
The Align encoding is logarithmic so it would occupy less space and not need further conversion.

cc @oli-obk @nagisa @rkruppe (curious what other people think)

let dl = self.data_layout();
if repr.packed() {
let pack = Align::from_bytes(repr.pack as u64).unwrap();
if repr.align > 0 {
bug!("adt cannot be packed and aligned");
}
(dl.i8_align, Some(pack))
} else {
let align = Align::from_bytes(repr.align as u64).unwrap();
(dl.aggregate_align.max(AbiAndPrefAlign::new(align)), None)
}
}

fn univariant_uninterned(&self,
ty: Ty<'tcx>,
fields: &[TyLayout<'_>],
repr: &ReprOptions,
kind: StructKind) -> Result<LayoutDetails, LayoutError<'tcx>> {
let dl = self.data_layout();
let packed = repr.packed();
if packed && repr.align > 0 {
bug!("struct cannot be packed and aligned");
}

let pack = Align::from_bytes(repr.pack as u64).unwrap();

let mut align = if packed {
dl.i8_align
} else {
dl.aggregate_align
};
let (mut align, pack) = self.align_and_pack(repr);

let mut sized = true;
let mut offsets = vec![Size::ZERO; fields.len()];
Expand All @@ -303,7 +306,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
};
let optimizing = &mut inverse_memory_index[..end];
let field_align = |f: &TyLayout<'_>| {
if packed { f.align.abi.min(pack) } else { f.align.abi }
if let Some(pack) = pack { f.align.abi.min(pack) } else { f.align.abi }
};
match kind {
StructKind::AlwaysSized |
Expand Down Expand Up @@ -334,7 +337,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
let mut largest_niche_available = 0;

if let StructKind::Prefixed(prefix_size, prefix_align) = kind {
let prefix_align = if packed {
let prefix_align = if let Some(pack) = pack {
prefix_align.min(pack)
} else {
prefix_align
Expand All @@ -355,7 +358,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
}

// Invariant: offset < dl.obj_size_bound() <= 1<<61
let field_align = if packed {
let field_align = if let Some(pack) = pack {
field.align.min(AbiAndPrefAlign::new(pack))
} else {
field.align
Expand All @@ -379,12 +382,6 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
.ok_or(LayoutError::SizeOverflow(ty))?;
}

if repr.align > 0 {
let repr_align = repr.align as u64;
align = align.max(AbiAndPrefAlign::new(Align::from_bytes(repr_align).unwrap()));
debug!("univariant repr_align: {:?}", repr_align);
}

debug!("univariant min_size: {:?}", offset);
let min_size = offset;

Expand Down Expand Up @@ -730,24 +727,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
}).collect::<Result<IndexVec<VariantIdx, _>, _>>()?;

if def.is_union() {
let packed = def.repr.packed();
if packed && def.repr.align > 0 {
bug!("Union cannot be packed and aligned");
}

let pack = Align::from_bytes(def.repr.pack as u64).unwrap();

let mut align = if packed {
dl.i8_align
} else {
dl.aggregate_align
};

if def.repr.align > 0 {
let repr_align = def.repr.align as u64;
align = align.max(
AbiAndPrefAlign::new(Align::from_bytes(repr_align).unwrap()));
}
let (mut align, pack) = self.align_and_pack(&def.repr);

let optimize = !def.repr.inhibit_union_abi_opt();
let mut size = Size::ZERO;
Expand All @@ -756,7 +736,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
for field in &variants[index] {
assert!(!field.is_unsized());

let field_align = if packed {
let field_align = if let Some(pack) = pack {
field.align.min(AbiAndPrefAlign::new(pack))
} else {
field.align
Expand Down