Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion compiler/rustc_const_eval/src/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {

// # Global allocations
if let Some(global_alloc) = self.tcx.try_get_global_alloc(id) {
let (size, align) = global_alloc.size_and_align(*self.tcx, self.typing_env);
let (size, mut align) = global_alloc.size_and_align(*self.tcx, self.typing_env);
if let Some(min_global_align) = self.tcx.sess.target.min_global_align {
align = Ord::max(align, min_global_align);
}
let mutbl = global_alloc.mutability(*self.tcx, self.typing_env);
let kind = match global_alloc {
GlobalAlloc::Static { .. } | GlobalAlloc::Memory { .. } => AllocKind::LiveData,
Expand Down
37 changes: 37 additions & 0 deletions src/tools/miri/tests/pass/static_align.rs
Copy link
Member

Choose a reason for hiding this comment

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

Any chance this could be done with a macro rather than having to repeat the same code many times?

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Test that miri respects the `target.min_global_align` value for the target.
//
// The only way to observe its effect currently is to test for the alignment of statics with a
// natural alignment of 1 on s390x. On that target, the `min_global_align` is 2 bytes.

fn main() {
let min_align = if cfg!(target_arch = "s390x") { 2 } else { 1 };

macro_rules! check {
($x:ident, $v:expr) => {
static $x: bool = $v;
assert!(core::ptr::from_ref(&$x).addr().is_multiple_of(min_align));
};
}

check!(T0, true);
check!(T1, true);
check!(T2, true);
check!(T3, true);
check!(T4, true);
check!(T5, true);
check!(T6, true);
check!(T7, true);
check!(T8, true);
check!(T9, true);

check!(F0, false);
check!(F1, false);
check!(F2, false);
check!(F3, false);
check!(F4, false);
check!(F5, false);
check!(F6, false);
check!(F7, false);
check!(F8, false);
check!(F9, false);
}
Loading