Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make codegen threadsafe, sinking the necessary lock now into JuliaOJIT #55106

Merged
merged 1 commit into from
Jul 14, 2024

Conversation

vtjnash
Copy link
Member

@vtjnash vtjnash commented Jul 11, 2024

This adds a new helper jl_read_codeinst_invoke that should help manage reading the state out of a CodeInstance correctly everywhere. Then replaces all of the places where we have optimizations in codegen where we check for this (to build a name in the JIT for it) with that call. And finally moves the jl_codegen_lock into jl_ExecutionEngine->jitlock so that it is now more clear that this is only protecting concurrent access to the JIT state it manages (which includes the invoke field of all CodeInstance objects). In a subsequent followup, that jitlock and codeinst_in_flight will be replaced with something akin to the new engine (for CodeInfo inference) which helps partition that JIT lock mechanism (for CodeInstance / JIT insertion) to correspond just to a single CodeInstance, and not globally to all of them.

Copy link
Member

@pchintalapudi pchintalapudi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very exciting!

@@ -412,7 +411,6 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
// finally, make sure all referenced methods also get compiled or fixed up
jl_compile_workqueue(params, policy);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be interesting to parallelize the jl_emit_codeinst call some lines above this; iirc that was occupying a good amount of time during precompilation after the LLVM portion was parallelized. Might also serve to quickly root out any threading bugs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That call doesn't take any significant time. My plan was to move this code around later, so that it can more properly call addModule earlier, when each subgraph is complete, as LLVM is where the time is mostly spent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm misremembering and it was the entirety of this for loop that was expensive... still I think this for loop should be parallel since addModule doesn't get called here as part of image generation.

void *fptr;
jl_read_codeinst_invoke(codeinst, &specsigflags, &invoke, &fptr, 0);
if (specsig ? specsigflags & 0b1 : invoke == jl_fptr_args_addr) {
protoname = jl_ExecutionEngine->getFunctionAtAddress((uintptr_t)fptr, invoke, codeinst);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should just go all the way and just make up names for the functions in codegen; the JIT can substitute in proper names later, but this would drop one more jit dependency from codegen and the associated locking.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is possible. I didn't want to drop the optimization quite yet, but it probably doesn't matter and does make this messier. One reason we did this it this was is because it causes code_llvm to lie about the names, so that those remain somewhat constant between calls. It may still take a bit more work to make sure they are always locally generated names, so that they are constant for code_llvm, and then get corrected later to the real name.

src/codegen.cpp Outdated Show resolved Hide resolved
@vtjnash vtjnash added the merge me PR is reviewed. Merge when all tests are passing label Jul 13, 2024
@IanButterworth IanButterworth merged commit b88f64f into master Jul 14, 2024
7 checks passed
@IanButterworth IanButterworth deleted the jn/concurrent-codegen branch July 14, 2024 17:27
@oscardssmith oscardssmith added compiler:codegen Generation of LLVM IR and native code multithreading Base.Threads and related functionality and removed merge me PR is reviewed. Merge when all tests are passing labels Jul 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:codegen Generation of LLVM IR and native code multithreading Base.Threads and related functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants