Repository navigation
feat(cuda-macros): add inline PTX macro support - #215
Conversation
|
Really nice addition, thanks for the PR. We'd held off on full inline-asm support so far, mostly to keep folks on the typed-intrinsic path instead of reaching for the inline-asm escape hatch. But I think the pathways are clear enough now: where an LLVM intrinsic exists upstream, we model it as an What I really like:
Two things worth fixing before it goes in: 1. (I actually reproduced that hoist under 2. A literal One line in the template conversion plus a test. On the documented gaps: all fine as follow-ups, nothing there blocks this. If you're up for more after, multiple outputs and read-write ( Some changes to the docs, in priority order:
|
Signed-off-by: Jack Patterson <[email protected]>
|
@nihalpasham Thanks for the detailed review, especially the repro for the convergent issue. I amended the commit to keep the history tidy and addressed the requested changes:
As you mentioned, multiple outputs and read-write ( |
|
LGTM 👍🏾 One small thing, nothing for you to do: you turned the "document the constraint letters" suggestion into actually enforcing an allowlist. The friendlier errors are nice, but it does tighten the escape hatch a bit, since an unlisted-but-otherwise-valid constraint is now a hard macro error instead of falling through to LLVM. Inline PTX is meant to be the "we don't model this yet" hatch, so we'll probably just relax that to a warning or pass-through in a tiny follow-up on our end. Thanks again for this. |
Summary
Closes #149.
Adds
cuda_device::ptx_asm!, a CUDA inline PTX macro for device code. This gives cuda-oxide users an escape hatch for advanced optimization and PTX instructions that are not yet exposed as typed device APIs or compiler intrinsics.The initial implementation supports the common single-output and void-statement cases:
%0,%1, ... operand placeholders%%laneidin("...")operandsout("...")operandclobber("memory")options(register_only)for pure register snippetsDefault lowering is conservative: inline PTX is emitted as LLVM inline asm with
sideeffectandconvergent.options(register_only)opts out of both markers for snippets that only read explicit inputs and write the explicit output.Example
How It Works
The proc macro validates the PTX template and operands at Rust compile time, then lowers the call into hidden cuda_device::ptx marker functions:
Those marker calls carry the PTX template, constraint string, and options as static byte strings. The MIR importer recognizes the marker calls and replaces them with an NVVM dialect inline PTX op instead of importing the marker body.
The lowering path is:
The collector skips the hidden marker bodies so their unreachable!() fallback does not pull panic machinery into device compilation.
Design Decisions
Default lowering is conservative because arbitrary user PTX may touch memory, depend on scheduling, or have cross-lane/control-flow semantics that are not visible to LLVM. Users can opt into options(register_only) only for pure register snippets with an explicit output and no clobbers.
The hidden marker functions are generated for fixed arities up to 16 because Rust does not have variadic generics. Sixteen inputs seems enough for the initial surface and keeps the marker API simple and explicit.
Missing vs CUDA Inline PTX
This is not full CUDA inline PTX parity yet. Gaps:
These are documented as current limitations and can be improved independently.
Testing
Run the tests:
cargo test -p cuda-macros -p mir-lower -p llvm-exportRun the example: