Skip to content

Commit c8780f3

Browse files
authored
Merge pull request #2233 from swiftwasm/main
[pull] swiftwasm from main
2 parents 2bb9c8f + beeefbc commit c8780f3

16 files changed

+506
-44
lines changed

include/swift/AST/Expr.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,6 +3553,79 @@ class SequenceExpr final : public Expr,
35533553
}
35543554
};
35553555

3556+
/// Actor isolation for a closure.
3557+
class ClosureActorIsolation {
3558+
public:
3559+
enum Kind {
3560+
/// The closure is independent of any actor.
3561+
Independent,
3562+
3563+
/// The closure is tied to the actor instance described by the given
3564+
/// \c VarDecl*, which is the (captured) `self` of an actor.
3565+
ActorInstance,
3566+
3567+
/// The closure is tied to the global actor described by the given type.
3568+
GlobalActor,
3569+
};
3570+
3571+
private:
3572+
/// The actor to which this closure is isolated.
3573+
///
3574+
/// There are three possible states:
3575+
/// - NULL: The closure is independent of any actor.
3576+
/// - VarDecl*: The 'self' variable for the actor instance to which
3577+
/// this closure is isolated. It will always have a type that conforms
3578+
/// to the \c Actor protocol.
3579+
/// - Type: The type of the global actor on which
3580+
llvm::PointerUnion<VarDecl *, Type> storage;
3581+
3582+
ClosureActorIsolation(VarDecl *selfDecl) : storage(selfDecl) { }
3583+
ClosureActorIsolation(Type globalActorType) : storage(globalActorType) { }
3584+
3585+
public:
3586+
ClosureActorIsolation() : storage() { }
3587+
3588+
static ClosureActorIsolation forIndependent() {
3589+
return ClosureActorIsolation();
3590+
}
3591+
3592+
static ClosureActorIsolation forActorInstance(VarDecl *selfDecl) {
3593+
return ClosureActorIsolation(selfDecl);
3594+
}
3595+
3596+
static ClosureActorIsolation forGlobalActor(Type globalActorType) {
3597+
return ClosureActorIsolation(globalActorType);
3598+
}
3599+
3600+
/// Determine the kind of isolation.
3601+
Kind getKind() const {
3602+
if (storage.isNull())
3603+
return Kind::Independent;
3604+
3605+
if (storage.is<VarDecl *>())
3606+
return Kind::ActorInstance;
3607+
3608+
return Kind::GlobalActor;
3609+
}
3610+
3611+
/// Whether the closure is isolated at all.
3612+
explicit operator bool() const {
3613+
return getKind() != Kind::Independent;
3614+
}
3615+
3616+
/// Whether the closure is isolated at all.
3617+
operator Kind() const {
3618+
return getKind();
3619+
}
3620+
3621+
VarDecl *getActorInstance() const {
3622+
return storage.dyn_cast<VarDecl *>();
3623+
}
3624+
3625+
Type getGlobalActor() const {
3626+
return storage.dyn_cast<Type>();
3627+
}
3628+
};
35563629

35573630
/// A base class for closure expressions.
35583631
class AbstractClosureExpr : public DeclContext, public Expr {
@@ -3561,6 +3634,9 @@ class AbstractClosureExpr : public DeclContext, public Expr {
35613634
/// The set of parameters.
35623635
ParameterList *parameterList;
35633636

3637+
/// Actor isolation of the closure.
3638+
ClosureActorIsolation actorIsolation;
3639+
35643640
public:
35653641
AbstractClosureExpr(ExprKind Kind, Type FnType, bool Implicit,
35663642
unsigned Discriminator, DeclContext *Parent)
@@ -3625,6 +3701,12 @@ class AbstractClosureExpr : public DeclContext, public Expr {
36253701
/// Only valid when \c hasSingleExpressionBody() is true.
36263702
Expr *getSingleExpressionBody() const;
36273703

3704+
ClosureActorIsolation getActorIsolation() const { return actorIsolation; }
3705+
3706+
void setActorIsolation(ClosureActorIsolation actorIsolation) {
3707+
this->actorIsolation = actorIsolation;
3708+
}
3709+
36283710
static bool classof(const Expr *E) {
36293711
return E->getKind() >= ExprKind::First_AbstractClosureExpr &&
36303712
E->getKind() <= ExprKind::Last_AbstractClosureExpr;

lib/AST/ASTDumper.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,6 +2505,22 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
25052505
printCommon(E, name);
25062506
PrintWithColorRAII(OS, DiscriminatorColor)
25072507
<< " discriminator=" << E->getDiscriminator();
2508+
2509+
switch (auto isolation = E->getActorIsolation()) {
2510+
case ClosureActorIsolation::Independent:
2511+
break;
2512+
2513+
case ClosureActorIsolation::ActorInstance:
2514+
PrintWithColorRAII(OS, CapturesColor) << " actor-isolated="
2515+
<< isolation.getActorInstance()->printRef();
2516+
break;
2517+
2518+
case ClosureActorIsolation::GlobalActor:
2519+
PrintWithColorRAII(OS, CapturesColor) << " global-actor-isolated="
2520+
<< isolation.getGlobalActor().getString();
2521+
break;
2522+
}
2523+
25082524
if (!E->getCaptureInfo().isTrivial()) {
25092525
OS << " ";
25102526
E->getCaptureInfo().print(PrintWithColorRAII(OS, CapturesColor).getOS());

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,7 @@ void swift::visitAccessedAddress(SILInstruction *I,
19631963
case SILInstructionKind::ExistentialMetatypeInst:
19641964
case SILInstructionKind::FixLifetimeInst:
19651965
case SILInstructionKind::GlobalAddrInst:
1966+
case SILInstructionKind::HopToExecutorInst:
19661967
case SILInstructionKind::InitExistentialValueInst:
19671968
case SILInstructionKind::IsUniqueInst:
19681969
case SILInstructionKind::IsEscapingClosureInst:

lib/SILOptimizer/Utils/CheckedCastBrJumpThreading.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,12 @@ void CheckedCastBrJumpThreading::Edit::modifyCFGForFailurePreds(
251251
assert(!Cloner.wasCloned());
252252
Cloner.cloneBlock();
253253
SILBasicBlock *TargetFailureBB = Cloner.getNewBB();
254-
auto *TI = TargetFailureBB->getTerminator();
255-
SILBuilderWithScope Builder(TI);
256-
// This BB copy branches to a FailureBB.
257-
auto *CCBI = cast<CheckedCastBranchInst>(CCBBlock->getTerminator());
258-
Builder.createBranch(TI->getLoc(), CCBI->getFailureBB());
259-
TI->eraseFromParent();
260-
splitIfCriticalEdge(CCBBlock, CCBI->getFailureBB());
254+
auto *clonedCCBI =
255+
cast<CheckedCastBranchInst>(TargetFailureBB->getTerminator());
256+
SILBuilderWithScope Builder(clonedCCBI);
257+
// This BB copy branches to the FailureBB.
258+
Builder.createBranch(clonedCCBI->getLoc(), clonedCCBI->getFailureBB());
259+
clonedCCBI->eraseFromParent();
261260

262261
// Redirect all FailurePreds to the copy of BB.
263262
for (auto *Pred : FailurePreds) {
@@ -272,9 +271,9 @@ void CheckedCastBrJumpThreading::Edit::modifyCFGForFailurePreds(
272271
/// a landing basic block for all FailurePreds.
273272
void CheckedCastBrJumpThreading::Edit::modifyCFGForSuccessPreds(
274273
BasicBlockCloner &Cloner) {
275-
auto *CCBI = cast<CheckedCastBranchInst>(CCBBlock->getTerminator());
276274

277275
if (InvertSuccess) {
276+
auto *CCBI = cast<CheckedCastBranchInst>(CCBBlock->getTerminator());
278277
SILBuilderWithScope(CCBI).createBranch(CCBI->getLoc(),
279278
CCBI->getFailureBB());
280279
CCBI->eraseFromParent();
@@ -287,13 +286,14 @@ void CheckedCastBrJumpThreading::Edit::modifyCFGForSuccessPreds(
287286
assert(!Cloner.wasCloned());
288287
Cloner.cloneBlock();
289288
SILBasicBlock *TargetSuccessBB = Cloner.getNewBB();
290-
auto *TI = TargetSuccessBB->getTerminator();
291-
SILBuilderWithScope Builder(TI);
289+
auto *clonedCCBI =
290+
cast<CheckedCastBranchInst>(TargetSuccessBB->getTerminator());
291+
SILBuilderWithScope Builder(clonedCCBI);
292292
// This BB copy branches to SuccessBB.
293293
// Take argument value from the dominating BB.
294-
Builder.createBranch(TI->getLoc(), CCBI->getSuccessBB(), {SuccessArg});
295-
TI->eraseFromParent();
296-
splitIfCriticalEdge(CCBBlock, CCBI->getSuccessBB());
294+
Builder.createBranch(clonedCCBI->getLoc(), clonedCCBI->getSuccessBB(),
295+
{SuccessArg});
296+
clonedCCBI->eraseFromParent();
297297

298298
// Redirect all SuccessPreds to the copy of BB.
299299
for (auto *Pred : SuccessPreds) {
@@ -312,6 +312,7 @@ void CheckedCastBrJumpThreading::Edit::modifyCFGForSuccessPreds(
312312

313313
// Add an unconditional jump at the end of the block.
314314
// Take argument value from the dominating BB
315+
auto *CCBI = cast<CheckedCastBranchInst>(CCBBlock->getTerminator());
315316
SILBuilderWithScope(CCBI).createBranch(CCBI->getLoc(), CCBI->getSuccessBB(),
316317
{SuccessArg});
317318
CCBI->eraseFromParent();

lib/Sema/MiscDiagnostics.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4612,7 +4612,6 @@ void swift::performSyntacticExprDiagnostics(const Expr *E,
46124612
if (ctx.LangOpts.EnableObjCInterop)
46134613
diagDeprecatedObjCSelectors(DC, E);
46144614
diagnoseConstantArgumentRequirement(E, DC);
4615-
checkActorIsolation(E, DC);
46164615
}
46174616

46184617
void swift::performStmtDiagnostics(const Stmt *S, DeclContext *DC) {

0 commit comments

Comments
 (0)