Skip to content

[concurrency] SILGen: emit @asyncHandler functions. #34876

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

Merged
merged 4 commits into from
Dec 1, 2020

Conversation

eeckstein
Copy link
Contributor

@eeckstein eeckstein commented Nov 26, 2020

An asyncHandler function is split into two functions:

  1. The asyncHandler body function: it contains the body of the function, but is emitted as an async function.
  2. The original function: it just contains
    _runAsyncHandler(operation: asyncHandlerBodyFunction)

To distinguish the names of the original asyncHandler function and it's generated "body-function", the body-function name is mangled with an async attribute, i.e. as if it was declared as async.

rdar://problem/71247879

@eeckstein
Copy link
Contributor Author

@swift-ci smoke test

Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

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

Looking pretty good, thanks!

@@ -2870,7 +2874,7 @@ void ASTMangler::appendEntity(const ValueDecl *decl, StringRef EntityOp,
appendOperator("Z");
}

void ASTMangler::appendEntity(const ValueDecl *decl) {
void ASTMangler::appendEntity(const ValueDecl *decl, bool isAsyncHandlerBody) {
Copy link
Member

Choose a reason for hiding this comment

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

Should this bool be a FunctionManglingKind instead to make it clearer?

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 thought of that, but at the end decided to pass a "bool", because FunctionManglingKind has 3 cases.

We don't introduce a new mangling here.
To distinguish the names of the original asyncHandler function and it's generated "body-function", we just mangle the body-function with an async attribute, i.e. as if it was declared as async.
This change is mostly to pass information to the ASTMangler to mangle a not async function as "async".
It just calls Task.runDetatched.
It's more efficient to have a non-generic compiler intrinsic than to let the compiler call the generic Task.runDetatched.
The _runAsyncHandler doesn't have to be generic because the return value of the run function is defined to be Void.
… a non-generic closure argument.

It fixes a crash in SILGen if Builtin.createAsyncTaskFuture is used in a non-generic context.
I found this by experiment - we don't use it currently in the stdlib. But it doesn't harm to fix this.
An asyncHandler function is split into two functions:
1. The asyncHandler body function: it contains the body of the function, but is emitted as an async function.
2. The original function: it just contains
      _runAsyncHandler(operation: asyncHandlerBodyFunction)

rdar://problem/71247879
@eeckstein eeckstein force-pushed the silgen-async-handler branch from c0d5710 to 8e03bd3 Compare December 1, 2020 08:09
@eeckstein
Copy link
Contributor Author

@swift-ci smoke test

@eeckstein eeckstein merged commit 14350f8 into swiftlang:main Dec 1, 2020
@eeckstein eeckstein deleted the silgen-async-handler branch December 1, 2020 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants