Skip to content

Commit

Permalink
init: avoid an undesirable compiler optimization
Browse files Browse the repository at this point in the history
jl_current_task is not constant after this point in the function, so we
split the function so that the compiler won't try to optimize it
incorrectly (hoisting the JL_GC_PUSH to the top of the function for
example).

Fixes #42346
  • Loading branch information
vtjnash committed Sep 24, 2021
1 parent b9fba13 commit 5bf74e9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,8 @@ static void restore_fp_env(void)
}
}

static NOINLINE void _finish_julia_init(JL_IMAGE_SEARCH rel, jl_ptls_t ptls, jl_task_t *ct);

JL_DLLEXPORT void julia_init(JL_IMAGE_SEARCH rel)
{
jl_init_timing();
Expand Down Expand Up @@ -722,9 +724,13 @@ JL_DLLEXPORT void julia_init(JL_IMAGE_SEARCH rel)
jl_init_threading();

jl_ptls_t ptls = jl_init_threadtls(0);
jl_init_root_task(ptls, stack_lo, stack_hi);
jl_task_t *ct = jl_current_task;
// warning: this changes `jl_current_task`, so be careful not to call that from this function
jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi);
_finish_julia_init(rel, ptls, ct);
}

static NOINLINE void _finish_julia_init(JL_IMAGE_SEARCH rel, jl_ptls_t ptls, jl_task_t *ct)
{
jl_init_threadinginfra();

jl_resolve_sysimg_location(rel);
Expand Down
2 changes: 1 addition & 1 deletion src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ void jl_init_intrinsic_functions(void);
void jl_init_intrinsic_properties(void);
void jl_init_tasks(void) JL_GC_DISABLED;
void jl_init_stack_limits(int ismaster, void **stack_hi, void **stack_lo);
void jl_init_root_task(jl_ptls_t ptls, void *stack_lo, void *stack_hi);
jl_task_t *jl_init_root_task(jl_ptls_t ptls, void *stack_lo, void *stack_hi);
void jl_init_serializer(void);
void jl_gc_init(void);
void jl_init_uv(void);
Expand Down
5 changes: 3 additions & 2 deletions src/partr.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ void jl_threadfun(void *arg)
jl_ptls_t ptls = jl_init_threadtls(targ->tid);
void *stack_lo, *stack_hi;
jl_init_stack_limits(0, &stack_lo, &stack_hi);
jl_init_root_task(ptls, stack_lo, stack_hi);
// warning: this changes `jl_current_task`, so be careful not to call that from this function
jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi);
jl_install_thread_signal_handler(ptls);

// set up sleep mechanism for this thread
Expand All @@ -262,7 +263,7 @@ void jl_threadfun(void *arg)
free(targ);

(void)jl_gc_unsafe_enter(ptls);
jl_finish_task(jl_current_task); // noreturn
jl_finish_task(ct); // noreturn
}


Expand Down
5 changes: 3 additions & 2 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ static char *jl_alloc_fiber(jl_ucontext_t *t, size_t *ssize, jl_task_t *owner) J
#endif

// Initialize a root task using the given stack.
void jl_init_root_task(jl_ptls_t ptls, void *stack_lo, void *stack_hi)
jl_task_t *jl_init_root_task(jl_ptls_t ptls, void *stack_lo, void *stack_hi)
{
assert(ptls->root_task == NULL);
// We need `gcstack` in `Task` to allocate Julia objects; *including* the `Task` type.
Expand Down Expand Up @@ -1327,13 +1327,14 @@ void jl_init_root_task(jl_ptls_t ptls, void *stack_lo, void *stack_hi)
#endif
if (jl_setjmp(ptls->copy_stack_ctx.uc_mcontext, 0))
start_task(); // sanitizer_finish_switch_fiber is part of start_task
return;
return ct;
}
ssize = JL_STACK_SIZE;
char *stkbuf = jl_alloc_fiber(&ptls->base_ctx, &ssize, NULL);
ptls->stackbase = stkbuf + ssize;
ptls->stacksize = ssize;
#endif
return ct;
}

JL_DLLEXPORT int jl_is_task_started(jl_task_t *t) JL_NOTSAFEPOINT
Expand Down

0 comments on commit 5bf74e9

Please sign in to comment.