Skip to content

Commit

Permalink
Specialize codegen for Core.current_scope
Browse files Browse the repository at this point in the history
  • Loading branch information
topolarity committed Sep 18, 2024
1 parent e6f71a6 commit 09174f1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -2444,7 +2444,7 @@ void jl_init_primitives(void) JL_GC_DISABLED
add_builtin_func("finalizer", jl_f_finalizer);
add_builtin_func("_compute_sparams", jl_f__compute_sparams);
add_builtin_func("_svec_ref", jl_f__svec_ref);
add_builtin_func("current_scope", jl_f_current_scope);
jl_builtin_current_scope = add_builtin_func("current_scope", jl_f_current_scope);
add_builtin_func("throw_methoderror", jl_f_throw_methoderror);

// builtin types
Expand Down
9 changes: 9 additions & 0 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2119,6 +2119,7 @@ static jl_cgval_t emit_sparam(jl_codectx_t &ctx, size_t i);
static Value *emit_condition(jl_codectx_t &ctx, const jl_cgval_t &condV, const Twine &msg);
static Value *get_current_task(jl_codectx_t &ctx);
static Value *get_current_ptls(jl_codectx_t &ctx);
static Value *get_scope_field(jl_codectx_t &ctx);
static Value *get_tls_world_age_field(jl_codectx_t &ctx);
static void CreateTrap(IRBuilder<> &irbuilder, bool create_new_block = true);
static CallInst *emit_jlcall(jl_codectx_t &ctx, FunctionCallee theFptr, Value *theF,
Expand Down Expand Up @@ -4943,6 +4944,14 @@ static bool emit_builtin_call(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
return true;
}

else if (f == jl_builtin_current_scope && (nargs == 0)) {
jl_aliasinfo_t scope_ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_gcframe);
Instruction *v = scope_ai.decorateInst(
ctx.builder.CreateAlignedLoad(ctx.types().T_prjlvalue, get_scope_field(ctx), ctx.types().alignof_ptr));
*ret = mark_julia_type(ctx, v, /*boxed*/ true, rt);
return true;
}

else if (f == jl_builtin_donotdelete) {
// For now we emit this as a vararg call to the builtin
// (which doesn't look at the arguments). In the future,
Expand Down
3 changes: 2 additions & 1 deletion src/staticdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ extern "C" {
// TODO: put WeakRefs on the weak_refs list during deserialization
// TODO: handle finalizers

#define NUM_TAGS 192
#define NUM_TAGS 193

// An array of references that need to be restored from the sysimg
// This is a manually constructed dual of the gvars array, which would be produced by codegen for Julia code, for C.
Expand Down Expand Up @@ -312,6 +312,7 @@ jl_value_t **const*const get_tags(void) {
INSERT_TAG(jl_builtin_modifyglobal);
INSERT_TAG(jl_builtin_replaceglobal);
INSERT_TAG(jl_builtin_setglobalonce);
INSERT_TAG(jl_builtin_current_scope);
// n.b. must update NUM_TAGS when you add something here
#undef INSERT_TAG
assert(i == NUM_TAGS - 1);
Expand Down

0 comments on commit 09174f1

Please sign in to comment.