Skip to content

Commit dd98510

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 3546c63 commit dd98510

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
@@ -1669,10 +1669,10 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
16691669
}
16701670
else if (!jl_has_free_typevars(ety)) {
16711671
Value *idx = emit_unbox(ctx, T_size, idxv, (jl_value_t*)jl_ulong_type);
1672-
Value *arrayptr = emit_bitcast(ctx, emit_arrayptr(ctx, aryv, aryex), T_ppjlvalue);
1673-
Value *slot_addr = ctx.builder.CreateGEP(arrayptr, idx);
1674-
Value *load = tbaa_decorate(tbaa_arraybuf, ctx.builder.CreateLoad(slot_addr));
1675-
Value *res = ctx.builder.CreateZExt(ctx.builder.CreateICmpNE(load, V_null), T_int32);
1672+
Value *arrayptr = emit_bitcast(ctx, emit_arrayptr(ctx, aryv, aryex), T_pprjlvalue);
1673+
Value *slot_addr = ctx.builder.CreateInBoundsGEP(T_prjlvalue, arrayptr, idx);
1674+
Value *load = tbaa_decorate(tbaa_ptrarraybuf, ctx.builder.CreateLoad(T_prjlvalue, slot_addr));
1675+
Value *res = ctx.builder.CreateZExt(ctx.builder.CreateICmpNE(load, Constant::getNullValue(T_prjlvalue)), T_int32);
16761676
JL_GC_POP();
16771677
return mark_or_box_ccall_result(ctx, res, retboxed, rt, unionall, static_rt);
16781678
}

src/cgutils.cpp

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

786786
static Value *emit_nthptr_addr(jl_codectx_t &ctx, Value *v, ssize_t n, bool gctracked = true)
787787
{
788-
return ctx.builder.CreateInBoundsGEP(emit_bitcast(ctx, maybe_decay_tracked(v), T_pprjlvalue),
789-
ConstantInt::get(T_size, n));
788+
return ctx.builder.CreateInBoundsGEP(
789+
T_prjlvalue,
790+
emit_bitcast(ctx, maybe_decay_tracked(v), T_pprjlvalue),
791+
ConstantInt::get(T_size, n));
790792
}
791793

792794
static Value *emit_nthptr_addr(jl_codectx_t &ctx, Value *v, Value *idx)
793795
{
794-
return ctx.builder.CreateInBoundsGEP(emit_bitcast(ctx, maybe_decay_tracked(v), T_pprjlvalue), idx);
796+
return ctx.builder.CreateInBoundsGEP(
797+
T_prjlvalue,
798+
emit_bitcast(ctx, maybe_decay_tracked(v), T_pprjlvalue),
799+
idx);
795800
}
796801

797802
static Value *emit_nthptr(jl_codectx_t &ctx, Value *v, ssize_t n, MDNode *tbaa)
@@ -1482,8 +1487,10 @@ static bool emit_getfield_unknownidx(jl_codectx_t &ctx,
14821487
minimum_align = std::min(minimum_align,
14831488
(size_t)julia_alignment(ft));
14841489
}
1485-
Value *fldptr = ctx.builder.CreateInBoundsGEP(maybe_decay_tracked(
1486-
emit_bitcast(ctx, data_pointer(ctx, strct), T_pprjlvalue)), idx);
1490+
Value *fldptr = ctx.builder.CreateInBoundsGEP(
1491+
T_prjlvalue,
1492+
maybe_decay_tracked(emit_bitcast(ctx, data_pointer(ctx, strct), T_pprjlvalue)),
1493+
idx);
14871494
Value *fld = tbaa_decorate(strct.tbaa,
14881495
maybe_mark_load_dereferenceable(
14891496
ctx.builder.CreateLoad(T_prjlvalue, fldptr),
@@ -1565,8 +1572,9 @@ static jl_cgval_t emit_getfield_knownidx(jl_codectx_t &ctx, const jl_cgval_t &st
15651572
// can pessimize mem2reg
15661573
if (byte_offset > 0) {
15671574
addr = ctx.builder.CreateInBoundsGEP(
1568-
emit_bitcast(ctx, staddr, T_pint8),
1569-
ConstantInt::get(T_size, byte_offset));
1575+
T_int8,
1576+
emit_bitcast(ctx, staddr, T_pint8),
1577+
ConstantInt::get(T_size, byte_offset));
15701578
}
15711579
else {
15721580
addr = staddr;
@@ -1921,8 +1929,8 @@ static Value *emit_array_nd_index(
19211929
ctx.builder.SetInsertPoint(failBB);
19221930
// CreateAlloca is OK here since we are on an error branch
19231931
Value *tmp = ctx.builder.CreateAlloca(T_size, ConstantInt::get(T_size, nidxs));
1924-
for(size_t k=0; k < nidxs; k++) {
1925-
ctx.builder.CreateStore(idxs[k], ctx.builder.CreateInBoundsGEP(tmp, ConstantInt::get(T_size, k)));
1932+
for (size_t k = 0; k < nidxs; k++) {
1933+
ctx.builder.CreateStore(idxs[k], ctx.builder.CreateInBoundsGEP(T_size, tmp, ConstantInt::get(T_size, k)));
19261934
}
19271935
ctx.builder.CreateCall(prepare_call(jlboundserrorv_func),
19281936
{ mark_callee_rooted(a), tmp, ConstantInt::get(T_size, nidxs) });
@@ -2435,8 +2443,9 @@ static void emit_setfield(jl_codectx_t &ctx,
24352443
Value *addr = data_pointer(ctx, strct);
24362444
if (byte_offset > 0) {
24372445
addr = ctx.builder.CreateInBoundsGEP(
2438-
emit_bitcast(ctx, maybe_decay_tracked(addr), T_pint8),
2439-
ConstantInt::get(T_size, byte_offset)); // TODO: use emit_struct_gep
2446+
T_int8,
2447+
emit_bitcast(ctx, maybe_decay_tracked(addr), T_pint8),
2448+
ConstantInt::get(T_size, byte_offset)); // TODO: use emit_struct_gep
24402449
}
24412450
jl_value_t *jfty = jl_svecref(sty->types, idx0);
24422451
if (jl_field_isptr(sty, idx0)) {
@@ -2558,7 +2567,9 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
25582567
if (!jl_field_isptr(sty, i) && jl_is_uniontype(jl_field_type(sty, i))) {
25592568
tbaa_decorate(tbaa_unionselbyte, ctx.builder.CreateStore(
25602569
ConstantInt::get(T_int8, 0),
2561-
ctx.builder.CreateInBoundsGEP(emit_bitcast(ctx, strct, T_pint8),
2570+
ctx.builder.CreateInBoundsGEP(
2571+
T_int8,
2572+
emit_bitcast(ctx, strct, T_pint8),
25622573
ConstantInt::get(T_size, jl_field_offset(sty, i) + jl_field_size(sty, i) - 1))));
25632574
}
25642575
}
@@ -2578,15 +2589,15 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
25782589
tbaa_decorate(strctinfo.tbaa, ctx.builder.CreateStore(
25792590
ConstantPointerNull::get(cast<PointerType>(T_prjlvalue)),
25802591
ctx.builder.CreateInBoundsGEP(T_prjlvalue, emit_bitcast(ctx, strct, T_pprjlvalue),
2581-
ConstantInt::get(T_size, jl_field_offset(sty, i) / sizeof(void*)))));
2592+
ConstantInt::get(T_size, jl_field_offset(sty, i) / sizeof(void*)))));
25822593
}
25832594
}
25842595
for (size_t i = nargs; i < nf; i++) {
25852596
if (!jl_field_isptr(sty, i) && jl_is_uniontype(jl_field_type(sty, i))) {
25862597
tbaa_decorate(tbaa_unionselbyte, ctx.builder.CreateStore(
25872598
ConstantInt::get(T_int8, 0),
25882599
ctx.builder.CreateInBoundsGEP(emit_bitcast(ctx, strct, T_pint8),
2589-
ConstantInt::get(T_size, jl_field_offset(sty, i) + jl_field_size(sty, i) - 1))));
2600+
ConstantInt::get(T_size, jl_field_offset(sty, i) + jl_field_size(sty, i) - 1))));
25902601
}
25912602
}
25922603
bool need_wb = false;

0 commit comments

Comments
 (0)