-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Avoid GenFuture
shim when compiling async constructs
#104321
Conversation
r? @oli-obk (rustbot has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs
Outdated
Show resolved
Hide resolved
compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs
Outdated
Show resolved
Hide resolved
compiler/rustc_trait_selection/src/traits/select/confirmation.rs
Outdated
Show resolved
Hide resolved
compiler/rustc_trait_selection/src/traits/select/confirmation.rs
Outdated
Show resolved
Hide resolved
compiler/rustc_trait_selection/src/traits/select/confirmation.rs
Outdated
Show resolved
Hide resolved
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@cjgillot thanks for the review. I implemented the feedback, defining separate structures / functions for the future case in trait selection. I also fixed a couple of tests and blessed some test assertions that are clear improvements:
Maybe I missed a couple of things though. The main regression that now remains is, and that you pointed out on zulip:
This may be related to another change in diagnostics:
As this now avoids the whole
rust/library/core/src/future/mod.rs Line 106 in 084ad59
As I was asking on zulip: Do we need this at all, or can we just avoid checking the resume type at all? I’m also wondering why the generator is "capturing" that type at all? It shouldn’t be? As a separate question: Should I remove the whole |
This comment has been minimized.
This comment has been minimized.
r? @cjgillot @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
⌛ Trying commit 69930d3 with merge eaf9cebf4a95bc065d5aee8056b24fc910ac20e0... |
💔 Test failed - checks-actions |
This comment has been minimized.
This comment has been minimized.
I switched back to using I’m down to 12 failures:
That leaves:
I believe for these I might need to hook up the return type in a different way somehow, not yet sure how to do that. |
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #104219) made this pull request unmergeable. Please resolve the merge conflicts. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Avoid `GenFuture` shim when compiling async constructs Previously, async constructs would be lowered to "normal" generators, with an additional `from_generator` / `GenFuture` shim in between to convert from `Generator` to `Future`. The compiler will now special-case these generators internally so that async constructs will *directly* implement `Future` without the need to go through the `from_generator` / `GenFuture` shim. The primary motivation for this change was hiding this implementation detail in stack traces and debuginfo, but it can in theory also help the optimizer as there is less abstractions to see through. --- Given this demo code: ```rust pub async fn a(arg: u32) -> Backtrace { let bt = b().await; let _arg = arg; bt } pub async fn b() -> Backtrace { Backtrace::force_capture() } ``` I would get the following with the latest stable compiler (on Windows): ``` 4: async_codegen::b::async_fn$0 at .\src\lib.rs:10 5: core::future::from_generator::impl$1::poll<enum2$<async_codegen::b::async_fn_env$0> > at /rustc/897e37553bba8b42751c67658967889d11ecd120\library\core\src\future\mod.rs:91 6: async_codegen::a::async_fn$0 at .\src\lib.rs:4 7: core::future::from_generator::impl$1::poll<enum2$<async_codegen::a::async_fn_env$0> > at /rustc/897e37553bba8b42751c67658967889d11ecd120\library\core\src\future\mod.rs:91 ``` whereas now I get a much cleaner stack trace: ``` 3: async_codegen::b::async_fn$0 at .\src\lib.rs:10 4: async_codegen::a::async_fn$0 at .\src\lib.rs:4 ```
As of this PR cg_clif's CI is failing with thread 'rustc' panicked at 'assertion failed: `(left == right)`
left: `&mut std::task::Context<'_>`,
right: `std::future::ResumeTy`: Can't write value with incompatible type Ref(ReErased, std::task::Context<'_>, Mut) to place with type Adt(std::future::ResumeTy, []) It seems that an |
And after a patch to make it ignore this particular mismatch I get:
Poking a bit around confirms that
A minimal reproducing example is: #![feature(pin_macro)]
use std::pin::{pin, Pin};
use std::future::Future;
use std::ptr::null;
use std::task::{Context, Waker, RawWaker, RawWakerVTable};
fn main() {
let waker = unsafe {
Waker::from_raw(RawWaker::new(null(), &RawWakerVTable::new(|_| todo!(), |_| todo!(), |_| todo!(), |_| todo!())))
};
let mut cx = Context::from_waker(&waker);
call_future(pin!(future()), &mut cx);
}
fn call_future(fut: Pin<&mut impl Future<Output = ()>>, cx: &mut Context) {
fut.poll(cx);
}
async fn future() {} |
This is being fixed in #105082 (comment) |
Thanks for the pointer! |
…piler-errors Make sure async constructs do not `impl Generator` Async lowering turns async functions and blocks into generators internally. Though these special kinds of generators should not `impl Generator` themselves. The other way around, normal generators should not `impl Future`. This was discovered in rust-lang#105082 (comment) and is a regression from rust-lang#104321. r? `@compiler-errors`
Make sure async constructs do not `impl Generator` Async lowering turns async functions and blocks into generators internally. Though these special kinds of generators should not `impl Generator` themselves. The other way around, normal generators should not `impl Future`. This was discovered in rust-lang/rust#105082 (comment) and is a regression from rust-lang/rust#104321. r? `@compiler-errors`
Replace usage of `ResumeTy` in async lowering with `Context` Replaces using `ResumeTy` / `get_context` in favor of using `&'static mut Context<'_>`. Usage of the `'static` lifetime here is technically "cheating", and replaces the raw pointer in `ResumeTy` and the `get_context` fn that pulls the correct lifetimes out of thin air. fixes rust-lang#104828 and rust-lang#104321 (comment) r? `@oli-obk`
Replace usage of `ResumeTy` in async lowering with `Context` Replaces using `ResumeTy` / `get_context` in favor of using `&'static mut Context<'_>`. Usage of the `'static` lifetime here is technically "cheating", and replaces the raw pointer in `ResumeTy` and the `get_context` fn that pulls the correct lifetimes out of thin air. fixes rust-lang#104828 and rust-lang#104321 (comment) r? ``@oli-obk``
Replace usage of `ResumeTy` in async lowering with `Context` Replaces using `ResumeTy` / `get_context` in favor of using `&'static mut Context<'_>`. Usage of the `'static` lifetime here is technically "cheating", and replaces the raw pointer in `ResumeTy` and the `get_context` fn that pulls the correct lifetimes out of thin air. fixes rust-lang#104828 and rust-lang#104321 (comment) r? ```@oli-obk```
Replace usage of `ResumeTy` in async lowering with `Context` Replaces using `ResumeTy` / `get_context` in favor of using `&'static mut Context<'_>`. Usage of the `'static` lifetime here is technically "cheating", and replaces the raw pointer in `ResumeTy` and the `get_context` fn that pulls the correct lifetimes out of thin air. fixes rust-lang#104828 and rust-lang#104321 (comment) r? `@oli-obk`
Replace usage of `ResumeTy` in async lowering with `Context` Replaces using `ResumeTy` / `get_context` in favor of using `&'static mut Context<'_>`. Usage of the `'static` lifetime here is technically "cheating", and replaces the raw pointer in `ResumeTy` and the `get_context` fn that pulls the correct lifetimes out of thin air. fixes rust-lang/rust#104828 and rust-lang/rust#104321 (comment) r? `@oli-obk`
This function/lang_item was introduced in rust-lang#104321 as a temporary workaround of future lowering. The usage and need for it went away in rust-lang#104833. After a bootstrap update, the function itself can be removed from `std`.
…rk-Simulacrum Remove `identity_future` from stdlib This function/lang_item was introduced in rust-lang#104321 as a temporary workaround of future lowering. The usage and need for it went away in rust-lang#104833. After a bootstrap update, the function itself can be removed from `std`.
This function/lang_item was introduced in rust-lang#104321 as a temporary workaround of future lowering. The usage and need for it went away in rust-lang#104833. After a bootstrap update, the function itself can be removed from `std`.
…th-late-bound-vars-again, r=jackh726 Normalize opaques with late-bound vars again We have a hack in the compiler where if an opaque has escaping late-bound vars, we skip revealing it even though we *could* reveal it from a technical perspective. First of all, this is weird, since we really should be revealing all opaques in `Reveal::All` mode. Second of all, it causes subtle bugs (linked below). I attempted to fix this in rust-lang#100980, which was unfortunately reverted due to perf regressions on codebases that used really deeply nested futures in some interesting ways. The worst of which was rust-lang#103423, which caused the project to hang on build. Another one was rust-lang#104842, which was just a slow-down, but not a hang. I took some time afterwards to investigate how to rework `normalize_erasing_regions` to take advantage of better caching, but that effort kinda fizzled out (rust-lang#104133). However, recently, I was made aware of more bugs whose root cause is not revealing opaques during codegen. That made me want to fix this again -- in the process, interestingly, I took the the minimized example from rust-lang#103423 (comment), and it doesn't seem to hang any more... Thinking about this harder, there have been some changes to the way we lower and typecheck async futures that may have reduced the pathologically large number of outlives obligations (see description of rust-lang#103423) that we were encountering when normalizing opaques with bound vars the last time around: * rust-lang#104321 (lower `async { .. }` directly as a generator that implements `Future`, removing the `from_generator` shim) * rust-lang#104833 (removing an `identity_future` fn that was wrapping desugared future generators) ... so given that I can see: * No significant regression on rust perf bot (rust-lang#107620 (comment)) * No timeouts in crater run I did (rust-lang#107620 (comment), rechecked failing crates in rust-lang#107620 (comment)) ... and given that this PR: * Fixes rust-lang#104601 * Fixes rust-lang#107557 * Fixes rust-lang#109464 * Allows us to remove a `DefiningAnchor::Bubble` from codegen (75a8f68) I'm inclined to give this another shot at landing this. Best case, it just works -- worst case, we get more examples to study how we need to improve the compiler to make this work. r? types
Previously, async constructs would be lowered to "normal" generators, with an additional
from_generator
/GenFuture
shim in between to convert fromGenerator
toFuture
.The compiler will now special-case these generators internally so that async constructs will directly implement
Future
without the need to go through thefrom_generator
/GenFuture
shim.The primary motivation for this change was hiding this implementation detail in stack traces and debuginfo, but it can in theory also help the optimizer as there is less abstractions to see through.
Given this demo code:
I would get the following with the latest stable compiler (on Windows):
whereas now I get a much cleaner stack trace: