From 213b5901ceb68ed5c37a9178647a2433d353d012 Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Wed, 18 Aug 2021 14:42:12 -0400 Subject: [PATCH] Make the compile time flag actually atomic, not just data race free. --- src/aotcompile.cpp | 10 ++++++---- src/gf.c | 11 ++++++++--- src/jitlayers.cpp | 20 ++++++++++++-------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/aotcompile.cpp b/src/aotcompile.cpp index 6906d81b805d4..e6afa691554d3 100644 --- a/src/aotcompile.cpp +++ b/src/aotcompile.cpp @@ -286,7 +286,8 @@ void *jl_create_native(jl_array_t *methods, const jl_cgparams_t cgparams, int _p JL_GC_PUSH1(&src); JL_LOCK(&codegen_lock); uint64_t compiler_start_time = 0; - if (jl_atomic_load(&jl_measure_compile_time)) + uint8_t measure_compile_time = jl_atomic_load(&jl_measure_compile_time); + if (measure_compile_time) compiler_start_time = jl_hrtime(); CompilationPolicy policy = (CompilationPolicy) _policy; @@ -414,7 +415,7 @@ void *jl_create_native(jl_array_t *methods, const jl_cgparams_t cgparams, int _p } data->M = std::move(clone); - if (jl_atomic_load(&jl_measure_compile_time)) + if (measure_compile_time) jl_atomic_fetch_add(&jl_measure_compile_time, (jl_hrtime() - compiler_start_time)); if (policy == CompilationPolicy::ImagingMode) imaging_mode = 0; @@ -915,7 +916,8 @@ void *jl_get_llvmf_defn(jl_method_instance_t *mi, size_t world, char getwrapper, jl_llvm_functions_t decls; JL_LOCK(&codegen_lock); uint64_t compiler_start_time = 0; - if (jl_atomic_load(&jl_measure_compile_time)) + uint8_t measure_compile_time = jl_atomic_load(&jl_measure_compile_time); + if (measure_compile_time) compiler_start_time = jl_hrtime(); std::tie(m, decls) = jl_emit_code(mi, src, jlrettype, output); @@ -940,7 +942,7 @@ void *jl_get_llvmf_defn(jl_method_instance_t *mi, size_t world, char getwrapper, m.release(); // the return object `llvmf` will be the owning pointer } JL_GC_POP(); - if (jl_atomic_load(&jl_measure_compile_time)) + if (measure_compile_time) jl_atomic_fetch_add(&jl_measure_compile_time, (jl_hrtime() - compiler_start_time)); JL_UNLOCK(&codegen_lock); // Might GC if (F) diff --git a/src/gf.c b/src/gf.c index 5809721ceaadd..7dd39dd84d20e 100644 --- a/src/gf.c +++ b/src/gf.c @@ -3160,18 +3160,23 @@ int jl_has_concrete_subtype(jl_value_t *typ) #define typeinf_lock codegen_lock static uint64_t inference_start_time = 0; +static uint8_t inference_is_measuring_compile_time = 0; JL_DLLEXPORT void jl_typeinf_begin(void) { JL_LOCK(&typeinf_lock); - if (jl_atomic_load(&jl_measure_compile_time)) + if (jl_atomic_load(&jl_measure_compile_time)) { inference_start_time = jl_hrtime(); + inference_is_measuring_compile_time = 1; + } } JL_DLLEXPORT void jl_typeinf_end(void) { - if (typeinf_lock.count == 1 && jl_atomic_load(&jl_measure_compile_time)) - jl_atomic_fetch_add(&jl_measure_compile_time, (jl_hrtime() - inference_start_time)); + if (typeinf_lock.count == 1 && inference_is_measuring_compile_time) { + jl_atomic_fetch_add(&jl_cumulative_compile_time, (jl_hrtime() - inference_start_time)); + inference_is_measuring_compile_time = 0; + } JL_UNLOCK(&typeinf_lock); } diff --git a/src/jitlayers.cpp b/src/jitlayers.cpp index e20daae5cb1d8..666a0f71524c7 100644 --- a/src/jitlayers.cpp +++ b/src/jitlayers.cpp @@ -238,7 +238,8 @@ int jl_compile_extern_c(void *llvmmod, void *p, void *sysimg, jl_value_t *declrt { JL_LOCK(&codegen_lock); uint64_t compiler_start_time = 0; - if (jl_atomic_load(&jl_measure_compile_time)) + uint8_t measure_compile_time = jl_atomic_load(&jl_measure_compile_time); + if (measure_compile_time) compiler_start_time = jl_hrtime(); jl_codegen_params_t params; jl_codegen_params_t *pparams = (jl_codegen_params_t*)p; @@ -262,7 +263,7 @@ int jl_compile_extern_c(void *llvmmod, void *p, void *sysimg, jl_value_t *declrt if (success && llvmmod == NULL) jl_add_to_ee(std::unique_ptr(into)); } - if (codegen_lock.count == 1 && jl_atomic_load(&jl_measure_compile_time)) + if (codegen_lock.count == 1 && measure_compile_time) jl_atomic_fetch_add(&jl_cumulative_compile_time, (jl_hrtime() - compiler_start_time)); JL_UNLOCK(&codegen_lock); return success; @@ -319,7 +320,8 @@ jl_code_instance_t *jl_generate_fptr(jl_method_instance_t *mi JL_PROPAGATES_ROOT { JL_LOCK(&codegen_lock); // also disables finalizers, to prevent any unexpected recursion uint64_t compiler_start_time = 0; - if (jl_atomic_load(&jl_measure_compile_time)) + uint8_t measure_compile_time = jl_atomic_load(&jl_measure_compile_time); + if (measure_compile_time) compiler_start_time = jl_hrtime(); // if we don't have any decls already, try to generate it now jl_code_info_t *src = NULL; @@ -357,7 +359,7 @@ jl_code_instance_t *jl_generate_fptr(jl_method_instance_t *mi JL_PROPAGATES_ROOT else { codeinst = NULL; } - if (codegen_lock.count == 1 && jl_atomic_load(&jl_measure_compile_time)) + if (codegen_lock.count == 1 && measure_compile_time) jl_atomic_fetch_add(&jl_cumulative_compile_time, (jl_hrtime() - compiler_start_time)); JL_UNLOCK(&codegen_lock); JL_GC_POP(); @@ -372,7 +374,8 @@ void jl_generate_fptr_for_unspecialized(jl_code_instance_t *unspec) } JL_LOCK(&codegen_lock); uint64_t compiler_start_time = 0; - if (jl_atomic_load(&jl_measure_compile_time)) + uint8_t measure_compile_time = jl_atomic_load(&jl_measure_compile_time); + if (measure_compile_time) compiler_start_time = jl_hrtime(); if (unspec->invoke == NULL) { jl_code_info_t *src = NULL; @@ -400,7 +403,7 @@ void jl_generate_fptr_for_unspecialized(jl_code_instance_t *unspec) } JL_GC_POP(); } - if (codegen_lock.count == 1 && jl_atomic_load(&jl_measure_compile_time)) + if (codegen_lock.count == 1 && measure_compile_time) jl_atomic_fetch_add(&jl_cumulative_compile_time, (jl_hrtime() - compiler_start_time)); JL_UNLOCK(&codegen_lock); // Might GC } @@ -424,7 +427,8 @@ jl_value_t *jl_dump_method_asm(jl_method_instance_t *mi, size_t world, // so create an exception here so we can print pretty our lies JL_LOCK(&codegen_lock); // also disables finalizers, to prevent any unexpected recursion uint64_t compiler_start_time = 0; - if (jl_atomic_load(&jl_measure_compile_time)) + uint8_t measure_compile_time = jl_atomic_load(&jl_measure_compile_time); + if (measure_compile_time) compiler_start_time = jl_hrtime(); specfptr = (uintptr_t)codeinst->specptr.fptr; if (specfptr == 0) { @@ -449,7 +453,7 @@ jl_value_t *jl_dump_method_asm(jl_method_instance_t *mi, size_t world, } JL_GC_POP(); } - if (jl_atomic_load(&jl_measure_compile_time)) + if (measure_compile_time) jl_atomic_fetch_add(&jl_cumulative_compile_time, (jl_hrtime() - compiler_start_time)); JL_UNLOCK(&codegen_lock); }