Skip to content

Commit

Permalink
handle vector layout
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdesjardins committed Aug 30, 2020
1 parent 6fc1d8b commit 24e0913
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
20 changes: 12 additions & 8 deletions compiler/rustc_codegen_ssa/src/mir/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
let llval = match self.layout.abi {
_ if offset.bytes() == 0 => {
// Unions and newtypes only use an offset of 0.
// Also handles the first field of Scalar and ScalarPair layouts.
// Also handles the first field of Scalar, ScalarPair, and Vector layouts.
self.llval
}
Abi::ScalarPair(ref a, ref b)
Expand All @@ -105,16 +105,20 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
// Offset matches second field.
bx.struct_gep(self.llval, 1)
}
Abi::ScalarPair(..) | Abi::Scalar(_) => {
// ZST fields are not included in Scalar and ScalarPair layouts, so manually offset the pointer.
assert!(
field.is_zst(),
"non-ZST field offset does not match layout: {:?}",
field
);
Abi::Scalar(_) | Abi::ScalarPair(..) | Abi::Vector { .. } if field.is_zst() => {
// ZST fields are not included in Scalar, ScalarPair, and Vector layouts, so manually offset the pointer.
let byte_ptr = bx.pointercast(self.llval, bx.cx().type_i8p());
bx.gep(byte_ptr, &[bx.const_usize(offset.bytes())])
}
Abi::Scalar(_) | Abi::ScalarPair(..) => {
// All fields of Scalar and ScalarPair layouts must have been handled by this point.
// Vector layouts have additional fields for each element of the vector, so don't panic in that case.
bug!(
"offset of non-ZST field `{:?}` does not match layout `{:#?}`",
field,
self.layout
);
}
_ => bx.struct_gep(self.llval, bx.cx().backend_field_index(self.layout, ix)),
};
PlaceRef {
Expand Down
14 changes: 14 additions & 0 deletions src/test/codegen/zst-offset.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// compile-flags: -C no-prepopulate-passes

#![crate_type = "lib"]
#![feature(repr_simd)]

// Hack to get the correct size for the length part in slices
// CHECK: @helper([[USIZE:i[0-9]+]] %_1)
Expand All @@ -27,3 +28,16 @@ pub fn scalarpair_layout(s: &(u64, u32, ())) {
let x = &s.2;
&x; // keep variable in an alloca
}

#[repr(simd)]
pub struct U64x4(u64, u64, u64, u64);

// Check that we correctly generate a GEP for a ZST that is not included in Vector layout
// CHECK-LABEL: @vector_layout
#[no_mangle]
pub fn vector_layout(s: &(U64x4, ())) {
// CHECK: [[X0:%[0-9]+]] = bitcast <4 x i64>* %s to i8*
// CHECK-NEXT: [[X1:%[0-9]+]] = getelementptr i8, i8* [[X0]], [[USIZE]] 32
let x = &s.1;
&x; // keep variable in an alloca
}

0 comments on commit 24e0913

Please sign in to comment.