Skip to content

Commit 123015e

Browse files
always use gep inbounds i8 (ptradd) for field offsets
1 parent 71ffdf7 commit 123015e

File tree

8 files changed

+11
-63
lines changed

8 files changed

+11
-63
lines changed

compiler/rustc_codegen_gcc/src/type_of.rs

-23
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ pub trait LayoutGccExt<'tcx> {
151151
fn immediate_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
152152
fn scalar_gcc_type_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, scalar: &abi::Scalar, offset: Size) -> Type<'gcc>;
153153
fn scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize) -> Type<'gcc>;
154-
fn gcc_field_index(&self, index: usize) -> u64;
155154
fn pointee_info_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, offset: Size) -> Option<PointeeInfo>;
156155
}
157156

@@ -304,24 +303,6 @@ impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
304303
self.scalar_gcc_type_at(cx, scalar, offset)
305304
}
306305

307-
fn gcc_field_index(&self, index: usize) -> u64 {
308-
match self.abi {
309-
Abi::Scalar(_) | Abi::ScalarPair(..) => {
310-
bug!("TyAndLayout::gcc_field_index({:?}): not applicable", self)
311-
}
312-
_ => {}
313-
}
314-
match self.fields {
315-
FieldsShape::Primitive | FieldsShape::Union(_) => {
316-
bug!("TyAndLayout::gcc_field_index({:?}): not applicable", self)
317-
}
318-
319-
FieldsShape::Array { .. } => index as u64,
320-
321-
FieldsShape::Arbitrary { .. } => 1 + (self.fields.memory_index(index) as u64) * 2,
322-
}
323-
}
324-
325306
fn pointee_info_at<'a>(&self, cx: &CodegenCx<'a, 'tcx>, offset: Size) -> Option<PointeeInfo> {
326307
if let Some(&pointee) = cx.pointee_infos.borrow().get(&(self.ty, offset)) {
327308
return pointee;
@@ -351,10 +332,6 @@ impl<'gcc, 'tcx> LayoutTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
351332
layout.is_gcc_scalar_pair()
352333
}
353334

354-
fn backend_field_index(&self, layout: TyAndLayout<'tcx>, index: usize) -> u64 {
355-
layout.gcc_field_index(index)
356-
}
357-
358335
fn scalar_pair_element_backend_type(&self, layout: TyAndLayout<'tcx>, index: usize, _immediate: bool) -> Type<'gcc> {
359336
layout.scalar_pair_element_gcc_type(self, index)
360337
}

compiler/rustc_codegen_llvm/src/type_.rs

-3
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,6 @@ impl<'ll, 'tcx> LayoutTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
250250
fn is_backend_scalar_pair(&self, layout: TyAndLayout<'tcx>) -> bool {
251251
layout.is_llvm_scalar_pair()
252252
}
253-
fn backend_field_index(&self, layout: TyAndLayout<'tcx>, index: usize) -> u64 {
254-
layout.llvm_field_index(self, index)
255-
}
256253
fn scalar_pair_element_backend_type(
257254
&self,
258255
layout: TyAndLayout<'tcx>,

compiler/rustc_codegen_ssa/src/mir/place.rs

+5-29
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::mir;
99
use rustc_middle::mir::tcx::PlaceTy;
1010
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
1111
use rustc_middle::ty::{self, Ty};
12-
use rustc_target::abi::{Abi, Align, FieldsShape, Int, Pointer, TagEncoding};
12+
use rustc_target::abi::{Align, FieldsShape, Int, Pointer, TagEncoding};
1313
use rustc_target::abi::{VariantIdx, Variants};
1414

1515
#[derive(Copy, Clone, Debug)]
@@ -102,34 +102,10 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
102102
// `simple` is called when we don't need to adjust the offset to
103103
// the dynamic alignment of the field.
104104
let mut simple = || {
105-
let llval = match self.layout.abi {
106-
_ if offset.bytes() == 0 => {
107-
// Unions and newtypes only use an offset of 0.
108-
// Also handles the first field of Scalar, ScalarPair, and Vector layouts.
109-
self.llval
110-
}
111-
Abi::ScalarPair(..) => {
112-
// FIXME(nikic): Generate this for all ABIs.
113-
bx.inbounds_gep(bx.type_i8(), self.llval, &[bx.const_usize(offset.bytes())])
114-
}
115-
Abi::Scalar(_) | Abi::Vector { .. } if field.is_zst() => {
116-
// ZST fields (even some that require alignment) are not included in Scalar,
117-
// ScalarPair, and Vector layouts, so manually offset the pointer.
118-
bx.gep(bx.cx().type_i8(), self.llval, &[bx.const_usize(offset.bytes())])
119-
}
120-
Abi::Scalar(_) => {
121-
// All fields of Scalar layouts must have been handled by this point.
122-
// Vector layouts have additional fields for each element of the vector, so don't panic in that case.
123-
bug!(
124-
"offset of non-ZST field `{:?}` does not match layout `{:#?}`",
125-
field,
126-
self.layout
127-
);
128-
}
129-
_ => {
130-
let ty = bx.backend_type(self.layout);
131-
bx.struct_gep(ty, self.llval, bx.cx().backend_field_index(self.layout, ix))
132-
}
105+
let llval = if offset.bytes() == 0 {
106+
self.llval
107+
} else {
108+
bx.inbounds_gep(bx.type_i8(), self.llval, &[bx.const_usize(offset.bytes())])
133109
};
134110
PlaceRef {
135111
llval,

compiler/rustc_codegen_ssa/src/traits/type_.rs

-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ pub trait LayoutTypeMethods<'tcx>: Backend<'tcx> {
111111
fn immediate_backend_type(&self, layout: TyAndLayout<'tcx>) -> Self::Type;
112112
fn is_backend_immediate(&self, layout: TyAndLayout<'tcx>) -> bool;
113113
fn is_backend_scalar_pair(&self, layout: TyAndLayout<'tcx>) -> bool;
114-
fn backend_field_index(&self, layout: TyAndLayout<'tcx>, index: usize) -> u64;
115114
fn scalar_pair_element_backend_type(
116115
&self,
117116
layout: TyAndLayout<'tcx>,

tests/codegen/align-struct.rs

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub enum Enum64 {
2626
B(i32),
2727
}
2828
// CHECK: %Enum64 = type { i32, [31 x i32] }
29-
// CHECK: %"Enum64::A" = type { [8 x i64], %Align64 }
3029

3130
// CHECK-LABEL: @align64
3231
#[no_mangle]

tests/codegen/i128-x86-align.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ pub fn store_struct(x: &mut Struct) {
9494
// CHECK-SAME: align 16 dereferenceable(32) %x
9595
// CHECK: [[TMP:%.*]] = alloca %Struct, align 16
9696
// CHECK: store i32 1, ptr [[TMP]], align 16
97-
// CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds %Struct, ptr [[TMP]], i32 0, i32 1
97+
// CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i8, ptr [[TMP]], i64 4
9898
// CHECK-NEXT: store i32 2, ptr [[GEP1]], align 4
99-
// CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds %Struct, ptr [[TMP]], i32 0, i32 3
99+
// CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds i8, ptr [[TMP]], i64 16
100100
// CHECK-NEXT: store i128 3, ptr [[GEP2]], align 16
101101
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 %x, ptr align 16 [[TMP]], i64 32, i1 false)
102102
*x = Struct { a: 1, b: 2, c: 3 };

tests/codegen/issues/issue-105386-ub-in-debuginfo.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ pub fn outer_function(x: S, y: S) -> usize {
1616
// when generating debuginfo.
1717
// CHECK-LABEL: @outer_function
1818
// CHECK: [[spill:%.*]] = alloca %"{closure@{{.*.rs}}:9:23: 9:25}"
19-
// CHECK-NOT: [[ptr_tmp:%.*]] = getelementptr inbounds %"{closure@{{.*.rs}}:9:23: 9:25}", ptr [[spill]]
19+
// CHECK-NOT: [[ptr_tmp:%.*]] = getelementptr inbounds i8, ptr [[spill]]
2020
// CHECK-NOT: [[load:%.*]] = load ptr, ptr
2121
// CHECK: call void @llvm.lifetime.start{{.*}}({{.*}}, ptr [[spill]])
22-
// CHECK: [[inner:%.*]] = getelementptr inbounds %"{{.*}}", ptr [[spill]]
22+
// CHECK: [[inner:%.*]] = getelementptr inbounds i8, ptr [[spill]]
2323
// CHECK: call void @llvm.memcpy{{.*}}(ptr {{align .*}} [[inner]], ptr {{align .*}} %x

tests/codegen/zst-offset.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn helper(_: usize) {
1313
// CHECK-LABEL: @scalar_layout
1414
#[no_mangle]
1515
pub fn scalar_layout(s: &(u64, ())) {
16-
// CHECK: getelementptr i8, {{.+}}, [[USIZE]] 8
16+
// CHECK: getelementptr inbounds i8, {{.+}}, [[USIZE]] 8
1717
let x = &s.1;
1818
witness(&x); // keep variable in an alloca
1919
}
@@ -34,7 +34,7 @@ pub struct U64x4(u64, u64, u64, u64);
3434
// CHECK-LABEL: @vector_layout
3535
#[no_mangle]
3636
pub fn vector_layout(s: &(U64x4, ())) {
37-
// CHECK: getelementptr i8, {{.+}}, [[USIZE]] 32
37+
// CHECK: getelementptr inbounds i8, {{.+}}, [[USIZE]] 32
3838
let x = &s.1;
3939
witness(&x); // keep variable in an alloca
4040
}

0 commit comments

Comments
 (0)