Skip to content

Commit 24e0913

Browse files
committedAug 30, 2020
handle vector layout
1 parent 6fc1d8b commit 24e0913

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed
 

‎compiler/rustc_codegen_ssa/src/mir/place.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
9696
let llval = match self.layout.abi {
9797
_ if offset.bytes() == 0 => {
9898
// Unions and newtypes only use an offset of 0.
99-
// Also handles the first field of Scalar and ScalarPair layouts.
99+
// Also handles the first field of Scalar, ScalarPair, and Vector layouts.
100100
self.llval
101101
}
102102
Abi::ScalarPair(ref a, ref b)
@@ -105,16 +105,20 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
105105
// Offset matches second field.
106106
bx.struct_gep(self.llval, 1)
107107
}
108-
Abi::ScalarPair(..) | Abi::Scalar(_) => {
109-
// ZST fields are not included in Scalar and ScalarPair layouts, so manually offset the pointer.
110-
assert!(
111-
field.is_zst(),
112-
"non-ZST field offset does not match layout: {:?}",
113-
field
114-
);
108+
Abi::Scalar(_) | Abi::ScalarPair(..) | Abi::Vector { .. } if field.is_zst() => {
109+
// ZST fields are not included in Scalar, ScalarPair, and Vector layouts, so manually offset the pointer.
115110
let byte_ptr = bx.pointercast(self.llval, bx.cx().type_i8p());
116111
bx.gep(byte_ptr, &[bx.const_usize(offset.bytes())])
117112
}
113+
Abi::Scalar(_) | Abi::ScalarPair(..) => {
114+
// All fields of Scalar and ScalarPair layouts must have been handled by this point.
115+
// Vector layouts have additional fields for each element of the vector, so don't panic in that case.
116+
bug!(
117+
"offset of non-ZST field `{:?}` does not match layout `{:#?}`",
118+
field,
119+
self.layout
120+
);
121+
}
118122
_ => bx.struct_gep(self.llval, bx.cx().backend_field_index(self.layout, ix)),
119123
};
120124
PlaceRef {

‎src/test/codegen/zst-offset.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// compile-flags: -C no-prepopulate-passes
22

33
#![crate_type = "lib"]
4+
#![feature(repr_simd)]
45

56
// Hack to get the correct size for the length part in slices
67
// CHECK: @helper([[USIZE:i[0-9]+]] %_1)
@@ -27,3 +28,16 @@ pub fn scalarpair_layout(s: &(u64, u32, ())) {
2728
let x = &s.2;
2829
&x; // keep variable in an alloca
2930
}
31+
32+
#[repr(simd)]
33+
pub struct U64x4(u64, u64, u64, u64);
34+
35+
// Check that we correctly generate a GEP for a ZST that is not included in Vector layout
36+
// CHECK-LABEL: @vector_layout
37+
#[no_mangle]
38+
pub fn vector_layout(s: &(U64x4, ())) {
39+
// CHECK: [[X0:%[0-9]+]] = bitcast <4 x i64>* %s to i8*
40+
// CHECK-NEXT: [[X1:%[0-9]+]] = getelementptr i8, i8* [[X0]], [[USIZE]] 32
41+
let x = &s.1;
42+
&x; // keep variable in an alloca
43+
}

0 commit comments

Comments
 (0)