Skip to content

Commit 63babf4

Browse files
committed
Remove most implicit TLS getter calls
1 parent ffd9c76 commit 63babf4

21 files changed

+215
-162
lines changed

src/alloc.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ JL_DLLEXPORT void jl_method_init_properties(jl_method_t *m)
576576

577577
JL_DLLEXPORT jl_method_t *jl_new_method_uninit(void)
578578
{
579+
jl_ptls_t ptls = jl_get_ptls_states();
579580
jl_method_t *m =
580581
(jl_method_t*)newobj((jl_value_t*)jl_method_type,
581582
NWORDS(sizeof(jl_method_t)));
@@ -584,7 +585,7 @@ JL_DLLEXPORT jl_method_t *jl_new_method_uninit(void)
584585
m->tvars = NULL;
585586
m->ambig = NULL;
586587
m->roots = NULL;
587-
m->module = jl_current_module;
588+
m->module = ptls->current_module;
588589
m->lambda_template = NULL;
589590
m->name = NULL;
590591
m->file = empty_sym;
@@ -841,7 +842,8 @@ JL_DLLEXPORT jl_typename_t *jl_new_typename_in(jl_sym_t *name, jl_module_t *modu
841842

842843
JL_DLLEXPORT jl_typename_t *jl_new_typename(jl_sym_t *name)
843844
{
844-
return jl_new_typename_in(name, jl_current_module);
845+
jl_ptls_t ptls = jl_get_ptls_states();
846+
return jl_new_typename_in(name, ptls->current_module);
845847
}
846848

847849
jl_datatype_t *jl_new_abstracttype(jl_value_t *name, jl_datatype_t *super,
@@ -986,6 +988,7 @@ JL_DLLEXPORT jl_datatype_t *jl_new_datatype(jl_sym_t *name, jl_datatype_t *super
986988
int abstract, int mutabl,
987989
int ninitialized)
988990
{
991+
jl_ptls_t ptls = jl_get_ptls_states();
989992
jl_datatype_t *t=NULL;
990993
jl_typename_t *tn=NULL;
991994
JL_GC_PUSH2(&t, &tn);
@@ -1032,7 +1035,7 @@ JL_DLLEXPORT jl_datatype_t *jl_new_datatype(jl_sym_t *name, jl_datatype_t *super
10321035
else {
10331036
tn = jl_new_typename((jl_sym_t*)name);
10341037
if (!abstract) {
1035-
tn->mt = jl_new_method_table(name, jl_current_module);
1038+
tn->mt = jl_new_method_table(name, ptls->current_module);
10361039
jl_gc_wb(tn, tn->mt);
10371040
}
10381041
}

src/ast.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,35 +105,39 @@ static value_t julia_to_scm(fl_context_t *fl_ctx, jl_value_t *v);
105105

106106
value_t fl_defined_julia_global(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
107107
{
108+
jl_ptls_t ptls = jl_get_ptls_states();
108109
// tells whether a var is defined in and *by* the current module
109110
argcount(fl_ctx, "defined-julia-global", nargs, 1);
110111
(void)tosymbol(fl_ctx, args[0], "defined-julia-global");
111-
if (jl_current_module == NULL)
112+
if (ptls->current_module == NULL)
112113
return fl_ctx->F;
113114
jl_sym_t *var = jl_symbol(symbol_name(fl_ctx, args[0]));
114115
jl_binding_t *b =
115-
(jl_binding_t*)ptrhash_get(&jl_current_module->bindings, var);
116-
return (b != HT_NOTFOUND && b->owner==jl_current_module) ? fl_ctx->T : fl_ctx->F;
116+
(jl_binding_t*)ptrhash_get(&ptls->current_module->bindings, var);
117+
return (b != HT_NOTFOUND && b->owner==ptls->current_module) ? fl_ctx->T : fl_ctx->F;
117118
}
118119

119120
value_t fl_current_julia_module(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
120121
{
122+
jl_ptls_t ptls = jl_get_ptls_states();
121123
value_t opaque = cvalue(fl_ctx, jl_ast_ctx(fl_ctx)->jvtype, sizeof(void*));
122-
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = (jl_value_t*)jl_current_module;
124+
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = (jl_value_t*)ptls->current_module;
123125
return opaque;
124126
}
125127

126128
value_t fl_current_module_counter(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
127129
{
130+
jl_ptls_t ptls = jl_get_ptls_states();
128131
static uint32_t fallback_counter = 0;
129-
if (jl_current_module == NULL)
132+
if (ptls->current_module == NULL)
130133
return fixnum(++fallback_counter);
131134
else
132-
return fixnum(jl_module_next_counter(jl_current_module));
135+
return fixnum(jl_module_next_counter(ptls->current_module));
133136
}
134137

135138
value_t fl_invoke_julia_macro(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
136139
{
140+
jl_ptls_t ptls = jl_get_ptls_states();
137141
if (nargs < 1)
138142
argcount(fl_ctx, "invoke-julia-macro", nargs, 1);
139143
jl_lambda_info_t *mfunc = NULL;
@@ -158,7 +162,7 @@ value_t fl_invoke_julia_macro(fl_context_t *fl_ctx, value_t *args, uint32_t narg
158162
JL_CATCH {
159163
JL_GC_POP();
160164
value_t opaque = cvalue(fl_ctx, jl_ast_ctx(fl_ctx)->jvtype, sizeof(void*));
161-
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = jl_exception_in_transit;
165+
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = ptls->exception_in_transit;
162166
return fl_list2(fl_ctx, jl_ast_ctx(fl_ctx)->error_sym, opaque);
163167
}
164168
// protect result from GC, otherwise it could be freed during future
@@ -172,7 +176,7 @@ value_t fl_invoke_julia_macro(fl_context_t *fl_ctx, value_t *args, uint32_t narg
172176
fl_gc_handle(fl_ctx, &scm);
173177
value_t scmresult;
174178
jl_module_t *defmod = mfunc->def->module;
175-
if (defmod == NULL || defmod == jl_current_module) {
179+
if (defmod == NULL || defmod == ptls->current_module) {
176180
scmresult = fl_cons(fl_ctx, scm, fl_ctx->F);
177181
}
178182
else {
@@ -232,14 +236,15 @@ static jl_ast_context_list_t *jl_ast_ctx_freed = NULL;
232236

233237
static jl_ast_context_t *jl_ast_ctx_enter(void)
234238
{
239+
jl_ptls_t ptls = jl_get_ptls_states();
235240
JL_SIGATOMIC_BEGIN();
236241
JL_LOCK_NOGC(&flisp_lock);
237242
jl_ast_context_list_t *node;
238243
jl_ast_context_t *ctx;
239244
// First check if the current task is using one of the contexts
240245
for (node = jl_ast_ctx_using;node;(node = node->next)) {
241246
ctx = jl_ast_context_list_item(node);
242-
if (ctx->task == jl_current_task) {
247+
if (ctx->task == ptls->current_task) {
243248
ctx->ref++;
244249
JL_UNLOCK_NOGC(&flisp_lock);
245250
return ctx;
@@ -251,7 +256,7 @@ static jl_ast_context_t *jl_ast_ctx_enter(void)
251256
jl_ast_context_list_insert(&jl_ast_ctx_using, node);
252257
ctx = jl_ast_context_list_item(node);
253258
ctx->ref = 1;
254-
ctx->task = jl_current_task;
259+
ctx->task = ptls->current_task;
255260
ctx->roots = NULL;
256261
JL_UNLOCK_NOGC(&flisp_lock);
257262
return ctx;
@@ -260,7 +265,7 @@ static jl_ast_context_t *jl_ast_ctx_enter(void)
260265
ctx = (jl_ast_context_t*)calloc(1, sizeof(jl_ast_context_t));
261266
// ctx->roots is NULL already due to calloc.
262267
ctx->ref = 1;
263-
ctx->task = jl_current_task;
268+
ctx->task = ptls->current_task;
264269
node = &ctx->list;
265270
jl_ast_context_list_insert(&jl_ast_ctx_using, node);
266271
JL_UNLOCK_NOGC(&flisp_lock);
@@ -283,10 +288,11 @@ static void jl_ast_ctx_leave(jl_ast_context_t *ctx)
283288

284289
void jl_init_frontend(void)
285290
{
291+
jl_ptls_t ptls = jl_get_ptls_states();
286292
if (jl_ast_ctx_using || jl_ast_ctx_freed)
287293
return;
288294
jl_ast_main_ctx.ref = 1;
289-
jl_ast_main_ctx.task = jl_current_task;
295+
jl_ast_main_ctx.task = ptls->current_task;
290296
jl_ast_context_list_insert(&jl_ast_ctx_using, &jl_ast_main_ctx.list);
291297
jl_init_ast_ctx(&jl_ast_main_ctx);
292298
// To match the one in jl_ast_ctx_leave
@@ -344,6 +350,7 @@ extern int64_t conv_to_int64(void *data, numerictype_t tag);
344350

345351
static jl_value_t *scm_to_julia_(fl_context_t *fl_ctx, value_t e, int eo)
346352
{
353+
jl_ptls_t ptls = jl_get_ptls_states();
347354
if (fl_isnumber(fl_ctx, e)) {
348355
int64_t i64;
349356
if (isfixnum(e)) {
@@ -444,7 +451,7 @@ static jl_value_t *scm_to_julia_(fl_context_t *fl_ctx, value_t e, int eo)
444451
if (sym == top_sym) {
445452
scmv = scm_to_julia_(fl_ctx,car_(e),0);
446453
assert(jl_is_symbol(scmv));
447-
temp = jl_module_globalref(jl_base_relative_to(jl_current_module), (jl_sym_t*)scmv);
454+
temp = jl_module_globalref(jl_base_relative_to(ptls->current_module), (jl_sym_t*)scmv);
448455
JL_GC_POP();
449456
return temp;
450457
}
@@ -644,6 +651,7 @@ JL_DLLEXPORT jl_value_t *jl_parse_string(const char *str, size_t len,
644651
jl_value_t *jl_parse_eval_all(const char *fname,
645652
const char *content, size_t contentlen)
646653
{
654+
jl_ptls_t ptls = jl_get_ptls_states();
647655
if (in_pure_callback)
648656
jl_error("cannot use include inside a generated function");
649657
jl_ast_context_t *ctx = jl_ast_ctx_enter();
@@ -717,7 +725,7 @@ jl_value_t *jl_parse_eval_all(const char *fname,
717725
jl_rethrow();
718726
else
719727
jl_rethrow_other(jl_new_struct(jl_loaderror_type, form, result,
720-
jl_exception_in_transit));
728+
ptls->exception_in_transit));
721729
}
722730
JL_GC_POP();
723731
return result;

src/builtins.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,10 @@ JL_DLLEXPORT void jl_enter_handler(jl_handler_t *eh)
210210

211211
JL_DLLEXPORT void jl_pop_handler(int n)
212212
{
213+
jl_ptls_t ptls = jl_get_ptls_states();
213214
if (__unlikely(n <= 0))
214215
return;
215-
jl_handler_t *eh = jl_current_task->eh;
216+
jl_handler_t *eh = ptls->current_task->eh;
216217
while (--n > 0)
217218
eh = eh->prev;
218219
jl_eh_restore_state(eh);
@@ -540,6 +541,7 @@ JL_DLLEXPORT jl_value_t *jl_toplevel_eval_in(jl_module_t *m, jl_value_t *ex)
540541

541542
jl_value_t *jl_toplevel_eval_in_warn(jl_module_t *m, jl_value_t *ex, int delay_warn)
542543
{
544+
jl_ptls_t ptls = jl_get_ptls_states();
543545
static int jl_warn_on_eval = 0;
544546
int last_delay_warn = jl_warn_on_eval;
545547
if (m == NULL)
@@ -548,8 +550,8 @@ jl_value_t *jl_toplevel_eval_in_warn(jl_module_t *m, jl_value_t *ex, int delay_w
548550
return jl_eval_global_var(m, (jl_sym_t*)ex);
549551
jl_value_t *v=NULL;
550552
int last_lineno = jl_lineno;
551-
jl_module_t *last_m = jl_current_module;
552-
jl_module_t *task_last_m = jl_current_task->current_module;
553+
jl_module_t *last_m = ptls->current_module;
554+
jl_module_t *task_last_m = ptls->current_task->current_module;
553555
if (!delay_warn && jl_options.incremental && jl_generating_output()) {
554556
if (m != last_m) {
555557
jl_printf(JL_STDERR, "WARNING: eval from module %s to %s: \n",
@@ -567,27 +569,28 @@ jl_value_t *jl_toplevel_eval_in_warn(jl_module_t *m, jl_value_t *ex, int delay_w
567569
jl_error("eval cannot be used in a generated function");
568570
JL_TRY {
569571
jl_warn_on_eval = delay_warn && (jl_warn_on_eval || m != last_m); // compute whether a warning was suppressed
570-
jl_current_task->current_module = jl_current_module = m;
572+
ptls->current_task->current_module = ptls->current_module = m;
571573
v = jl_toplevel_eval(ex);
572574
}
573575
JL_CATCH {
574576
jl_warn_on_eval = last_delay_warn;
575577
jl_lineno = last_lineno;
576-
jl_current_module = last_m;
577-
jl_current_task->current_module = task_last_m;
578+
ptls->current_module = last_m;
579+
ptls->current_task->current_module = task_last_m;
578580
jl_rethrow();
579581
}
580582
jl_warn_on_eval = last_delay_warn;
581583
jl_lineno = last_lineno;
582-
jl_current_module = last_m;
583-
jl_current_task->current_module = task_last_m;
584+
ptls->current_module = last_m;
585+
ptls->current_task->current_module = task_last_m;
584586
assert(v);
585587
return v;
586588
}
587589

588590
JL_CALLABLE(jl_f_isdefined)
589591
{
590-
jl_module_t *m = jl_current_module;
592+
jl_ptls_t ptls = jl_get_ptls_states();
593+
jl_module_t *m = ptls->current_module;
591594
jl_sym_t *s=NULL;
592595
JL_NARGSV(isdefined, 1);
593596
if (jl_is_array(args[0])) {

src/ccall.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@ static std::string generate_func_sig(
10101010
// ccall(pointer, rettype, (argtypes...), args...)
10111011
static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
10121012
{
1013+
jl_ptls_t ptls = jl_get_ptls_states();
10131014
JL_NARGSV(ccall, 3);
10141015
jl_value_t *rt=NULL, *at=NULL;
10151016
JL_GC_PUSH2(&rt, &at);
@@ -1069,9 +1070,10 @@ static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
10691070
}
10701071
}
10711072
if (rt == NULL) {
1072-
if (jl_exception_in_transit && jl_typeis(jl_exception_in_transit,
1073-
jl_undefvarerror_type)
1074-
&& jl_is_symbol(args[2])) {
1073+
if (ptls->exception_in_transit &&
1074+
jl_typeis(ptls->exception_in_transit,
1075+
jl_undefvarerror_type) &&
1076+
jl_is_symbol(args[2])) {
10751077
std::string msg = "ccall return type undefined: " +
10761078
std::string(jl_symbol_name((jl_sym_t*)args[2]));
10771079
emit_error(msg.c_str(), ctx);

src/codegen.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,9 @@ static void emit_write_barrier(jl_codectx_t*, Value*, Value*);
690690

691691
static void jl_rethrow_with_add(const char *fmt, ...)
692692
{
693-
if (jl_typeis(jl_exception_in_transit, jl_errorexception_type)) {
694-
char *str = jl_string_data(jl_fieldref(jl_exception_in_transit,0));
693+
jl_ptls_t ptls = jl_get_ptls_states();
694+
if (jl_typeis(ptls->exception_in_transit, jl_errorexception_type)) {
695+
char *str = jl_string_data(jl_fieldref(ptls->exception_in_transit,0));
695696
char buf[1024];
696697
va_list args;
697698
va_start(args, fmt);
@@ -3295,7 +3296,7 @@ static jl_cgval_t emit_expr(jl_value_t *expr, jl_codectx_t *ctx)
32953296
Value *val = emit_jlcall(jlnew_func, typ, &args[1], nargs-1, ctx);
32963297
return mark_julia_type(val, true, ty, ctx);
32973298
}
3298-
else if (head == exc_sym) { // *jl_exception_in_transit
3299+
else if (head == exc_sym) { // *ptls->exception_in_transit
32993300
return mark_julia_type(builder.CreateLoad(emit_exc_in_transit(ctx),
33003301
/*isvolatile*/true),
33013302
true, jl_any_type, ctx);
@@ -4009,6 +4010,7 @@ static Function *gen_jlcall_wrapper(jl_lambda_info_t *lam, Function *f, bool sre
40094010
// Compile to LLVM IR, using a specialized signature if applicable.
40104011
static std::unique_ptr<Module> emit_function(jl_lambda_info_t *lam, jl_llvm_functions_t *declarations)
40114012
{
4013+
jl_ptls_t ptls = jl_get_ptls_states();
40124014
assert(declarations && "Capturing declarations is always required");
40134015

40144016
// step 1. unpack AST and allocate codegen context for this function
@@ -4025,7 +4027,7 @@ static std::unique_ptr<Module> emit_function(jl_lambda_info_t *lam, jl_llvm_func
40254027
ctx.arrayvars = &arrayvars;
40264028
ctx.labels = &labels;
40274029
ctx.handlers = &handlers;
4028-
ctx.module = lam->def ? lam->def->module : jl_current_module;
4030+
ctx.module = lam->def ? lam->def->module : ptls->current_module;
40294031
ctx.linfo = lam;
40304032
ctx.name = jl_symbol_name(lam->def ? lam->def->name : anonymous_sym);
40314033
ctx.funcName = ctx.name;

src/dump.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,7 +1796,9 @@ static void jl_finalize_serializer(ios_t *f) {
17961796
}
17971797

17981798
void jl_typemap_rehash(union jl_typemap_t ml, int8_t offs);
1799-
static void jl_reinit_item(ios_t *f, jl_value_t *v, int how, arraylist_t *tracee_list) {
1799+
static void jl_reinit_item(ios_t *f, jl_value_t *v, int how, arraylist_t *tracee_list)
1800+
{
1801+
jl_ptls_t ptls = jl_get_ptls_states();
18001802
JL_TRY {
18011803
switch (how) {
18021804
case 1: { // rehash ObjectIdDict
@@ -1846,11 +1848,13 @@ static void jl_reinit_item(ios_t *f, jl_value_t *v, int how, arraylist_t *tracee
18461848
jl_printf(JL_STDERR, "WARNING: error while reinitializing value ");
18471849
jl_static_show(JL_STDERR, v);
18481850
jl_printf(JL_STDERR, ":\n");
1849-
jl_static_show(JL_STDERR, jl_exception_in_transit);
1851+
jl_static_show(JL_STDERR, ptls->exception_in_transit);
18501852
jl_printf(JL_STDERR, "\n");
18511853
}
18521854
}
1853-
static jl_array_t *jl_finalize_deserializer(ios_t *f, arraylist_t *tracee_list) {
1855+
1856+
static jl_array_t *jl_finalize_deserializer(ios_t *f, arraylist_t *tracee_list)
1857+
{
18541858
jl_array_t *init_order = NULL;
18551859
if (mode != MODE_MODULE)
18561860
init_order = (jl_array_t*)jl_deserialize_value(f, NULL);
@@ -1984,6 +1988,7 @@ JL_DLLEXPORT void jl_preload_sysimg_so(const char *fname)
19841988

19851989
static void jl_restore_system_image_from_stream(ios_t *f)
19861990
{
1991+
jl_ptls_t ptls = jl_get_ptls_states();
19871992
JL_LOCK(&dump_lock); // Might GC
19881993
int en = jl_gc_enable(0);
19891994
DUMP_MODES last_mode = mode;
@@ -2011,7 +2016,7 @@ static void jl_restore_system_image_from_stream(ios_t *f)
20112016
jl_symbol("Core"));
20122017
jl_base_module = (jl_module_t*)jl_get_global(jl_main_module,
20132018
jl_symbol("Base"));
2014-
jl_current_module = jl_base_module; // run start_image in Base
2019+
ptls->current_module = jl_base_module; // run start_image in Base
20152020

20162021
// ensure everything in deser_tag is reassociated with its GlobalValue
20172022
for (i = 2; i < 255; i++) {
@@ -2392,6 +2397,7 @@ JL_DLLEXPORT jl_value_t *jl_restore_incremental(const char *fname)
23922397

23932398
void jl_init_serializer(void)
23942399
{
2400+
jl_ptls_t ptls = jl_get_ptls_states();
23952401
htable_new(&ser_tag, 0);
23962402
htable_new(&common_symbol_tag, 0);
23972403
htable_new(&fptr_to_id, sizeof(id_to_fptrs)/sizeof(*id_to_fptrs));
@@ -2474,7 +2480,7 @@ void jl_init_serializer(void)
24742480
jl_gotonode_type->name, jl_quotenode_type->name,
24752481
jl_globalref_type->name,
24762482

2477-
jl_root_task,
2483+
ptls->root_task,
24782484

24792485
NULL };
24802486

0 commit comments

Comments
 (0)