|
| 1 | +//! This file tests that we correctly generate GEP instructions for DST |
| 2 | +//! field offsets. |
| 3 | +//@ compile-flags: -C no-prepopulate-passes -Copt-level=0 |
| 4 | + |
| 5 | +#![crate_type = "lib"] |
| 6 | + |
| 7 | +#![feature(extern_types)] |
| 8 | + |
| 9 | +use std::ptr::addr_of; |
| 10 | + |
| 11 | +// Hack to get the correct type for usize |
| 12 | +// CHECK: @helper([[USIZE:i[0-9]+]] %_1) |
| 13 | +#[no_mangle] |
| 14 | +pub fn helper(_: usize) { |
| 15 | +} |
| 16 | + |
| 17 | +struct Dst<T: ?Sized> { |
| 18 | + x: u32, |
| 19 | + y: u8, |
| 20 | + z: T, |
| 21 | +} |
| 22 | + |
| 23 | +// CHECK: @dst_dyn_trait_offset(ptr align {{[0-9]+}} [[DATA_PTR:%.+]], ptr align {{[0-9]+}} [[VTABLE_PTR:%.+]]) |
| 24 | +#[no_mangle] |
| 25 | +pub fn dst_dyn_trait_offset(s: &Dst<dyn Drop>) -> &dyn Drop { |
| 26 | +// The alignment of dyn trait is unknown, so we compute the offset based on align from the vtable. |
| 27 | + |
| 28 | +// CHECK: [[SIZE_PTR:%[0-9]+]] = getelementptr inbounds {{.+}} [[VTABLE_PTR]] |
| 29 | +// CHECK: load [[USIZE]], ptr [[SIZE_PTR]] |
| 30 | +// CHECK: [[ALIGN_PTR:%[0-9]+]] = getelementptr inbounds {{.+}} [[VTABLE_PTR]] |
| 31 | +// CHECK: load [[USIZE]], ptr [[ALIGN_PTR]] |
| 32 | + |
| 33 | +// CHECK: getelementptr inbounds i8, ptr [[DATA_PTR]] |
| 34 | +// CHECK-NEXT: insertvalue |
| 35 | +// CHECK-NEXT: insertvalue |
| 36 | +// CHECK-NEXT: ret |
| 37 | + &s.z |
| 38 | +} |
| 39 | + |
| 40 | +// CHECK-LABEL: @dst_slice_offset |
| 41 | +#[no_mangle] |
| 42 | +pub fn dst_slice_offset(s: &Dst<[u16]>) -> &[u16] { |
| 43 | +// The alignment of [u16] is known, so we generate a GEP directly. |
| 44 | + |
| 45 | +// CHECK: start: |
| 46 | +// CHECK-NEXT: getelementptr inbounds i8, {{.+}}, [[USIZE]] 6 |
| 47 | +// CHECK-NEXT: insertvalue |
| 48 | +// CHECK-NEXT: insertvalue |
| 49 | +// CHECK-NEXT: ret |
| 50 | + &s.z |
| 51 | +} |
| 52 | + |
| 53 | +#[repr(packed)] |
| 54 | +struct PackedDstSlice { |
| 55 | + x: u32, |
| 56 | + y: u8, |
| 57 | + z: [u16], |
| 58 | +} |
| 59 | + |
| 60 | +// CHECK-LABEL: @packed_dst_slice_offset |
| 61 | +#[no_mangle] |
| 62 | +pub fn packed_dst_slice_offset(s: &PackedDstSlice) -> *const [u16] { |
| 63 | +// The alignment of [u16] is known, so we generate a GEP directly. |
| 64 | + |
| 65 | +// CHECK: start: |
| 66 | +// CHECK-NEXT: getelementptr inbounds i8, {{.+}}, [[USIZE]] 5 |
| 67 | +// CHECK-NEXT: insertvalue |
| 68 | +// CHECK-NEXT: insertvalue |
| 69 | +// CHECK-NEXT: ret |
| 70 | + addr_of!(s.z) |
| 71 | +} |
| 72 | + |
| 73 | +extern { |
| 74 | + pub type Extern; |
| 75 | +} |
| 76 | + |
| 77 | +// CHECK-LABEL: @dst_extern |
| 78 | +#[no_mangle] |
| 79 | +pub fn dst_extern(s: &Dst<Extern>) -> &Extern { |
| 80 | +// Computing the alignment of an extern type is currently unsupported and just panics. |
| 81 | + |
| 82 | +// CHECK: call void @{{.+}}panic |
| 83 | + &s.z |
| 84 | +} |
0 commit comments