Skip to content

Commit f3b5aae

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 f3b5aae

File tree

5 files changed

+23
-19
lines changed

5 files changed

+23
-19
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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ 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 over-alignment from attributes into account.
42+
let align = match ecx.tcx.codegen_fn_attrs(static_def_id).alignment {
43+
Some(align_from_attribute) => Ord::max(align_from_attribute, layout.align.abi),
44+
None => layout.align.abi,
45+
};
46+
47+
let alloc = Allocation::try_new(layout.size, align, AllocInit::Uninit, ())?;
4248
let alloc_id = ecx.tcx.reserve_and_set_static_alloc(static_def_id.into());
4349
assert_eq!(ecx.machine.static_root_ids, None);
4450
ecx.machine.static_root_ids = Some((alloc_id, static_def_id));

compiler/rustc_middle/src/mir/interpret/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,16 @@ impl<'tcx> GlobalAlloc<'tcx> {
386386
.expect("statics should not have generic parameters");
387387
let layout = tcx.layout_of(typing_env.as_query_input(ty)).unwrap();
388388
assert!(layout.is_sized());
389-
(layout.size, layout.align.abi)
389+
390+
// Take over-alignment from attributes into account.
391+
let align = match tcx.codegen_fn_attrs(def_id).alignment {
392+
Some(align_from_attribute) => {
393+
Ord::max(align_from_attribute, layout.align.abi)
394+
}
395+
None => layout.align.abi,
396+
};
397+
398+
(layout.size, align)
390399
}
391400
}
392401
GlobalAlloc::Memory(alloc) => {

0 commit comments

Comments
 (0)