Repository navigation
Don't switch tasks when the context registry is contended - #735
Merged
Merged
Conversation
The registry of diagnostic states is locked when a context is created or disposed of, and when running passes or emitting code looks up a context's diagnostic state. It used a ReentrantLock, which switches tasks when it is contended. That is not allowed in generated functions, and on Julia 1.10 and 1.11 GPUCompiler does this while holding Julia's codegen lock: a task that switches holding a runtime lock and resumes on another thread corrupts the runtime's per-thread lists of held locks, which crashed concurrent compilations in jl_eh_restore_state. The critical sections are a few dictionary operations that don't switch tasks, so use a spin lock. Do the same for the lock that records exceptions thrown by callbacks, which run while compiling.
Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@
## main #735 +/- ##
==========================================
+ Coverage 94.39% 94.47% +0.08%
==========================================
Files 59 59
Lines 10020 10011 -9
==========================================
Hits 9458 9458
+ Misses 562 553 -9
... and 3 files with indirect coverage changes 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GPUCompiler's test that compiles from 8 threads at once segfaulted on Julia 1.10 on aarch64 (https://github.com/JuliaGPU/GPUCompiler.jl/actions/runs/46842403015/job/143326648823):
On Julia 1.10 and 1.11, GPUCompiler holds Julia's codegen lock while it generates and optimizes code. Julia records the runtime locks a thread holds in a per-thread list, which exception handlers save and restore, so a task must not switch while it holds one. LLVM.jl's registry of diagnostic states is locked with a
ReentrantLockwhen a context is created or disposed of, and when running passes or emitting code looks up a context's diagnostic state. If another thread is creating or disposing of a context at that moment, the lock is contended and the compiling task switches. If it resumes on another thread, releasing the codegen lock removes it from that thread's list instead, which underflows the list's length when it is empty, after which restoring an exception handler on that thread can unlock garbage. Generated functions have the same constraint: on 1.10 and 1.11, a contended lock there throws "task switch not allowed from inside staged nor pure functions" when it attempts to switch to another task.This uses
Threads.SpinLockfor that registry, and for the lock that records exceptions thrown by callbacks, which also run while compiling. Both critical sections are a few dictionary or field operations that don't switch tasks.libraries_lockhas the same problem in principle, as GPUCompiler looks up Julia's library while holding the codegen lock when generating output, but registering a library holds it across loading and initializing the library, so a spin lock doesn't fit there. That is left as is.With Julia 1.10 on x86_64, compiling from 6 threads in a loop while 2 tasks create and dispose of contexts crashed 5 out of 24 runs, with a few hundred task switches under the codegen lock per run. With this change, 12 out of 12 runs passed with zero task switches under the codegen lock. The new test runs a generator that creates a context while another thread holds the registry; it fails with a
ReentrantLockon 1.10 and 1.11, and passes on 1.10 through 1.12.