Skip to content

Write a canonical-storage enum payload by rebuilding the enum - #673

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
alejandro-soto-franco:feat/enum-noncanonical-payload-mutation
Aug 6, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
alejandro-soto-franco:feat/enum-noncanonical-payload-mutation

Conversation

@alejandro-soto-franco

Copy link
Copy Markdown
Contributor

Mutating a bool enum payload has no lowering, even when the write never leaves the function it is written in.

What

Enum storage keeps two payload kinds in a converted form: a bool occupies a full i8 byte and a shared-memory pointer is stored generic. A raw payload address cannot carry that conversion, because the address escapes and the loads and stores made through it elsewhere are typed with the semantic type. #652 therefore refused to hand one out, which left this with no lowering at all:

if let Flag::On(value) = &mut flag {
    *value = input[i] > 0.0;
}

How

The borrow in that shape does not survive to the importer. Optimisation folds it into a store to the payload place:

((_9 as On).0: bool) = Gt(move _12, const 0f32)

It is that store which asks for the address, so the write needs none. The enum is rebuilt around the new payload and stored whole, which is what a whole-enum assignment already does, and construct_enum coerces each payload to its canonical storage on the way in.

(_flag as On).0 = v   ->   _e  = load _flag
                           _e' = construct_enum On(v)
                           store _e' -> _flag

Valid MIR assigns to a variant's field only when the enum already holds that variant, so rebuilding with the same variant leaves the discriminant where it was. A variant of several fields keeps the others as they stand: each is read back out of the loaded value and passed through unchanged.

A borrow that escapes, into a #[device] call for instance, reaches no store site and keeps the refusal. The diagnostic now names which of the two cases it is.

Testing

On an RTX 5060 (sm_120):

  • enum_payload_addr gains mutate_bool_payload, whose match arms differ so a write landing in a copy reports a mismatch rather than passing, and mutate_multi_field_payload, where the sibling f32 of a two-field variant has to survive the rebuild. Both exact over 1048576 elements.
  • Removing the write from the first kernel fails it at element 1, so the check discriminates.
  • error_enum_bool_payload_addr still fails closed on the escaping borrow, and the smoketest verdict matches the updated diagnostic.
  • The other six kernels of enum_payload_addr are unchanged, in-place over rebuild 1.070.
  • cargo test --workspace and cargo clippy --workspace --all-targets clean.

Follow-up to #652.

Enum storage keeps two payload kinds in a converted form: a `bool`
occupies a full `i8` byte and a shared-memory pointer is stored generic.
A raw payload address cannot carry that conversion, because the address
escapes and the loads and stores made through it elsewhere are typed
with the semantic type, so NVIDIA#652 refused to hand one out. That left
`if let Flag::On(b) = &mut flag { *b = value }` with no lowering.

The borrow in that shape does not survive to the importer. Optimisation
folds it into `(_flag as On).0 = value`, a store to the payload place,
and it is that store which asks for the address. So the write needs no
address at all: rebuild the enum around the new payload and store the
whole value, which is what a whole-enum assignment already does.
`construct_enum` coerces each payload to its canonical storage on the
way in, which is the conversion the address could not carry.

Valid MIR assigns to a variant's field only when the enum already holds
that variant, so rebuilding with the same variant leaves the
discriminant where it was. A variant of several fields keeps the others
as they stand: each is read back out of the loaded value and passed
through unchanged.

A borrow that escapes, into a `#[device]` call for instance,
reaches no store site and keeps the refusal. The diagnostic now says
which of the two it is, and the `error_enum_bool_payload_addr` fixture
still fails closed on the escaping shape.

Tests: `enum_payload_addr` gains `mutate_bool_payload`, whose match arms
differ so a write landing in a copy reports a mismatch rather than
passing, and `mutate_multi_field_payload`, where the sibling `f32` of a
two-field variant has to survive the rebuild. Both exact over 1048576
elements on an RTX 5060. Dropping the write from the first kernel fails
it at element 1, so the check discriminates.
@nihalpasham nihalpasham added bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Aug 6, 2026
…r::Neither variant

Neither exists so Pair stays a genuine two-variant enum for the payload-addressing
test; the kernel only ever constructs Both. clippy's dead_code lint, denied by
-D warnings, was blocking NVIDIA#673.
@nihalpasham

Copy link
Copy Markdown
Collaborator

Verified the rewrite at both layers and on hardware:

  • enum_payload_addr on RTX 5090: SUCCESS, in-place/rebuild ratio 1.005, and the error example still refuses escaping borrows with the sharpened diagnostic.
  • cargo test -p mir-importer green (923 tests); clippy clean after your follow-up commit.
  • the rebuild path reuses construct_enum's existing storage coercion, so the bool-as-i8 conversion happens in one place and sibling fields of multi-field variants carry through unchanged.

This unblocks the if let ... = &mut pattern #652 made loud. Nested canonical payloads (a bool inside a struct payload) stay refused; a follow-up issue for those is welcome. #306 (SetDiscriminant) stays open by design.

@nihalpasham
nihalpasham merged commit e224458 into NVIDIA:main Aug 6, 2026
34 checks passed
@alejandro-soto-franco
alejandro-soto-franco deleted the feat/enum-noncanonical-payload-mutation branch September 9, 2026 17:08
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants