Skip to content

Commit

Permalink
Make a few more GEP's inbounds (#37040)
Browse files Browse the repository at this point in the history
Also add a few missing llvm eltypes for load and GEPs.
  • Loading branch information
yuyichao authored Aug 14, 2020
1 parent 03e1a89 commit 6de97d5
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 50 deletions.
10 changes: 5 additions & 5 deletions src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
JL_GC_POP();
ctx.builder.CreateCall(prepare_call(gcroot_flush_func));
emit_signal_fence(ctx);
ctx.builder.CreateLoad(ctx.signalPage, true);
ctx.builder.CreateLoad(T_size, ctx.signalPage, true);
emit_signal_fence(ctx);
return ghostValue(jl_nothing_type);
}
Expand All @@ -1582,7 +1582,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
JL_GC_POP();
Value *ptls_i16 = emit_bitcast(ctx, ctx.ptlsStates, T_pint16);
const int tid_offset = offsetof(jl_tls_states_t, tid);
Value *ptid = ctx.builder.CreateGEP(ptls_i16, ConstantInt::get(T_size, tid_offset / 2));
Value *ptid = ctx.builder.CreateInBoundsGEP(ptls_i16, ConstantInt::get(T_size, tid_offset / 2));
LoadInst *tid = ctx.builder.CreateAlignedLoad(ptid, sizeof(int16_t));
tbaa_decorate(tbaa_const, tid);
return mark_or_box_ccall_result(ctx, tid, retboxed, rt, unionall, static_rt);
Expand All @@ -1593,7 +1593,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
JL_GC_POP();
Value *ptls_pv = emit_bitcast(ctx, ctx.ptlsStates, T_pprjlvalue);
const int ct_offset = offsetof(jl_tls_states_t, current_task);
Value *pct = ctx.builder.CreateGEP(ptls_pv, ConstantInt::get(T_size, ct_offset / sizeof(void*)));
Value *pct = ctx.builder.CreateInBoundsGEP(ptls_pv, ConstantInt::get(T_size, ct_offset / sizeof(void*)));
LoadInst *ct = ctx.builder.CreateAlignedLoad(pct, sizeof(void*));
tbaa_decorate(tbaa_const, ct);
return mark_or_box_ccall_result(ctx, ct, retboxed, rt, unionall, static_rt);
Expand All @@ -1604,7 +1604,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
JL_GC_POP();
Value *ptls_pv = emit_bitcast(ctx, ctx.ptlsStates, T_ppjlvalue);
const int nt_offset = offsetof(jl_tls_states_t, next_task);
Value *pnt = ctx.builder.CreateGEP(ptls_pv, ConstantInt::get(T_size, nt_offset / sizeof(void*)));
Value *pnt = ctx.builder.CreateInBoundsGEP(ptls_pv, ConstantInt::get(T_size, nt_offset / sizeof(void*)));
ctx.builder.CreateStore(emit_pointer_from_objref(ctx, boxed(ctx, argv[0])), pnt);
return ghostValue(jl_nothing_type);
}
Expand Down Expand Up @@ -1643,7 +1643,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
checkBB, contBB);
ctx.builder.SetInsertPoint(checkBB);
ctx.builder.CreateLoad(
ctx.builder.CreateConstGEP1_32(ctx.signalPage, -1),
ctx.builder.CreateConstInBoundsGEP1_32(T_size, ctx.signalPage, -1),
true);
ctx.builder.CreateBr(contBB);
ctx.f->getBasicBlockList().push_back(contBB);
Expand Down
6 changes: 3 additions & 3 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ static Value *maybe_bitcast(jl_codectx_t &ctx, Value *V, Type *to) {
static Value *julia_binding_gv(jl_codectx_t &ctx, Value *bv)
{
Value *offset = ConstantInt::get(T_size, offsetof(jl_binding_t, value) / sizeof(size_t));
return ctx.builder.CreateInBoundsGEP(bv, offset);
return ctx.builder.CreateInBoundsGEP(T_prjlvalue, bv, offset);
}

static Value *julia_binding_gv(jl_codectx_t &ctx, jl_binding_t *b)
Expand Down Expand Up @@ -2822,7 +2822,7 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
if (!jl_field_isptr(sty, i) && jl_is_uniontype(jl_field_type(sty, i))) {
tbaa_decorate(tbaa_unionselbyte, ctx.builder.CreateAlignedStore(
ConstantInt::get(T_int8, 0),
ctx.builder.CreateInBoundsGEP(emit_bitcast(ctx, strct, T_pint8),
ctx.builder.CreateInBoundsGEP(T_int8, emit_bitcast(ctx, strct, T_pint8),
ConstantInt::get(T_size, jl_field_offset(sty, i) + jl_field_size(sty, i) - 1)),
1));
}
Expand Down Expand Up @@ -2871,7 +2871,7 @@ static Value *emit_defer_signal(jl_codectx_t &ctx)
PointerType::get(T_sigatomic, 0));
Constant *offset = ConstantInt::getSigned(T_int32,
offsetof(jl_tls_states_t, defer_signal) / sizeof(sig_atomic_t));
return ctx.builder.CreateInBoundsGEP(ptls, ArrayRef<Value*>(offset), "jl_defer_signal");
return ctx.builder.CreateInBoundsGEP(T_sigatomic, ptls, ArrayRef<Value*>(offset), "jl_defer_signal");
}

static int compare_cgparams(const jl_cgparams_t *a, const jl_cgparams_t *b)
Expand Down
13 changes: 7 additions & 6 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2812,7 +2812,7 @@ static bool emit_builtin_call(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
Instruction *own_ptr;
if (jl_is_long(ndp)) {
own_ptr = ctx.builder.CreateAlignedLoad(T_prjlvalue,
ctx.builder.CreateConstGEP1_32(T_prjlvalue,
ctx.builder.CreateConstInBoundsGEP1_32(T_prjlvalue,
emit_bitcast(ctx, decay_derived(ctx, aryv), T_pprjlvalue),
jl_array_data_owner_offset(nd) / sizeof(jl_value_t*)),
sizeof(void*));
Expand Down Expand Up @@ -4457,6 +4457,7 @@ static void allocate_gc_frame(jl_codectx_t &ctx, BasicBlock *b0)
static void emit_last_age_field(jl_codectx_t &ctx)
{
ctx.world_age_field = ctx.builder.CreateInBoundsGEP(
T_size,
ctx.builder.CreateBitCast(ctx.ptlsStates, T_psize),
ConstantInt::get(T_size, offsetof(jl_tls_states_t, world_age) / sizeof(size_t)));
}
Expand Down Expand Up @@ -4791,7 +4792,7 @@ static Function* gen_cfun_wrapper(
*closure_types = jl_alloc_vec_any(0);
jl_array_ptr_1d_push(*closure_types, jargty);
Value *runtime_dt = ctx.builder.CreateAlignedLoad(T_prjlvalue,
ctx.builder.CreateConstGEP1_32(T_prjlvalue, nestPtr, jl_array_len(*closure_types)),
ctx.builder.CreateConstInBoundsGEP1_32(T_prjlvalue, nestPtr, jl_array_len(*closure_types)),
sizeof(void*));
BasicBlock *boxedBB = BasicBlock::Create(jl_LLVMContext, "isboxed", cw);
BasicBlock *loadBB = BasicBlock::Create(jl_LLVMContext, "need-load", cw);
Expand Down Expand Up @@ -4859,7 +4860,7 @@ static Function* gen_cfun_wrapper(
*closure_types = jl_alloc_vec_any(0);
jl_array_ptr_1d_push(*closure_types, jargty);
Value *runtime_dt = ctx.builder.CreateAlignedLoad(T_prjlvalue,
ctx.builder.CreateConstGEP1_32(T_prjlvalue, nestPtr, jl_array_len(*closure_types)),
ctx.builder.CreateConstInBoundsGEP1_32(T_prjlvalue, nestPtr, jl_array_len(*closure_types)),
sizeof(void*));
Value *strct = box_ccall_result(ctx, val, runtime_dt, jargty);
inputarg = mark_julia_type(ctx, strct, true, jargty_proper);
Expand Down Expand Up @@ -5251,12 +5252,12 @@ static jl_cgval_t emit_cfunction(jl_codectx_t &ctx, jl_value_t *output_type, con
tbaa_decorate(tbaa, ctx.builder.CreateStore(F, derived_strct));
tbaa_decorate(tbaa, ctx.builder.CreateStore(
ctx.builder.CreatePtrToInt(literal_pointer_val(ctx, fexpr_rt.constant), T_size),
ctx.builder.CreateConstGEP1_32(T_size, derived_strct, 1)));
ctx.builder.CreateConstInBoundsGEP1_32(T_size, derived_strct, 1)));
Value *zero = ConstantInt::get(T_size, 0);
tbaa_decorate(tbaa, ctx.builder.CreateStore(zero,
ctx.builder.CreateConstGEP1_32(T_size, derived_strct, 2)));
ctx.builder.CreateConstInBoundsGEP1_32(T_size, derived_strct, 2)));
tbaa_decorate(tbaa, ctx.builder.CreateStore(zero,
ctx.builder.CreateConstGEP1_32(T_size, derived_strct, 3)));
ctx.builder.CreateConstInBoundsGEP1_32(T_size, derived_strct, 3)));
F = strct;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ static jl_cgval_t emit_pointerref(jl_codectx_t &ctx, jl_cgval_t *argv)
Value *thePtr = emit_unbox(ctx, T_pprjlvalue, e, e.typ);
return mark_julia_type(
ctx,
ctx.builder.CreateAlignedLoad(ctx.builder.CreateGEP(T_prjlvalue, thePtr, im1), align_nb),
ctx.builder.CreateAlignedLoad(ctx.builder.CreateInBoundsGEP(T_prjlvalue, thePtr, im1), align_nb),
true,
ety);
}
Expand All @@ -596,7 +596,7 @@ static jl_cgval_t emit_pointerref(jl_codectx_t &ctx, jl_cgval_t *argv)
im1 = ctx.builder.CreateMul(im1, ConstantInt::get(T_size,
LLT_ALIGN(size, jl_datatype_align(ety))));
Value *thePtr = emit_unbox(ctx, T_pint8, e, e.typ);
thePtr = ctx.builder.CreateGEP(T_int8, emit_bitcast(ctx, thePtr, T_pint8), im1);
thePtr = ctx.builder.CreateInBoundsGEP(T_int8, emit_bitcast(ctx, thePtr, T_pint8), im1);
MDNode *tbaa = best_tbaa(ety);
emit_memcpy(ctx, strct, tbaa, thePtr, nullptr, size, 1);
return mark_julia_type(ctx, strct, true, ety);
Expand Down Expand Up @@ -655,7 +655,7 @@ static jl_cgval_t emit_pointerset(jl_codectx_t &ctx, jl_cgval_t *argv)
thePtr = emit_unbox(ctx, T_psize, e, e.typ);
Instruction *store = ctx.builder.CreateAlignedStore(
ctx.builder.CreatePtrToInt(emit_pointer_from_objref(ctx, boxed(ctx, x)), T_size),
ctx.builder.CreateGEP(T_size, thePtr, im1), align_nb);
ctx.builder.CreateInBoundsGEP(T_size, thePtr, im1), align_nb);
tbaa_decorate(tbaa_data, store);
}
else if (!jl_isbits(ety)) {
Expand All @@ -667,7 +667,7 @@ static jl_cgval_t emit_pointerset(jl_codectx_t &ctx, jl_cgval_t *argv)
uint64_t size = jl_datatype_size(ety);
im1 = ctx.builder.CreateMul(im1, ConstantInt::get(T_size,
LLT_ALIGN(size, jl_datatype_align(ety))));
emit_memcpy(ctx, ctx.builder.CreateGEP(T_int8, thePtr, im1), nullptr, x, size, align_nb);
emit_memcpy(ctx, ctx.builder.CreateInBoundsGEP(T_int8, thePtr, im1), nullptr, x, size, align_nb);
}
else {
bool isboxed;
Expand Down
12 changes: 6 additions & 6 deletions src/llvm-final-gc-lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ void FinalLowerGC::lowerPushGCFrame(CallInst *target, Function &F)
StoreInst *inst = builder.CreateAlignedStore(
ConstantInt::get(T_size, JL_GC_ENCODE_PUSHARGS(nRoots)),
builder.CreateBitCast(
builder.CreateConstGEP1_32(gcframe, 0),
builder.CreateConstInBoundsGEP1_32(T_prjlvalue, gcframe, 0),
T_size->getPointerTo()),
sizeof(void*));
inst->setMetadata(LLVMContext::MD_tbaa, tbaa_gcframe);
Value *pgcstack = builder.Insert(getPgcstack(ptlsStates));
inst = builder.CreateAlignedStore(
builder.CreateAlignedLoad(pgcstack, sizeof(void*)),
builder.CreatePointerCast(
builder.CreateConstGEP1_32(gcframe, 1),
builder.CreateConstInBoundsGEP1_32(T_prjlvalue, gcframe, 1),
PointerType::get(T_ppjlvalue, 0)),
sizeof(void*));
inst->setMetadata(LLVMContext::MD_tbaa, tbaa_gcframe);
Expand All @@ -133,7 +133,7 @@ void FinalLowerGC::lowerPopGCFrame(CallInst *target, Function &F)
IRBuilder<> builder(target->getContext());
builder.SetInsertPoint(target);
Instruction *gcpop =
cast<Instruction>(builder.CreateConstGEP1_32(gcframe, 1));
cast<Instruction>(builder.CreateConstInBoundsGEP1_32(T_prjlvalue, gcframe, 1));
Instruction *inst = builder.CreateAlignedLoad(gcpop, sizeof(void*));
inst->setMetadata(LLVMContext::MD_tbaa, tbaa_gcframe);
inst = builder.CreateAlignedStore(
Expand All @@ -159,7 +159,7 @@ Value *FinalLowerGC::lowerGetGCFrameSlot(CallInst *target, Function &F)
index = builder.CreateAdd(index, ConstantInt::get(T_int32, 2));

// Lower the intrinsic as a GEP.
auto gep = builder.CreateGEP(gcframe, index);
auto gep = builder.CreateInBoundsGEP(T_prjlvalue, gcframe, index);
gep->takeName(target);
return gep;
}
Expand All @@ -174,8 +174,8 @@ Value *FinalLowerGC::lowerQueueGCRoot(CallInst *target, Function &F)
Instruction *FinalLowerGC::getPgcstack(Instruction *ptlsStates)
{
Constant *offset = ConstantInt::getSigned(T_int32, offsetof(jl_tls_states_t, pgcstack) / sizeof(void*));
return GetElementPtrInst::Create(
nullptr,
return GetElementPtrInst::CreateInBounds(
T_ppjlvalue,
ptlsStates,
ArrayRef<Value*>(offset),
"jl_pgcstack");
Expand Down
6 changes: 3 additions & 3 deletions src/llvm-late-gc-lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,7 @@ static Value *ExtractScalar(Value *V, Type *VTy, bool isptr, ArrayRef<unsigned>
for (unsigned j = 0; j < Idxs.size(); ++j) {
IdxList[j + 1] = ConstantInt::get(T_int32, Idxs[j]);
}
Value *GEP = irbuilder.CreateGEP(VTy, V, IdxList);
Value *GEP = irbuilder.CreateInBoundsGEP(VTy, V, IdxList);
Type *T = GetElementPtrInst::getIndexedType(VTy, IdxList);
assert(T->isPointerTy());
V = irbuilder.CreateAlignedLoad(T, GEP, sizeof(void*));
Expand Down Expand Up @@ -1976,7 +1976,7 @@ Value *LateLowerGCFrame::EmitTagPtr(IRBuilder<> &builder, Type *T, Value *V)
assert(T == T_size || isa<PointerType>(T));
auto TV = cast<PointerType>(V->getType());
auto cast = builder.CreateBitCast(V, T->getPointerTo(TV->getAddressSpace()));
return builder.CreateGEP(T, cast, ConstantInt::get(T_size, -1));
return builder.CreateInBoundsGEP(T, cast, ConstantInt::get(T_size, -1));
}

Value *LateLowerGCFrame::EmitLoadTag(IRBuilder<> &builder, Value *V)
Expand Down Expand Up @@ -2161,7 +2161,7 @@ bool LateLowerGCFrame::CleanupIR(Function &F, State *S) {
IRBuilder<> Builder (CI);
for (; arg_it != CI->arg_end(); ++arg_it) {
Builder.CreateAlignedStore(*arg_it,
Builder.CreateGEP(T_prjlvalue, Frame, ConstantInt::get(T_int32, slot++)),
Builder.CreateInBoundsGEP(T_prjlvalue, Frame, ConstantInt::get(T_int32, slot++)),
sizeof(void*));
}
ReplacementArgs.push_back(nframeargs == 0 ?
Expand Down
14 changes: 7 additions & 7 deletions test/llvmpasses/final-lower-gc.ll
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ top:
%gcframe = call {} addrspace(10)** @julia.new_gc_frame(i32 2)
; CHECK: %ptls = call {}*** @julia.ptls_states()
%ptls = call {}*** @julia.ptls_states()
; CHECK-DAG: [[GCFRAME_SIZE_PTR:%.*]] = getelementptr {} addrspace(10)*, {} addrspace(10)** %gcframe, i32 0
; CHECK-DAG: [[GCFRAME_SIZE_PTR:%.*]] = getelementptr inbounds {} addrspace(10)*, {} addrspace(10)** %gcframe, i32 0
; CHECK-DAG: [[GCFRAME_SIZE_PTR2:%.*]] = bitcast {} addrspace(10)** [[GCFRAME_SIZE_PTR]] to i64*
; CHECK-DAG: store i64 8, i64* [[GCFRAME_SIZE_PTR2]], align 8, !tbaa !0
; CHECK-DAG: [[GCFRAME_SLOT:%.*]] = getelementptr {}**, {}*** %ptls, i32 0
; CHECK-DAG: [[PREV_GCFRAME_PTR:%.*]] = getelementptr {} addrspace(10)*, {} addrspace(10)** %gcframe, i32 1
; CHECK-DAG: [[GCFRAME_SLOT:%.*]] = getelementptr inbounds {}**, {}*** %ptls, i32 0
; CHECK-DAG: [[PREV_GCFRAME_PTR:%.*]] = getelementptr inbounds {} addrspace(10)*, {} addrspace(10)** %gcframe, i32 1
; CHECK-DAG: [[PREV_GCFRAME_PTR2:%.*]] = bitcast {} addrspace(10)** [[PREV_GCFRAME_PTR]] to {}***
; CHECK-DAG: [[PREV_GCFRAME:%.*]] = load {}**, {}*** [[GCFRAME_SLOT]], align 8
; CHECK-DAG: store {}** [[PREV_GCFRAME]], {}*** [[PREV_GCFRAME_PTR2]], align 8, !tbaa !0
; CHECK-DAG: [[GCFRAME_SLOT2:%.*]] = bitcast {}*** [[GCFRAME_SLOT]] to {} addrspace(10)***
; CHECK-NEXT: store {} addrspace(10)** %gcframe, {} addrspace(10)*** [[GCFRAME_SLOT2]], align 8
call void @julia.push_gc_frame({} addrspace(10)** %gcframe, i32 2)
%aboxed = call {} addrspace(10)* @jl_box_int64(i64 signext %a)
; CHECK: %frame_slot_1 = getelementptr {} addrspace(10)*, {} addrspace(10)** %gcframe, i32 3
; CHECK: %frame_slot_1 = getelementptr inbounds {} addrspace(10)*, {} addrspace(10)** %gcframe, i32 3
%frame_slot_1 = call {} addrspace(10)** @julia.get_gc_frame_slot({} addrspace(10)** %gcframe, i32 1)
store {} addrspace(10)* %aboxed, {} addrspace(10)** %frame_slot_1, align 8
%bboxed = call {} addrspace(10)* @jl_box_int64(i64 signext %b)
; CHECK: %frame_slot_2 = getelementptr {} addrspace(10)*, {} addrspace(10)** %gcframe, i32 2
; CHECK: %frame_slot_2 = getelementptr inbounds {} addrspace(10)*, {} addrspace(10)** %gcframe, i32 2
%frame_slot_2 = call {} addrspace(10)** @julia.get_gc_frame_slot({} addrspace(10)** %gcframe, i32 0)
store {} addrspace(10)* %bboxed, {} addrspace(10)** %frame_slot_2, align 8
; CHECK: call void @boxed_simple({} addrspace(10)* %aboxed, {} addrspace(10)* %bboxed)
call void @boxed_simple({} addrspace(10)* %aboxed, {} addrspace(10)* %bboxed)
; CHECK-NEXT: [[PREV_GCFRAME_PTR3:%.*]] = getelementptr {} addrspace(10)*, {} addrspace(10)** %gcframe, i32 1
; CHECK-NEXT: [[PREV_GCFRAME_PTR3:%.*]] = getelementptr inbounds {} addrspace(10)*, {} addrspace(10)** %gcframe, i32 1
; CHECK-NEXT: [[PREV_GCFRAME_PTR4:%.*]] = load {} addrspace(10)*, {} addrspace(10)** [[PREV_GCFRAME_PTR3]], align 8, !tbaa !0
; CHECK-NEXT: [[GCFRAME_SLOT3:%.*]] = getelementptr {}**, {}*** %ptls, i32 0
; CHECK-NEXT: [[GCFRAME_SLOT3:%.*]] = getelementptr inbounds {}**, {}*** %ptls, i32 0
; CHECK-NEXT: [[GCFRAME_SLOT4:%.*]] = bitcast {}*** [[GCFRAME_SLOT3]] to {} addrspace(10)**
; CHECK-NEXT: store {} addrspace(10)* [[PREV_GCFRAME_PTR4]], {} addrspace(10)** [[GCFRAME_SLOT4]], align 8, !tbaa !0
call void @julia.pop_gc_frame({} addrspace(10)** %gcframe)
Expand Down
Loading

0 comments on commit 6de97d5

Please sign in to comment.