Skip to content

test(codegen): cover Option and Result enum state transitions - #826

Merged
nihalpasham merged 1 commit into
NVIDIA:mainfrom
uurl:test/enum-state-transitions-conformance
Aug 12, 2026
Merged

nihalpasham merged 1 commit into
NVIDIA:mainfrom
uurl:test/enum-state-transitions-conformance

Conversation

@uurl

@uurl uurl commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Closes #825

Summary

Add an enum_state_transitions codegen example covering device-side execution of:

  • Option::take
  • Option::replace
  • Option::get_or_insert
  • Result::map
  • Result::map_err
  • Result::and_then
  • Result::or_else

No compiler changes are required.

The example confirms that the existing enum lowering machinery correctly handles the construction, discriminant reads, payload extraction, replacement, and payload mutation produced by these standard core APIs.

Option coverage

The option_transitions kernel starts from a runtime-selected Some or None, preventing the initial variant from being fixed at compile time.

It verifies Option::take in both directions:

Some(value) -> None, returns Some(value)
None        -> None, returns None

It verifies Option::replace with both initial variants:

Some(old) -> Some(new), returns Some(old)
None      -> Some(new), returns None

It also exercises Option::get_or_insert.

For an initial None, the operation must construct a new Some payload and return a mutable reference to it.

For an existing Some, the supplied replacement value must not overwrite the existing payload.

In both cases the returned &mut payload is mutated and the final Option is observed afterward, covering payload addressing and mutation as well as the state transition itself.

Result coverage

The result_combinators kernel starts from a runtime-selected:

Result<u32, u32>

and exercises:

Result::map
Result::map_err
Result::and_then
Result::or_else

Both Ok and Err starting states are tested.

This gives explicit coverage of:

Ok  -> Ok
Ok  -> Err
Err -> Err
Err -> Ok

including the two cross-variant transitions:

Ok  -> Err through Result::and_then
Err -> Ok  through Result::or_else

Every intermediate Option or Result is matched and packed into a host-observable integer. This makes the resulting discriminant and payload part of the regression rather than merely checking that the methods compile.

Why no compiler change is needed

The compiler already has the enum infrastructure required by these APIs:

  • field-faithful enum layout and construction
  • direct and niche discriminant handling
  • payload extraction
  • Downcast -> Field payload addressing
  • enum payload mutation
  • whole-enum rebuilding where canonical storage requires it

The standard-library APIs covered here lower onto those existing MIR paths.

This PR therefore adds focused conformance coverage rather than another enum representation or special-case intrinsic lowering.

Scope

This PR only adds:

crates/rustc-codegen-cuda/examples/enum_state_transitions/
├── Cargo.lock
├── Cargo.toml
├── README.md
└── src/main.rs

No compiler, dialect, exporter, smoketest-script, or Rustlantis sources are changed.

Validation

Validated on an NVIDIA GeForce RTX 5060 Laptop GPU (sm_120a).

Optimized execution:

cargo oxide run enum_state_transitions

passes with:

=== enum_state_transitions ===
PASS: Option::take
PASS: Option::replace
PASS: Option::get_or_insert
PASS: Result::map
PASS: Result::map_err
PASS: Result::and_then
PASS: Result::or_else
PASS: enum_state_transitions

Low-MIR-optimization execution also passes:

CUDA_OXIDE_NO_OPT=1 \
cargo oxide run enum_state_transitions

Scoped runtime smoketest:

[ 1/ 1] enum_state_transitions ... PASS

Pass:    1 / 1
Fail:    0 / 1

Scoped compile-only smoketest:

[ 1/ 1] enum_state_transitions ... PASS (compiled)

Pass:    1 / 1
Fail:    0 / 1

The standalone example also passes:

cargo fmt --all -- --check
cargo clippy --all-targets -- -D warnings

Repository-level formatting and diff checks are clean.

@uurl uurl changed the title test(codegen): cover Option and Result enum state transitions test(codegen): cover Option and Result enum state transitions Aug 12, 2026
@nihalpasham nihalpasham added enhancement New feature or request codegen Device code-generation pipeline (Rust MIR to IR to PTX) tests Adds or improves test coverage labels Aug 12, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Gist: device-side coverage for seven Option/Result state transitions (take, replace, get_or_insert, map, map_err, and_then, or_else), with runtime seeds keeping everything out of const-folding and collision-free packing making every slot host-checkable. Zero compiler changes needed: the SetDiscriminant path already lowers these, and now it's gated.

Verified on an RTX 5090 (sm_120):

  • enum_state_transitions: all eight PASS lines, optimized and CUDA_OXIDE_NO_OPT=1 on regenerated PTX;
  • all 22 host assertions re-derived by hand, including both launch variants.

Landing once CI is green. Closes #825.

@nihalpasham
nihalpasham merged commit 97d16b9 into NVIDIA:main Aug 12, 2026
41 checks passed
@uurl
uurl deleted the test/enum-state-transitions-conformance branch August 12, 2026 22:31
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) enhancement New feature or request tests Adds or improves test coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add conformance coverage for Option and Result enum state transitions

2 participants