Skip to content

Device-side SetDiscriminant is unsupported for both direct-tag and niche-encoded enums #306

Open

Description

@goog00

What

MIR StatementKind::SetDiscriminant is currently rejected on the device with:

SetDiscriminant statements are not yet supported on the device; until they are lowered, enum discriminant writes would be silently dropped

This means any kernel code that writes an enum discriminant cannot compile. Common triggers include:

  • Mutating an enum variant inside a kernel:

    enum DeviceState { Empty, Full(u32) }
    let mut state = DeviceState::Full(42);
    state = DeviceState::Empty;
  • Option::take(&mut opt) or similar standard library operations that update an Option's discriminant.

The diagnostic was added to replace an earlier silent miscompile where the discriminant write was dropped entirely, but the underlying lowering is still missing.


Scope

This issue tracks implementing SetDiscriminant lowering for all enum kinds:

  1. Direct-tag enums — enums with a separate discriminant tag in memory (e.g. enum DeviceState { Empty, Full(u32) }). Lowering should write the declared discriminant value to the tag slot.

  2. Niche-encoded enums — enums whose discriminant is encoded in the payload scalar itself (e.g. Option<&T>, Option<NonZeroU32>). Lowering must rewrite the payload scalar rather than storing a separate tag.

Activity

  1. added
    enhancement
    TBD
    on Jun 28, 2026
  2. added
    bug
    codegen
    IR-lowering
    on Jun 29, 2026
  3. vyncint commented on Aug 12, 2026

    @vyncint
    Contributor

    This looks resolved. Both halves named in the title are implemented and covered by executing examples.

    The diagnostic is gone. grep for "SetDiscriminant statements are not yet supported on the device" finds nothing on 15fa9d9d. The importer lowers it: crates/mir-importer/src/translator/statement.rs builds a MirSetDiscriminantOp (around lines 936-1025), rejecting only the cases that genuinely cannot be encoded — an unsupported enum type, a variant index out of bounds, an uninhabited variant, an unsupported layout kind, and a place with no addressable slot.

    Three examples cover it, and the two positive ones pass on an A10G (sm_86, driver 595.71.05):

    example covers result
    set_discriminant direct-tag, incl. #[repr(i8)] with explicit discriminants (Empty = -5, Full(u32) = 7) and a Single layout PASS: all 64 threads observed the SetDiscriminant write.
    set_discriminant_niche niche encoding, including a later-field niche (MaybeWrapper's niche is at byte 4, not byte 0) and a device-constructed Option<bool> set_discriminant_niche: PASS (64 host->device decodes and device->host enum writes)
    error_set_discriminant_uninhabited the negative case, pinned in STATUS.md and smoketest's ERROR_EXAMPLES compile-must-fail

    Both positive examples run through #[custom_mir(dialect = "runtime", phase = "optimized")] helpers that emit StatementKind::SetDiscriminant directly, so they exercise the statement rather than something that happens to lower nearby.

    The issue's own trigger list is worth re-checking if you want it kept open for a narrower reason — the Option::take case in particular goes through libcore rather than a hand-written discriminant write, and I did not test that specific path. But the two mechanisms the title names both work.

    Landing this as a comment rather than closing it, since you may be tracking something more specific here.

  4. nihalpasham commented on Sep 3, 2026

    @nihalpasham
    Collaborator

    Agree with the read above: both halves the title names landed (#307 direct tag, #349 niche) and the lowering is intact on current main after the #1186 fact-oracle and #1201 toolchain changes.

    On the triggers in the issue body. A "whole-value store" here writes the entire enum, tag and payload together, as opposed to a SetDiscriminant statement that rewrites only the tag.

    trigger MIR it produces covered by
    state = DeviceState::Empty whole-value store (aggregate, or a folded constant at opt-level 3), not SetDiscriminant set_discriminant_niche (device-built Option<bool>, DirectBoolean), enum_state_transitions
    opt.take() mem::replace -> *self = None, again a whole-value store enum_state_transitions (#826, runs take from both Some and None on device)
    hand-written SetDiscriminant (custom MIR) the statement itself set_discriminant, set_discriminant_niche, error_set_discriminant_uninhabited

    I consider this covered. Was there anything narrower you were tracking under this issue?

  5. vyncint commented on Sep 6, 2026

    @vyncint
    Contributor

    No, nothing narrower — Option::take was the only loose end I had, and you have settled it.

    That was the one caveat in my comment above: the trigger list goes through libcore rather than a hand-written discriminant write, and I had not tested that path. Your table answers it exactly, and #826 plus the enum_state_transitions run on the 5090 (PASS: Option::take, zero mir.set_discriminant ops under CUDA_OXIDE_DUMP_MIR=1) covers it better than I could have — the point that ordinary enum writes are whole-value stores and never reach the statement at all is the part I had not worked out.

    Happy for this to close as covered.

  6. added
    cuda-oxide
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    IR-loweringTBDbugcodegencuda-oxideenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions