Skip to content

Commit 86c567f

Browse files
authored
Improve codegen for Core.throw_methoderror and Core.current_scope (JuliaLang#55803)
This slightly improves our (LLVM) codegen for `Core.throw_methoderror` and `Core.current_scope` ```julia julia> foo() = Core.current_scope() julia> bar() = Core.throw_methoderror(+, nothing) ``` Before: ```llvm ; Function Signature: foo() define nonnull ptr @julia_foo_2488() #0 { top: %0 = call ptr @jl_get_builtin_fptr(ptr nonnull @"+Core.#current_scope#2491.jit") %Builtin_ret = call nonnull ptr %0(ptr nonnull @"jl_global#2492.jit", ptr null, i32 0) ret ptr %Builtin_ret } ; Function Signature: bar() define void @julia_bar_589() #0 { top: %jlcallframe1 = alloca [2 x ptr], align 8 %0 = call ptr @jl_get_builtin_fptr(ptr nonnull @"+Core.#throw_methoderror#591.jit") %jl_nothing = load ptr, ptr @jl_nothing, align 8 store ptr @"jl_global#593.jit", ptr %jlcallframe1, align 8 %1 = getelementptr inbounds ptr, ptr %jlcallframe1, i64 1 store ptr %jl_nothing, ptr %1, align 8 %Builtin_ret = call nonnull ptr %0(ptr nonnull @"jl_global#592.jit", ptr nonnull %jlcallframe1, i32 2) call void @llvm.trap() unreachable } ``` After: ```llvm ; Function Signature: foo() define nonnull ptr @julia_foo_713() #0 { top: %thread_ptr = call ptr asm "movq %fs:0, $0", "=r"() #5 %tls_ppgcstack = getelementptr inbounds i8, ptr %thread_ptr, i64 -8 %tls_pgcstack = load ptr, ptr %tls_ppgcstack, align 8 %current_scope = getelementptr inbounds i8, ptr %tls_pgcstack, i64 -72 %0 = load ptr, ptr %current_scope, align 8 ret ptr %0 } ; Function Signature: bar() define void @julia_bar_1581() #0 { top: %jlcallframe1 = alloca [2 x ptr], align 8 %jl_nothing = load ptr, ptr @jl_nothing, align 8 store ptr @"jl_global#1583.jit", ptr %jlcallframe1, align 8 %0 = getelementptr inbounds ptr, ptr %jlcallframe1, i64 1 store ptr %jl_nothing, ptr %0, align 8 %jl_f_throw_methoderror_ret = call nonnull ptr @jl_f_throw_methoderror(ptr null, ptr nonnull %jlcallframe1, i32 2) call void @llvm.trap() unreachable } ```
1 parent 441bcd0 commit 86c567f

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/builtins.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2444,7 +2444,7 @@ void jl_init_primitives(void) JL_GC_DISABLED
24442444
add_builtin_func("finalizer", jl_f_finalizer);
24452445
add_builtin_func("_compute_sparams", jl_f__compute_sparams);
24462446
add_builtin_func("_svec_ref", jl_f__svec_ref);
2447-
add_builtin_func("current_scope", jl_f_current_scope);
2447+
jl_builtin_current_scope = add_builtin_func("current_scope", jl_f_current_scope);
24482448
add_builtin_func("throw_methoderror", jl_f_throw_methoderror);
24492449

24502450
// builtin types

src/codegen.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,7 @@ static const auto &builtin_func_map() {
16191619
{ jl_f__call_in_world_addr, new JuliaFunction<>{XSTR(jl_f__call_in_world), get_func_sig, get_func_attrs} },
16201620
{ jl_f__call_in_world_total_addr, new JuliaFunction<>{XSTR(jl_f__call_in_world_total), get_func_sig, get_func_attrs} },
16211621
{ jl_f_throw_addr, new JuliaFunction<>{XSTR(jl_f_throw), get_func_sig, get_func_attrs} },
1622+
{ jl_f_throw_methoderror_addr, new JuliaFunction<>{XSTR(jl_f_throw_methoderror), get_func_sig, get_func_attrs} },
16221623
{ jl_f_tuple_addr, jltuple_func },
16231624
{ jl_f_svec_addr, new JuliaFunction<>{XSTR(jl_f_svec), get_func_sig, get_func_attrs} },
16241625
{ jl_f_applicable_addr, new JuliaFunction<>{XSTR(jl_f_applicable), get_func_sig, get_func_attrs} },
@@ -1644,7 +1645,8 @@ static const auto &builtin_func_map() {
16441645
{ jl_f_donotdelete_addr, new JuliaFunction<>{XSTR(jl_f_donotdelete), get_donotdelete_sig, get_donotdelete_func_attrs} },
16451646
{ jl_f_compilerbarrier_addr, new JuliaFunction<>{XSTR(jl_f_compilerbarrier), get_func_sig, get_func_attrs} },
16461647
{ jl_f_finalizer_addr, new JuliaFunction<>{XSTR(jl_f_finalizer), get_func_sig, get_func_attrs} },
1647-
{ jl_f__svec_ref_addr, new JuliaFunction<>{XSTR(jl_f__svec_ref), get_func_sig, get_func_attrs} }
1648+
{ jl_f__svec_ref_addr, new JuliaFunction<>{XSTR(jl_f__svec_ref), get_func_sig, get_func_attrs} },
1649+
{ jl_f_current_scope_addr, new JuliaFunction<>{XSTR(jl_f_current_scope), get_func_sig, get_func_attrs} },
16481650
};
16491651
return builtins;
16501652
}
@@ -2117,6 +2119,7 @@ static jl_cgval_t emit_sparam(jl_codectx_t &ctx, size_t i);
21172119
static Value *emit_condition(jl_codectx_t &ctx, const jl_cgval_t &condV, const Twine &msg);
21182120
static Value *get_current_task(jl_codectx_t &ctx);
21192121
static Value *get_current_ptls(jl_codectx_t &ctx);
2122+
static Value *get_scope_field(jl_codectx_t &ctx);
21202123
static Value *get_tls_world_age_field(jl_codectx_t &ctx);
21212124
static void CreateTrap(IRBuilder<> &irbuilder, bool create_new_block = true);
21222125
static CallInst *emit_jlcall(jl_codectx_t &ctx, FunctionCallee theFptr, Value *theF,
@@ -4944,6 +4947,14 @@ static bool emit_builtin_call(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
49444947
return true;
49454948
}
49464949

4950+
else if (f == jl_builtin_current_scope && (nargs == 0)) {
4951+
jl_aliasinfo_t scope_ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_gcframe);
4952+
Instruction *v = scope_ai.decorateInst(
4953+
ctx.builder.CreateAlignedLoad(ctx.types().T_prjlvalue, get_scope_field(ctx), ctx.types().alignof_ptr));
4954+
*ret = mark_julia_type(ctx, v, /*boxed*/ true, rt);
4955+
return true;
4956+
}
4957+
49474958
else if (f == jl_builtin_donotdelete) {
49484959
// For now we emit this as a vararg call to the builtin
49494960
// (which doesn't look at the arguments). In the future,

src/staticdata.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ extern "C" {
100100
// TODO: put WeakRefs on the weak_refs list during deserialization
101101
// TODO: handle finalizers
102102

103-
#define NUM_TAGS 192
103+
#define NUM_TAGS 193
104104

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

0 commit comments

Comments
 (0)