Skip to content

Commit 1abbf61

Browse files
committed
Auto merge of rust-lang#121665 - erikdesjardins:ptradd, r=nikic
Always generate GEP i8 / ptradd for struct offsets This implements rust-lang#98615, and goes a bit further to remove `struct_gep` entirely. Upstream LLVM is in the beginning stages of [migrating to `ptradd`](https://discourse.llvm.org/t/rfc-replacing-getelementptr-with-ptradd/68699). LLVM 19 will [canonicalize](llvm/llvm-project#68882) all constant-offset GEPs to i8, which has roughly the same effect as this change. Fixes rust-lang#121719. Split out from rust-lang#121577. r? `@nikic`
2 parents 27b669a + 70346fe commit 1abbf61

File tree

2 files changed

+5
-52
lines changed

2 files changed

+5
-52
lines changed

src/builder.rs

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -834,10 +834,13 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
834834
}
835835
else if let abi::Abi::ScalarPair(ref a, ref b) = place.layout.abi {
836836
let b_offset = a.size(self).align_to(b.align(self).abi);
837-
let pair_type = place.layout.gcc_type(self);
838837

839838
let mut load = |i, scalar: &abi::Scalar, align| {
840-
let llptr = self.struct_gep(pair_type, place.llval, i as u64);
839+
let llptr = if i == 0 {
840+
place.llval
841+
} else {
842+
self.inbounds_ptradd(place.llval, self.const_usize(b_offset.bytes()))
843+
};
841844
let llty = place.layout.scalar_pair_element_gcc_type(self, i);
842845
let load = self.load(llty, llptr, align);
843846
scalar_load_metadata(self, load, scalar);
@@ -971,33 +974,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
971974
result.get_address(None)
972975
}
973976

974-
fn struct_gep(&mut self, value_type: Type<'gcc>, ptr: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
975-
// FIXME(antoyo): it would be better if the API only called this on struct, not on arrays.
976-
assert_eq!(idx as usize as u64, idx);
977-
let value = ptr.dereference(None).to_rvalue();
978-
979-
if value_type.dyncast_array().is_some() {
980-
let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
981-
let element = self.context.new_array_access(None, value, index);
982-
element.get_address(None)
983-
}
984-
else if let Some(vector_type) = value_type.dyncast_vector() {
985-
let array_type = vector_type.get_element_type().make_pointer();
986-
let array = self.bitcast(ptr, array_type);
987-
let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
988-
let element = self.context.new_array_access(None, array, index);
989-
element.get_address(None)
990-
}
991-
else if let Some(struct_type) = value_type.is_struct() {
992-
// NOTE: due to opaque pointers now being used, we need to bitcast here.
993-
let ptr = self.bitcast_if_needed(ptr, value_type.make_pointer());
994-
ptr.dereference_field(None, struct_type.get_field(idx as i32)).get_address(None)
995-
}
996-
else {
997-
panic!("Unexpected type {:?}", value_type);
998-
}
999-
}
1000-
1001977
/* Casts */
1002978
fn trunc(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1003979
// TODO(antoyo): check that it indeed truncate the value.

src/type_of.rs

Lines changed: 0 additions & 23 deletions
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

@@ -306,24 +305,6 @@ impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
306305
self.scalar_gcc_type_at(cx, scalar, offset)
307306
}
308307

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

356-
fn backend_field_index(&self, layout: TyAndLayout<'tcx>, index: usize) -> u64 {
357-
layout.gcc_field_index(index)
358-
}
359-
360337
fn scalar_pair_element_backend_type(&self, layout: TyAndLayout<'tcx>, index: usize, _immediate: bool) -> Type<'gcc> {
361338
layout.scalar_pair_element_gcc_type(self, index)
362339
}

0 commit comments

Comments
 (0)