Skip to content

feat(llvm-export): AsmKind enum for inline asm optimization hints - #195

Merged
nihalpasham merged 5 commits into
NVIDIA:mainfrom
honeyspoon:upstream-pure-inline-asm
Jun 18, 2026
Merged

nihalpasham merged 5 commits into
NVIDIA:mainfrom
honeyspoon:upstream-pure-inline-asm

Conversation

@honeyspoon

@honeyspoon honeyspoon commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Replace the convergent: bool parameter on inline asm with an AsmKind enum that captures the two orthogonal GPU axes — convergent (thread-set dependent) × side effects (observable state changes):

pub enum AsmKind {
    Convergent,      // bar.sync, mma.sync, wgmma, tcgen05, cp.async
    ConvergentPure,  // shfl.sync, vote.sync, match.sync
    SideEffect,      // st.global via asm, hardware timer reads
    Pure,            // cvt.rn.f16x2.f32, cvt.rn.bf16x2.f32, prmt
}

Maps to LLVM IR:

Variant sideeffect convergent LLVM can…
Convergent yes yes nothing (fully pinned)
ConvergentPure no yes CSE/DCE within uniform flow, not across divergent branches
SideEffect yes no not move/delete, but no thread-set constraint
Pure no no CSE, DCE, LICM freely

Suggested labels: refactor, llvm-export, IR-lowering, codegen

Motivation

Inline asm ops currently only model convergent: bool. The sideeffect flag is hardcoded — every inline asm call blocks LLVM from optimizing it, even for pure data conversions like cvt.rn.bf16x2.f32 that have no observable side effects.

Rust's own asm! macro (RFC 2873) separates side effects (pure) from memory behavior (nomem/readonly) as orthogonal axes. This PR applies the same principle to GPU inline asm, where the relevant axes are convergence and side effects.

This is complementary to #215 (ptx_asm! macro): AsmKind classifies compiler-generated NVVM intrinsic asm, while ptx_asm! handles user-written inline PTX. The export layer detects which path produced each InlineAsmOp and reads the appropriate attributes — AsmKind tag for NVVM ops, sideeffect/convergent bools for ptx_asm! ops.

Why four variants is the complete set

The remaining LLVM inline asm axes do not apply to PTX:

  • Memory effects (nomem/readonly): unnecessary — cuda-oxide's inline asm is either a pure register-to-register conversion or a full side-effecting op. Loads and stores go through proper LLVM ops, not inline asm.
  • noreturn/may_unwind: PTX inline asm always returns and never unwinds.
  • preserves_flags/nostack: CPU concepts (flags register, stack red zone) with no PTX equivalent.

Implementation

Follows the existing InlineAsmOpExt trait pattern in llvm-export:

  • llvm-export/src/lib.rs: AsmKind enum + InlineAsmOpExt::build() constructor + asm_kind()/asm_kind_opt() queries. Stores the kind as a StringAttr on the op (pliron's InlineAsmOp only models convergent natively).
  • llvm-export/src/export/ops.rs: asm_kind_opt() checks for the AsmKind tag; if absent (i.e., a ptx_asm! op), falls back to reading the individual sideeffect/convergent attributes. Exhaustive matching ensures new variants are handled.
  • mir-lower/intrinsics/: All call sites updated — new_convergent(...) → build(..., AsmKind::Convergent), raw InlineAsmOp::new(..., false) → build(..., AsmKind::Pure).

Testing

cargo test -p llvm-export --all-targets  # 13 passed (4 new: AsmKind round-trip)
cargo test -p mir-lower --all-targets    # 47 passed
cargo build --workspace                  # pass
cargo fmt --check                        # pass
cargo clippy -p llvm-export -D warnings  # pass
cargo clippy -p mir-lower -D warnings    # pass
RUSTDOCFLAGS='-D warnings' cargo doc -p llvm-export -p mir-lower --no-deps  # pass

@honeyspoon
honeyspoon force-pushed the upstream-pure-inline-asm branch from b8fc0cf to 2aabffd Compare June 12, 2026 21:32
@honeyspoon honeyspoon changed the title feat(llvm-export): pure (no-side-effect) inline asm attribute feat(llvm-export): AsmKind enum for inline asm optimization hints Jun 12, 2026
@honeyspoon
honeyspoon force-pushed the upstream-pure-inline-asm branch from 2c6a4df to 4e419b5 Compare June 15, 2026 17:55
@nihalpasham nihalpasham added refactor Internal restructuring with no behavior change perf Performance of generated code or of the compiler itself llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Jun 18, 2026
honeyspoon and others added 4 commits June 18, 2026 11:16
The two orthogonal GPU axes (convergent × side-effects) produce
exactly four valid combinations. ConvergentPure covers warp-collective
ops like shfl.sync and vote.sync that depend on which threads are
active but have no observable side effects beyond their register output.

No further variants are needed because the remaining LLVM inline asm
axes do not apply to PTX:
- Memory effects (nomem/readonly): unnecessary — our inline asm is
  either pure register-to-register or full side-effecting; loads/stores
  go through proper LLVM ops, not inline asm.
- noreturn/may_unwind: PTX inline asm always returns, never unwinds.
- preserves_flags/nostack: CPU concepts with no PTX equivalent.
… bf16x2 to AsmKind::Pure

The asm_kind docblock was accidentally prepended to asm_kind_opt in the
original PR. Remove the stray block so asm_kind_opt has a single, accurate
doc comment.

Migrate convert_fma_bf16x2 from InlineAsmOp::new(..., false) to
InlineAsmOp::build(..., AsmKind::Pure), covering the bf16x2 FMA op added
by PR NVIDIA#191 after this branch was cut. The PTX instruction is a pure
register-to-register FMA with no memory side effects and no warp
synchronization, so Pure is the correct classification.
@nihalpasham
nihalpasham force-pushed the upstream-pure-inline-asm branch from 4e419b5 to d6b6516 Compare June 18, 2026 05:48
@nihalpasham

nihalpasham commented Jun 18, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for this. LGTM

I quite like the AsmKind abstraction that gives LLVM what it needs to CSE/DCE/LICM pure register conversions.

layered two small fixes:

  • removed a duplicate docstring on asm_kind_opt, and migrated convert_fma_bf16x2 (which landed after your branch was cut) to use the new API.
  • Lifted AsmKind from a plain Rust enum serialised as StringAttr to a proper #[pliron_attr] type. I'd always recommend this as a best practice that typed attributes should be first-class IR citizens, not strings.

Reviewer note: verified, cvt.rn.bf16x2.f32 now emits as pure asm in the .ll, and all convergent ops remain correctly tagged.

Replace the plain Rust enum stored as StringAttr with a proper
#[pliron_attr(name = "llvm.asm_kind")] type. This follows the pliron
best practice: domain-specific attribute types should use the pliron
attribute infrastructure, not encode as StringAttr.

build() now calls attributes.set(key, kind) directly with the typed
AsmKind value, and asm_kind_opt() retrieves it via
attributes.get::<AsmKind>() instead of string-matching against a
serialized StringAttr.
@nihalpasham
nihalpasham merged commit da9fd1c into NVIDIA:main Jun 18, 2026
24 checks passed
@honeyspoon
honeyspoon deleted the upstream-pure-inline-asm branch June 30, 2026 01:40
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
Closes the async-vs-sync launch latency gap: Async was ~22us at N=1, and it's now within noise of sync at every pipeline length.

Signed-off-by: Melih Elibol <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) perf Performance of generated code or of the compiler itself refactor Internal restructuring with no behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants