-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathSILGenConcurrency.cpp
718 lines (619 loc) · 27.6 KB
/
SILGenConcurrency.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//===--- SILGenConcurrency.cpp - Concurrency-specific SILGen --------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "ArgumentSource.h"
#include "ExecutorBreadcrumb.h"
#include "RValue.h"
#include "Scope.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Availability.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/Basic/Range.h"
using namespace swift;
using namespace Lowering;
void SILGenFunction::emitExpectedExecutor() {
// Whether the given declaration context is nested within an actor's
// destructor.
auto isInActorDestructor = [](DeclContext *dc) {
while (!dc->isModuleScopeContext() && !dc->isTypeContext()) {
if (auto destructor = dyn_cast<DestructorDecl>(dc)) {
switch (getActorIsolation(destructor)) {
case ActorIsolation::ActorInstance:
return true;
case ActorIsolation::GlobalActor:
// Global-actor-isolated types should likely have deinits that
// are not themselves actor-isolated, yet still have access to
// the instance properties of the class.
return false;
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
case ActorIsolation::Unspecified:
return false;
case ActorIsolation::Erased:
llvm_unreachable("deinit cannot have erased isolation");
}
}
dc = dc->getParent();
}
return false;
};
// Initialize ExpectedExecutor if:
// - this function is async or
// - this function is sync and isolated to an actor, and we want to
// dynamically check that we're on the right executor.
//
// Actor destructors are isolated in the sense that we now have a
// unique reference to the actor, but we probably aren't running on
// the actor's executor, so we cannot safely do this check.
//
// Defer bodies are always called synchronously within their enclosing
// function, so the check is unnecessary; in addition, we cannot
// necessarily perform the check because the defer may not have
// captured the isolated parameter of the enclosing function.
bool wantDataRaceChecks = getOptions().EnableActorDataRaceChecks &&
!F.isAsync() &&
!isInActorDestructor(FunctionDC) &&
!F.isDefer();
// FIXME: Avoid loading and checking the expected executor if concurrency is
// unavailable. This is specifically relevant for MainActor isolated contexts,
// which are allowed to be available on OSes where concurrency is not
// available. rdar://106827064
// Local function to load the expected executor from a local actor
auto loadExpectedExecutorForLocalVar = [&](VarDecl *var) {
auto loc = RegularLocation::getAutoGeneratedLocation(F.getLocation());
Type actorType = var->getTypeInContext();
RValue actorInstanceRV = emitRValueForDecl(
loc, var, actorType, AccessSemantics::Ordinary);
ManagedValue actorInstance =
std::move(actorInstanceRV).getScalarValue();
ExpectedExecutor = emitLoadActorExecutor(loc, actorInstance);
};
if (auto *funcDecl =
dyn_cast_or_null<AbstractFunctionDecl>(FunctionDC->getAsDecl())) {
auto actorIsolation = getActorIsolation(funcDecl);
switch (actorIsolation.getKind()) {
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
break;
case ActorIsolation::Erased:
llvm_unreachable("method cannot have erased isolation");
case ActorIsolation::ActorInstance: {
// Only produce an executor for actor-isolated functions that are async
// or are local functions. The former require a hop, while the latter
// are prone to dynamic data races in code that does not enforce Sendable
// completely.
if (F.isAsync() ||
(wantDataRaceChecks && funcDecl->isLocalCapture())) {
if (auto isolatedParam = funcDecl->getCaptureInfo()
.getIsolatedParamCapture()) {
loadExpectedExecutorForLocalVar(isolatedParam);
} else {
auto loc = RegularLocation::getAutoGeneratedLocation(F.getLocation());
ManagedValue actorArg;
if (actorIsolation.getActorInstanceParameter() == 0) {
ManagedValue selfArg;
if (F.getSelfArgument()->getOwnershipKind() ==
OwnershipKind::Guaranteed) {
selfArg = ManagedValue::forBorrowedRValue(F.getSelfArgument());
} else {
selfArg =
ManagedValue::forUnmanagedOwnedValue(F.getSelfArgument());
}
ExpectedExecutor = emitLoadActorExecutor(loc, selfArg);
} else {
unsigned isolatedParamIdx =
actorIsolation.getActorInstanceParameter() - 1;
auto param = funcDecl->getParameters()->get(isolatedParamIdx);
assert(param->isIsolated());
loadExpectedExecutorForLocalVar(param);
}
}
}
break;
}
case ActorIsolation::GlobalActor:
if (F.isAsync() || wantDataRaceChecks) {
ExpectedExecutor =
emitLoadGlobalActorExecutor(actorIsolation.getGlobalActor());
}
break;
}
} else if (auto *closureExpr = dyn_cast<AbstractClosureExpr>(FunctionDC)) {
bool wantExecutor = F.isAsync() || wantDataRaceChecks;
auto actorIsolation = closureExpr->getActorIsolation();
switch (actorIsolation.getKind()) {
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
break;
case ActorIsolation::Erased:
llvm_unreachable("closure cannot have erased isolation");
case ActorIsolation::ActorInstance: {
if (wantExecutor) {
loadExpectedExecutorForLocalVar(actorIsolation.getActorInstance());
}
break;
}
case ActorIsolation::GlobalActor:
if (wantExecutor) {
ExpectedExecutor =
emitLoadGlobalActorExecutor(actorIsolation.getGlobalActor());
break;
}
}
}
// In async functions, the generic executor is our expected executor
// if we don't have any sort of isolation.
if (!ExpectedExecutor && F.isAsync() && !unsafelyInheritsExecutor()) {
ExpectedExecutor = emitGenericExecutor(
RegularLocation::getAutoGeneratedLocation(F.getLocation()));
}
// Jump to the expected executor.
if (ExpectedExecutor) {
if (F.isAsync()) {
// For an async function, hop to the executor.
B.createHopToExecutor(
RegularLocation::getDebugOnlyLocation(F.getLocation(), getModule()),
ExpectedExecutor,
/*mandatory*/ false);
} else {
// For a synchronous function, check that we're on the same executor.
// Note: if we "know" that the code is completely Sendable-safe, this
// is unnecessary. The type checker will need to make this determination.
emitPreconditionCheckExpectedExecutor(
RegularLocation::getAutoGeneratedLocation(F.getLocation()),
ExpectedExecutor);
}
}
}
void SILGenFunction::emitConstructorPrologActorHop(
SILLocation loc, std::optional<ActorIsolation> maybeIso) {
loc = loc.asAutoGenerated();
if (maybeIso) {
if (auto executor = emitExecutor(loc, *maybeIso, std::nullopt)) {
ExpectedExecutor = *executor;
}
}
if (!ExpectedExecutor)
ExpectedExecutor = emitGenericExecutor(loc);
B.createHopToExecutor(loc, ExpectedExecutor, /*mandatory*/ false);
}
void SILGenFunction::emitPrologGlobalActorHop(SILLocation loc,
Type globalActor) {
ExpectedExecutor = emitLoadGlobalActorExecutor(globalActor);
B.createHopToExecutor(RegularLocation::getDebugOnlyLocation(loc, getModule()),
ExpectedExecutor, /*mandatory*/ false);
}
SILValue SILGenFunction::emitMainExecutor(SILLocation loc) {
auto &ctx = getASTContext();
auto builtinName = ctx.getIdentifier(
getBuiltinName(BuiltinValueKind::BuildMainActorExecutorRef));
auto resultType = SILType::getPrimitiveObjectType(ctx.TheExecutorType);
return B.createBuiltin(loc, builtinName, resultType, {}, {});
}
SILValue SILGenFunction::emitGenericExecutor(SILLocation loc) {
// The generic executor is encoded as the nil value of
// std::optional<Builtin.SerialExecutor>.
auto ty = SILType::getOptionalType(
SILType::getPrimitiveObjectType(
getASTContext().TheExecutorType));
return B.createOptionalNone(loc, ty);
}
ManagedValue SILGenFunction::emitNonIsolatedIsolation(SILLocation loc) {
return B.createManagedOptionalNone(loc,
SILType::getOpaqueIsolationType(getASTContext()));
}
SILValue SILGenFunction::emitLoadGlobalActorExecutor(Type globalActor) {
auto loc = RegularLocation::getAutoGeneratedLocation(F.getLocation());
auto actorAndFormalType =
emitLoadOfGlobalActorShared(loc, globalActor->getCanonicalType());
return emitLoadActorExecutor(loc, actorAndFormalType.first);
}
std::pair<ManagedValue, CanType>
SILGenFunction::emitLoadOfGlobalActorShared(SILLocation loc, CanType actorType) {
NominalTypeDecl *nominal = actorType->getNominalOrBoundGenericNominal();
VarDecl *sharedInstanceDecl = nominal->getGlobalActorInstance();
assert(sharedInstanceDecl && "no shared actor field in global actor");
SubstitutionMap subs =
actorType->getContextSubstitutionMap(SGM.SwiftModule, nominal);
Type instanceType =
actorType->getTypeOfMember(SGM.SwiftModule, sharedInstanceDecl);
auto metaRepr =
nominal->isResilient(SGM.SwiftModule, F.getResilienceExpansion())
? MetatypeRepresentation::Thick
: MetatypeRepresentation::Thin;
CanType actorMetaType = CanMetatypeType::get(actorType, metaRepr);
ManagedValue actorMetaTypeValue =
ManagedValue::forObjectRValueWithoutOwnership(B.createMetatype(
loc, SILType::getPrimitiveObjectType(actorMetaType)));
RValue actorInstanceRV = emitRValueForStorageLoad(loc, actorMetaTypeValue,
actorMetaType, /*isSuper*/ false, sharedInstanceDecl, PreparedArguments(),
subs, AccessSemantics::Ordinary, instanceType, SGFContext());
ManagedValue actorInstance = std::move(actorInstanceRV).getScalarValue();
return {actorInstance, instanceType->getCanonicalType()};
}
ManagedValue
SILGenFunction::emitGlobalActorIsolation(SILLocation loc,
CanType globalActorType) {
// Load the .shared property. Note that this isn't necessarily a value
// of the global actor type.
auto actorAndFormalType = emitLoadOfGlobalActorShared(loc, globalActorType);
// Since it's just a normal actor instance, we can use the normal path.
return emitActorInstanceIsolation(loc, actorAndFormalType.first,
actorAndFormalType.second);
}
/// Given a value of some non-optional distributed actor type, convert it
/// to the non-optional `any Actor` type.
static ManagedValue
emitDistributedActorIsolation(SILGenFunction &SGF, SILLocation loc,
ManagedValue actor, CanType actorType) {
// First, open the actor type if it's an existential type.
if (actorType->isExistentialType()) {
CanType openedType = OpenedArchetypeType::getAny(actorType,
SGF.F.getGenericSignature());
SILType loweredOpenedType = SGF.getLoweredType(openedType);
actor = SGF.emitOpenExistential(loc, actor, loweredOpenedType,
AccessKind::Read);
actorType = openedType;
}
auto &ctx = SGF.getASTContext();
auto distributedActorProto =
ctx.getProtocol(KnownProtocolKind::DistributedActor);
// Build <T: DistributedActor> and its substitutions for actorType.
// Doing this manually is ill-advised in general, but this is such a
// simple case that it's okay.
auto sig = distributedActorProto->getGenericSignature();
auto distributedActorConf =
SGF.SGM.SwiftModule->lookupConformance(actorType, distributedActorProto);
auto distributedActorSubs = SubstitutionMap::get(sig, {actorType},
{distributedActorConf});
// Use that to build the magical conformance to Actor for the distributed
// actor type.
return SGF.emitDistributedActorAsAnyActor(loc, distributedActorSubs, actor);
}
/// Given a value of some non-optional actor type, convert it to
/// non-optional `any Actor` type.
static ManagedValue
emitNonOptionalActorInstanceIsolation(SILGenFunction &SGF, SILLocation loc,
ManagedValue actor, CanType actorType,
SILType anyActorTy) {
// If we have an `any Actor` already, we're done.
if (actor.getType() == anyActorTy)
return actor;
CanType anyActorType = anyActorTy.getASTType();
// If the actor is a distributed actor, (1) it had better be local
// and (2) we need to use the special conformance.
if (actorType->isDistributedActor()) {
return emitDistributedActorIsolation(SGF, loc, actor, actorType);
}
return SGF.emitTransformExistential(loc, actor, actorType, anyActorType);
}
ManagedValue
SILGenFunction::emitActorInstanceIsolation(SILLocation loc, ManagedValue actor,
CanType actorType) {
// $Optional<any Actor>
auto optionalAnyActorTy = SILType::getOpaqueIsolationType(getASTContext());
// Optional<any Actor> as a formal type (it's invariant to lowering)
auto optionalAnyActorType = optionalAnyActorTy.getASTType();
// If we started with an Optional<any Actor>, we're done.
if (actorType == optionalAnyActorType) {
return actor;
}
// Otherwise, if we have an optional value, we need to transform the payload.
auto actorObjectType = actorType.getOptionalObjectType();
if (actorObjectType) {
return emitOptionalToOptional(loc, actor, optionalAnyActorTy,
[&](SILGenFunction &SGF, SILLocation loc, ManagedValue actorObject,
SILType anyActorTy, SGFContext C) {
return emitNonOptionalActorInstanceIsolation(*this, loc, actorObject,
actorObjectType, anyActorTy);
});
}
// Otherwise, transform the non-optional value we have, then inject that
// into Optional.
SILType anyActorTy = optionalAnyActorTy.getOptionalObjectType();
ManagedValue anyActor =
emitNonOptionalActorInstanceIsolation(*this, loc, actor, actorType,
anyActorTy);
// Inject into `Optional`.
auto result = B.createOptionalSome(loc, anyActor);
return result;
}
SILValue SILGenFunction::emitLoadActorExecutor(SILLocation loc,
ManagedValue actor) {
// FIXME: Checking for whether we're in a formal evaluation scope
// like this doesn't seem like a good pattern.
SILValue actorV;
if (isInFormalEvaluationScope())
actorV = actor.formalAccessBorrow(*this, loc).getValue();
else
actorV = actor.borrow(*this, loc).getValue();
// For now, we just want to emit a hop_to_executor directly to the
// actor; LowerHopToActor will add the emission logic necessary later.
return actorV;
}
SILValue SILGenFunction::emitLoadErasedExecutor(SILLocation loc,
ManagedValue fn) {
// As with emitLoadActorExecutor, we just emit the actor reference
// for now and let LowerHopToActor deal with the executor projection.
return emitLoadErasedIsolation(loc, fn).getUnmanagedValue();
}
ManagedValue
SILGenFunction::emitLoadErasedIsolation(SILLocation loc,
ManagedValue fn) {
fn = fn.borrow(*this, loc);
// This expects a borrowed function and returns a borrowed (any Actor)?.
auto actor = B.createFunctionExtractIsolation(loc, fn.getValue());
return ManagedValue::forBorrowedObjectRValue(actor);
}
ManagedValue
SILGenFunction::emitFunctionTypeIsolation(SILLocation loc,
FunctionTypeIsolation isolation,
ManagedValue fn) {
switch (isolation.getKind()) {
// Parameter-isolated functions don't have a specific actor they're isolated
// to; they're essentially polymorphic over isolation.
case FunctionTypeIsolation::Kind::Parameter:
llvm_unreachable("cannot load isolation from parameter-isoaltion function "
"reference");
// Emit nonisolated by simply emitting Optional.none in the result type.
case FunctionTypeIsolation::Kind::NonIsolated:
return emitNonIsolatedIsolation(loc);
// Emit global actor isolation by loading .shared from the global actor,
// erasing it into `any Actor`, and injecting that into Optional.
case FunctionTypeIsolation::Kind::GlobalActor:
return emitGlobalActorIsolation(loc,
isolation.getGlobalActorType()->getCanonicalType());
// Emit @isolated(any) isolation by loading the actor reference from the
// function.
case FunctionTypeIsolation::Kind::Erased: {
Scope scope(*this, CleanupLocation(loc));
auto value = emitLoadErasedIsolation(loc, fn).copy(*this, loc);
return scope.popPreservingValue(value);
}
}
llvm_unreachable("bad kind");
}
static ActorIsolation getClosureIsolationInfo(SILDeclRef constant) {
if (auto closure = constant.getAbstractClosureExpr()) {
return closure->getActorIsolation();
}
auto func = constant.getAbstractFunctionDecl();
assert(func && "unexpected closure constant");
return getActorIsolation(func);
}
static ManagedValue emitLoadOfCaptureIsolation(SILGenFunction &SGF,
SILLocation loc,
VarDecl *isolatedCapture,
SILDeclRef constant,
ArrayRef<ManagedValue> captureArgs) {
auto &TC = SGF.SGM.Types;
auto captureInfo = TC.getLoweredLocalCaptures(constant);
auto isolatedVarType =
isolatedCapture->getInterfaceType()->getCanonicalType();
// Capture arguments are 1-1 with the lowered capture info.
auto captures = captureInfo.getCaptures();
for (auto i : indices(captures)) {
const auto &capture = captures[i];
if (capture.isDynamicSelfMetadata()) continue;
auto capturedVar = capture.getDecl();
if (capturedVar != isolatedCapture) continue;
// Captured actor references should always be captured as constants.
assert(TC.getDeclCaptureKind(capture,
TC.getCaptureTypeExpansionContext(constant))
== CaptureKind::Constant);
auto value = captureArgs[i].copy(SGF, loc);
return SGF.emitActorInstanceIsolation(loc, value, isolatedVarType);
}
// The capture not being a lowered capture can happen in global code.
auto value = SGF.emitRValueForDecl(loc, isolatedCapture, isolatedVarType,
AccessSemantics::Ordinary)
.getAsSingleValue(SGF, loc);
return SGF.emitActorInstanceIsolation(loc, value, isolatedVarType);
}
ManagedValue
SILGenFunction::emitClosureIsolation(SILLocation loc, SILDeclRef constant,
ArrayRef<ManagedValue> captures) {
auto isolation = getClosureIsolationInfo(constant);
switch (isolation) {
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
return emitNonIsolatedIsolation(loc);
case ActorIsolation::Erased:
llvm_unreachable("closures cannot directly have erased isolation");
case ActorIsolation::GlobalActor:
return emitGlobalActorIsolation(loc,
isolation.getGlobalActor()->getCanonicalType());
case ActorIsolation::ActorInstance: {
// This should always be a capture. That's not expressed super-cleanly
// in ActorIsolation, unfortunately.
assert(isolation.getActorInstanceParameter() == 0);
auto capture = isolation.getActorInstance();
assert(capture);
return emitLoadOfCaptureIsolation(*this, loc, capture, constant, captures);
}
}
llvm_unreachable("bad kind");
}
ExecutorBreadcrumb
SILGenFunction::emitHopToTargetActor(SILLocation loc,
std::optional<ActorIsolation> maybeIso,
std::optional<ManagedValue> maybeSelf) {
if (!maybeIso)
return ExecutorBreadcrumb();
if (auto executor = emitExecutor(loc, *maybeIso, maybeSelf)) {
return emitHopToTargetExecutor(loc, *executor);
} else {
return ExecutorBreadcrumb();
}
}
ExecutorBreadcrumb SILGenFunction::emitHopToTargetExecutor(
SILLocation loc, SILValue executor) {
// Record that we need to hop back to the current executor.
auto breadcrumb = ExecutorBreadcrumb(true);
B.createHopToExecutor(RegularLocation::getDebugOnlyLocation(loc, getModule()),
executor, /*mandatory*/ false);
return breadcrumb;
}
std::optional<SILValue>
SILGenFunction::emitExecutor(SILLocation loc, ActorIsolation isolation,
std::optional<ManagedValue> maybeSelf) {
switch (isolation.getKind()) {
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
return std::nullopt;
case ActorIsolation::Erased:
llvm_unreachable("executor emission for erased isolation is unimplemented");
case ActorIsolation::ActorInstance: {
// "self" here means the actor instance's "self" value.
assert(maybeSelf.has_value() && "actor-instance but no self provided?");
auto self = maybeSelf.value();
return emitLoadActorExecutor(loc, self);
}
case ActorIsolation::GlobalActor:
return emitLoadGlobalActorExecutor(isolation.getGlobalActor());
}
llvm_unreachable("covered switch");
}
void SILGenFunction::emitHopToActorValue(SILLocation loc, ManagedValue actor) {
// TODO: can the type system enforce this async requirement?
if (!F.isAsync()) {
llvm::report_fatal_error("Builtin.hopToActor must be in an async function");
}
auto isolation =
getActorIsolationOfContext(FunctionDC, [](AbstractClosureExpr *CE) {
return CE->getActorIsolation();
});
if (isolation != ActorIsolation::Nonisolated &&
isolation != ActorIsolation::NonisolatedUnsafe &&
isolation != ActorIsolation::Unspecified) {
// TODO: Explicit hop with no hop-back should only be allowed in nonisolated
// async functions. But it needs work for any closure passed to
// Task.detached, which currently has unspecified isolation.
llvm::report_fatal_error(
"Builtin.hopToActor must be in an actor-independent function");
}
SILValue executor = emitLoadActorExecutor(loc, actor);
B.createHopToExecutor(RegularLocation::getDebugOnlyLocation(loc, getModule()),
executor, /*mandatory*/ true);
}
void SILGenFunction::emitPreconditionCheckExpectedExecutor(
SILLocation loc, SILValue executorOrActor) {
auto checkExecutor = SGM.getCheckExpectedExecutor();
if (!checkExecutor)
return;
// Forego a check if instrinsic is unavailable, this could happen
// in main-actor context.
auto &C = checkExecutor->getASTContext();
if (!C.LangOpts.DisableAvailabilityChecking) {
auto deploymentAvailability = AvailabilityContext::forDeploymentTarget(C);
auto declAvailability =
AvailabilityInference::availableRange(checkExecutor, C);
if (!deploymentAvailability.isContainedIn(declAvailability))
return;
}
// We don't want the debugger to step into these.
loc.markAutoGenerated();
// Get the executor.
SILValue executor = B.createExtractExecutor(loc, executorOrActor);
// Call the library function that performs the checking.
auto args = emitSourceLocationArgs(loc.getSourceLoc(), loc);
emitApplyOfLibraryIntrinsic(
loc, checkExecutor, SubstitutionMap(),
{args.filenameStartPointer, args.filenameLength, args.filenameIsAscii,
args.line, ManagedValue::forObjectRValueWithoutOwnership(executor)},
SGFContext());
}
bool SILGenFunction::unsafelyInheritsExecutor() {
if (auto fn = dyn_cast<AbstractFunctionDecl>(FunctionDC))
return fn->getAttrs().hasAttribute<UnsafeInheritExecutorAttr>();
return false;
}
void ExecutorBreadcrumb::emit(SILGenFunction &SGF, SILLocation loc) {
if (mustReturnToExecutor) {
assert(SGF.ExpectedExecutor || SGF.unsafelyInheritsExecutor());
if (auto executor = SGF.ExpectedExecutor)
SGF.B.createHopToExecutor(
RegularLocation::getDebugOnlyLocation(loc, SGF.getModule()), executor,
/*mandatory*/ false);
}
}
SILValue SILGenFunction::emitGetCurrentExecutor(SILLocation loc) {
assert(ExpectedExecutor && "prolog failed to set up expected executor?");
return ExpectedExecutor;
}
/// Find the extension on DistributedActor that defines __actorUnownedExecutor.
static ExtensionDecl *findDistributedActorAsActorExtension(
ProtocolDecl *distributedActorProto, ModuleDecl *module) {
ASTContext &ctx = distributedActorProto->getASTContext();
auto name = ctx.getIdentifier("__actorUnownedExecutor");
auto results = distributedActorProto->lookupDirect(
name, SourceLoc(),
NominalTypeDecl::LookupDirectFlags::IncludeAttrImplements);
for (auto result : results) {
if (auto var = dyn_cast<VarDecl>(result)) {
return dyn_cast<ExtensionDecl>(var->getDeclContext());
}
}
return nullptr;
}
ProtocolConformanceRef
SILGenModule::getDistributedActorAsActorConformance(SubstitutionMap subs) {
ASTContext &ctx = M.getASTContext();
auto actorProto = ctx.getProtocol(KnownProtocolKind::Actor);
Type distributedActorType = subs.getReplacementTypes()[0];
if (!distributedActorAsActorConformance) {
auto distributedActorProto = ctx.getProtocol(KnownProtocolKind::DistributedActor);
if (!distributedActorProto)
return ProtocolConformanceRef();
auto ext = findDistributedActorAsActorExtension(
distributedActorProto, M.getSwiftModule());
if (!ext)
return ProtocolConformanceRef();
// Conformance of DistributedActor to Actor.
auto genericParam = subs.getGenericSignature().getGenericParams()[0];
distributedActorAsActorConformance = ctx.getNormalConformance(
Type(genericParam), actorProto, SourceLoc(), ext,
ProtocolConformanceState::Incomplete, /*isUnchecked=*/false,
/*isPreconcurrency=*/false);
}
return ProtocolConformanceRef(
actorProto,
ctx.getSpecializedConformance(distributedActorType,
distributedActorAsActorConformance,
subs));
}
ManagedValue
SILGenFunction::emitDistributedActorAsAnyActor(SILLocation loc,
SubstitutionMap distributedActorSubs,
ManagedValue actorValue) {
ProtocolConformanceRef conformances[1] = {
SGM.getDistributedActorAsActorConformance(distributedActorSubs)
};
// Erase the distributed actor instance into an `any Actor` existential with
// the special conformance.
auto &ctx = SGM.getASTContext();
CanType distributedActorType =
distributedActorSubs.getReplacementTypes()[0]->getCanonicalType();
auto &distributedActorTL = getTypeLowering(distributedActorType);
auto actorProto = ctx.getProtocol(KnownProtocolKind::Actor);
auto &anyActorTL = getTypeLowering(actorProto->getDeclaredExistentialType());
return emitExistentialErasure(loc, distributedActorType,
distributedActorTL, anyActorTL,
ctx.AllocateCopy(conformances),
SGFContext(),
[actorValue](SGFContext) {
return actorValue;
});
}