Skip to content

Refactor split_block in check_pointers to avoid Terminator: None - #165716

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
panstromek:no-terminato-option-split-block
Nov 20, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
panstromek:no-terminato-option-split-block

Conversation

@panstromek

@panstromek panstromek commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Step N towards changing Option<Terminator> to Terminator in BasicBlockData. More info in this thread.

This one used None for because it again needs new block id to create the terminator to put into the split point. We use the same push_with strategy as we used in coroutine transform.

We also inline the split_block function and instead outline insert_check, to keep the push_with block smaller and focused.

r? hanna-kruppe

@rustbot

rustbot commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@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 Nov 9, 2026
@rustbot

rustbot commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Error: Failed to set assignee to hannah-kruppe: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

@rustbot

rustbot commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

hanna-kruppe is currently at their maximum review capacity.
They may take a while to respond.

@hanna-kruppe

Copy link
Copy Markdown
Contributor

The closure inception here makes me want to try the reserve -> commit API even more, I don't like how the control flow jumps all over the place. I tried to inline the split_block function but it didn't seem to make it better.

Yeah, I see what you mean, this isn't good. I'm reminded why I generally prefer first-order control flow and drop guards over higher order functions and (all but the most trivial) functional combinators. I think my earlier skepticism was over-indexing on the possible extension to multiple reservations. For the case where only a single block at a time is pending, I think it could be pretty straightforward to implement and easier to use then nesting closures. Please feel free to give it a shot!

@panstromek

panstromek commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Update: I tried some reserve -> commit version and I hate it as much as this version 😄 .

The problem is that if I do something like this:

let (reserved, blocks) = blocks.reserve_idx();

blocks[some_existing_bb].terminator.some_target_bb = reserved.idx();
// ...
reserved.push_reserved(Terminator::new(...))

then, without linear types, there's nothing that guarantees the final push actually happens. You can just forget the Reserved struct and move on. So it's pretty much the same thing as simply using .next_index() and push(), just with extra steps. The way to enforce that is either

  • through a closure (i.e. with_mut)
  • or if reserve_idx() takes blocks by value - but this would imply we have to mem::take it and not forget to mem::swap it the back again (and also make sure some other part of the code doesn't use the swapped out empty blocks during the pass).
  • or we let the Reserved struct panic in destructor if the reserved index is not filled in, which feels pretty ugly (and doesn't work with mem::forget)

I was imagining this a bit like HashMap's entry api, or as rusqlite's transaction, but came to realize those are different from what I actually want - HashMap doesn't need to guarantee that the entry is filled, it's fine to forget the entry. And similarly, Transaction doesn't need to guarantee that any statement is run in it, it's fine to drop it without running any SQL as well.


The other point is that it didn't actually make the code easier to understand - in fact, the code looks almost identical, it's pretty much just less indented. I think the thing that makes this difficult to understand is that we're doing some operations that fundementally overlap:

  • splitting block needs new terminator
  • new terminator needs new block index
  • new block index needs the old terminator
  • and old terminator needs the new terminator (to be swapped with).

The previous implementation could at least neatly separate the "move everything after the split point into a new block" operation from finalizing the block that has been split. The new implementation can't do that because we can't keep the edited block without terminator, so these two opreations become fundamentally intertwined.

In other words, the previous code did use None terminator to break the circular dependency, the new code will need to predict future block id to break it. And with_mut is probably the cleanest way to do this.


So, I think I will try to shuffle the code around and maybe get rid of one of those closures, either by inlining the split_block function or by using next_index instead (and hope for the best :D). I'll maybe outline some different piece of code to keep the "critical section" small.


Maybe too much overthinking for such a simple piece of code, but I treat this as a bit of a testing ground for how to write MIR edits in general without the Option<Terminator>. That's something people will encounter whenever they try to write some non trivial MIR transform in the future.

@panstromek

Copy link
Copy Markdown
Contributor Author

I added a second commit which reshuffles this a little bit to separate inserting the checks form finalizing the new block - we inline the split_block function and instead outline insert_check, to keep the push_with block smaller and focused.

This way the push_with call isn't at the begining, which was a bit confusing because it's actually the last thing we need to do. I extracted the check insertion into a separate closure - it could be a free function but it'd have to contain gazilion parameteres which I think would loose the benefits. This feels better to me, I think it's easier to follow.

@rustbot ready

@hanna-kruppe hanna-kruppe left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Splitting out the statement insertion to make the block manipulation visually go last and fit on one screen makes sense to me. I hope that this approach scales to other uses of block splitting, we'll see.

I don't love the extra nesting, but this code was already near or over my limit for nesting before, and yeah I wouldn't expect turning it into a free function would be any better. The complex signature of the on_finding callback in particular is Not Great but none of that is really related to or affected by this PR.

r=me after squashing and updating the PR description for posterity (you can keep the history if you want, but right now it contradicts the final version of the diff).

View changes since this review

@hanna-kruppe

Copy link
Copy Markdown
Contributor

then, without linear types, there's nothing that guarantees the final push actually happens. You can just forget the Reserved struct and move on. So it's pretty much the same thing as simply using .next_index() and push(), just with extra steps. The way to enforce that is either

FWIW, I think that an Entry-like API could still be useful for other cases (it didn't end up necessary here). It's better than next_index() + push() because it prevents you from pushing something else until you commit or drop the reservation. If you can keep the scope of the reservation very small (similar to how you factored out insert_check here), then it may be sufficiently obvious that we do not forget it (or only forget it intentionally!). In particular, we do not have to leave behind sensible MIR if something unwinds, and MIR transforms are generally not fallible, so usually early exits will be explicit returns rather than entirely implicit or hidden in ?.

@panstromek

Copy link
Copy Markdown
Contributor Author

Thanks ;)

I don't love the extra nesting, but this code was already near or over my limit for nesting before, ... The complex signature of the on_finding callback in particular is Not Great but none of that is really related to or affected by this PR.

Yea, I really don't like how this thing is tangled with that callback in there. I don't like that this code just gives out its partially broken datastructures with some unclear invariants into an opaque function call. Those callbacks do some fairly simple things in the end I think it could somehow be unified better.

If the callback just returned some impl Iterator<Statement> instead of taking &mut Vec<Statement>, it'd already be nicer I think. I'd honestly be fine with even removing the callbacks altogether and replacing them with if-else cascade, calling the functions directly. But yea, that's something to try out another time.

We also inline split_block and outline insert_check instead in check_pointers. The goal is to separate inserting the check from creating and inserting the new block. This should make the logic slightly easier to follow
@panstromek
panstromek force-pushed the no-terminato-option-split-block branch from 2c77a98 to d7bfe5e Compare November 19, 2026 19:03
@panstromek

Copy link
Copy Markdown
Contributor Author

@bors r=hanna-kruppe

@rust-bors

rust-bors Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

📌 Commit d7bfe5e has been tentatively approved by hanna-kruppe

It will be put into the queue for this repository once PR CI succeeds.

@rust-bors rust-bors Bot 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 Nov 19, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 20, 2026
…it-block, r=hanna-kruppe

Refactor split_block in check_pointers to avoid Terminator: None

Step N towards changing `Option<Terminator>` to `Terminator` in `BasicBlockData`. More info in [this thread](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/vibe.20check.20-.20removing.20Option.20from.20Terminator/with/637588808).

This one used `None` for because it again needs new block id to create the terminator to put into the split point. We use the same `push_with` strategy as we used in coroutine transform.

We also inline the `split_block` function and instead outline insert_check, to keep the `push_with` block smaller and focused.

r? hanna-kruppe
rust-bors Bot pushed a commit that referenced this pull request Nov 20, 2026
Rollup of 21 pull requests

Successful merges:

 - #165707 (bootstrap: Make low priority the default)
 - #165716 (Refactor split_block in check_pointers to avoid Terminator: None)
 - #165934 (Avoid using Terminator:None in simplify_cfg)
 - #166050 (Use attribute parser for lang attributes in `LateResolutionVisitor`)
 - #166069 (bootstrap: fix keep-stage-std)
 - #166117 (Remove ebadf handling from stdio)
 - #166119 (Avoid an ICE on re-exports of private imports without a DefId)
 - #165094 (reflection: doc attributes from adts, fields, and variants)
 - #165216 (Clearer diagnostics when `&FnOnce` was provided when `FnOnce` was expected)
 - #165694 (view-types: make sure view types are !Sized)
 - #165802 (Add CI workflow to publish backtrace after a push to main)
 - #165883 (Report a recursion limit error instead of delaying a bug in RPITIT collector)
 - #165906 (fix ICE when a forced keyword follows a parameter pattern)
 - #166064 (Add const generic transmute min support)
 - #166136 (Move ub_checks() inside the const_eval_select branch.)
 - #166158 (bump some tools' `toml` dependency)
 - #166166 (Don't recover if not allowed in `parse_seq_to_before_tokens`)
 - #166176 (compiletest: Move all code out of the crate root)
 - #166185 (simd-test: Fix cfg syntax in the test macro)
 - #166194 (std: Unstably constify `std::process::abort`)
 - #166210 (Avoid terminator: None in insert_direct_enum_check)

Failed merges:

 - #166150 (Reflection: refactor Type and TypeKind)
 - #166160 (refactor CTFE-specific intrinsics to different file)
rust-bors Bot pushed a commit that referenced this pull request Nov 20, 2026
Rollup of 20 pull requests

Successful merges:

 - #165716 (Refactor split_block in check_pointers to avoid Terminator: None)
 - #165934 (Avoid using Terminator:None in simplify_cfg)
 - #166050 (Use attribute parser for lang attributes in `LateResolutionVisitor`)
 - #166069 (bootstrap: fix keep-stage-std)
 - #166117 (Remove ebadf handling from stdio)
 - #166119 (Avoid an ICE on re-exports of private imports without a DefId)
 - #165094 (reflection: doc attributes from adts, fields, and variants)
 - #165216 (Clearer diagnostics when `&FnOnce` was provided when `FnOnce` was expected)
 - #165694 (view-types: make sure view types are !Sized)
 - #165802 (Add CI workflow to publish backtrace after a push to main)
 - #165883 (Report a recursion limit error instead of delaying a bug in RPITIT collector)
 - #165906 (fix ICE when a forced keyword follows a parameter pattern)
 - #166064 (Add const generic transmute min support)
 - #166136 (Move ub_checks() inside the const_eval_select branch.)
 - #166158 (bump some tools' `toml` dependency)
 - #166166 (Don't recover if not allowed in `parse_seq_to_before_tokens`)
 - #166176 (compiletest: Move all code out of the crate root)
 - #166185 (simd-test: Fix cfg syntax in the test macro)
 - #166194 (std: Unstably constify `std::process::abort`)
 - #166210 (Avoid terminator: None in insert_direct_enum_check)

Failed merges:

 - #166150 (Reflection: refactor Type and TypeKind)
 - #166160 (refactor CTFE-specific intrinsics to different file)
@rust-bors
rust-bors Bot merged commit a64d4d9 into rust-lang:main Nov 20, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 20, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 20, 2026
Rollup merge of #165716 - panstromek:no-terminato-option-split-block, r=hanna-kruppe

Refactor split_block in check_pointers to avoid Terminator: None

Step N towards changing `Option<Terminator>` to `Terminator` in `BasicBlockData`. More info in [this thread](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/vibe.20check.20-.20removing.20Option.20from.20Terminator/with/637588808).

This one used `None` for because it again needs new block id to create the terminator to put into the split point. We use the same `push_with` strategy as we used in coroutine transform.

We also inline the `split_block` function and instead outline insert_check, to keep the `push_with` block smaller and focused.

r? hanna-kruppe
folkertdev pushed a commit to folkertdev/stdarch that referenced this pull request Nov 22, 2026
Rollup of 20 pull requests

Successful merges:

 - rust-lang/rust#165716 (Refactor split_block in check_pointers to avoid Terminator: None)
 - rust-lang/rust#165934 (Avoid using Terminator:None in simplify_cfg)
 - rust-lang/rust#166050 (Use attribute parser for lang attributes in `LateResolutionVisitor`)
 - rust-lang/rust#166069 (bootstrap: fix keep-stage-std)
 - rust-lang/rust#166117 (Remove ebadf handling from stdio)
 - rust-lang/rust#166119 (Avoid an ICE on re-exports of private imports without a DefId)
 - rust-lang/rust#165094 (reflection: doc attributes from adts, fields, and variants)
 - rust-lang/rust#165216 (Clearer diagnostics when `&FnOnce` was provided when `FnOnce` was expected)
 - rust-lang/rust#165694 (view-types: make sure view types are !Sized)
 - rust-lang/rust#165802 (Add CI workflow to publish backtrace after a push to main)
 - rust-lang/rust#165883 (Report a recursion limit error instead of delaying a bug in RPITIT collector)
 - rust-lang/rust#165906 (fix ICE when a forced keyword follows a parameter pattern)
 - rust-lang/rust#166064 (Add const generic transmute min support)
 - rust-lang/rust#166136 (Move ub_checks() inside the const_eval_select branch.)
 - rust-lang/rust#166158 (bump some tools' `toml` dependency)
 - rust-lang/rust#166166 (Don't recover if not allowed in `parse_seq_to_before_tokens`)
 - rust-lang/rust#166176 (compiletest: Move all code out of the crate root)
 - rust-lang/rust#166185 (simd-test: Fix cfg syntax in the test macro)
 - rust-lang/rust#166194 (std: Unstably constify `std::process::abort`)
 - rust-lang/rust#166210 (Avoid terminator: None in insert_direct_enum_check)

Failed merges:

 - rust-lang/rust#166150 (Reflection: refactor Type and TypeKind)
 - rust-lang/rust#166160 (refactor CTFE-specific intrinsics to different file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

3 participants