Skip to content

Commit 553ae22

Browse files
committed
const eval: respect target.min_global_align
1 parent 845b15d commit 553ae22

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
893893

894894
// # Global allocations
895895
if let Some(global_alloc) = self.tcx.try_get_global_alloc(id) {
896-
let (size, align) = global_alloc.size_and_align(*self.tcx, self.typing_env);
896+
let (size, mut align) = global_alloc.size_and_align(*self.tcx, self.typing_env);
897+
if let Some(min_global_align) = self.tcx.sess.target.min_global_align {
898+
align = Ord::max(align, min_global_align);
899+
}
897900
let mutbl = global_alloc.mutability(*self.tcx, self.typing_env);
898901
let kind = match global_alloc {
899902
GlobalAlloc::Static { .. } | GlobalAlloc::Memory { .. } => AllocKind::LiveData,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Test that miri respects the `target.min_global_align` value for the target.
2+
//
3+
// The only way to observe its effect currently is to test for the alignment of statics with a
4+
// natural alignment of 1 on s390x. On that target, the `min_global_align` is 2 bytes.
5+
use core::ptr;
6+
7+
static T0: bool = true;
8+
static T1: bool = true;
9+
static T2: bool = true;
10+
static T3: bool = true;
11+
static T4: bool = true;
12+
static T5: bool = true;
13+
static T6: bool = true;
14+
static T7: bool = true;
15+
static T8: bool = true;
16+
static T9: bool = true;
17+
18+
static F0: bool = false;
19+
static F1: bool = false;
20+
static F2: bool = false;
21+
static F3: bool = false;
22+
static F4: bool = false;
23+
static F5: bool = false;
24+
static F6: bool = false;
25+
static F7: bool = false;
26+
static F8: bool = false;
27+
static F9: bool = false;
28+
29+
fn main() {
30+
let min_align = if cfg!(target_arch = "s390x") { 2 } else { 1 };
31+
32+
assert!(ptr::from_ref(&T0).addr().is_multiple_of(min_align));
33+
assert!(ptr::from_ref(&T1).addr().is_multiple_of(min_align));
34+
assert!(ptr::from_ref(&T2).addr().is_multiple_of(min_align));
35+
assert!(ptr::from_ref(&T3).addr().is_multiple_of(min_align));
36+
assert!(ptr::from_ref(&T4).addr().is_multiple_of(min_align));
37+
assert!(ptr::from_ref(&T5).addr().is_multiple_of(min_align));
38+
assert!(ptr::from_ref(&T6).addr().is_multiple_of(min_align));
39+
assert!(ptr::from_ref(&T7).addr().is_multiple_of(min_align));
40+
assert!(ptr::from_ref(&T8).addr().is_multiple_of(min_align));
41+
assert!(ptr::from_ref(&T9).addr().is_multiple_of(min_align));
42+
43+
assert!(ptr::from_ref(&F0).addr().is_multiple_of(min_align));
44+
assert!(ptr::from_ref(&F1).addr().is_multiple_of(min_align));
45+
assert!(ptr::from_ref(&F2).addr().is_multiple_of(min_align));
46+
assert!(ptr::from_ref(&F3).addr().is_multiple_of(min_align));
47+
assert!(ptr::from_ref(&F4).addr().is_multiple_of(min_align));
48+
assert!(ptr::from_ref(&F5).addr().is_multiple_of(min_align));
49+
assert!(ptr::from_ref(&F6).addr().is_multiple_of(min_align));
50+
assert!(ptr::from_ref(&F7).addr().is_multiple_of(min_align));
51+
assert!(ptr::from_ref(&F8).addr().is_multiple_of(min_align));
52+
assert!(ptr::from_ref(&F9).addr().is_multiple_of(min_align));
53+
}

0 commit comments

Comments
 (0)