Skip to content

Commit 09a6ff8

Browse files
Reduce codegen lock scope (#46836)
1 parent b03439c commit 09a6ff8

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

src/aotcompile.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
267267
jl_method_instance_t *mi = NULL;
268268
jl_code_info_t *src = NULL;
269269
JL_GC_PUSH1(&src);
270-
JL_LOCK(&jl_codegen_lock);
271270
auto ct = jl_current_task;
272271
ct->reentrant_codegen++;
273272
orc::ThreadSafeContext ctx;
@@ -278,16 +277,18 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
278277
}
279278
orc::ThreadSafeModule &clone = llvmmod ? *unwrap(llvmmod) : backing;
280279
auto ctxt = clone.getContext();
281-
jl_codegen_params_t params(ctxt);
282-
params.params = cgparams;
280+
283281
uint64_t compiler_start_time = 0;
284282
uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed(&jl_measure_compile_time_enabled);
285283
if (measure_compile_time_enabled)
286284
compiler_start_time = jl_hrtime();
287285

288-
params.imaging = imaging;
289-
290286
// compile all methods for the current world and type-inference world
287+
288+
JL_LOCK(&jl_codegen_lock);
289+
jl_codegen_params_t params(ctxt);
290+
params.params = cgparams;
291+
params.imaging = imaging;
291292
size_t compile_for[] = { jl_typeinf_world, jl_atomic_load_acquire(&jl_world_counter) };
292293
for (int worlds = 0; worlds < 2; worlds++) {
293294
params.world = compile_for[worlds];
@@ -332,15 +333,18 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
332333
// finally, make sure all referenced methods also get compiled or fixed up
333334
jl_compile_workqueue(emitted, *clone.getModuleUnlocked(), params, policy);
334335
}
336+
JL_UNLOCK(&jl_codegen_lock); // Might GC
335337
JL_GC_POP();
336338

337339
// process the globals array, before jl_merge_module destroys them
338-
std::vector<std::string> gvars;
340+
std::vector<std::string> gvars(params.globals.size());
339341
data->jl_value_to_llvm.resize(params.globals.size());
340342

343+
size_t idx = 0;
341344
for (auto &global : params.globals) {
342-
data->jl_value_to_llvm.at(gvars.size()) = global.first;
343-
gvars.push_back(std::string(global.second->getName()));
345+
gvars[idx] = global.second->getName().str();
346+
data->jl_value_to_llvm[idx] = global.first;
347+
idx++;
344348
}
345349
CreateNativeMethods += emitted.size();
346350

@@ -423,7 +427,6 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
423427
jl_ExecutionEngine->releaseContext(std::move(ctx));
424428
}
425429
ct->reentrant_codegen--;
426-
JL_UNLOCK(&jl_codegen_lock); // Might GC
427430
return (void*)data;
428431
}
429432

@@ -1013,17 +1016,18 @@ void jl_get_llvmf_defn_impl(jl_llvmf_dump_t* dump, jl_method_instance_t *mi, siz
10131016

10141017
// emit this function into a new llvm module
10151018
if (src && jl_is_code_info(src)) {
1016-
JL_LOCK(&jl_codegen_lock);
10171019
auto ctx = jl_ExecutionEngine->getContext();
1018-
jl_codegen_params_t output(*ctx);
1019-
output.world = world;
1020-
output.params = &params;
1021-
orc::ThreadSafeModule m = jl_create_llvm_module(name_from_method_instance(mi), output.tsctx, output.imaging);
1020+
orc::ThreadSafeModule m = jl_create_llvm_module(name_from_method_instance(mi), *ctx, imaging_default());
10221021
uint64_t compiler_start_time = 0;
10231022
uint8_t measure_compile_time_enabled = jl_atomic_load_relaxed(&jl_measure_compile_time_enabled);
10241023
if (measure_compile_time_enabled)
10251024
compiler_start_time = jl_hrtime();
1025+
JL_LOCK(&jl_codegen_lock);
1026+
jl_codegen_params_t output(*ctx);
1027+
output.world = world;
1028+
output.params = &params;
10261029
auto decls = jl_emit_code(m, mi, src, jlrettype, output);
1030+
JL_UNLOCK(&jl_codegen_lock); // Might GC
10271031

10281032
Function *F = NULL;
10291033
if (m) {
@@ -1059,7 +1063,6 @@ void jl_get_llvmf_defn_impl(jl_llvmf_dump_t* dump, jl_method_instance_t *mi, siz
10591063
JL_GC_POP();
10601064
if (measure_compile_time_enabled)
10611065
jl_atomic_fetch_add_relaxed(&jl_cumulative_compile_time, (jl_hrtime() - compiler_start_time));
1062-
JL_UNLOCK(&jl_codegen_lock); // Might GC
10631066
if (F) {
10641067
dump->TSM = wrap(new orc::ThreadSafeModule(std::move(m)));
10651068
dump->F = wrap(F);

src/gf.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,8 +3539,6 @@ int jl_has_concrete_subtype(jl_value_t *typ)
35393539
return ((jl_datatype_t*)typ)->has_concrete_subtype;
35403540
}
35413541

3542-
#define typeinf_lock jl_codegen_lock
3543-
35443542
JL_DLLEXPORT void jl_typeinf_timing_begin(void)
35453543
{
35463544
jl_task_t *ct = jl_current_task;
@@ -3563,7 +3561,7 @@ JL_DLLEXPORT void jl_typeinf_timing_end(void)
35633561

35643562
JL_DLLEXPORT void jl_typeinf_lock_begin(void)
35653563
{
3566-
JL_LOCK(&typeinf_lock);
3564+
JL_LOCK(&jl_codegen_lock);
35673565
//Although this is claiming to be a typeinfer lock, it is actually
35683566
//affecting the codegen lock count, not type inference's inferencing count
35693567
jl_task_t *ct = jl_current_task;
@@ -3574,7 +3572,7 @@ JL_DLLEXPORT void jl_typeinf_lock_end(void)
35743572
{
35753573
jl_task_t *ct = jl_current_task;
35763574
ct->reentrant_codegen--;
3577-
JL_UNLOCK(&typeinf_lock);
3575+
JL_UNLOCK(&jl_codegen_lock);
35783576
}
35793577

35803578
#ifdef __cplusplus

src/jitlayers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,9 @@ jl_value_t *jl_dump_method_asm_impl(jl_method_instance_t *mi, size_t world,
551551
}
552552
JL_GC_POP();
553553
}
554+
JL_UNLOCK(&jl_codegen_lock);
554555
if (!--ct->reentrant_codegen && measure_compile_time_enabled)
555556
jl_atomic_fetch_add_relaxed(&jl_cumulative_compile_time, (jl_hrtime() - compiler_start_time));
556-
JL_UNLOCK(&jl_codegen_lock);
557557
}
558558
if (specfptr != 0)
559559
return jl_dump_fptr_asm(specfptr, raw_mc, asm_variant, debuginfo, binary);

0 commit comments

Comments
 (0)