Skip to content

Comments

UnsafePinned: implement coroutine lowering changes#152946

Open
b-naber wants to merge 4 commits intorust-lang:mainfrom
b-naber:coroutine-unsafe-pinned
Open

UnsafePinned: implement coroutine lowering changes#152946
b-naber wants to merge 4 commits intorust-lang:mainfrom
b-naber:coroutine-unsafe-pinned

Conversation

@b-naber
Copy link
Contributor

@b-naber b-naber commented Feb 21, 2026

Implements the lowering part for #125735

This introduces a visitor that looks for self-referential fields in the coroutine struct and wraps all of these types into UnsafePinned. I've also included a commit where we change the condition for using noalias. That change is based on my understanding of the RFC, but is likely wrong given the change in #151365. I've included it for now to further discuss this.

cc @RalfJung

@rustbot
Copy link
Collaborator

rustbot commented Feb 21, 2026

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 Feb 21, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 21, 2026

r? @mati865

rustbot has assigned @mati865.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, mir, mir-opt
  • compiler, mir, mir-opt expanded to 68 candidates
  • Random selection from 15 candidates

@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms bot force-pushed the coroutine-unsafe-pinned branch from 50482bd to b226e46 Compare February 21, 2026 20:58
PointerKind::SharedRef { frozen } => frozen,
PointerKind::MutableRef { unpin } => unpin && noalias_mut_ref,
PointerKind::MutableRef { unpin, unsafe_unpin } => {
(unpin || unsafe_unpin) && noalias_mut_ref
Copy link
Member

Choose a reason for hiding this comment

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

This looks very wrong, it means a safe impl Unpin for X can add noalias to a type that contains an UnsafePinned!

Copy link
Member

Choose a reason for hiding this comment

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

I am very surprised that the codegen tests I added are not catching this. Specifically these two

// CHECK: @trait_box_pin1(ptr noundef nonnull align 1{{( %0)?}}, {{.+}} noalias noundef readonly align {{.*}} dereferenceable({{.*}}){{( %1)?}})
#[no_mangle]
pub fn trait_box_pin1(_: Box<dyn Drop + Unpin>) {}
// CHECK: @trait_box_pin2(ptr noundef nonnull align 1{{( %0)?}}, {{.+}} noalias noundef readonly align {{.*}} dereferenceable({{.*}}){{( %1)?}})
#[no_mangle]
pub fn trait_box_pin2(_: Box<dyn Drop + UnsafeUnpin>) {}

They have one of the traits so shouldn't this check add the noalias?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, you only changed the logic for &mut, not for Box. Argh. Clearly we need more tests.^^

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This looks very wrong, it means a safe impl Unpin for X can add noalias to a type that contains an UnsafePinned!

oh yes ^^. Really it should just be unsafe_unpin, right? But we cannot do this because of backward compatibility issues? It's kind of unfortunate that we cannot use noalias on the coroutine struct argument because of this. Can you say how much using noalias for the coroutine struct would give us in terms of optimization potential? Would it maybe make sense to use an internal type analogous to UnsafePinned for the coroutine struct?

Copy link
Member

Choose a reason for hiding this comment

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

Eventually, it should just be unsafe_unpin, yes. For now, we keep the Unpin hack alive, to avoid Miri going red everywhere at the same time.

Why did you change this code at all? What's wrong with unpin && noalias_mut_ref?

Copy link
Contributor Author

@b-naber b-naber Feb 23, 2026

Choose a reason for hiding this comment

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

Ok that makes sense.

Initially I was unaware of #151365 and implemented those changes with only unsafe_unpinned && noalias_mut_ref as the condition. When rebasing I saw your commit and was confused, because I thought that the goal of introducing UnsafePinned in coroutines was to allow using noalias more liberally there, and then included the condition in this PR without really thinking things through.

Copy link
Member

@RalfJung RalfJung Feb 23, 2026

Choose a reason for hiding this comment

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

The goal is to remove the Unpin hack, but we have to take this one step at a time. We're transitioning from "noalias if A" to "noalias if B" via "noalias if A && B" to ensure that the intermediate state is sound for both old and new code. This PR will move a lot of code to the new world. :)

Zalathar added a commit to Zalathar/rust that referenced this pull request Feb 23, 2026
…rk-Simulacrum

extend unpin noalias tests to cover mutable references

rust-lang#152946 made a change to the logic for this attribute that the test should have flagged as problematic -- but the test only checked `Box`, not `&mut`, and those have independent code paths. So extend the test to also cover `&mut`.

@b-naber would be nice if you could confirm that the added tests do fail with your PR.
rust-timer added a commit that referenced this pull request Feb 23, 2026
Rollup merge of #152970 - RalfJung:unpin-noalias-tests, r=Mark-Simulacrum

extend unpin noalias tests to cover mutable references

#152946 made a change to the logic for this attribute that the test should have flagged as problematic -- but the test only checked `Box`, not `&mut`, and those have independent code paths. So extend the test to also cover `&mut`.

@b-naber would be nice if you could confirm that the added tests do fail with your PR.
@mati865
Copy link
Member

mati865 commented Feb 23, 2026

I'm not a good fit for this code, perhaps r? @RalfJung ?

Feel free to reroll

@rustbot rustbot assigned RalfJung and unassigned mati865 Feb 23, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 23, 2026

RalfJung is not on the review rotation at the moment.
They may take a while to respond.

@RalfJung
Copy link
Member

Definitely not, I don't know the coroutine lowering code at all.

@rustbot reroll

@rustbot rustbot assigned adwinwhite and unassigned RalfJung Feb 23, 2026
@b-naber
Copy link
Contributor Author

b-naber commented Feb 23, 2026

@cjgillot Could you maybe review this?

@rust-cloud-vms rust-cloud-vms bot force-pushed the coroutine-unsafe-pinned branch from b226e46 to d8b3c3d Compare February 23, 2026 17:46
@rustbot
Copy link
Collaborator

rustbot commented Feb 23, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms bot force-pushed the coroutine-unsafe-pinned branch from d8b3c3d to 3d40415 Compare February 23, 2026 20:28
@cjgillot cjgillot self-assigned this Feb 24, 2026
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Feb 24, 2026
…crum

extend unpin noalias tests to cover mutable references

rust-lang/rust#152946 made a change to the logic for this attribute that the test should have flagged as problematic -- but the test only checked `Box`, not `&mut`, and those have independent code paths. So extend the test to also cover `&mut`.

@b-naber would be nice if you could confirm that the added tests do fail with your PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants