Skip to content

Commit d66847e

Browse files
committed
consider the align attribute when allocating a static
i.e. no longer have each backend do that, which is error-prone
1 parent 85f6fe8 commit d66847e

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

compiler/rustc_codegen_gcc/src/consts.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,8 @@ impl<'gcc, 'tcx> StaticCodegenMethods for CodegenCx<'gcc, 'tcx> {
8282
global.to_rvalue().set_type(val_llty);
8383
}
8484

85-
let align = match attrs.alignment {
86-
Some(alignment) => Ord::max(alignment, alloc.align),
87-
None => alloc.align,
88-
};
89-
set_global_alignment(self, global, align);
85+
// NOTE: alignment from attributes has already been applied to the allocation.
86+
set_global_alignment(self, global, alloc.align);
9087

9188
global.global_set_initializer_rvalue(value);
9289

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,8 @@ impl<'ll> CodegenCx<'ll, '_> {
453453
new_g
454454
};
455455

456-
let align = match attrs.alignment {
457-
Some(alignment) => Ord::max(alignment, alloc.align),
458-
None => alloc.align,
459-
};
460-
461-
set_global_alignment(self, g, align);
456+
// NOTE: alignment from attributes has already been applied to the allocation.
457+
set_global_alignment(self, g, alloc.align);
462458
llvm::set_initializer(g, v);
463459

464460
self.assume_dso_local(g, true);

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -953,12 +953,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
953953

954954
// # Global allocations
955955
if let Some(global_alloc) = self.tcx.try_get_global_alloc(id) {
956-
let (size, mut align) = global_alloc.size_and_align(*self.tcx, self.typing_env);
957-
if let GlobalAlloc::Static(def_id) = global_alloc {
958-
if let Some(static_align) = self.tcx.codegen_fn_attrs(def_id).alignment {
959-
align = Ord::max(align, static_align);
960-
}
961-
}
956+
// NOTE: static alignment from attributes has already been applied to the allocation.
957+
let (size, align) = global_alloc.size_and_align(*self.tcx, self.typing_env);
962958
let mutbl = global_alloc.mutability(*self.tcx, self.typing_env);
963959
let kind = match global_alloc {
964960
GlobalAlloc::Static { .. } | GlobalAlloc::Memory { .. } => AllocKind::LiveData,

compiler/rustc_const_eval/src/interpret/util.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@ pub(crate) fn create_static_alloc<'tcx>(
3838
static_def_id: LocalDefId,
3939
layout: TyAndLayout<'tcx>,
4040
) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
41-
let alloc = Allocation::try_new(layout.size, layout.align.abi, AllocInit::Uninit, ())?;
41+
// Take alignment from attributes into account here so that there is one source
42+
// of truth for the alignment of this allocation.
43+
let align = match ecx.tcx.codegen_fn_attrs(static_def_id).alignment {
44+
Some(align_from_attribute) => Ord::max(align_from_attribute, layout.align.abi),
45+
None => layout.align.abi,
46+
};
47+
48+
let alloc = Allocation::try_new(layout.size, align, AllocInit::Uninit, ())?;
4249
let alloc_id = ecx.tcx.reserve_and_set_static_alloc(static_def_id.into());
4350
assert_eq!(ecx.machine.static_root_ids, None);
4451
ecx.machine.static_root_ids = Some((alloc_id, static_def_id));

0 commit comments

Comments
 (0)