Skip to content

Simplify discriminant codegen for niche-encoded variants which don't wrap across an integer boundary#143784

Merged
bors merged 3 commits intorust-lang:masterfrom
scottmcm:enums-again-new-ex2
Jul 19, 2025
Merged

Simplify discriminant codegen for niche-encoded variants which don't wrap across an integer boundary#143784
bors merged 3 commits intorust-lang:masterfrom
scottmcm:enums-again-new-ex2

Conversation

@scottmcm
Copy link
Copy Markdown
Member

@scottmcm scottmcm commented Jul 11, 2025

Inspired by #139729, this attempts to be a much-simpler and more-localized change while still making a difference. (Specifically, this does not try to solve the problem with select-sinking, leaving that to be fixed by llvm/llvm-project#134024 -- once it gets released -- instead of in rustc's codegen.)

What this does improve is checking for the variant in a 3+ variant enum when that variant is the type providing the niche. Something like if let Foo::WithBool(_) = ... previously compiled to ugt(add(x, -2), 2), which is non-trivial to think about because it's depending on the unsigned wrapping to shift the 0/1 up above 2. With this PR it compiles to just ult(x, 2), which is probably what you'd have written yourself if you were doing it by hand to look for "is this byte a bool?".

That's done by leaving most of the codegen alone, but adding a couple new special cases to the is_niche check. The default looks at the relative discriminant, but in the common cases where there's no wraparound involved, we can just check the original value, rather than the offsetted one.

The first commit just adds some tests, so the best way to see the effect of this change is to look at the second commit and how it updates the test expectations.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants