Skip to content

Commit c6d0356

Browse files
vtjnashKristofferC
authored andcommitted
codegen,tbaa: fix array isassigned tbaa information (#32356)
This avoids a regression (correctness and performance) caused by #21262 where we were no longer able to fold away the isnull assertion in the case where the source also contains an explicit isassigned check. Also upgrade many other CreateInBoundsGEP calls to include the element type (NFC). (cherry picked from commit a7427aa)
1 parent 60b1fea commit c6d0356

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/ccall.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,10 +1808,10 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
18081808
}
18091809
else if (!jl_has_free_typevars(ety)) {
18101810
Value *idx = emit_unbox(ctx, T_size, idxv, (jl_value_t*)jl_ulong_type);
1811-
Value *arrayptr = emit_bitcast(ctx, emit_arrayptr(ctx, aryv, aryex), T_ppjlvalue);
1812-
Value *slot_addr = ctx.builder.CreateGEP(arrayptr, idx);
1813-
Value *load = tbaa_decorate(tbaa_arraybuf, ctx.builder.CreateLoad(slot_addr));
1814-
Value *res = ctx.builder.CreateZExt(ctx.builder.CreateICmpNE(load, V_null), T_int32);
1811+
Value *arrayptr = emit_bitcast(ctx, emit_arrayptr(ctx, aryv, aryex), T_pprjlvalue);
1812+
Value *slot_addr = ctx.builder.CreateInBoundsGEP(T_prjlvalue, arrayptr, idx);
1813+
Value *load = tbaa_decorate(tbaa_ptrarraybuf, ctx.builder.CreateLoad(T_prjlvalue, slot_addr));
1814+
Value *res = ctx.builder.CreateZExt(ctx.builder.CreateICmpNE(load, Constant::getNullValue(T_prjlvalue)), T_int32);
18151815
JL_GC_POP();
18161816
return mark_or_box_ccall_result(ctx, res, retboxed, rt, unionall, static_rt);
18171817
}

src/cgutils.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -779,13 +779,18 @@ static unsigned get_box_tindex(jl_datatype_t *jt, jl_value_t *ut)
779779

780780
static Value *emit_nthptr_addr(jl_codectx_t &ctx, Value *v, ssize_t n, bool gctracked = true)
781781
{
782-
return ctx.builder.CreateInBoundsGEP(emit_bitcast(ctx, maybe_decay_tracked(v), T_pprjlvalue),
783-
ConstantInt::get(T_size, n));
782+
return ctx.builder.CreateInBoundsGEP(
783+
T_prjlvalue,
784+
emit_bitcast(ctx, maybe_decay_tracked(v), T_pprjlvalue),
785+
ConstantInt::get(T_size, n));
784786
}
785787

786788
static Value *emit_nthptr_addr(jl_codectx_t &ctx, Value *v, Value *idx)
787789
{
788-
return ctx.builder.CreateInBoundsGEP(emit_bitcast(ctx, maybe_decay_tracked(v), T_pprjlvalue), idx);
790+
return ctx.builder.CreateInBoundsGEP(
791+
T_prjlvalue,
792+
emit_bitcast(ctx, maybe_decay_tracked(v), T_pprjlvalue),
793+
idx);
789794
}
790795

791796
static Value *emit_nthptr(jl_codectx_t &ctx, Value *v, ssize_t n, MDNode *tbaa)
@@ -1457,8 +1462,10 @@ static bool emit_getfield_unknownidx(jl_codectx_t &ctx,
14571462
minimum_align = std::min(minimum_align,
14581463
(size_t)julia_alignment(ft));
14591464
}
1460-
Value *fldptr = ctx.builder.CreateInBoundsGEP(maybe_decay_tracked(
1461-
emit_bitcast(ctx, data_pointer(ctx, strct), T_pprjlvalue)), idx);
1465+
Value *fldptr = ctx.builder.CreateInBoundsGEP(
1466+
T_prjlvalue,
1467+
maybe_decay_tracked(emit_bitcast(ctx, data_pointer(ctx, strct), T_pprjlvalue)),
1468+
idx);
14621469
Value *fld = tbaa_decorate(strct.tbaa,
14631470
maybe_mark_load_dereferenceable(
14641471
ctx.builder.CreateLoad(T_prjlvalue, fldptr),
@@ -1539,8 +1546,9 @@ static jl_cgval_t emit_getfield_knownidx(jl_codectx_t &ctx, const jl_cgval_t &st
15391546
// can pessimize mem2reg
15401547
if (byte_offset > 0) {
15411548
addr = ctx.builder.CreateInBoundsGEP(
1542-
emit_bitcast(ctx, staddr, T_pint8),
1543-
ConstantInt::get(T_size, byte_offset));
1549+
T_int8,
1550+
emit_bitcast(ctx, staddr, T_pint8),
1551+
ConstantInt::get(T_size, byte_offset));
15441552
}
15451553
else {
15461554
addr = staddr;
@@ -1896,8 +1904,8 @@ static Value *emit_array_nd_index(
18961904
ctx.builder.SetInsertPoint(failBB);
18971905
// CreateAlloca is OK here since we are on an error branch
18981906
Value *tmp = ctx.builder.CreateAlloca(T_size, ConstantInt::get(T_size, nidxs));
1899-
for(size_t k=0; k < nidxs; k++) {
1900-
ctx.builder.CreateStore(idxs[k], ctx.builder.CreateInBoundsGEP(tmp, ConstantInt::get(T_size, k)));
1907+
for (size_t k = 0; k < nidxs; k++) {
1908+
ctx.builder.CreateStore(idxs[k], ctx.builder.CreateInBoundsGEP(T_size, tmp, ConstantInt::get(T_size, k)));
19011909
}
19021910
ctx.builder.CreateCall(prepare_call(jlboundserrorv_func),
19031911
{ mark_callee_rooted(a), tmp, ConstantInt::get(T_size, nidxs) });
@@ -2410,8 +2418,9 @@ static void emit_setfield(jl_codectx_t &ctx,
24102418
Value *addr = data_pointer(ctx, strct);
24112419
if (byte_offset > 0) {
24122420
addr = ctx.builder.CreateInBoundsGEP(
2413-
emit_bitcast(ctx, maybe_decay_tracked(addr), T_pint8),
2414-
ConstantInt::get(T_size, byte_offset)); // TODO: use emit_struct_gep
2421+
T_int8,
2422+
emit_bitcast(ctx, maybe_decay_tracked(addr), T_pint8),
2423+
ConstantInt::get(T_size, byte_offset)); // TODO: use emit_struct_gep
24152424
}
24162425
jl_value_t *jfty = jl_svecref(sty->types, idx0);
24172426
if (jl_field_isptr(sty, idx0)) {
@@ -2531,7 +2540,9 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
25312540
if (!jl_field_isptr(sty, i) && jl_is_uniontype(jl_field_type(sty, i))) {
25322541
tbaa_decorate(tbaa_unionselbyte, ctx.builder.CreateStore(
25332542
ConstantInt::get(T_int8, 0),
2534-
ctx.builder.CreateInBoundsGEP(emit_bitcast(ctx, strct, T_pint8),
2543+
ctx.builder.CreateInBoundsGEP(
2544+
T_int8,
2545+
emit_bitcast(ctx, strct, T_pint8),
25352546
ConstantInt::get(T_size, jl_field_offset(sty, i) + jl_field_size(sty, i) - 1))));
25362547
}
25372548
}
@@ -2551,15 +2562,15 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
25512562
tbaa_decorate(strctinfo.tbaa, ctx.builder.CreateStore(
25522563
ConstantPointerNull::get(cast<PointerType>(T_prjlvalue)),
25532564
ctx.builder.CreateInBoundsGEP(T_prjlvalue, emit_bitcast(ctx, strct, T_pprjlvalue),
2554-
ConstantInt::get(T_size, jl_field_offset(sty, i) / sizeof(void*)))));
2565+
ConstantInt::get(T_size, jl_field_offset(sty, i) / sizeof(void*)))));
25552566
}
25562567
}
25572568
for (size_t i = nargs; i < nf; i++) {
25582569
if (!jl_field_isptr(sty, i) && jl_is_uniontype(jl_field_type(sty, i))) {
25592570
tbaa_decorate(tbaa_unionselbyte, ctx.builder.CreateStore(
25602571
ConstantInt::get(T_int8, 0),
25612572
ctx.builder.CreateInBoundsGEP(emit_bitcast(ctx, strct, T_pint8),
2562-
ConstantInt::get(T_size, jl_field_offset(sty, i) + jl_field_size(sty, i) - 1))));
2573+
ConstantInt::get(T_size, jl_field_offset(sty, i) + jl_field_size(sty, i) - 1))));
25632574
}
25642575
}
25652576
bool need_wb = false;

0 commit comments

Comments
 (0)