Skip to content

Avoid dynamic executor checks when calling synchronous non-escaping closures #82795

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6278,7 +6278,7 @@ ArgumentList *ExprRewriter::coerceCallArguments(
if (auto declaredIn = decl->findImport(dc))
return !declaredIn->module.importedModule->isConcurrencyChecked();

// Both the caller and the allee are in the same module.
// Both the caller and the callee are in the same module.
if (dc->getParentModule() == decl->getModuleContext()) {
return !dc->getASTContext().isSwiftVersionAtLeast(6);
}
Expand All @@ -6288,16 +6288,47 @@ ArgumentList *ExprRewriter::coerceCallArguments(
return true;
}();

auto applyFlagsToArgument = [&paramInfo,
&closuresRequireDynamicIsolationChecking,
&locator](unsigned paramIdx, Expr *argument) {
auto isNonEscapingDirectlyDispatchedParameter = [&](unsigned paramIdx) -> bool {
auto *decl = callee.getDecl();
if (!decl) return false;

// If this is a non-@escaping synchronous function parameter, there's no need for
// an executor check as it will not escape.
auto isolation = getActorIsolation(decl);
switch (isolation) {
case ActorIsolation::Unspecified:
// Unspecified isolation would be an issue if this parameter was @escaping, but
// given that we will not be hopping to another executor, this is fine.
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
case ActorIsolation::CallerIsolationInheriting:
break;
case ActorIsolation::ActorInstance:
case ActorIsolation::Erased:
case ActorIsolation::GlobalActor:
return false;
}

auto *param = getParameterAt(decl, paramIdx);
if (!param) return false;

auto ty = param->getInterfaceType();
auto *funcTy = dyn_cast<AnyFunctionType>(ty);
if (!funcTy) return false;

return funcTy->isNoEscape() && !funcTy->isAsync();
};

auto applyFlagsToArgument = [&](unsigned paramIdx, Expr *argument) {
if (!isClosureLiteralExpr(argument))
return;

bool isMacroArg = isExpr<MacroExpansionExpr>(locator.getAnchor());

bool requiresDynamicIsolationChecking = closuresRequireDynamicIsolationChecking &&
!isNonEscapingDirectlyDispatchedParameter(paramIdx);
applyContextualClosureFlags(argument, paramIdx, paramInfo,
closuresRequireDynamicIsolationChecking,
requiresDynamicIsolationChecking,
isMacroArg);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -module-name output -emit-silgen -swift-version 6 | swift-demangle | %FileCheck %s

// REQUIRES: concurrency

@MainActor
public func isEven(_ x: Int) -> Bool {
x.isMultiple(of: 2)
}

// CHECK: sil [ossa] @output.mainActorFunc(xs: [Swift.Int])
// CHECK-NOT: _checkTaskExecutor
// CHECK: end sil function 'output.mainActorFunc(xs: [Swift.Int]) -> Swift.Int'
@MainActor
public func mainActorFunc(xs: [Int]) -> Int {
xs.count { isEven($0) }
}