From f6a3c5f8d07faea45653b34a74175d7e8a46999b Mon Sep 17 00:00:00 2001 From: Kiran Pamnany Date: Wed, 20 Mar 2024 09:28:56 -0400 Subject: [PATCH] Remove some duplicates from emitted compilation traces for Julia 1.10 (#53776) When multiple threads concurrently attempt to compile the same method, `--trace-compile` could emit duplicate `precompile` statements. This small tweak eliminates one source of these duplicates. Backported to 1.10 from https://github.com/JuliaLang/julia/pull/53774. --- src/codegen-stubs.c | 2 +- src/gf.c | 5 +++-- src/jitlayers.cpp | 8 ++++++-- src/julia_internal.h | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/codegen-stubs.c b/src/codegen-stubs.c index de5f2a2770c04..b48ad7b98791f 100644 --- a/src/codegen-stubs.c +++ b/src/codegen-stubs.c @@ -38,7 +38,7 @@ JL_DLLEXPORT void jl_register_fptrs_fallback(uint64_t image_base, const struct _ (void)image_base; (void)fptrs; (void)linfos; (void)n; } -JL_DLLEXPORT jl_code_instance_t *jl_generate_fptr_fallback(jl_method_instance_t *mi JL_PROPAGATES_ROOT, size_t world) +JL_DLLEXPORT jl_code_instance_t *jl_generate_fptr_fallback(jl_method_instance_t *mi JL_PROPAGATES_ROOT, size_t world, int *did_compile) { return NULL; } diff --git a/src/gf.c b/src/gf.c index 1b6251078fa1f..186f8787cf519 100644 --- a/src/gf.c +++ b/src/gf.c @@ -2477,7 +2477,8 @@ jl_code_instance_t *jl_compile_method_internal(jl_method_instance_t *mi, size_t } } - codeinst = jl_generate_fptr(mi, world); + int did_compile = 0; + codeinst = jl_generate_fptr(mi, world, &did_compile); if (!codeinst) { jl_method_instance_t *unspec = jl_get_unspecialized_from_mi(mi); jl_code_instance_t *ucache = jl_get_method_inferred(unspec, (jl_value_t*)jl_any_type, 1, ~(size_t)0); @@ -2517,7 +2518,7 @@ jl_code_instance_t *jl_compile_method_internal(jl_method_instance_t *mi, size_t jl_atomic_store_release(&codeinst->invoke, ucache_invoke); jl_mi_cache_insert(mi, codeinst); } - else { + else if (did_compile) { record_precompile_statement(mi); } jl_atomic_store_relaxed(&codeinst->precompile, 1); diff --git a/src/jitlayers.cpp b/src/jitlayers.cpp index 099d74ccb37c1..d55090e161523 100644 --- a/src/jitlayers.cpp +++ b/src/jitlayers.cpp @@ -463,8 +463,10 @@ void jl_extern_c_impl(jl_value_t *declrt, jl_tupletype_t *sigt) // this compiles li and emits fptr extern "C" JL_DLLEXPORT_CODEGEN -jl_code_instance_t *jl_generate_fptr_impl(jl_method_instance_t *mi JL_PROPAGATES_ROOT, size_t world) +jl_code_instance_t *jl_generate_fptr_impl(jl_method_instance_t *mi JL_PROPAGATES_ROOT, size_t world, int *did_compile) { + if (did_compile != NULL) + *did_compile = 0; auto ct = jl_current_task; bool timed = (ct->reentrant_timing & 1) == 0; if (timed) @@ -518,6 +520,8 @@ jl_code_instance_t *jl_generate_fptr_impl(jl_method_instance_t *mi JL_PROPAGATES _jl_compile_codeinst(codeinst, src, world, *jl_ExecutionEngine->getContext(), is_recompile); if (jl_atomic_load_relaxed(&codeinst->invoke) == NULL) codeinst = NULL; + else if (did_compile != NULL) + *did_compile = 1; } else { codeinst = NULL; @@ -605,7 +609,7 @@ jl_value_t *jl_dump_method_asm_impl(jl_method_instance_t *mi, size_t world, char emit_mc, char getwrapper, const char* asm_variant, const char *debuginfo, char binary) { // printing via disassembly - jl_code_instance_t *codeinst = jl_generate_fptr(mi, world); + jl_code_instance_t *codeinst = jl_generate_fptr(mi, world, NULL); if (codeinst) { uintptr_t fptr = (uintptr_t)jl_atomic_load_acquire(&codeinst->invoke); if (getwrapper) diff --git a/src/julia_internal.h b/src/julia_internal.h index 4fa3154b765d1..85ce95b65613d 100644 --- a/src/julia_internal.h +++ b/src/julia_internal.h @@ -1661,7 +1661,7 @@ JL_DLLEXPORT uint32_t jl_crc32c(uint32_t crc, const char *buf, size_t len); // -- exports from codegen -- // -JL_DLLIMPORT jl_code_instance_t *jl_generate_fptr(jl_method_instance_t *mi JL_PROPAGATES_ROOT, size_t world); +JL_DLLIMPORT jl_code_instance_t *jl_generate_fptr(jl_method_instance_t *mi JL_PROPAGATES_ROOT, size_t world, int *did_compile); JL_DLLIMPORT void jl_generate_fptr_for_unspecialized(jl_code_instance_t *unspec); JL_DLLIMPORT void jl_generate_fptr_for_oc_wrapper(jl_code_instance_t *unspec); JL_DLLIMPORT int jl_compile_extern_c(LLVMOrcThreadSafeModuleRef llvmmod, void *params, void *sysimg, jl_value_t *declrt, jl_value_t *sigt);