Skip to content

[cuda.compute]: Serialize hostjit builds and improve tests#9998

Open
NaderAlAwar wants to merge 1 commit into
NVIDIA:mainfrom
NaderAlAwar:fix-v2-hostjit-concurrent-build
Open

[cuda.compute]: Serialize hostjit builds and improve tests#9998
NaderAlAwar wants to merge 1 commit into
NVIDIA:mainfrom
NaderAlAwar:fix-v2-hostjit-concurrent-build

Conversation

@NaderAlAwar

Copy link
Copy Markdown
Contributor

This fixes the issue seen here https://github.com/NVIDIA/cccl/actions/runs/29535459932/job/87894484801?pr=9985.

Root cause

The v2 (HostJIT) backend compiles each op by embedding clang/LLVM in-process, which is not thread-safe for concurrent compilation (shared global registries, cl::opt state, allocator use). Since cuda.compute releases the GIL around native builds, building distinct ops from multiple threads runs clang concurrently and corrupts shared state — surfacing as a SIGSEGV/SIGBUS/SIGABRT heap-corruption crash. This is not FT-specific; it reproduces on a stock GIL interpreter too. (v1/NVRTC is unaffected — NVRTC is thread-safe.)

Fix

Serialize native compilation with a process-wide mutex around the three public compiler entry points (compileToObject / compileToDeviceBitcode / linkToSharedLibrary) in c/parallel.v2/src/hostjit/compiler.cpp. Only one clang invocation runs at a time. This guards the (cached, one-time) build path only — kernel launches are unaffected.

Test

The crash was already covered by test_concurrent_distinct_python_ops_build_storm, but at THREADS = 4 it almost never fired — it only surfaced in CI under xdist load. Bumped the build-storm test to a dedicated 24 threads so it reliably trips the race if the lock regresses, and raised the shared THREADS for the file's other thread-safety tests from 4 → 8 (all use tiny inputs, so no GPU-memory concern).

@NaderAlAwar
NaderAlAwar requested review from a team as code owners July 17, 2026 16:42
@NaderAlAwar
NaderAlAwar requested a review from shwina July 17, 2026 16:42
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Jul 17, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Jul 17, 2026
@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 6b19738c-1686-44ba-b0c8-bbab7dadd51d

📥 Commits

Reviewing files that changed from the base of the PR and between e7c9b65 and ea47ab9.

📒 Files selected for processing (2)
  • c/parallel.v2/src/hostjit/compiler.cpp
  • python/cuda_cccl/tests/compute/test_numba_path_thread_safety.py

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability of concurrent native compilation and linking by preventing conflicting build operations from running simultaneously.
  • Tests

    • Expanded thread-safety coverage with a larger concurrent build-storm scenario, validating behavior across more parallel workers.

Walkthrough

Changes

The change serializes CUDACompiler’s native compilation and linking operations with a process-wide mutex and increases concurrency in the Numba build-storm thread-safety test.

Native Compile Safety

Layer / File(s) Summary
Process-wide native compilation mutex
c/parallel.v2/src/hostjit/compiler.cpp
Locks device bitcode compilation, object compilation, and shared-library linking before delegating to impl_.
Expanded concurrent build-storm validation
python/cuda_cccl/tests/compute/test_numba_path_thread_safety.py
Adds dedicated build-storm settings and uses them for worker creation, result tracking, cache clearing, and distinct-build assertions.

Suggested reviewers: shwina, jacobfaib


Comment @coderabbitai help to get the list of available commands.

// clang concurrently and corrupt that shared state. Serialize every native
// compile/link through one process-wide mutex. This guards only the (cached,
// one-time) build path; kernel launches are unaffected.
static std::mutex g_native_compile_mutex;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
static std::mutex g_native_compile_mutex;
namespace {
std::mutex g_native_compile_mutex;
} // namespace

Prefer anon namespaces to static

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 3h 08m: Pass: 100%/61 | Total: 20h 26m | Max: 1h 02m | Hits: 99%/5269

See results here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

2 participants