Skip to content

Commit 207c09a

Browse files
authored
Avoid generic call in most cases for getproperty (#50523)
More generic than #50444. Should we keep the boundscheck on the getfield since we got the index from the runtime itself?
1 parent 9124234 commit 207c09a

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/codegen.cpp

+25-4
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,18 @@ static const auto jlgetnthfieldchecked_func = new JuliaFunction<TypeFnContextAnd
11101110
Attributes(C, {Attribute::NonNull}),
11111111
None); },
11121112
};
1113+
static const auto jlfieldindex_func = new JuliaFunction<>{
1114+
XSTR(jl_field_index),
1115+
[](LLVMContext &C) {
1116+
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
1117+
return FunctionType::get(getInt32Ty(C),
1118+
{T_prjlvalue, T_prjlvalue, getInt32Ty(C)}, false);
1119+
},
1120+
[](LLVMContext &C) { return AttributeList::get(C,
1121+
Attributes(C, {Attribute::NoUnwind, Attribute::ReadOnly, Attribute::WillReturn}),
1122+
AttributeSet(),
1123+
None); }, // This function can error if the third argument is 1 so don't do that.
1124+
};
11131125
static const auto jlfieldisdefinedchecked_func = new JuliaFunction<TypeFnContextAndSizeT>{
11141126
XSTR(jl_field_isdefined_checked),
11151127
[](LLVMContext &C, Type *T_size) {
@@ -3841,9 +3853,9 @@ static bool emit_builtin_call(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
38413853
return true;
38423854
}
38433855
}
3844-
else if (fld.typ == (jl_value_t*)jl_symbol_type) {
3845-
if (jl_is_datatype(utt) && !jl_is_namedtuple_type(utt)) { // TODO: Look into this for NamedTuple
3846-
if (jl_struct_try_layout(utt) && (jl_datatype_nfields(utt) == 1)) {
3856+
else if (fld.typ == (jl_value_t*)jl_symbol_type) { // Known type but unknown symbol
3857+
if (jl_is_datatype(utt) && (utt != jl_module_type) && jl_struct_try_layout(utt)) {
3858+
if ((jl_datatype_nfields(utt) == 1 && !jl_is_namedtuple_type(utt))) {
38473859
jl_svec_t *fn = jl_field_names(utt);
38483860
assert(jl_svec_len(fn) == 1);
38493861
Value *typ_sym = literal_pointer_val(ctx, jl_svecref(fn, 0));
@@ -3852,9 +3864,17 @@ static bool emit_builtin_call(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
38523864
*ret = emit_getfield_knownidx(ctx, obj, 0, utt, order);
38533865
return true;
38543866
}
3867+
else {
3868+
Value *index = ctx.builder.CreateCall(prepare_call(jlfieldindex_func),
3869+
{emit_typeof(ctx, obj, false, false), boxed(ctx, fld), ConstantInt::get(getInt32Ty(ctx.builder.getContext()), 0)});
3870+
Value *cond = ctx.builder.CreateICmpNE(index, ConstantInt::get(getInt32Ty(ctx.builder.getContext()), -1));
3871+
emit_hasnofield_error_ifnot(ctx, cond, utt->name->name, fld);
3872+
Value *idx2 = ctx.builder.CreateAdd(ctx.builder.CreateIntCast(index, ctx.types().T_size, false), ConstantInt::get(ctx.types().T_size, 1)); // getfield_unknown is 1 based
3873+
if (emit_getfield_unknownidx(ctx, ret, obj, idx2, utt, jl_false, order))
3874+
return true;
3875+
}
38553876
}
38563877
}
3857-
// TODO: generic getfield func with more efficient calling convention
38583878
return false;
38593879
}
38603880

@@ -9179,6 +9199,7 @@ static void init_jit_functions(void)
91799199
add_named_global("jl_adopt_thread", &jl_adopt_thread);
91809200
add_named_global(jlgetcfunctiontrampoline_func, &jl_get_cfunction_trampoline);
91819201
add_named_global(jlgetnthfieldchecked_func, &jl_get_nth_field_checked);
9202+
add_named_global(jlfieldindex_func, &jl_field_index);
91829203
add_named_global(diff_gc_total_bytes_func, &jl_gc_diff_total_bytes);
91839204
add_named_global(sync_gc_total_bytes_func, &jl_gc_sync_total_bytes);
91849205
add_named_global(jlarray_data_owner_func, &jl_array_data_owner);

test/compiler/codegen.jl

+16
Original file line numberDiff line numberDiff line change
@@ -837,3 +837,19 @@ let res = @timed issue50317()
837837
@test res.bytes == 0
838838
return res # must return otherwise the compiler may eliminate the result entirely
839839
end
840+
struct Wrapper50317_2
841+
lock::ReentrantLock
842+
fun::Vector{Int}
843+
end
844+
const MONITOR50317_2 = Wrapper50317_2(ReentrantLock(),[1])
845+
issue50317_2() = @noinline MONITOR50317.lock
846+
issue50317_2()
847+
let res = @timed issue50317_2()
848+
@test res.bytes == 0
849+
return res
850+
end
851+
const a50317 = (b=3,)
852+
let res = @timed a50317[:b]
853+
@test res.bytes == 0
854+
return res
855+
end

0 commit comments

Comments
 (0)