Skip to content

Commit fdd16bf

Browse files
authored
Make tuple indexing nice in the IR (#50877)
1 parent a7eca00 commit fdd16bf

File tree

3 files changed

+55
-34
lines changed

3 files changed

+55
-34
lines changed

src/cgutils.cpp

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,27 +2487,6 @@ static jl_cgval_t emit_unionload(jl_codectx_t &ctx, Value *addr, Value *ptindex,
24872487
return mark_julia_slot(fsz > 0 ? addr : nullptr, jfty, tindex, tbaa);
24882488
}
24892489

2490-
static const char *get_fieldname(jl_datatype_t *jt, unsigned idx)
2491-
{
2492-
if (jl_is_namedtuple_type(jt)) {
2493-
auto names = jl_tparam0(jt);
2494-
assert(jl_is_tuple(names));
2495-
if (idx < jl_nfields(names)) {
2496-
auto name = jl_fieldref(names, idx);
2497-
assert(jl_is_symbol(name));
2498-
return jl_symbol_name((jl_sym_t*)name);
2499-
}
2500-
} else {
2501-
auto flds = jl_field_names(jt);
2502-
if (idx < jl_svec_len(flds)) {
2503-
auto name = jl_svec_ref(flds, idx);
2504-
assert(jl_is_symbol(name));
2505-
return jl_symbol_name((jl_sym_t*)name);
2506-
}
2507-
}
2508-
return "<unknown field>";
2509-
}
2510-
25112490
// If `nullcheck` is not NULL and a pointer NULL check is necessary
25122491
// store the pointer to be checked in `*nullcheck` instead of checking it
25132492
static jl_cgval_t emit_getfield_knownidx(jl_codectx_t &ctx, const jl_cgval_t &strct,
@@ -2571,13 +2550,13 @@ static jl_cgval_t emit_getfield_knownidx(jl_codectx_t &ctx, const jl_cgval_t &st
25712550
else
25722551
addr = ctx.builder.CreateConstInBoundsGEP2_32(lt, staddr, 0, idx);
25732552
if (addr != staddr) {
2574-
setName(ctx.emission_context, addr, [&]() { return (get_objname() + "." + get_fieldname(jt, idx) + "_ptr").str(); });
2553+
setNameWithField(ctx.emission_context, addr, get_objname, jt, idx, Twine("_ptr"));
25752554
}
25762555
}
25772556
if (jl_field_isptr(jt, idx)) {
2578-
setName(ctx.emission_context, addr, [&]() { return (get_objname() + "." + get_fieldname(jt, idx) + "_ptr").str(); });
2557+
setNameWithField(ctx.emission_context, addr, get_objname, jt, idx, Twine("_ptr"));
25792558
LoadInst *Load = ctx.builder.CreateAlignedLoad(ctx.types().T_prjlvalue, maybe_bitcast(ctx, addr, ctx.types().T_pprjlvalue), Align(sizeof(void*)));
2580-
setName(ctx.emission_context, Load, [&]() { return (get_objname() + "." + get_fieldname(jt, idx)).str(); });
2559+
setNameWithField(ctx.emission_context, Load, get_objname, jt, idx, Twine());
25812560
Load->setOrdering(order <= jl_memory_order_notatomic ? AtomicOrdering::Unordered : get_llvm_atomic_order(order));
25822561
maybe_mark_load_dereferenceable(Load, maybe_null, jl_field_type(jt, idx));
25832562
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, tbaa);
@@ -2601,7 +2580,7 @@ static jl_cgval_t emit_getfield_knownidx(jl_codectx_t &ctx, const jl_cgval_t &st
26012580
}
26022581
auto val = emit_unionload(ctx, addr, ptindex, jfty, fsz, al, tbaa, !jl_field_isconst(jt, idx), union_max, ctx.tbaa().tbaa_unionselbyte);
26032582
if (val.V && val.V != addr) {
2604-
setName(ctx.emission_context, val.V, [&]() { return (get_objname() + "." + get_fieldname(jt, idx)).str(); });
2583+
setNameWithField(ctx.emission_context, val.V, get_objname, jt, idx, Twine());
26052584
}
26062585
return val;
26072586
}
@@ -2618,7 +2597,7 @@ static jl_cgval_t emit_getfield_knownidx(jl_codectx_t &ctx, const jl_cgval_t &st
26182597
needlock ? AtomicOrdering::NotAtomic : get_llvm_atomic_order(order),
26192598
maybe_null, align, nullcheck);
26202599
if (ret.V) {
2621-
setName(ctx.emission_context, ret.V, [&]() { return (get_objname() + "." + get_fieldname(jt, idx)).str(); });
2600+
setNameWithField(ctx.emission_context, ret.V, get_objname, jt, idx, Twine());
26222601
}
26232602
if (needlock)
26242603
emit_lockstate_value(ctx, strct, false);
@@ -2637,7 +2616,7 @@ static jl_cgval_t emit_getfield_knownidx(jl_codectx_t &ctx, const jl_cgval_t &st
26372616
}
26382617
else if (isa<VectorType>(T)) {
26392618
fldv = ctx.builder.CreateExtractElement(obj, ConstantInt::get(getInt32Ty(ctx.builder.getContext()), idx));
2640-
setName(ctx.emission_context, fldv, [&]() { return (get_objname() + "." + get_fieldname(jt, idx)).str(); });
2619+
setNameWithField(ctx.emission_context, fldv, get_objname, jt, idx, Twine());
26412620
}
26422621
else if (!jl_field_isptr(jt, idx) && jl_is_uniontype(jfty)) {
26432622
int fsz = jl_field_size(jt, idx) - 1;
@@ -2667,11 +2646,11 @@ static jl_cgval_t emit_getfield_knownidx(jl_codectx_t &ctx, const jl_cgval_t &st
26672646
ctx.builder.CreateAlignedStore(fldv, fldp, Align(1));
26682647
}
26692648
}
2670-
setName(ctx.emission_context, lv, [&]() { return (get_objname() + "." + get_fieldname(jt, idx)).str(); });
2649+
setNameWithField(ctx.emission_context, lv, get_objname, jt, idx, Twine());
26712650
}
26722651
Value *tindex0 = ctx.builder.CreateExtractValue(obj, makeArrayRef(ptindex));
26732652
Value *tindex = ctx.builder.CreateNUWAdd(ConstantInt::get(getInt8Ty(ctx.builder.getContext()), 1), tindex0);
2674-
setName(ctx.emission_context, tindex, [&]() { return (get_objname() + "." + get_fieldname(jt, idx) + ".tindex").str(); });
2653+
setNameWithField(ctx.emission_context, tindex, get_objname, jt, idx, Twine(".tindex"));
26752654
return mark_julia_slot(lv, jfty, tindex, ctx.tbaa().tbaa_stack);
26762655
}
26772656
else {
@@ -2683,7 +2662,7 @@ static jl_cgval_t emit_getfield_knownidx(jl_codectx_t &ctx, const jl_cgval_t &st
26832662
else
26842663
llvm_unreachable("encountered incompatible type for a struct");
26852664
fldv = ctx.builder.CreateExtractValue(obj, makeArrayRef(st_idx));
2686-
setName(ctx.emission_context, fldv, [&]() { return (get_objname() + "." + get_fieldname(jt, idx)).str(); });
2665+
setNameWithField(ctx.emission_context, fldv, get_objname, jt, idx, Twine());
26872666
}
26882667
if (maybe_null) {
26892668
Value *first_ptr = jl_field_isptr(jt, idx) ? fldv : extract_first_ptr(ctx, fldv);
@@ -3792,7 +3771,7 @@ static jl_cgval_t emit_setfield(jl_codectx_t &ctx,
37923771
getInt8Ty(ctx.builder.getContext()),
37933772
emit_bitcast(ctx, addr, getInt8PtrTy(ctx.builder.getContext())),
37943773
ConstantInt::get(ctx.types().T_size, byte_offset)); // TODO: use emit_struct_gep
3795-
setName(ctx.emission_context, addr, [&](){ return (get_objname() + "." + get_fieldname(sty, idx0) + "_ptr").str(); });
3774+
setNameWithField(ctx.emission_context, addr, get_objname, sty, idx0, Twine("_ptr"));
37963775
}
37973776
jl_value_t *jfty = jl_field_type(sty, idx0);
37983777
if (!jl_field_isptr(sty, idx0) && jl_is_uniontype(jfty)) {
@@ -3807,7 +3786,7 @@ static jl_cgval_t emit_setfield(jl_codectx_t &ctx,
38073786
Value *ptindex = ctx.builder.CreateInBoundsGEP(getInt8Ty(ctx.builder.getContext()),
38083787
emit_bitcast(ctx, addr, getInt8PtrTy(ctx.builder.getContext())),
38093788
ConstantInt::get(ctx.types().T_size, fsz));
3810-
setName(ctx.emission_context, ptindex, [&](){ return (get_objname() + "." + get_fieldname(sty, idx0) + ".tindex_ptr").str(); });
3789+
setNameWithField(ctx.emission_context, ptindex, get_objname, sty, idx0, Twine(".tindex_ptr"));
38113790
if (needlock)
38123791
emit_lockstate_value(ctx, strct, true);
38133792
BasicBlock *ModifyBB = NULL;
@@ -3869,15 +3848,15 @@ static jl_cgval_t emit_setfield(jl_codectx_t &ctx,
38693848
jl_datatype_t *rettyp = jl_apply_cmpswap_type(jfty);
38703849
oldval = emit_new_struct(ctx, (jl_value_t*)rettyp, 2, argv);
38713850
if (oldval.V) {
3872-
setName(ctx.emission_context, oldval.V, [&](){ return (get_objname() + "." + get_fieldname(sty, idx0)).str(); });
3851+
setNameWithField(ctx.emission_context, oldval.V, get_objname, sty, idx0, Twine());
38733852
}
38743853
}
38753854
else if (ismodifyfield) {
38763855
jl_cgval_t argv[2] = {oldval, rhs};
38773856
jl_datatype_t *rettyp = jl_apply_modify_type(jfty);
38783857
oldval = emit_new_struct(ctx, (jl_value_t*)rettyp, 2, argv);
38793858
if (oldval.V) {
3880-
setName(ctx.emission_context, oldval.V, [&](){ return (get_objname() + "." + get_fieldname(sty, idx0)).str(); });
3859+
setNameWithField(ctx.emission_context, oldval.V, get_objname, sty, idx0, Twine());
38813860
}
38823861
}
38833862
return oldval;

src/codegen.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,37 @@ void setName(jl_codegen_params_t &params, Value *V, std::function<std::string()>
188188
}
189189
}
190190

191+
void setNameWithField(jl_codegen_params_t &params, Value *V, std::function<StringRef()> GetObjName, jl_datatype_t *jt, unsigned idx, const Twine &suffix)
192+
{
193+
assert((isa<Constant>(V) || isa<Instruction>(V)) && "Should only set names on instructions!");
194+
if (params.debug_level >= 2 && !isa<Constant>(V)) {
195+
if (jl_is_tuple_type(jt)){
196+
V->setName(Twine(GetObjName()) + "[" + Twine(idx + 1) + "]"+ suffix);
197+
return;
198+
}
199+
200+
if (jl_is_namedtuple_type(jt)) {
201+
auto names = jl_tparam0(jt);
202+
assert(jl_is_tuple(names));
203+
if (idx < jl_nfields(names)) {
204+
auto name = jl_fieldref(names, idx);
205+
assert(jl_is_symbol(name));
206+
V->setName(Twine(GetObjName()) + "." + Twine(jl_symbol_name((jl_sym_t*)name)) + suffix);
207+
return;
208+
}
209+
} else {
210+
auto flds = jl_field_names(jt);
211+
if (idx < jl_svec_len(flds)) {
212+
auto name = jl_svec_ref(flds, idx);
213+
assert(jl_is_symbol(name));
214+
V->setName(Twine(GetObjName()) + "." + Twine(jl_symbol_name((jl_sym_t*)name)) + suffix);
215+
return;
216+
}
217+
}
218+
V->setName(Twine(GetObjName()) + "." + Twine("unknown field") + suffix);
219+
}
220+
}
221+
191222
STATISTIC(EmittedAllocas, "Number of allocas emitted");
192223
STATISTIC(EmittedIntToPtrs, "Number of inttoptrs emitted");
193224
STATISTIC(ModulesCreated, "Number of LLVM Modules created");

test/llvmpasses/names.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ function f6(e)
6767
return e.f[].g[].h[]
6868
end
6969

70+
# COM: check getfield for Tuples
71+
function f7(a)
72+
return a[2]
73+
end
74+
7075
# CHECK-LABEL: define {{(swiftcc )?}}double @julia_f1
7176
# CHECK-SAME: double %"a::Float64"
7277
# CHECK-SAME: double %"b::Float64"
@@ -162,3 +167,9 @@ emit(f5, A)
162167
# CHECK: @"jl_sym#g
163168
# CHECK: @"jl_sym#h
164169
emit(f6, E)
170+
171+
172+
# CHECK: define {{(swiftcc )?}}i64 @julia_f7
173+
# CHECK-SAME: %"a::Tuple"
174+
# CHECK: %"a::Tuple[2]_ptr.unbox
175+
emit(f7,Tuple{Int,Int})

0 commit comments

Comments
 (0)