Skip to content

feat(mir): implement device-side SetDiscriminant lowering - #307

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
goog00:feat-set-discriminant
Jul 1, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
goog00:feat-set-discriminant

Conversation

@goog00

@goog00 goog00 commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Part of #306.

Implements lowering for MIR StatementKind::SetDiscriminant on the device.Previously, enum discriminant writes were rejected with an "unsupported" diagnostic (after an earlier silent-miscompile regression where they were dropped entirely). This PR adds a dedicated mir.set_discriminant operation and lowers it through the full pipeline so that direct-tag enums can now be mutated inside #[kernel] functions. Niche-encoded enums are still rejected loudly until their payload-scalar encoding is handled.

Changes

  • Added MirSetDiscriminantOp to dialect-mir with verifier tests.
  • Updated mir-importer to translate SetDiscriminant into mir.constant + mir.set_discriminant instead of erroring.
  • Updated mir-lower to convert mir.set_discriminant to llvm.getelementptr (to the enum tag slot) + llvm.store.
  • Renamed the negative example error_set_discriminant_unhandled to the positive example set_discriminant and updated STATUS.md and scripts/smoketest.sh accordingly.

Testing

  • cargo test -p dialect-mir -p mir-importer -p mir-lower --lib --tests passes.
  • cargo clippy -p dialect-mir -p mir-importer -p mir-lower --lib -- -D warnings passes.
  • cargo oxide run set_discriminant passes on RTX 5090 (sm_120a):
    PASS: all 64 threads observed the SetDiscriminant write.
  • scripts/smoketest.sh passes.

Follow-up

Issue #306 also covers niche-encoded enums such as Option<&T> and Option<NonZeroU32>, where the discriminant is encoded in the payload scalar itself rather than a separate tag. A follow-up PR will implement the payload-scalar rewrite path for those cases.

Checklist

  • All commits signed off (git commit -s)
  • No files under cuda-bindings/ modified by an external contributor (see CONTRIBUTING.md)
  • SPDX headers on new source files

@goog00
goog00 force-pushed the feat-set-discriminant branch from 0bb8c20 to fa87944 Compare June 29, 2026 11:45
Add mir.set_discriminant op, wire it through mir-importer and mir-lower,
and convert error_set_discriminant_unhandled into a passing example.
Direct-tag enums are now supported; niche-encoded enums are rejected
loudly until their payload encoding is handled.

Signed-off-by: sunteng <[email protected]>
@nihalpasham
nihalpasham force-pushed the feat-set-discriminant branch 2 times, most recently from 88fde1b to af3b5e9 Compare July 1, 2026 09:04
Classify direct, niche, single, and empty layouts explicitly; reject impossible variants; and add lowering plus end-to-end regression coverage.

Signed-off-by: nihalp <[email protected]>
@nihalpasham
nihalpasham force-pushed the feat-set-discriminant branch from af3b5e9 to b564a25 Compare July 1, 2026 09:11
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks for the PR. It LGTM

A couple of points:

  • preserving it as a typed MIR operation and reusing the enum slot map are both in the right direction, and the it handles signed, sparse, wide, nested, indexed, global, and shared-memory cases correctly.
  • I layered a couple of safety checks on top:
    • uninhabited variants reject,
    • direct/niche/no-tag layouts are classified explicitly, and
    • focused compiler tests cover the edge cases.

This rounds out the inhabited direct-tag part of #306; but I think #306 should stay open for niche encoding.

@nihalpasham
nihalpasham merged commit 8377854 into NVIDIA:main Jul 1, 2026
28 checks passed
@nihalpasham nihalpasham added bug Something isn't working enhancement New feature or request codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) tests Adds or improves test coverage examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples labels Jul 1, 2026
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
Two host-side launch-path fixes, measured with the new `host_overhead` bench (RTX 5090, µs per launch).

Site misses (a scalar argument's divisibility hint changed, or a new specialization) no longer pay filesystem and environment lookups per launch: the assembler fingerprint, resolved toolchain and `CUTILE_BYTECODE_VERSION` override are trusted for one second before being re-checked, so a mid-process toolchain switch still takes effect within that window. Missed launch 5.73 → 4.29.

Site hits no longer allocate in the launcher: the kernel-argument arena lives inline in the launch (heap only beyond 32 parameter slots), shape validation compares the bound partition shape in place, the site probe borrows fixed arrays, and grid inference takes the first exact bound without collecting. Heap allocations per four-tensor launch 15 → 2; hit 1.905 → 1.696 against a 1.30 driver floor for the same twelve-parameter `cuLaunchKernel`. Awaited launches and graph replays are unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) tests Adds or improves test coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants