Skip to content

Commit 77e5e17

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

File tree

11 files changed

+61
-22
lines changed

11 files changed

+61
-22
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,11 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
544544
.val
545545
.store(&mut body_bx, PlaceRef::new_sized_aligned(current, cg_elem.layout, align));
546546

547-
let next = body_bx.inbounds_gep(current, &[self.const_usize(1)]);
547+
let next = body_bx.inbounds_gep(
548+
self.backend_type(cg_elem.layout),
549+
current,
550+
&[self.const_usize(1)],
551+
);
548552
body_bx.br(header_bx.llbb());
549553
header_bx.add_incoming_to_phi(current, next, body_bx.llbb());
550554

@@ -653,10 +657,16 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
653657
}
654658
}
655659

656-
fn inbounds_gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
660+
fn inbounds_gep(
661+
&mut self,
662+
ty: &'ll Type,
663+
ptr: &'ll Value,
664+
indices: &[&'ll Value],
665+
) -> &'ll Value {
657666
unsafe {
658-
llvm::LLVMBuildInBoundsGEP(
667+
llvm::LLVMBuildInBoundsGEP2(
659668
self.llbuilder,
669+
ty,
660670
ptr,
661671
indices.as_ptr(),
662672
indices.len() as c_uint,

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ use rustc_span::symbol::sym;
1515
/// .debug_gdb_scripts global is referenced, so it isn't removed by the linker.
1616
pub fn insert_reference_to_gdb_debug_scripts_section_global(bx: &mut Builder<'_, '_, '_>) {
1717
if needs_gdb_debug_scripts_section(bx) {
18-
let gdb_debug_scripts_section = get_or_insert_gdb_debug_scripts_section_global(bx);
18+
let gdb_debug_scripts_section =
19+
bx.const_bitcast(get_or_insert_gdb_debug_scripts_section_global(bx), bx.type_i8p());
1920
// Load just the first byte as that's all that's necessary to force
2021
// LLVM to keep around the reference to the global.
21-
let indices = [bx.const_i32(0), bx.const_i32(0)];
22-
let element = bx.inbounds_gep(gdb_debug_scripts_section, &indices);
23-
let volative_load_instruction = bx.volatile_load(bx.type_i8(), element);
22+
let volative_load_instruction = bx.volatile_load(bx.type_i8(), gdb_debug_scripts_section);
2423
unsafe {
2524
llvm::LLVMSetAlignment(volative_load_instruction, 1);
2625
}

compiler/rustc_codegen_llvm/src/intrinsic.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -686,11 +686,19 @@ fn codegen_emcc_try(
686686
// create an alloca and pass a pointer to that.
687687
let ptr_align = bx.tcx().data_layout.pointer_align.abi;
688688
let i8_align = bx.tcx().data_layout.i8_align.abi;
689-
let catch_data =
690-
catch.alloca(bx.type_struct(&[bx.type_i8p(), bx.type_bool()], false), ptr_align);
691-
let catch_data_0 = catch.inbounds_gep(catch_data, &[bx.const_usize(0), bx.const_usize(0)]);
689+
let catch_data_type = bx.type_struct(&[bx.type_i8p(), bx.type_bool()], false);
690+
let catch_data = catch.alloca(catch_data_type, ptr_align);
691+
let catch_data_0 = catch.inbounds_gep(
692+
catch_data_type,
693+
catch_data,
694+
&[bx.const_usize(0), bx.const_usize(0)],
695+
);
692696
catch.store(ptr, catch_data_0, ptr_align);
693-
let catch_data_1 = catch.inbounds_gep(catch_data, &[bx.const_usize(0), bx.const_usize(1)]);
697+
let catch_data_1 = catch.inbounds_gep(
698+
catch_data_type,
699+
catch_data,
700+
&[bx.const_usize(0), bx.const_usize(1)],
701+
);
694702
catch.store(is_rust_panic, catch_data_1, i8_align);
695703
let catch_data = catch.bitcast(catch_data, bx.type_i8p());
696704

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1402,8 +1402,9 @@ extern "C" {
14021402
NumIndices: c_uint,
14031403
Name: *const c_char,
14041404
) -> &'a Value;
1405-
pub fn LLVMBuildInBoundsGEP(
1405+
pub fn LLVMBuildInBoundsGEP2(
14061406
B: &Builder<'a>,
1407+
Ty: &'a Type,
14071408
Pointer: &'a Value,
14081409
Indices: *const &'a Value,
14091410
NumIndices: c_uint,

compiler/rustc_codegen_llvm/src/va_arg.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ fn emit_direct_ptr_va_arg(
5050

5151
let aligned_size = size.align_to(slot_size).bytes() as i32;
5252
let full_direct_size = bx.cx().const_i32(aligned_size);
53-
let next = bx.inbounds_gep(addr, &[full_direct_size]);
53+
let next = bx.inbounds_gep(bx.type_i8(), addr, &[full_direct_size]);
5454
bx.store(next, va_list_addr, bx.tcx().data_layout.pointer_align.abi);
5555

5656
if size.bytes() < slot_size.bytes() && bx.tcx().sess.target.endian == Endian::Big {
5757
let adjusted_size = bx.cx().const_i32((slot_size.bytes() - size.bytes()) as i32);
58-
let adjusted = bx.inbounds_gep(addr, &[adjusted_size]);
58+
let adjusted = bx.inbounds_gep(bx.type_i8(), addr, &[adjusted_size]);
5959
(bx.bitcast(adjusted, bx.cx().type_ptr_to(llty)), addr_align)
6060
} else {
6161
(bx.bitcast(addr, bx.cx().type_ptr_to(llty)), addr_align)

compiler/rustc_codegen_ssa/src/base.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,11 @@ pub fn unsized_info<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
168168
let ptr_ty = cx.type_i8p();
169169
let ptr_align = cx.tcx().data_layout.pointer_align.abi;
170170
let llvtable = bx.pointercast(old_info, bx.type_ptr_to(ptr_ty));
171-
let gep =
172-
bx.inbounds_gep(llvtable, &[bx.const_usize(u64::try_from(entry_idx).unwrap())]);
171+
let gep = bx.inbounds_gep(
172+
ptr_ty,
173+
llvtable,
174+
&[bx.const_usize(u64::try_from(entry_idx).unwrap())],
175+
);
173176
let new_vptr = bx.load(ptr_ty, gep, ptr_align);
174177
bx.nonnull_metadata(new_vptr);
175178
// Vtable loads are invariant.

compiler/rustc_codegen_ssa/src/meth.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl<'a, 'tcx> VirtualIndex {
2323
let llty = bx.fn_ptr_backend_type(fn_abi);
2424
let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(llty));
2525
let ptr_align = bx.tcx().data_layout.pointer_align.abi;
26-
let gep = bx.inbounds_gep(llvtable, &[bx.const_usize(self.0)]);
26+
let gep = bx.inbounds_gep(llty, llvtable, &[bx.const_usize(self.0)]);
2727
let ptr = bx.load(llty, gep, ptr_align);
2828
bx.nonnull_metadata(ptr);
2929
// Vtable loads are invariant.
@@ -42,7 +42,7 @@ impl<'a, 'tcx> VirtualIndex {
4242
let llty = bx.type_isize();
4343
let llvtable = bx.pointercast(llvtable, bx.type_ptr_to(llty));
4444
let usize_align = bx.tcx().data_layout.pointer_align.abi;
45-
let gep = bx.inbounds_gep(llvtable, &[bx.const_usize(self.0)]);
45+
let gep = bx.inbounds_gep(llty, llvtable, &[bx.const_usize(self.0)]);
4646
let ptr = bx.load(llty, gep, usize_align);
4747
// Vtable loads are invariant.
4848
bx.set_invariant_load(ptr);

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
116116
OperandRef::from_const(bx, value, ret_ty).immediate_or_packed_pair(bx)
117117
}
118118
sym::offset => {
119+
let ty = substs.type_at(0);
120+
let layout = bx.layout_of(ty);
119121
let ptr = args[0].immediate();
120122
let offset = args[1].immediate();
121-
bx.inbounds_gep(ptr, &[offset])
123+
bx.inbounds_gep(bx.backend_type(layout), ptr, &[offset])
122124
}
123125
sym::arith_offset => {
124126
let ty = substs.type_at(0);

compiler/rustc_codegen_ssa/src/mir/place.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,11 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
384384
};
385385

386386
PlaceRef {
387-
llval: bx.inbounds_gep(self.llval, &[bx.cx().const_usize(0), llindex]),
387+
llval: bx.inbounds_gep(
388+
bx.cx().backend_type(self.layout),
389+
self.llval,
390+
&[bx.cx().const_usize(0), llindex],
391+
),
388392
llextra: None,
389393
layout,
390394
align: self.align.restrict_for_offset(offset),

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
636636
mir::BinOp::BitOr => bx.or(lhs, rhs),
637637
mir::BinOp::BitAnd => bx.and(lhs, rhs),
638638
mir::BinOp::BitXor => bx.xor(lhs, rhs),
639-
mir::BinOp::Offset => bx.inbounds_gep(lhs, &[rhs]),
639+
mir::BinOp::Offset => {
640+
let pointee_type = input_ty
641+
.builtin_deref(true)
642+
.unwrap_or_else(|| bug!("deref of non-pointer {:?}", input_ty))
643+
.ty;
644+
let llty = bx.cx().backend_type(bx.cx().layout_of(pointee_type));
645+
bx.inbounds_gep(llty, lhs, &[rhs])
646+
}
640647
mir::BinOp::Shl => common::build_unchecked_lshift(bx, lhs, rhs),
641648
mir::BinOp::Shr => common::build_unchecked_rshift(bx, input_ty, lhs, rhs),
642649
mir::BinOp::Ne

compiler/rustc_codegen_ssa/src/traits/builder.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,12 @@ pub trait BuilderMethods<'a, 'tcx>:
177177
);
178178

179179
fn gep(&mut self, ty: Self::Type, ptr: Self::Value, indices: &[Self::Value]) -> Self::Value;
180-
fn inbounds_gep(&mut self, ptr: Self::Value, indices: &[Self::Value]) -> Self::Value;
180+
fn inbounds_gep(
181+
&mut self,
182+
ty: Self::Type,
183+
ptr: Self::Value,
184+
indices: &[Self::Value],
185+
) -> Self::Value;
181186
fn struct_gep(&mut self, ty: Self::Type, ptr: Self::Value, idx: u64) -> Self::Value;
182187

183188
fn trunc(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;

0 commit comments

Comments
 (0)