Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Stop using the unpack! macro in MIR building #127416

Closed
wants to merge 3 commits into from

Conversation

Zalathar
Copy link
Contributor

@Zalathar Zalathar commented Jul 6, 2024

MIR building currently has an unpack! macro (documented in the dev guide) that is used to unpack values of BlockAnd<T>, by assigning the block to a variable and then returning the value. It is used in around 100 places in the rustc_mir_build crate.

The downside is that understanding this code requires understanding a custom macro, which to me seems like a poor tradeoff relative to the modest benefits of using the macro.

To explore this, I've prepared a PR that removes the unpack! macro completely, replacing it with a small number of ordinary methods on the BlockAnd type.

In the majority of cases, we replace this code:

// Note that the actual type of `self.foo(...)` does not match `value` or `block`.
let value = unpack!(block = self.foo(...));

With this code:

// This is just normal Rust code.
let value = self.foo(...).unpack(&mut block);

Zulip thread

@rustbot
Copy link
Collaborator

rustbot commented Jul 6, 2024

r? @fmease

rustbot has assigned @fmease.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added 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 Jul 6, 2024
@Zalathar
Copy link
Contributor Author

Zalathar commented Jul 6, 2024

One of the things I strongly dislike about unpack! is that it has two different forms that are inconsistent with each other. One assigns the block and returns the value, while the other returns the block and discards the value (which must be ()).

@Zalathar Zalathar marked this pull request as ready for review July 6, 2024 08:30
@rustbot
Copy link
Collaborator

rustbot commented Jul 6, 2024

Some changes occurred in match lowering

cc @Nadrieril

Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

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

Thanks, that's a lot nicer!

r=me with feedback addressed in one way or another

///
/// This is similar to writing `let _ = ...`, except that it is also
/// (a) postfix, and (b) searchable.
fn unpack_discard(self) {}
Copy link
Member

Choose a reason for hiding this comment

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

It's not really unpacking anything. I'm not super convinced by this method, I'd just use let _ = … as the docs mention but er, it's fine.

@@ -102,8 +102,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
);

// Unpack `BlockAnd<BasicBlock>` into `(then_blk, else_blk)`.
let (then_blk, mut else_blk);
else_blk = unpack!(then_blk = then_and_else_blocks);
let (then_blk, mut else_blk) = then_and_else_blocks.unpack_to_pair();
Copy link
Member

Choose a reason for hiding this comment

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

Why not just

Suggested change
let (then_blk, mut else_blk) = then_and_else_blocks.unpack_to_pair();
let BlockAnd(then_blk, mut else_blk) = then_and_else_blocks;

this would allow us to get rid of unpack_to_pair which essentially converts a tuple struct to a tuple.

let mut block;
let rv = unpack!(block = f(self));

let (mut block, rv) = f(self).unpack_to_pair();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let (mut block, rv) = f(self).unpack_to_pair();
let BlockAnd(mut block, rv) = f(self);

ditto

@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 7, 2024
@@ -128,7 +128,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block.and(Operand::Constant(Box::new(constant)))
}
Category::Constant | Category::Place | Category::Rvalue(..) => {
let operand = unpack!(block = this.as_temp(block, scope, expr_id, Mutability::Mut));
let operand =
this.as_temp(block, scope, expr_id, Mutability::Mut).unpack(&mut block);
Copy link
Member

Choose a reason for hiding this comment

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

I have mixed feelings about this, the flow of block is easier to follow with the macro imo. Have we considered:

Suggested change
this.as_temp(block, scope, expr_id, Mutability::Mut).unpack(&mut block);
this.as_temp(&mut block, scope, expr_id, Mutability::Mut);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I quite like using &mut block as an in+out parameter, and have used it in some of my previous coverage work.

The main reason I didn't try to use it here was to keep this PR as modest as possible. But it's certainly something I'd like to explore.

impl BlockAnd<()> {
/// Unpacks `BlockAnd<()>` into a [`BasicBlock`].
#[must_use]
fn unpack_block(self) -> BasicBlock {
Copy link
Member

Choose a reason for hiding this comment

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

The name wasn't clear to me. How about unpack_unit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I might just try unpack_block_and_unit instead.

@Zalathar
Copy link
Contributor Author

Zalathar commented Jul 7, 2024

After considering feedback, I think my current plan is to split this up into 3 stages:

  • Extract the BlockAnd<()> changes into a separate smaller PR to land first (with feedback addressed).
  • Replace unpack! with destructuring assignment on BlockAnd in most places, but leave unpack! in the minority of cases where shadowing makes this awkward.
  • Leave the remaining awkward cases (and the complete removal of unpack!) for a later PR where the stakes are lower and it's easier to agree on a good replacement.

Zalathar added 2 commits July 8, 2024 10:22
This kind of unpacking can be expressed as an ordinary method on
`BlockAnd<()>`.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 17, 2024
MIR building: Stop using `unpack!` for `BlockAnd<()>`

This is a subset of rust-lang#127416, containing only the parts related to `BlockAnd<()>`.

The first patch removes the non-assigning form of the `unpack!` macro, because it is frustratingly inconsistent with the main form. We can replace it with an ordinary method that discards the `()` and returns the block.

The second patch then finds all of the remaining code that was using `unpack!` with `BlockAnd<()>`, and updates it to use that new method instead.

---

Changes since original review of rust-lang#127416:
- Renamed `fn unpack_block` → `fn into_block`
- Removed `fn unpack_discard`, replacing it with `let _: BlockAnd<()> = ...` (2 occurrences)
- Tweaked `arm_end_blocks` to unpack earlier and build `Vec<BasicBlock>` instead of `Vec<BlockAnd<()>>`
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jul 18, 2024
Rollup merge of rust-lang#127472 - Zalathar:block-and-unit, r=fmease

MIR building: Stop using `unpack!` for `BlockAnd<()>`

This is a subset of rust-lang#127416, containing only the parts related to `BlockAnd<()>`.

The first patch removes the non-assigning form of the `unpack!` macro, because it is frustratingly inconsistent with the main form. We can replace it with an ordinary method that discards the `()` and returns the block.

The second patch then finds all of the remaining code that was using `unpack!` with `BlockAnd<()>`, and updates it to use that new method instead.

---

Changes since original review of rust-lang#127416:
- Renamed `fn unpack_block` → `fn into_block`
- Removed `fn unpack_discard`, replacing it with `let _: BlockAnd<()> = ...` (2 occurrences)
- Tweaked `arm_end_blocks` to unpack earlier and build `Vec<BasicBlock>` instead of `Vec<BlockAnd<()>>`
@bors
Copy link
Contributor

bors commented Jul 18, 2024

☔ The latest upstream changes (presumably #127865) made this pull request unmergeable. Please resolve the merge conflicts.

@Zalathar
Copy link
Contributor Author

Having dealt with the BlockAnd<()> cases, the rest of this feels a lot less compelling to me, especially since the alternatives aren't necessarily clear improvements.

So I think I'm going to close this for now.

@Zalathar Zalathar closed this Jul 26, 2024
@Zalathar Zalathar deleted the unpack branch July 26, 2024 05:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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