Skip to content

Commit 4013e09

Browse files
committed
Prepare gep for opaque pointers
Implement gep using LLVMBuildGEP2 which takes an explicit type argument instead of deriving it from a pointer type.
1 parent 838042a commit 4013e09

File tree

6 files changed

+13
-9
lines changed

6 files changed

+13
-9
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -640,10 +640,11 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
640640
}
641641
}
642642

643-
fn gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
643+
fn gep(&mut self, ty: &'ll Type, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
644644
unsafe {
645-
llvm::LLVMBuildGEP(
645+
llvm::LLVMBuildGEP2(
646646
self.llbuilder,
647+
ty,
647648
ptr,
648649
indices.as_ptr(),
649650
indices.len() as c_uint,

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1394,8 +1394,9 @@ extern "C" {
13941394

13951395
pub fn LLVMBuildStore(B: &Builder<'a>, Val: &'a Value, Ptr: &'a Value) -> &'a Value;
13961396

1397-
pub fn LLVMBuildGEP(
1397+
pub fn LLVMBuildGEP2(
13981398
B: &Builder<'a>,
1399+
Ty: &'a Type,
13991400
Pointer: &'a Value,
14001401
Indices: *const &'a Value,
14011402
NumIndices: c_uint,

compiler/rustc_codegen_llvm/src/va_arg.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ fn emit_aapcs_va_arg(
146146
let top = in_reg.load(top_type, top, bx.tcx().data_layout.pointer_align.abi);
147147

148148
// reg_value = *(@top + reg_off_v);
149-
let mut reg_addr = in_reg.gep(top, &[reg_off_v]);
149+
let mut reg_addr = in_reg.gep(bx.type_i8(), top, &[reg_off_v]);
150150
if bx.tcx().sess.target.endian == Endian::Big && layout.size.bytes() != slot_size {
151151
// On big-endian systems the value is right-aligned in its slot.
152152
let offset = bx.const_i32((slot_size - layout.size.bytes()) as i32);
153-
reg_addr = in_reg.gep(reg_addr, &[offset]);
153+
reg_addr = in_reg.gep(bx.type_i8(), reg_addr, &[offset]);
154154
}
155155
let reg_type = layout.llvm_type(bx);
156156
let reg_addr = in_reg.bitcast(reg_addr, bx.cx.type_ptr_to(reg_type));

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
121121
bx.inbounds_gep(ptr, &[offset])
122122
}
123123
sym::arith_offset => {
124+
let ty = substs.type_at(0);
125+
let layout = bx.layout_of(ty);
124126
let ptr = args[0].immediate();
125127
let offset = args[1].immediate();
126-
bx.gep(ptr, &[offset])
128+
bx.gep(bx.backend_type(layout), ptr, &[offset])
127129
}
128130
sym::copy => {
129131
copy_intrinsic(

compiler/rustc_codegen_ssa/src/mir/place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
109109
Abi::Scalar(_) | Abi::ScalarPair(..) | Abi::Vector { .. } if field.is_zst() => {
110110
// ZST fields are not included in Scalar, ScalarPair, and Vector layouts, so manually offset the pointer.
111111
let byte_ptr = bx.pointercast(self.llval, bx.cx().type_i8p());
112-
bx.gep(byte_ptr, &[bx.const_usize(offset.bytes())])
112+
bx.gep(bx.cx().type_i8(), byte_ptr, &[bx.const_usize(offset.bytes())])
113113
}
114114
Abi::Scalar(_) | Abi::ScalarPair(..) => {
115115
// All fields of Scalar and ScalarPair layouts must have been handled by this point.
@@ -189,7 +189,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
189189

190190
// Cast and adjust pointer.
191191
let byte_ptr = bx.pointercast(self.llval, bx.cx().type_i8p());
192-
let byte_ptr = bx.gep(byte_ptr, &[offset]);
192+
let byte_ptr = bx.gep(bx.cx().type_i8(), byte_ptr, &[offset]);
193193

194194
// Finally, cast back to the type expected.
195195
let ll_fty = bx.cx().backend_type(field);

compiler/rustc_codegen_ssa/src/traits/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ pub trait BuilderMethods<'a, 'tcx>:
176176
size: Size,
177177
);
178178

179-
fn gep(&mut self, ptr: Self::Value, indices: &[Self::Value]) -> Self::Value;
179+
fn gep(&mut self, ty: Self::Type, ptr: Self::Value, indices: &[Self::Value]) -> Self::Value;
180180
fn inbounds_gep(&mut self, ptr: Self::Value, indices: &[Self::Value]) -> Self::Value;
181181
fn struct_gep(&mut self, ty: Self::Type, ptr: Self::Value, idx: u64) -> Self::Value;
182182

0 commit comments

Comments
 (0)