@@ -76,16 +76,16 @@ void jl_jit_globals(std::map<void *, GlobalVariable*> &globals)
76
76
extern " C" JL_DLLEXPORT
77
77
uint64_t jl_cumulative_compile_time_ns_before ()
78
78
{
79
- int tid = jl_threadid ();
80
- jl_measure_compile_time[tid] = 1 ;
81
- return jl_cumulative_compile_time[tid] ;
79
+ // Increment the flag to allow reentrant callers to `@time`.
80
+ jl_atomic_fetch_add (&jl_measure_compile_time_enabled, 1 ) ;
81
+ return jl_atomic_load_relaxed (& jl_cumulative_compile_time) ;
82
82
}
83
83
extern " C" JL_DLLEXPORT
84
84
uint64_t jl_cumulative_compile_time_ns_after ()
85
85
{
86
- int tid = jl_threadid ();
87
- jl_measure_compile_time[tid] = 0 ;
88
- return jl_cumulative_compile_time[tid] ;
86
+ // Decrement the flag when done measuring, allowing other callers to continue measuring.
87
+ jl_atomic_fetch_add (&jl_measure_compile_time_enabled, - 1 ) ;
88
+ return jl_atomic_load_relaxed (& jl_cumulative_compile_time) ;
89
89
}
90
90
91
91
// this generates llvm code for the lambda info
@@ -231,8 +231,8 @@ int jl_compile_extern_c(void *llvmmod, void *p, void *sysimg, jl_value_t *declrt
231
231
{
232
232
JL_LOCK (&codegen_lock);
233
233
uint64_t compiler_start_time = 0 ;
234
- int tid = jl_threadid ( );
235
- if (jl_measure_compile_time[tid] )
234
+ uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed (&jl_measure_compile_time_enabled );
235
+ if (measure_compile_time_enabled )
236
236
compiler_start_time = jl_hrtime ();
237
237
jl_codegen_params_t params;
238
238
jl_codegen_params_t *pparams = (jl_codegen_params_t *)p;
@@ -256,8 +256,8 @@ int jl_compile_extern_c(void *llvmmod, void *p, void *sysimg, jl_value_t *declrt
256
256
if (success && llvmmod == NULL )
257
257
jl_add_to_ee (std::unique_ptr<Module>(into));
258
258
}
259
- if (codegen_lock.count == 1 && jl_measure_compile_time[tid] )
260
- jl_cumulative_compile_time[tid] += (jl_hrtime () - compiler_start_time);
259
+ if (codegen_lock.count == 1 && measure_compile_time_enabled )
260
+ jl_atomic_fetch_add_relaxed (& jl_cumulative_compile_time, (jl_hrtime () - compiler_start_time) );
261
261
JL_UNLOCK (&codegen_lock);
262
262
return success;
263
263
}
@@ -313,8 +313,8 @@ jl_code_instance_t *jl_generate_fptr(jl_method_instance_t *mi JL_PROPAGATES_ROOT
313
313
{
314
314
JL_LOCK (&codegen_lock); // also disables finalizers, to prevent any unexpected recursion
315
315
uint64_t compiler_start_time = 0 ;
316
- int tid = jl_threadid ( );
317
- if (jl_measure_compile_time[tid] )
316
+ uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed (&jl_measure_compile_time_enabled );
317
+ if (measure_compile_time_enabled )
318
318
compiler_start_time = jl_hrtime ();
319
319
// if we don't have any decls already, try to generate it now
320
320
jl_code_info_t *src = NULL ;
@@ -352,8 +352,8 @@ jl_code_instance_t *jl_generate_fptr(jl_method_instance_t *mi JL_PROPAGATES_ROOT
352
352
else {
353
353
codeinst = NULL ;
354
354
}
355
- if (codegen_lock.count == 1 && jl_measure_compile_time[tid] )
356
- jl_cumulative_compile_time[tid] += (jl_hrtime () - compiler_start_time);
355
+ if (codegen_lock.count == 1 && measure_compile_time_enabled )
356
+ jl_atomic_fetch_add_relaxed (& jl_cumulative_compile_time, (jl_hrtime () - compiler_start_time) );
357
357
JL_UNLOCK (&codegen_lock);
358
358
JL_GC_POP ();
359
359
return codeinst;
@@ -367,8 +367,8 @@ void jl_generate_fptr_for_unspecialized(jl_code_instance_t *unspec)
367
367
}
368
368
JL_LOCK (&codegen_lock);
369
369
uint64_t compiler_start_time = 0 ;
370
- int tid = jl_threadid ( );
371
- if (jl_measure_compile_time[tid] )
370
+ uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed (&jl_measure_compile_time_enabled );
371
+ if (measure_compile_time_enabled )
372
372
compiler_start_time = jl_hrtime ();
373
373
if (unspec->invoke == NULL ) {
374
374
jl_code_info_t *src = NULL ;
@@ -396,8 +396,8 @@ void jl_generate_fptr_for_unspecialized(jl_code_instance_t *unspec)
396
396
}
397
397
JL_GC_POP ();
398
398
}
399
- if (codegen_lock.count == 1 && jl_measure_compile_time[tid] )
400
- jl_cumulative_compile_time[tid] += (jl_hrtime () - compiler_start_time);
399
+ if (codegen_lock.count == 1 && measure_compile_time_enabled )
400
+ jl_atomic_fetch_add_relaxed (& jl_cumulative_compile_time, (jl_hrtime () - compiler_start_time) );
401
401
JL_UNLOCK (&codegen_lock); // Might GC
402
402
}
403
403
@@ -420,8 +420,8 @@ jl_value_t *jl_dump_method_asm(jl_method_instance_t *mi, size_t world,
420
420
// so create an exception here so we can print pretty our lies
421
421
JL_LOCK (&codegen_lock); // also disables finalizers, to prevent any unexpected recursion
422
422
uint64_t compiler_start_time = 0 ;
423
- int tid = jl_threadid ( );
424
- if (jl_measure_compile_time[tid] )
423
+ uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed (&jl_measure_compile_time_enabled );
424
+ if (measure_compile_time_enabled )
425
425
compiler_start_time = jl_hrtime ();
426
426
specfptr = (uintptr_t )codeinst->specptr .fptr ;
427
427
if (specfptr == 0 ) {
@@ -446,8 +446,8 @@ jl_value_t *jl_dump_method_asm(jl_method_instance_t *mi, size_t world,
446
446
}
447
447
JL_GC_POP ();
448
448
}
449
- if (jl_measure_compile_time[tid] )
450
- jl_cumulative_compile_time[tid] += (jl_hrtime () - compiler_start_time);
449
+ if (measure_compile_time_enabled )
450
+ jl_atomic_fetch_add_relaxed (& jl_cumulative_compile_time, (jl_hrtime () - compiler_start_time) );
451
451
JL_UNLOCK (&codegen_lock);
452
452
}
453
453
if (specfptr != 0 )
0 commit comments