Skip to content

Commit

Permalink
Remove most implicit TLS getter calls
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed Jul 3, 2016
1 parent ffd9c76 commit 63babf4
Show file tree
Hide file tree
Showing 21 changed files with 215 additions and 162 deletions.
9 changes: 6 additions & 3 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ JL_DLLEXPORT void jl_method_init_properties(jl_method_t *m)

JL_DLLEXPORT jl_method_t *jl_new_method_uninit(void)
{
jl_ptls_t ptls = jl_get_ptls_states();
jl_method_t *m =
(jl_method_t*)newobj((jl_value_t*)jl_method_type,
NWORDS(sizeof(jl_method_t)));
Expand All @@ -584,7 +585,7 @@ JL_DLLEXPORT jl_method_t *jl_new_method_uninit(void)
m->tvars = NULL;
m->ambig = NULL;
m->roots = NULL;
m->module = jl_current_module;
m->module = ptls->current_module;
m->lambda_template = NULL;
m->name = NULL;
m->file = empty_sym;
Expand Down Expand Up @@ -841,7 +842,8 @@ JL_DLLEXPORT jl_typename_t *jl_new_typename_in(jl_sym_t *name, jl_module_t *modu

JL_DLLEXPORT jl_typename_t *jl_new_typename(jl_sym_t *name)
{
return jl_new_typename_in(name, jl_current_module);
jl_ptls_t ptls = jl_get_ptls_states();
return jl_new_typename_in(name, ptls->current_module);
}

jl_datatype_t *jl_new_abstracttype(jl_value_t *name, jl_datatype_t *super,
Expand Down Expand Up @@ -986,6 +988,7 @@ JL_DLLEXPORT jl_datatype_t *jl_new_datatype(jl_sym_t *name, jl_datatype_t *super
int abstract, int mutabl,
int ninitialized)
{
jl_ptls_t ptls = jl_get_ptls_states();
jl_datatype_t *t=NULL;
jl_typename_t *tn=NULL;
JL_GC_PUSH2(&t, &tn);
Expand Down Expand Up @@ -1032,7 +1035,7 @@ JL_DLLEXPORT jl_datatype_t *jl_new_datatype(jl_sym_t *name, jl_datatype_t *super
else {
tn = jl_new_typename((jl_sym_t*)name);
if (!abstract) {
tn->mt = jl_new_method_table(name, jl_current_module);
tn->mt = jl_new_method_table(name, ptls->current_module);
jl_gc_wb(tn, tn->mt);
}
}
Expand Down
36 changes: 22 additions & 14 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,35 +105,39 @@ static value_t julia_to_scm(fl_context_t *fl_ctx, jl_value_t *v);

value_t fl_defined_julia_global(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
{
jl_ptls_t ptls = jl_get_ptls_states();
// tells whether a var is defined in and *by* the current module
argcount(fl_ctx, "defined-julia-global", nargs, 1);
(void)tosymbol(fl_ctx, args[0], "defined-julia-global");
if (jl_current_module == NULL)
if (ptls->current_module == NULL)
return fl_ctx->F;
jl_sym_t *var = jl_symbol(symbol_name(fl_ctx, args[0]));
jl_binding_t *b =
(jl_binding_t*)ptrhash_get(&jl_current_module->bindings, var);
return (b != HT_NOTFOUND && b->owner==jl_current_module) ? fl_ctx->T : fl_ctx->F;
(jl_binding_t*)ptrhash_get(&ptls->current_module->bindings, var);
return (b != HT_NOTFOUND && b->owner==ptls->current_module) ? fl_ctx->T : fl_ctx->F;
}

value_t fl_current_julia_module(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
{
jl_ptls_t ptls = jl_get_ptls_states();
value_t opaque = cvalue(fl_ctx, jl_ast_ctx(fl_ctx)->jvtype, sizeof(void*));
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = (jl_value_t*)jl_current_module;
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = (jl_value_t*)ptls->current_module;
return opaque;
}

value_t fl_current_module_counter(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
{
jl_ptls_t ptls = jl_get_ptls_states();
static uint32_t fallback_counter = 0;
if (jl_current_module == NULL)
if (ptls->current_module == NULL)
return fixnum(++fallback_counter);
else
return fixnum(jl_module_next_counter(jl_current_module));
return fixnum(jl_module_next_counter(ptls->current_module));
}

value_t fl_invoke_julia_macro(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
{
jl_ptls_t ptls = jl_get_ptls_states();
if (nargs < 1)
argcount(fl_ctx, "invoke-julia-macro", nargs, 1);
jl_lambda_info_t *mfunc = NULL;
Expand All @@ -158,7 +162,7 @@ value_t fl_invoke_julia_macro(fl_context_t *fl_ctx, value_t *args, uint32_t narg
JL_CATCH {
JL_GC_POP();
value_t opaque = cvalue(fl_ctx, jl_ast_ctx(fl_ctx)->jvtype, sizeof(void*));
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = jl_exception_in_transit;
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = ptls->exception_in_transit;
return fl_list2(fl_ctx, jl_ast_ctx(fl_ctx)->error_sym, opaque);
}
// protect result from GC, otherwise it could be freed during future
Expand All @@ -172,7 +176,7 @@ value_t fl_invoke_julia_macro(fl_context_t *fl_ctx, value_t *args, uint32_t narg
fl_gc_handle(fl_ctx, &scm);
value_t scmresult;
jl_module_t *defmod = mfunc->def->module;
if (defmod == NULL || defmod == jl_current_module) {
if (defmod == NULL || defmod == ptls->current_module) {
scmresult = fl_cons(fl_ctx, scm, fl_ctx->F);
}
else {
Expand Down Expand Up @@ -232,14 +236,15 @@ static jl_ast_context_list_t *jl_ast_ctx_freed = NULL;

static jl_ast_context_t *jl_ast_ctx_enter(void)
{
jl_ptls_t ptls = jl_get_ptls_states();
JL_SIGATOMIC_BEGIN();
JL_LOCK_NOGC(&flisp_lock);
jl_ast_context_list_t *node;
jl_ast_context_t *ctx;
// First check if the current task is using one of the contexts
for (node = jl_ast_ctx_using;node;(node = node->next)) {
ctx = jl_ast_context_list_item(node);
if (ctx->task == jl_current_task) {
if (ctx->task == ptls->current_task) {
ctx->ref++;
JL_UNLOCK_NOGC(&flisp_lock);
return ctx;
Expand All @@ -251,7 +256,7 @@ static jl_ast_context_t *jl_ast_ctx_enter(void)
jl_ast_context_list_insert(&jl_ast_ctx_using, node);
ctx = jl_ast_context_list_item(node);
ctx->ref = 1;
ctx->task = jl_current_task;
ctx->task = ptls->current_task;
ctx->roots = NULL;
JL_UNLOCK_NOGC(&flisp_lock);
return ctx;
Expand All @@ -260,7 +265,7 @@ static jl_ast_context_t *jl_ast_ctx_enter(void)
ctx = (jl_ast_context_t*)calloc(1, sizeof(jl_ast_context_t));
// ctx->roots is NULL already due to calloc.
ctx->ref = 1;
ctx->task = jl_current_task;
ctx->task = ptls->current_task;
node = &ctx->list;
jl_ast_context_list_insert(&jl_ast_ctx_using, node);
JL_UNLOCK_NOGC(&flisp_lock);
Expand All @@ -283,10 +288,11 @@ static void jl_ast_ctx_leave(jl_ast_context_t *ctx)

void jl_init_frontend(void)
{
jl_ptls_t ptls = jl_get_ptls_states();
if (jl_ast_ctx_using || jl_ast_ctx_freed)
return;
jl_ast_main_ctx.ref = 1;
jl_ast_main_ctx.task = jl_current_task;
jl_ast_main_ctx.task = ptls->current_task;
jl_ast_context_list_insert(&jl_ast_ctx_using, &jl_ast_main_ctx.list);
jl_init_ast_ctx(&jl_ast_main_ctx);
// To match the one in jl_ast_ctx_leave
Expand Down Expand Up @@ -344,6 +350,7 @@ extern int64_t conv_to_int64(void *data, numerictype_t tag);

static jl_value_t *scm_to_julia_(fl_context_t *fl_ctx, value_t e, int eo)
{
jl_ptls_t ptls = jl_get_ptls_states();
if (fl_isnumber(fl_ctx, e)) {
int64_t i64;
if (isfixnum(e)) {
Expand Down Expand Up @@ -444,7 +451,7 @@ static jl_value_t *scm_to_julia_(fl_context_t *fl_ctx, value_t e, int eo)
if (sym == top_sym) {
scmv = scm_to_julia_(fl_ctx,car_(e),0);
assert(jl_is_symbol(scmv));
temp = jl_module_globalref(jl_base_relative_to(jl_current_module), (jl_sym_t*)scmv);
temp = jl_module_globalref(jl_base_relative_to(ptls->current_module), (jl_sym_t*)scmv);
JL_GC_POP();
return temp;
}
Expand Down Expand Up @@ -644,6 +651,7 @@ JL_DLLEXPORT jl_value_t *jl_parse_string(const char *str, size_t len,
jl_value_t *jl_parse_eval_all(const char *fname,
const char *content, size_t contentlen)
{
jl_ptls_t ptls = jl_get_ptls_states();
if (in_pure_callback)
jl_error("cannot use include inside a generated function");
jl_ast_context_t *ctx = jl_ast_ctx_enter();
Expand Down Expand Up @@ -717,7 +725,7 @@ jl_value_t *jl_parse_eval_all(const char *fname,
jl_rethrow();
else
jl_rethrow_other(jl_new_struct(jl_loaderror_type, form, result,
jl_exception_in_transit));
ptls->exception_in_transit));
}
JL_GC_POP();
return result;
Expand Down
21 changes: 12 additions & 9 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ JL_DLLEXPORT void jl_enter_handler(jl_handler_t *eh)

JL_DLLEXPORT void jl_pop_handler(int n)
{
jl_ptls_t ptls = jl_get_ptls_states();
if (__unlikely(n <= 0))
return;
jl_handler_t *eh = jl_current_task->eh;
jl_handler_t *eh = ptls->current_task->eh;
while (--n > 0)
eh = eh->prev;
jl_eh_restore_state(eh);
Expand Down Expand Up @@ -540,6 +541,7 @@ JL_DLLEXPORT jl_value_t *jl_toplevel_eval_in(jl_module_t *m, jl_value_t *ex)

jl_value_t *jl_toplevel_eval_in_warn(jl_module_t *m, jl_value_t *ex, int delay_warn)
{
jl_ptls_t ptls = jl_get_ptls_states();
static int jl_warn_on_eval = 0;
int last_delay_warn = jl_warn_on_eval;
if (m == NULL)
Expand All @@ -548,8 +550,8 @@ jl_value_t *jl_toplevel_eval_in_warn(jl_module_t *m, jl_value_t *ex, int delay_w
return jl_eval_global_var(m, (jl_sym_t*)ex);
jl_value_t *v=NULL;
int last_lineno = jl_lineno;
jl_module_t *last_m = jl_current_module;
jl_module_t *task_last_m = jl_current_task->current_module;
jl_module_t *last_m = ptls->current_module;
jl_module_t *task_last_m = ptls->current_task->current_module;
if (!delay_warn && jl_options.incremental && jl_generating_output()) {
if (m != last_m) {
jl_printf(JL_STDERR, "WARNING: eval from module %s to %s: \n",
Expand All @@ -567,27 +569,28 @@ jl_value_t *jl_toplevel_eval_in_warn(jl_module_t *m, jl_value_t *ex, int delay_w
jl_error("eval cannot be used in a generated function");
JL_TRY {
jl_warn_on_eval = delay_warn && (jl_warn_on_eval || m != last_m); // compute whether a warning was suppressed
jl_current_task->current_module = jl_current_module = m;
ptls->current_task->current_module = ptls->current_module = m;
v = jl_toplevel_eval(ex);
}
JL_CATCH {
jl_warn_on_eval = last_delay_warn;
jl_lineno = last_lineno;
jl_current_module = last_m;
jl_current_task->current_module = task_last_m;
ptls->current_module = last_m;
ptls->current_task->current_module = task_last_m;
jl_rethrow();
}
jl_warn_on_eval = last_delay_warn;
jl_lineno = last_lineno;
jl_current_module = last_m;
jl_current_task->current_module = task_last_m;
ptls->current_module = last_m;
ptls->current_task->current_module = task_last_m;
assert(v);
return v;
}

JL_CALLABLE(jl_f_isdefined)
{
jl_module_t *m = jl_current_module;
jl_ptls_t ptls = jl_get_ptls_states();
jl_module_t *m = ptls->current_module;
jl_sym_t *s=NULL;
JL_NARGSV(isdefined, 1);
if (jl_is_array(args[0])) {
Expand Down
8 changes: 5 additions & 3 deletions src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ static std::string generate_func_sig(
// ccall(pointer, rettype, (argtypes...), args...)
static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
{
jl_ptls_t ptls = jl_get_ptls_states();
JL_NARGSV(ccall, 3);
jl_value_t *rt=NULL, *at=NULL;
JL_GC_PUSH2(&rt, &at);
Expand Down Expand Up @@ -1069,9 +1070,10 @@ static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
}
}
if (rt == NULL) {
if (jl_exception_in_transit && jl_typeis(jl_exception_in_transit,
jl_undefvarerror_type)
&& jl_is_symbol(args[2])) {
if (ptls->exception_in_transit &&
jl_typeis(ptls->exception_in_transit,
jl_undefvarerror_type) &&
jl_is_symbol(args[2])) {
std::string msg = "ccall return type undefined: " +
std::string(jl_symbol_name((jl_sym_t*)args[2]));
emit_error(msg.c_str(), ctx);
Expand Down
10 changes: 6 additions & 4 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,9 @@ static void emit_write_barrier(jl_codectx_t*, Value*, Value*);

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

// step 1. unpack AST and allocate codegen context for this function
Expand All @@ -4025,7 +4027,7 @@ static std::unique_ptr<Module> emit_function(jl_lambda_info_t *lam, jl_llvm_func
ctx.arrayvars = &arrayvars;
ctx.labels = &labels;
ctx.handlers = &handlers;
ctx.module = lam->def ? lam->def->module : jl_current_module;
ctx.module = lam->def ? lam->def->module : ptls->current_module;
ctx.linfo = lam;
ctx.name = jl_symbol_name(lam->def ? lam->def->name : anonymous_sym);
ctx.funcName = ctx.name;
Expand Down
16 changes: 11 additions & 5 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,9 @@ static void jl_finalize_serializer(ios_t *f) {
}

void jl_typemap_rehash(union jl_typemap_t ml, int8_t offs);
static void jl_reinit_item(ios_t *f, jl_value_t *v, int how, arraylist_t *tracee_list) {
static void jl_reinit_item(ios_t *f, jl_value_t *v, int how, arraylist_t *tracee_list)
{
jl_ptls_t ptls = jl_get_ptls_states();
JL_TRY {
switch (how) {
case 1: { // rehash ObjectIdDict
Expand Down Expand Up @@ -1846,11 +1848,13 @@ static void jl_reinit_item(ios_t *f, jl_value_t *v, int how, arraylist_t *tracee
jl_printf(JL_STDERR, "WARNING: error while reinitializing value ");
jl_static_show(JL_STDERR, v);
jl_printf(JL_STDERR, ":\n");
jl_static_show(JL_STDERR, jl_exception_in_transit);
jl_static_show(JL_STDERR, ptls->exception_in_transit);
jl_printf(JL_STDERR, "\n");
}
}
static jl_array_t *jl_finalize_deserializer(ios_t *f, arraylist_t *tracee_list) {

static jl_array_t *jl_finalize_deserializer(ios_t *f, arraylist_t *tracee_list)
{
jl_array_t *init_order = NULL;
if (mode != MODE_MODULE)
init_order = (jl_array_t*)jl_deserialize_value(f, NULL);
Expand Down Expand Up @@ -1984,6 +1988,7 @@ JL_DLLEXPORT void jl_preload_sysimg_so(const char *fname)

static void jl_restore_system_image_from_stream(ios_t *f)
{
jl_ptls_t ptls = jl_get_ptls_states();
JL_LOCK(&dump_lock); // Might GC
int en = jl_gc_enable(0);
DUMP_MODES last_mode = mode;
Expand Down Expand Up @@ -2011,7 +2016,7 @@ static void jl_restore_system_image_from_stream(ios_t *f)
jl_symbol("Core"));
jl_base_module = (jl_module_t*)jl_get_global(jl_main_module,
jl_symbol("Base"));
jl_current_module = jl_base_module; // run start_image in Base
ptls->current_module = jl_base_module; // run start_image in Base

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

void jl_init_serializer(void)
{
jl_ptls_t ptls = jl_get_ptls_states();
htable_new(&ser_tag, 0);
htable_new(&common_symbol_tag, 0);
htable_new(&fptr_to_id, sizeof(id_to_fptrs)/sizeof(*id_to_fptrs));
Expand Down Expand Up @@ -2474,7 +2480,7 @@ void jl_init_serializer(void)
jl_gotonode_type->name, jl_quotenode_type->name,
jl_globalref_type->name,

jl_root_task,
ptls->root_task,

NULL };

Expand Down
Loading

0 comments on commit 63babf4

Please sign in to comment.