Skip to content

Change tag_field to FieldIdx in Variants::Multiple #142005

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jun 5, 2025

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Jun 4, 2025

It was already available as a generic parameter anyway, and it's not like we'll ever put a tag in the 5-billionth field.

This is a first part of pulling smaller pieces out of #138759, so
r? workingjubilee

It was already available as a generic parameter anyway, and it's not like we'll ever put a tag in the 5-billionth field.
@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 4, 2025

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@workingjubilee
Copy link
Member

oh sure.
@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jun 4, 2025

📌 Commit ee9901e has been approved by workingjubilee

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 4, 2025
@@ -26,7 +26,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
// No need to validate that the discriminant here because the
// `TyAndLayout::for_variant()` call earlier already checks the
// variant is valid.
let tag_dest = self.project_field(dest, tag_field)?;
let tag_dest = self.project_field(dest, tag_field.as_usize())?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

project_field should probably take FieldIdx... but that can be a future PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC this hits the problem that a bunch of these things are also used for arrays, and I've never dug in enough to know whether it's actually ok to convert a bunch of these things over to FieldIdx -- if this is used for [u8; 1 << 32] then the capped-at-0xFFFFFF00 FieldIdx wouldn't work.

(Though of course usize already doesn't work today either for cross-compiling to a 64-bit target from a 32-bit host, but I suspect that nobody actually ever does that so nobody's cared about it being broken.)

Copy link
Member

@RalfJung RalfJung Jun 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

project_field should not be used for arrays, we have project_index for that. (Those used to be one function but I refactored that a long time ago.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, wait, InterpCx. I was thinking of https://doc.rust-lang.org/nightly/nightly-rustc/rustc_abi/enum.FieldsShape.html#method.offset and such.

If you say it'll work always here, I can give that a shot (in another PR).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you say it'll work always here

I think it should, but I can't be sure without just trying it myself.^^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of https://doc.rust-lang.org/nightly/nightly-rustc/rustc_abi/enum.FieldsShape.html#method.offset and such.

Yeah that should be cleaned up to separate field offsets from array element offsets.

bors added a commit that referenced this pull request Jun 5, 2025
Rollup of 11 pull requests

Successful merges:

 - #141890 (Add link to correct documentation in htmldocck.py)
 - #141932 (Fix for async drop inside async gen fn)
 - #141960 (Use non-2015 edition paths in tests that do not test for their resolution)
 - #141968 (Run wfcheck in one big loop instead of per module)
 - #141969 (Triagebot: Remove `assign.users_on_vacation`)
 - #141985 (Ensure query keys are printed with reduced queries)
 - #141999 (Visit the ident in `PreciseCapturingNonLifetimeArg`.)
 - #142005 (Change `tag_field` to `FieldIdx` in `Variants::Multiple`)
 - #142017 (Fix incorrect use of "recommend" over "recommended")
 - #142024 (Don't refer to 'this tail expression' in expansion.)
 - #142025 (Don't refer to 'local binding' in extern macro.)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 98da8e6 into rust-lang:master Jun 5, 2025
10 checks passed
@rustbot rustbot added this to the 1.89.0 milestone Jun 5, 2025
rust-timer added a commit that referenced this pull request Jun 5, 2025
Rollup merge of #142005 - scottmcm:fieldidx-in-variantsmultiple, r=workingjubilee

Change `tag_field` to `FieldIdx` in `Variants::Multiple`

It was already available as a generic parameter anyway, and it's not like we'll ever put a tag in the 5-billionth field.

This is a first part of pulling smaller pieces out of #138759, so
r? workingjubilee
@scottmcm scottmcm deleted the fieldidx-in-variantsmultiple branch June 6, 2025 01:29
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jun 6, 2025
…-obk

Update `InterpCx::project_field` to take `FieldIdx`

As suggested by Ralf in rust-lang#142005 (comment)
rust-timer added a commit that referenced this pull request Jun 7, 2025
Rollup merge of #142103 - scottmcm:fieldidx-in-interp, r=oli-obk

Update `InterpCx::project_field` to take `FieldIdx`

As suggested by Ralf in #142005 (comment)
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Jun 7, 2025
Update `InterpCx::project_field` to take `FieldIdx`

As suggested by Ralf in rust-lang/rust#142005 (comment)
bjorn3 pushed a commit to rust-lang/rustc_codegen_cranelift that referenced this pull request Jun 8, 2025
Update `InterpCx::project_field` to take `FieldIdx`

As suggested by Ralf in rust-lang/rust#142005 (comment)
github-merge-queue bot pushed a commit to model-checking/kani that referenced this pull request Jun 14, 2025
Culprit PRs:
- June 4: rust-lang/rust#141741
- June 5: rust-lang/rust#142015
- June 6: rust-lang/rust#138677,
rust-lang/rust#142005
- June 7: rust-lang/rust#142012
- June 9: rust-lang/rust#141700 
- June 10: rust-lang/rust#142179,
rust-lang/rust#141803

Resolves #4140

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
bors added a commit that referenced this pull request Jun 19, 2025
Allow `enum` and `union` literals to also create SSA values

Today, `Some(x)` always goes through an `alloca`, even in trivial cases where the niching means the constructor doesn't even change the value.

For example, <https://rust.godbolt.org/z/6KG6PqoYz>
```rust
pub fn demo(r: &i32) -> Option<&i32> {
    Some(r)
}
```
currently emits the IR
```llvm
define align 4 ptr `@demo(ptr` align 4 %r) unnamed_addr {
start:
  %_0 = alloca [8 x i8], align 8
  store ptr %r, ptr %_0, align 8
  %0 = load ptr, ptr %_0, align 8
  ret ptr %0
}
```
but with this PR it becomes just
```llvm
define align 4 ptr `@demo(ptr` align 4 %r) unnamed_addr {
start:
  ret ptr %r
}
```
(Of course the optimizer can clean that up, but it'd be nice if it didn't have to -- especially in debug where it doesn't run.  This is like #123886, but that only handled non-simd `struct`s -- this PR generalizes it to all non-simd ADTs.)

Doing this means handing variants other than `FIRST_VARIANT`, handling the active field for unions, refactoring the discriminant code so the Place and Operand parts can share the calculation, etc.

Other PRs that led up to this one:
- #142005
- #142103
- #142324
- #142383
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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.

5 participants