Repository navigation
feat(compiler): in-process library-mode compiler (Rust kernel to PTX) - #312
alejandro-soto-franco wants to merge 1 commit into
Conversation
Add cuda-oxide-compiler: compile a device-kernel crate to PTX in-process via rustc_driver, with no cargo or rustc subprocess, addressing the library-mode deliverable in NVIDIA#96. - crates/cuda-oxide-compiler: a parameterised core (compile_core.rs) driving rustc_driver::run_compiler + Callbacks::after_analysis, reusing the existing collector/device_codegen/run_pipeline via a path-include (backend unchanged), plus a thin CLI bin. - crates/cuda-oxide-compiler-cdylib: the same core behind an extern "C" API. - crates/libmode-consumer: a normal Rust binary (no rustc_private) that dlopens the cdylib via libloading and compiles in-process, modelling a runtime-JIT host. - Golden sm_80 PTX parity test for vecadd. PTX is byte-identical to cargo oxide build; in-process steady-state compile is about 2.8 times faster than the cargo path. Device-only crates (codegen stops after analysis). Runtime requires GLIBC_TUNABLES=glibc.rtld.optional_static_tls and LD_LIBRARY_PATH to the rustc sysroot; llc and opt remain subprocesses. Hardening: - Broken kernels are recoverable, not fatal to the host. A kernel that fails rustc's front-end (e.g. a type error in a #[kernel] fn) makes rustc abort by unwinding with a sentinel FatalError; compile_to_ptx now wraps run_compiler in catch_fatal_errors (the hook rustc itself uses) so this returns a clean Err instead of a panic. A new (ignored) test compiles a deliberately broken kernel and asserts the Err. - The cdylib C ABI wraps its body in std::panic::catch_unwind, returning the distinct code 6 on a caught panic rather than unwinding across the FFI boundary (which aborts the host). A new (ignored) test asserts the C ABI returns a nonzero code, not an abort, for a broken kernel. - The in-process tests require the rustc_private runtime and prebuilt release rlibs, so they are marked #[ignore] and skipped by a plain cargo test; the README and module docs give the explicit --ignored run recipe. - compile_to_ptx removes its per-call output dir after reading the PTX back. - The three new manifests inherit edition/authors/license/repository from the workspace. Signed-off-by: Alejandro Soto Franco <[email protected]>
|
Thanks for the PR. Just a quick note: For compiler-embedding changes of this size, I'd recommend starting with a Discord/design discussion next time. It helps hash out the intended users, distribution model, and API boundary before committing to a significant change. After reviewing this PR, I understand it aims to:
The main concern is that CubeCL already has its own compiler IR. Converting that IR back into Rust source adds an unnecessary compiler frontend: I think the direct approach proposed by #314 is a better fit: #314 's compiler boundary better matches CubeCL. CubeCL should not need to generate Rust source and host a full Rust compiler just to reach a backend IR. #312 would make sense only for a separate use case where the input really is arbitrary Rust source and the caller needs Rust language semantics at runtime. The distribution requirements are also much larger than the shared library suggests: The PR does not invoke Cargo for each kernel, but it still requires an exact Rust compiler environment. It also searches for cuda-oxide rlibs using a build-time checkout path, so the resulting C library is not currently relocatable. There is also a process-safety problem. The library changes global environment variables during a safe compile call: Serializing compiler calls does not protect other Burn/CubeCL threads or libraries that read the process environment. Other API problems remain:
I think for the stated CubeCL/Burn motivation, we should not pursue this source-compilation approach. The cleaner direction is to extract a If arbitrary Rust-source JIT compilation is still desired as a separate feature, it needs its own design discussion. I would expect that design to include:
Though this would likely be a decision that would need much deliberation prior |
|
Agreed on the compiler boundary. The direct dialect-mir route in #314 is the one my downstream work uses: cubecl-cuda-oxide lowers CubeCL Your findings on this PR are accurate. The README documents a Rust API the bin-only crate does not export; the cdylib resolves dep rlibs through a path baked in at build time and the C ABI has no way to override it; the env mutation is process-global; Withdrawing this PR in favour of #314. The design context for the CubeCL direction is in #313 and the #96 thread; for the next change of this size I'll open the discussion before the code, on Discord or wherever the project prefers. One #314 question worth settling early: whether |
Bumps the all-actions group with 2 updates: [taiki-e/install-action](https://github.com/taiki-e/install-action) and [github/codeql-action](https://github.com/github/codeql-action). Updates `taiki-e/install-action` from 2.87.16 to 2.87.17 - [Release notes](https://github.com/taiki-e/install-action/releases) - [Changelog](https://github.com/taiki-e/install-action/blob/main/CHANGELOG.md) - [Commits](taiki-e/install-action@9114bf4...94c31af) Updates `github/codeql-action` from 4.38.0 to 4.38.1 - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](github/codeql-action@v4.38.0...v4.38.1) --- updated-dependencies: - dependency-name: taiki-e/install-action dependency-version: 2.87.17 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: all-actions - dependency-name: github/codeql-action dependency-version: 4.38.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: all-actions ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…ping it (NVIDIA#313) NVIDIA#312 moved taiki-e/install-action to v2.87.17 (94c31af), which is not on the organization's Actions allowlist, so `pr` and `cargo-deny` fail at startup on main since df048dc. Pin back to v2.87.16 (9114bf4), the commit the allowlist admits, in both workflows. Dependabot ignores install-action and rust-cache from now on: both are allowlisted by exact commit, so any bump it proposes is unmergeable until the list changes; they are bumped by hand together with the allowlist. Signed-off-by: Melih Elibol <[email protected]>
Summary
Compile a device-kernel crate to PTX in-process, with no
cargoorrustcsubprocess. This is the library-mode deliverable in #96. The existing backend
(
rustc-codegen-cuda) is unchanged.Added
crates/cuda-oxide-compiler: core (compile_core.rs) drivingrustc_driver::run_compilerwith aCallbacks::after_analysisthat runs theexisting
collect_device_functions,generate_device_code, andrun_pipeline, then returnsCompilation::Stop. Backend modules are reusedvia
#[path]include, not forked. Plus a CLI bin. API:compile_to_ptx(&CompileRequest) -> Result<Vec<u8>, _>.crates/cuda-oxide-compiler-cdylib: the same core behind anextern "C"API.Both targets
#[path]-include the onecompile_core.rs.crates/libmode-consumer: a normal Rust binary (norustc_private) thatdlopens the cdylib vialibloadingand compiles in-process.vecadd.Design
A
rustc_driverhost cannot link as anrlibor Rustdylib(the std andobjectgraphs double). The in-process form is acdylibwith a C ABI, loadedvia
libloading, the same way rustc loads this project's backend. Codegen stopsafter analysis, so the target is device-only crates.
Error handling
A kernel compile error is recoverable: rustc raises a
FatalErrorunwind, caughtby
catch_fatal_errors, socompile_to_ptxreturnsErrand the C ABI returnsa nonzero code. The host process is not terminated. The C ABI body is also
wrapped in
catch_unwind(code 6) so no panic crosses the boundary. Returncodes: 1 null, 2 utf8, 3 missing source, 4 dep resolve, 5 compile error, 6 panic.
Performance
Measured on
vecaddat sm_80, RTX 5060 Laptop, CUDA 13.1.cargo oxide build(subprocess)dlopenloadThe in-process PTX is byte-identical to
cargo oxide build(2079 bytes), lockedby the golden test. The ~158 ms still includes the
llcandoptsubprocessesinside
run_pipeline, so ~2.8x is conservative: the saving comes from removingthe per-kernel
cargostartup,rustcspawn, and backenddlopen.Runtime and test requirements
GLIBC_TUNABLES=glibc.rtld.optional_static_tls=2097152(or LD_PRELOADlibrustc_driver):librustc_driveruses initial-exec TLS and isdlopenedafter process start.
LD_LIBRARY_PATH="$(rustc --print sysroot)/lib".RUST_MIN_STACK=16777216.Limitations and follow-ups
compile_to_ptxmutatesCUDA_OXIDE_TARGETandCARGO_PKG_*). Per-call state is a follow-up.sysroot()andworkspace_root()panic on misconfiguration; should returnResult.llcandoptremain subprocesses.vecadd); the API is general.#[kernel]bridge is separate.Testing
cargo buildof the three crates: clean, no warnings.cargo testis unaffected: the tests that need therustc_privateruntime and prebuilt release rlibs are
#[ignore]d.cargo build --release -p cuda-core -p cuda-host -p cuda-device, thenRUST_MIN_STACK=16777216 LD_LIBRARY_PATH="$(rustc --print sysroot)/lib" cargo test -p cuda-oxide-compiler -- --ignored(golden parityand the broken-kernel recovery test included).
cargo clippyon the three crates: clean, no#[allow].rustc-codegen-cudaandmir-importerunchanged.DCO
All commits signed off.