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

Transform async ResumeTy in generator transform #105977

Merged
merged 1 commit into from
Jan 19, 2023

Conversation

Swatinem
Copy link
Contributor

  • Eliminates all the get_context calls that async lowering created.
  • Replace all Local ResumeTy types with &mut Context<'_>.

The Locals that have their types replaced are:

  • The resume argument itself.
  • The argument to get_context.
  • The yielded value of a yield.

The ResumeTy hides a &mut Context<'_> behind an unsafe raw pointer, and the get_context function is being used to convert that back to a &mut Context<'_>.

Ideally the async lowering would not use the ResumeTy/get_context indirection, but rather directly use &mut Context<'_>, however that would currently lead to higher-kinded lifetime errors.
See #105501.

The async lowering step and the type / lifetime inference / checking are still using the ResumeTy indirection for the time being, and that indirection is removed here. After this transform, the generator body only knows about &mut Context<'_>.


Fixes https://github.com/bjorn3/rustc_codegen_cranelift/issues/1330 CC @bjorn3

r? @compiler-errors

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 20, 2022
@rustbot
Copy link
Collaborator

rustbot commented Dec 20, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@compiler-errors
Copy link
Member

I'm probably not the best person to review mir-opt work.

r? @rust-lang/wg-mir-opt

@rustbot rustbot assigned eddyb and unassigned compiler-errors Dec 21, 2022
@Noratrieb Noratrieb removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Dec 27, 2022
@bors
Copy link
Contributor

bors commented Dec 29, 2022

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

@eddyb
Copy link
Member

eddyb commented Jan 2, 2023

@rustbot assigned @eddyb and unassigned @compiler-errors (2 weeks ago)

Uhh that's a bug I'm pretty sure? I shouldn't be given any reviews (cc @oli-obk).

r? @rust-lang/wg-mir-opt

@oli-obk
Copy link
Contributor

oli-obk commented Jan 9, 2023

Uhh that's a bug I'm pretty sure? I shouldn't be given any review

explicit team assignments will pick anyone from that team, even if they are not on the automatic rotation

@spastorino
Copy link
Member

r? @rust-lang/wg-mir-opt

Comment on lines 484 to 488
fn transform_async_context<'tcx>(
tcx: TyCtxt<'tcx>,
body: &mut Body<'tcx>,
context_mut_ref: Ty<'tcx>,
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we could register this as a special mir opt so we can see its diff in a mir opt test. Seems like a lot of work though, and out of scope for this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would be interesting, but not sure if its worth it.

I consider this PR just a temporary workaround that I would love to revert as soon as the compiler can handle &mut Context<'_> in generators directly.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 17, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Jan 17, 2023

📌 Commit 1db48e2 has been approved by oli-obk

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 Jan 17, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 18, 2023
Transform async `ResumeTy` in generator transform

- Eliminates all the `get_context` calls that async lowering created.
- Replace all `Local` `ResumeTy` types with `&mut Context<'_>`.

The `Local`s that have their types replaced are:
- The `resume` argument itself.
- The argument to `get_context`.
- The yielded value of a `yield`.

The `ResumeTy` hides a `&mut Context<'_>` behind an unsafe raw pointer, and the `get_context` function is being used to convert that back to a `&mut Context<'_>`.

Ideally the async lowering would not use the `ResumeTy`/`get_context` indirection, but rather directly use `&mut Context<'_>`, however that would currently lead to higher-kinded lifetime errors.
See <rust-lang#105501>.

The async lowering step and the type / lifetime inference / checking are still using the `ResumeTy` indirection for the time being, and that indirection is removed here. After this transform, the generator body only knows about `&mut Context<'_>`.

---

Fixes https://github.com/bjorn3/rustc_codegen_cranelift/issues/1330 CC `@bjorn3`

r? `@compiler-errors`
@compiler-errors
Copy link
Member

Needs mir-opt tests blessed: #107046 (comment)

@bors r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 18, 2023
- Eliminates all the `get_context` calls that async lowering created.
- Replace all `Local` `ResumeTy` types with `&mut Context<'_>`.

The `Local`s that have their types replaced are:
- The `resume` argument itself.
- The argument to `get_context`.
- The yielded value of a `yield`.

The `ResumeTy` hides a `&mut Context<'_>` behind an unsafe raw pointer, and the
`get_context` function is being used to convert that back to a `&mut Context<'_>`.

Ideally the async lowering would not use the `ResumeTy`/`get_context` indirection,
but rather directly use `&mut Context<'_>`, however that would currently
lead to higher-kinded lifetime errors.
See <rust-lang#105501>.

The async lowering step and the type / lifetime inference / checking are
still using the `ResumeTy` indirection for the time being, and that indirection
is removed here. After this transform, the generator body only knows about `&mut Context<'_>`.
@Swatinem
Copy link
Contributor Author

Rebased, and added compile-flags: -C panic=abort to the test.
The problem was that the wasm32 target defaults to panic=abort and thus no unwinding paths and future polled after panic was generated there.
Using panic=abort directly should make the output consistent across targets and even simpler.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 19, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Jan 19, 2023

📌 Commit 96931a7 has been approved by oli-obk

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 19, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 19, 2023
…llaumeGomez

Rollup of 5 pull requests

Successful merges:

 - rust-lang#105977 (Transform async `ResumeTy` in generator transform)
 - rust-lang#106927 (make `CastError::NeedsDeref` create a `MachineApplicable` suggestion)
 - rust-lang#106931 (document + UI test `E0208` and make its output more user-friendly)
 - rust-lang#107027 (Remove extra removal from test path)
 - rust-lang#107037 (Fix Dominators::rank_partial_cmp to match documentation)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 30ddeef into rust-lang:master Jan 19, 2023
@rustbot rustbot added this to the 1.68.0 milestone Jan 19, 2023
@Swatinem Swatinem deleted the async-mir-context branch January 19, 2023 14:07
@bjorn3
Copy link
Member

bjorn3 commented Jan 20, 2023

Updated cg_clif in bjorn3/rustc_codegen_cranelift@6c58be8. Works perfect!

celinval added a commit to celinval/kani-dev that referenced this pull request Jan 24, 2023
to overcome an issue with async generators. Updated the
codegen_generator to follow the new logic implemented in
rust-lang/rust#105977
Swatinem added a commit to Swatinem/rust that referenced this pull request Feb 1, 2023
This changes from the stdlib supported `ResumeTy`, and converting that to a `&mut Context<'_>` to directly use `&mut Context<'_>` in lowering.

It pretty much reverts rust-lang#105977 and re-applies an updated version of rust-lang#105250.

The PR is currently failing as it depends on `-Zdrop-tracking-mir` becoming the default, so that the compiler does not falsly believe the context is being held across await points,which would make async blocks `!Send` and `!UnwindSafe`.
However it also still fails the testcase added in rust-lang#106264 for reasons I don’t understand.
celinval added a commit to model-checking/kani that referenced this pull request Mar 8, 2023
Upgrade our toolchain to `nightly-2023-01-23`. The changes here are related to the following changes:
- rust-lang/rust#104986
- rust-lang/rust#105657
- rust-lang/rust#105603
- rust-lang/rust#105613
- rust-lang/rust#105977
- rust-lang/rust#104645
Swatinem added a commit to Swatinem/rust that referenced this pull request Sep 24, 2023
Instead of using the stdlib supported `ResumeTy`, which is being converting to a `&mut Context<'_>` during the Generator MIR pass,
this will use `&mut Context<'_>` directly in HIR lowering.

It pretty much reverts rust-lang#105977 and re-applies an updated version of rust-lang#105250.

This still fails the testcase added in rust-lang#106264 however, for reasons I don’t understand.
Swatinem added a commit to Swatinem/rust that referenced this pull request Dec 28, 2023
Instead of using the stdlib supported `ResumeTy`, which is being converting to a `&mut Context<'_>` during the Generator MIR pass,
this will use `&mut Context<'_>` directly in HIR lowering.

It pretty much reverts rust-lang#105977 and re-applies an updated version of rust-lang#105250.

This still fails the testcase added in rust-lang#106264 however, for reasons I don’t understand.
Swatinem added a commit to Swatinem/rust that referenced this pull request Dec 28, 2023
Instead of using the stdlib supported `ResumeTy`, which is being converting to a `&mut Context<'_>` during the Generator MIR pass,
this will use `&mut Context<'_>` directly in HIR lowering.

It pretty much reverts rust-lang#105977 and re-applies an updated version of rust-lang#105250.

This still fails the testcase added in rust-lang#106264 however, for reasons I don’t understand.
Swatinem added a commit to Swatinem/rust that referenced this pull request Jan 6, 2024
Instead of using the stdlib supported `ResumeTy`, which is being converting to a `&mut Context<'_>` during the Generator MIR pass,
this will use `&mut Context<'_>` directly in HIR lowering.

It pretty much reverts rust-lang#105977 and re-applies an updated version of rust-lang#105250.

This still fails the testcase added in rust-lang#106264 however, for reasons I don’t understand.
Swatinem added a commit to Swatinem/rust that referenced this pull request Feb 17, 2024
Instead of using the stdlib supported `ResumeTy`, which is being converting to a `&mut Context<'_>` during the Generator MIR pass,
this will use `&mut Context<'_>` directly in HIR lowering.

It pretty much reverts rust-lang#105977 and re-applies an updated version of rust-lang#105250.

This still fails the testcase added in rust-lang#106264 however, for reasons I don’t understand.
Swatinem added a commit to Swatinem/rust that referenced this pull request May 12, 2024
Instead of using the stdlib supported `ResumeTy`, which is being converting to a `&mut Context<'_>` during the Generator MIR pass,
this will use `&mut Context<'_>` directly in HIR lowering.

It pretty much reverts rust-lang#105977 and re-applies an updated version of rust-lang#105250.

This still fails the testcase added in rust-lang#106264 however, for reasons I don’t understand.
Swatinem added a commit to Swatinem/rust that referenced this pull request Jul 30, 2024
Instead of using the stdlib supported `ResumeTy`, which is being converting to a `&mut Context<'_>` during the Generator MIR pass,
this will use `&mut Context<'_>` directly in HIR lowering.

It pretty much reverts rust-lang#105977 and re-applies an updated version of rust-lang#105250.

This still fails the testcase added in rust-lang#106264 however, for reasons I don’t understand.
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.

ICE: Can't write value with incompatible type
9 participants