Skip to content

Commit 7b8bcdd

Browse files
committed
const eval: respect target.min_global_align
1 parent 845b15d commit 7b8bcdd

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
6+
static T0: bool = true;
7+
static T1: bool = true;
8+
static T2: bool = true;
9+
static T3: bool = true;
10+
static T4: bool = true;
11+
static T5: bool = true;
12+
static T6: bool = true;
13+
static T7: bool = true;
14+
static T8: bool = true;
15+
static T9: bool = true;
16+
17+
static F0: bool = false;
18+
static F1: bool = false;
19+
static F2: bool = false;
20+
static F3: bool = false;
21+
static F4: bool = false;
22+
static F5: bool = false;
23+
static F6: bool = false;
24+
static F7: bool = false;
25+
static F8: bool = false;
26+
static F9: bool = false;
27+
28+
fn main() {
29+
let min_align = if cfg!(target_arch = "s390x") { 2 } else { 1 };
30+
31+
assert!(((&T0) as *const bool).addr().is_multiple_of(min_align));
32+
assert!(((&T1) as *const bool).addr().is_multiple_of(min_align));
33+
assert!(((&T2) as *const bool).addr().is_multiple_of(min_align));
34+
assert!(((&T3) as *const bool).addr().is_multiple_of(min_align));
35+
assert!(((&T4) as *const bool).addr().is_multiple_of(min_align));
36+
assert!(((&T5) as *const bool).addr().is_multiple_of(min_align));
37+
assert!(((&T6) as *const bool).addr().is_multiple_of(min_align));
38+
assert!(((&T7) as *const bool).addr().is_multiple_of(min_align));
39+
assert!(((&T8) as *const bool).addr().is_multiple_of(min_align));
40+
assert!(((&T9) as *const bool).addr().is_multiple_of(min_align));
41+
42+
assert!(((&F0) as *const bool).addr().is_multiple_of(min_align));
43+
assert!(((&F1) as *const bool).addr().is_multiple_of(min_align));
44+
assert!(((&F2) as *const bool).addr().is_multiple_of(min_align));
45+
assert!(((&F3) as *const bool).addr().is_multiple_of(min_align));
46+
assert!(((&F4) as *const bool).addr().is_multiple_of(min_align));
47+
assert!(((&F5) as *const bool).addr().is_multiple_of(min_align));
48+
assert!(((&F6) as *const bool).addr().is_multiple_of(min_align));
49+
assert!(((&F7) as *const bool).addr().is_multiple_of(min_align));
50+
assert!(((&F8) as *const bool).addr().is_multiple_of(min_align));
51+
assert!(((&F9) as *const bool).addr().is_multiple_of(min_align));
52+
}

0 commit comments

Comments
 (0)