Skip to content

Commit 035198b

Browse files
nikicalexcrichton
authored andcommitted
Fix alignment of stores to scalar pair
The alignment for the second element of a scalar pair is not the same as for the first element. Make sure it is computed correctly based on the element size.
1 parent 04032d8 commit 035198b

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/librustc_codegen_llvm/mir/operand.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,21 @@ impl OperandValue<'ll> {
294294
bx.store_with_flags(val, dest.llval, dest.align, flags);
295295
}
296296
OperandValue::Pair(a, b) => {
297-
for (i, &x) in [a, b].iter().enumerate() {
298-
let llptr = bx.struct_gep(dest.llval, i as u64);
299-
let val = base::from_immediate(bx, x);
300-
bx.store_with_flags(val, llptr, dest.align, flags);
301-
}
297+
let (a_scalar, b_scalar) = match dest.layout.abi {
298+
layout::Abi::ScalarPair(ref a, ref b) => (a, b),
299+
_ => bug!("store_with_flags: invalid ScalarPair layout: {:#?}", dest.layout)
300+
};
301+
let b_offset = a_scalar.value.size(bx.cx).abi_align(b_scalar.value.align(bx.cx));
302+
303+
let llptr = bx.struct_gep(dest.llval, 0);
304+
let val = base::from_immediate(bx, a);
305+
let align = dest.align;
306+
bx.store_with_flags(val, llptr, align, flags);
307+
308+
let llptr = bx.struct_gep(dest.llval, 1);
309+
let val = base::from_immediate(bx, b);
310+
let align = dest.align.restrict_for_offset(b_offset);
311+
bx.store_with_flags(val, llptr, align, flags);
302312
}
303313
}
304314
}

src/test/codegen/issue-56267.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// compile-flags: -C no-prepopulate-passes
2+
3+
#![crate_type="rlib"]
4+
5+
#[allow(dead_code)]
6+
pub struct Foo<T> {
7+
foo: u64,
8+
bar: T,
9+
}
10+
11+
// The store writing to bar.1 should have alignment 4. Not checking
12+
// other stores here, as the alignment will be platform-dependent.
13+
14+
// CHECK: store i32 [[TMP1:%.+]], i32* [[TMP2:%.+]], align 4
15+
#[no_mangle]
16+
pub fn test(x: (i32, i32)) -> Foo<(i32, i32)> {
17+
Foo { foo: 0, bar: x }
18+
}

0 commit comments

Comments
 (0)