Skip to content

Commit f025f7f

Browse files
committed
Track reentrancy in current task state
1 parent 976d642 commit f025f7f

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

src/aotcompile.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
274274
jl_code_info_t *src = NULL;
275275
JL_GC_PUSH1(&src);
276276
JL_LOCK(&jl_codegen_lock);
277+
auto ct = jl_current_task;
278+
ct->reentrant_codegen++;
277279
orc::ThreadSafeContext ctx;
278280
orc::ThreadSafeModule backing;
279281
if (!llvmmod) {
@@ -425,6 +427,7 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
425427
if (ctx.getContext()) {
426428
jl_ExecutionEngine->releaseContext(std::move(ctx));
427429
}
430+
ct->reentrant_codegen--;
428431
JL_UNLOCK(&jl_codegen_lock); // Might GC
429432
return (void*)data;
430433
}

src/gf.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ jl_code_info_t *jl_type_infer(jl_method_instance_t *mi, size_t world, int force)
281281
JL_TIMING(INFERENCE);
282282
if (jl_typeinf_func == NULL)
283283
return NULL;
284-
static int in_inference;
285-
if (in_inference > 2)
284+
jl_task_t *ct = jl_current_task;
285+
if (ct->reentrant_inference > 2)
286286
return NULL;
287287

288288
jl_code_info_t *src = NULL;
@@ -302,15 +302,14 @@ jl_code_info_t *jl_type_infer(jl_method_instance_t *mi, size_t world, int force)
302302
jl_printf(JL_STDERR, "\n");
303303
}
304304
#endif
305-
jl_task_t *ct = jl_current_task;
306305
int last_errno = errno;
307306
#ifdef _OS_WINDOWS_
308307
DWORD last_error = GetLastError();
309308
#endif
310309
size_t last_age = ct->world_age;
311310
ct->world_age = jl_typeinf_world;
312311
mi->inInference = 1;
313-
in_inference++;
312+
ct->reentrant_inference++;
314313
JL_TRY {
315314
src = (jl_code_info_t*)jl_apply(fargs, 3);
316315
}
@@ -331,7 +330,7 @@ jl_code_info_t *jl_type_infer(jl_method_instance_t *mi, size_t world, int force)
331330
src = NULL;
332331
}
333332
ct->world_age = last_age;
334-
in_inference--;
333+
ct->reentrant_inference--;
335334
mi->inInference = 0;
336335
#ifdef _OS_WINDOWS_
337336
SetLastError(last_error);
@@ -3372,37 +3371,37 @@ int jl_has_concrete_subtype(jl_value_t *typ)
33723371

33733372
#define typeinf_lock jl_codegen_lock
33743373

3375-
static jl_mutex_t inference_timing_mutex;
3376-
static uint64_t inference_start_time = 0;
3377-
static uint8_t inference_is_measuring_compile_time = 0;
3378-
33793374
JL_DLLEXPORT void jl_typeinf_timing_begin(void)
33803375
{
33813376
if (jl_atomic_load_relaxed(&jl_measure_compile_time_enabled)) {
3382-
JL_LOCK_NOGC(&inference_timing_mutex);
3383-
if (inference_is_measuring_compile_time++ == 0) {
3384-
inference_start_time = jl_hrtime();
3385-
}
3386-
JL_UNLOCK_NOGC(&inference_timing_mutex);
3377+
jl_task_t *ct = jl_current_task;
3378+
if (ct->inference_start_time == 0 && ct->reentrant_inference == 1)
3379+
ct->inference_start_time = jl_hrtime();
33873380
}
33883381
}
33893382

33903383
JL_DLLEXPORT void jl_typeinf_timing_end(void)
33913384
{
3392-
JL_LOCK_NOGC(&inference_timing_mutex);
3393-
if (--inference_is_measuring_compile_time == 0) {
3394-
jl_atomic_fetch_add_relaxed(&jl_cumulative_compile_time, (jl_hrtime() - inference_start_time));
3385+
jl_task_t *ct = jl_current_task;
3386+
if (ct->inference_start_time != 0 && ct->reentrant_inference == 1) {
3387+
jl_atomic_fetch_add_relaxed(&jl_cumulative_compile_time, (jl_hrtime() - ct->inference_start_time));
3388+
ct->inference_start_time = 0;
33953389
}
3396-
JL_UNLOCK_NOGC(&inference_timing_mutex);
33973390
}
33983391

33993392
JL_DLLEXPORT void jl_typeinf_lock_begin(void)
34003393
{
34013394
JL_LOCK(&typeinf_lock);
3395+
//Although this is claiming to be a typeinfer lock, it is actually
3396+
//affecting the codegen lock count, not type inference's inferencing count
3397+
jl_task_t *ct = jl_current_task;
3398+
ct->reentrant_codegen++;
34023399
}
34033400

34043401
JL_DLLEXPORT void jl_typeinf_lock_end(void)
34053402
{
3403+
jl_task_t *ct = jl_current_task;
3404+
ct->reentrant_codegen--;
34063405
JL_UNLOCK(&typeinf_lock);
34073406
}
34083407

src/jitlayers.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ extern "C" JL_DLLEXPORT
295295
int jl_compile_extern_c_impl(LLVMOrcThreadSafeModuleRef llvmmod, void *p, void *sysimg, jl_value_t *declrt, jl_value_t *sigt)
296296
{
297297
JL_LOCK(&jl_codegen_lock);
298+
auto ct = jl_current_task;
299+
ct->reentrant_codegen++;
298300
uint64_t compiler_start_time = 0;
299301
uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed(&jl_measure_compile_time_enabled);
300302
if (measure_compile_time_enabled)
@@ -329,11 +331,12 @@ int jl_compile_extern_c_impl(LLVMOrcThreadSafeModuleRef llvmmod, void *p, void *
329331
if (success && llvmmod == NULL)
330332
jl_ExecutionEngine->addModule(std::move(*into));
331333
}
332-
if (jl_codegen_lock.count == 1 && measure_compile_time_enabled)
334+
if (ct->reentrant_codegen == 1 && measure_compile_time_enabled)
333335
jl_atomic_fetch_add_relaxed(&jl_cumulative_compile_time, (jl_hrtime() - compiler_start_time));
334336
if (ctx.getContext()) {
335337
jl_ExecutionEngine->releaseContext(std::move(ctx));
336338
}
339+
ct->reentrant_codegen--;
337340
JL_UNLOCK(&jl_codegen_lock);
338341
return success;
339342
}
@@ -386,6 +389,8 @@ extern "C" JL_DLLEXPORT
386389
jl_code_instance_t *jl_generate_fptr_impl(jl_method_instance_t *mi JL_PROPAGATES_ROOT, size_t world)
387390
{
388391
JL_LOCK(&jl_codegen_lock); // also disables finalizers, to prevent any unexpected recursion
392+
auto ct = jl_current_task;
393+
ct->reentrant_codegen++;
389394
uint64_t compiler_start_time = 0;
390395
uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed(&jl_measure_compile_time_enabled);
391396
bool is_recompile = false;
@@ -436,12 +441,13 @@ jl_code_instance_t *jl_generate_fptr_impl(jl_method_instance_t *mi JL_PROPAGATES
436441
else {
437442
codeinst = NULL;
438443
}
439-
if (jl_codegen_lock.count == 1 && measure_compile_time_enabled) {
444+
if (ct->reentrant_codegen == 1 && measure_compile_time_enabled) {
440445
uint64_t t_comp = jl_hrtime() - compiler_start_time;
441446
if (is_recompile)
442447
jl_atomic_fetch_add_relaxed(&jl_cumulative_recompile_time, t_comp);
443448
jl_atomic_fetch_add_relaxed(&jl_cumulative_compile_time, t_comp);
444449
}
450+
ct->reentrant_codegen--;
445451
JL_UNLOCK(&jl_codegen_lock);
446452
JL_GC_POP();
447453
return codeinst;
@@ -454,6 +460,8 @@ void jl_generate_fptr_for_unspecialized_impl(jl_code_instance_t *unspec)
454460
return;
455461
}
456462
JL_LOCK(&jl_codegen_lock);
463+
auto ct = jl_current_task;
464+
ct->reentrant_codegen++;
457465
uint64_t compiler_start_time = 0;
458466
uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed(&jl_measure_compile_time_enabled);
459467
if (measure_compile_time_enabled)
@@ -485,8 +493,9 @@ void jl_generate_fptr_for_unspecialized_impl(jl_code_instance_t *unspec)
485493
}
486494
JL_GC_POP();
487495
}
488-
if (jl_codegen_lock.count == 1 && measure_compile_time_enabled)
496+
if (ct->reentrant_codegen == 1 && measure_compile_time_enabled)
489497
jl_atomic_fetch_add_relaxed(&jl_cumulative_compile_time, (jl_hrtime() - compiler_start_time));
498+
ct->reentrant_codegen--;
490499
JL_UNLOCK(&jl_codegen_lock); // Might GC
491500
}
492501

@@ -508,6 +517,8 @@ jl_value_t *jl_dump_method_asm_impl(jl_method_instance_t *mi, size_t world,
508517
// (using sentinel value `1` instead)
509518
// so create an exception here so we can print pretty our lies
510519
JL_LOCK(&jl_codegen_lock); // also disables finalizers, to prevent any unexpected recursion
520+
auto ct = jl_current_task;
521+
ct->reentrant_codegen--;
511522
uint64_t compiler_start_time = 0;
512523
uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed(&jl_measure_compile_time_enabled);
513524
if (measure_compile_time_enabled)
@@ -535,8 +546,9 @@ jl_value_t *jl_dump_method_asm_impl(jl_method_instance_t *mi, size_t world,
535546
}
536547
JL_GC_POP();
537548
}
538-
if (measure_compile_time_enabled)
549+
if (ct->reentrant_codegen == 1 && measure_compile_time_enabled)
539550
jl_atomic_fetch_add_relaxed(&jl_cumulative_compile_time, (jl_hrtime() - compiler_start_time));
551+
ct->reentrant_codegen--;
540552
JL_UNLOCK(&jl_codegen_lock);
541553
}
542554
if (specfptr != 0)

src/julia.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,9 @@ typedef struct _jl_task_t {
19341934
jl_ucontext_t ctx;
19351935
void *stkbuf; // malloc'd memory (either copybuf or stack)
19361936
size_t bufsz; // actual sizeof stkbuf
1937+
uint64_t inference_start_time; // time when inference started
1938+
unsigned int reentrant_inference; // How many times we've reentered inference
1939+
unsigned int reentrant_codegen; // How many times we've reentered codegen
19371940
unsigned int copy_stack:31; // sizeof stack for copybuf
19381941
unsigned int started:1;
19391942
} jl_task_t;

0 commit comments

Comments
 (0)