Repository navigation
Write a canonical-storage enum payload by rebuilding the enum - #673
Merged
nihalpasham merged 2 commits intoAug 6, 2026
Merged
nihalpasham merged 2 commits into
nihalpasham merged 2 commits into
Conversation
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.
…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.
Collaborator
|
Verified the rewrite at both layers and on hardware:
This unblocks the if |
nihalpasham
approved these changes
Aug 6, 2026
alejandro-soto-franco
deleted the
feat/enum-noncanonical-payload-mutation
branch
September 9, 2026 17:08
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mutating a
boolenum 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
booloccupies a fulli8byte 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:How
The borrow in that shape does not survive to the importer. Optimisation folds it into a store to the payload place:
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_enumcoerces each payload to its canonical storage on the way in.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_addrgainsmutate_bool_payload, whose match arms differ so a write landing in a copy reports a mismatch rather than passing, andmutate_multi_field_payload, where the siblingf32of a two-field variant has to survive the rebuild. Both exact over 1048576 elements.error_enum_bool_payload_addrstill fails closed on the escaping borrow, and the smoketest verdict matches the updated diagnostic.enum_payload_addrare unchanged, in-place over rebuild 1.070.cargo test --workspaceandcargo clippy --workspace --all-targetsclean.Follow-up to #652.