Skip to content

Commit 60d0187

Browse files
authored
Merge pull request #2956 from swiftwasm/main
[pull] swiftwasm from main
2 parents 1c5516c + 6c11713 commit 60d0187

File tree

166 files changed

+2347
-1019
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+2347
-1019
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ Types
516516
type ::= 'BB' // Builtin.UnsafeValueBuffer
517517
type ::= 'Bc' // Builtin.RawUnsafeContinuation
518518
type ::= 'BD' // Builtin.DefaultActorStorage
519-
type ::= 'Be' // Builtin.ExecutorRef
519+
type ::= 'Be' // Builtin.Executor
520520
type ::= 'Bf' NATURAL '_' // Builtin.Float<n>
521521
type ::= 'Bi' NATURAL '_' // Builtin.Int<n>
522522
type ::= 'BI' // Builtin.IntLiteral

include/swift/ABI/Executor.h

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define SWIFT_ABI_EXECUTOR_H
1919

2020
#include <inttypes.h>
21+
#include "swift/ABI/Actor.h"
2122
#include "swift/ABI/HeapObject.h"
2223
#include "swift/Runtime/Casting.h"
2324

@@ -31,79 +32,89 @@ class Job;
3132
SWIFT_EXPORT_FROM(swift_Concurrency)
3233
Metadata* MainActorMetadata;
3334

34-
/// An ExecutorRef isn't necessarily just a pointer to an executor
35-
/// object; it may have other bits set.
35+
/// An unmanaged reference to an executor.
36+
///
37+
/// The representation is two words: identity and implementation.
38+
/// The identity word is a reference to the executor object; for
39+
/// default actors, this is the actor object. The implementation
40+
/// word describes how the executor works; it carries a witness table
41+
/// as well as a small number of bits indicating various special
42+
/// implementation properties. As an exception to both of these
43+
/// rules, a null identity represents a generic executor and
44+
/// implies a null implementation word.
3645
class ExecutorRef {
37-
static constexpr uintptr_t IsDefaultActor = 1;
38-
static constexpr uintptr_t PointerMask = 7;
39-
40-
uintptr_t Value;
46+
HeapObject *Identity; // Not necessarily Swift reference-countable
47+
uintptr_t Implementation;
4148

42-
constexpr ExecutorRef(uintptr_t value) : Value(value) {}
49+
constexpr ExecutorRef(HeapObject *identity, uintptr_t implementation)
50+
: Identity(identity), Implementation(implementation) {}
4351

4452
public:
4553
/// A generic execution environment. When running in a generic
4654
/// environment, it's presumed to be okay to switch synchronously
4755
/// to an actor. As an executor request, this represents a request
4856
/// to drop whatever the current actor is.
4957
constexpr static ExecutorRef generic() {
50-
return ExecutorRef(0);
58+
return ExecutorRef(nullptr, 0);
5159
}
5260

5361
/// FIXME: only exists for the quick-and-dirty MainActor implementation.
5462
/// NOTE: I didn't go with Executor::forMainActor(DefaultActor*) because
5563
/// __swift_run_job_main_executor can't take more than one argument.
56-
constexpr static ExecutorRef mainExecutor() {
57-
return ExecutorRef(2);
64+
static ExecutorRef mainExecutor() {
65+
auto identity = getMainActorIdentity();
66+
return ExecutorRef(identity, 0);
67+
}
68+
static HeapObject *getMainActorIdentity() {
69+
return reinterpret_cast<HeapObject*>(
70+
ExecutorRefFlags::MainActorIdentity);
5871
}
5972

6073
/// Given a pointer to a default actor, return an executor reference
6174
/// for it.
6275
static ExecutorRef forDefaultActor(DefaultActor *actor) {
6376
assert(actor);
64-
return ExecutorRef(reinterpret_cast<uintptr_t>(actor) | IsDefaultActor);
77+
return ExecutorRef(actor, unsigned(ExecutorRefFlags::DefaultActor));
78+
}
79+
80+
HeapObject *getIdentity() const {
81+
return Identity;
6582
}
6683

6784
/// Is this the generic executor reference?
6885
bool isGeneric() const {
69-
return Value == 0;
86+
return Identity == 0;
7087
}
7188

7289
/// FIXME: only exists for the quick-and-dirty MainActor implementation.
7390
bool isMainExecutor() const {
74-
if (Value == ExecutorRef::mainExecutor().Value)
91+
if (Identity == getMainActorIdentity())
7592
return true;
7693

77-
HeapObject *heapObj = reinterpret_cast<HeapObject*>(Value & ~PointerMask);
78-
79-
if (heapObj == nullptr || MainActorMetadata == nullptr)
94+
if (Identity == nullptr || MainActorMetadata == nullptr)
8095
return false;
8196

82-
Metadata const* metadata = swift_getObjectType(heapObj);
97+
Metadata const* metadata = swift_getObjectType(Identity);
8398
return metadata == MainActorMetadata;
8499
}
85100

86101
/// Is this a default-actor executor reference?
87102
bool isDefaultActor() const {
88-
return Value & IsDefaultActor;
103+
return Implementation & unsigned(ExecutorRefFlags::DefaultActor);
89104
}
90105
DefaultActor *getDefaultActor() const {
91106
assert(isDefaultActor());
92-
return reinterpret_cast<DefaultActor*>(Value & ~PointerMask);
93-
}
94-
95-
uintptr_t getRawValue() const {
96-
return Value;
107+
return reinterpret_cast<DefaultActor*>(Identity);
97108
}
98109

99110
/// Do we have to do any work to start running as the requested
100111
/// executor?
101112
bool mustSwitchToRun(ExecutorRef newExecutor) const {
102-
return *this != newExecutor;
113+
return Identity != newExecutor.Identity;
103114
}
104115

105116
bool operator==(ExecutorRef other) const {
106-
return Value == other.Value
117+
return Identity == other.Identity
107118
/// FIXME: only exists for the quick-and-dirty MainActor implementation.
108119
|| (isMainExecutor() && other.isMainExecutor());
109120
}

include/swift/ABI/Metadata.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4288,6 +4288,10 @@ class TargetClassDescriptor final
42884288
return FieldOffsetVectorOffset;
42894289
}
42904290

4291+
bool isDefaultActor() const {
4292+
return this->getTypeContextDescriptorFlags().class_isDefaultActor();
4293+
}
4294+
42914295
bool hasVTable() const {
42924296
return this->getTypeContextDescriptorFlags().class_hasVTable();
42934297
}

include/swift/ABI/MetadataValues.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,14 @@ class TypeContextDescriptorFlags : public FlagSet<uint16_t> {
13851385

13861386
// Type-specific flags:
13871387

1388+
/// Set if the class is a default actor class. Note that this is
1389+
/// based on the best knowledge available to the class; actor
1390+
/// classes with resilient superclassess might be default actors
1391+
/// without knowing it.
1392+
///
1393+
/// Only meaningful for class descriptors.
1394+
Class_IsDefaultActor = 8,
1395+
13881396
/// The kind of reference that this class makes to its resilient superclass
13891397
/// descriptor. A TypeReferenceKind.
13901398
///
@@ -1464,6 +1472,9 @@ class TypeContextDescriptorFlags : public FlagSet<uint16_t> {
14641472
FLAGSET_DEFINE_FLAG_ACCESSORS(Class_AreImmediateMembersNegative,
14651473
class_areImmediateMembersNegative,
14661474
class_setAreImmediateMembersNegative)
1475+
FLAGSET_DEFINE_FLAG_ACCESSORS(Class_IsDefaultActor,
1476+
class_isDefaultActor,
1477+
class_setIsDefaultActor)
14671478

14681479
FLAGSET_DEFINE_FIELD_ACCESSORS(Class_ResilientSuperclassReferenceKind,
14691480
Class_ResilientSuperclassReferenceKind_width,
@@ -2166,6 +2177,20 @@ enum class ContinuationStatus : size_t {
21662177
Resumed = 2
21672178
};
21682179

2180+
/// Flags describing the executor implementation that are stored
2181+
/// in the ExecutorRef.
2182+
enum class ExecutorRefFlags : size_t {
2183+
// The number of bits available here is very limited because it's
2184+
// potentially just the alignment bits of a protocol witness table
2185+
// pointer
2186+
2187+
/// The executor is a default actor.
2188+
DefaultActor = 0x1,
2189+
2190+
/// TODO: remove this
2191+
MainActorIdentity = 0x2,
2192+
};
2193+
21692194
} // end namespace swift
21702195

21712196
#endif // SWIFT_ABI_METADATAVALUES_H

include/swift/AST/Builtins.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,12 +758,15 @@ BUILTIN_MISC_OPERATION(BuildSerialExecutorRef,
758758
// Retrieve the ExecutorRef on which the current asynchronous
759759
// function is executing.
760760
// Does not retain an actor executor.
761-
BUILTIN_MISC_OPERATION_WITH_SILGEN(GetCurrentExecutor, "getCurrentExecutor", "n", Special)
761+
BUILTIN_MISC_OPERATION_WITH_SILGEN(GetCurrentExecutor, "getCurrentExecutor", "", Special)
762762

763763
// getCurrentAsyncTask: () -> Builtin.NativeObject
764764
//
765765
// Retrieve the pointer to the task in which the current asynchronous
766766
// function is executing.
767+
//
768+
// This is readnone because, within the world modeled by SIL, the
769+
// current async task of a thread never changes.
767770
BUILTIN_MISC_OPERATION_WITH_SILGEN(GetCurrentAsyncTask, "getCurrentAsyncTask", "n", Special)
768771

769772
/// cancelAsyncTask(): (Builtin.NativeObject) -> Void

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3766,14 +3766,19 @@ class ClassDecl final : public NominalTypeDecl {
37663766

37673767
/// Whether the class is (known to be) a default actor.
37683768
bool isDefaultActor() const;
3769+
bool isDefaultActor(ModuleDecl *M, ResilienceExpansion expansion) const;
37693770

37703771
/// Whether the class is known to be a *root* default actor,
37713772
/// i.e. the first class in its hierarchy that is a default actor.
37723773
bool isRootDefaultActor() const;
3774+
bool isRootDefaultActor(ModuleDecl *M, ResilienceExpansion expansion) const;
37733775

37743776
/// Whether the class was explicitly declared with the `actor` keyword.
37753777
bool isExplicitActor() const { return Bits.ClassDecl.IsActor; }
37763778

3779+
/// Get the closest-to-root superclass that's an actor class.
3780+
const ClassDecl *getRootActorClass() const;
3781+
37773782
/// Does this class explicitly declare any of the methods that
37783783
/// would prevent it from being a default actor?
37793784
bool hasExplicitCustomActorMethods() const;

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5291,6 +5291,10 @@ ERROR(availabilty_string_subscript_migration, none,
52915291
"subscripts returning String were obsoleted in Swift 4; explicitly "
52925292
"construct a String from subscripted result", ())
52935293

5294+
NOTE(availability_unavailable_implicit_init, none,
5295+
"call to unavailable %0 %1 from superclass %2 occurs implicitly at the "
5296+
"end of this initializer", (DescriptiveDeclKind, DeclName, DeclName))
5297+
52945298
// Conformance availability checking diagnostics
52955299

52965300
ERROR(conformance_availability_unavailable, none,

include/swift/AST/TypeCheckRequests.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,15 +938,16 @@ class IsActorRequest :
938938
/// Determine whether the given class is a default actor.
939939
class IsDefaultActorRequest :
940940
public SimpleRequest<IsDefaultActorRequest,
941-
bool(ClassDecl *),
941+
bool(ClassDecl *, ModuleDecl *, ResilienceExpansion),
942942
RequestFlags::Cached> {
943943
public:
944944
using SimpleRequest::SimpleRequest;
945945

946946
private:
947947
friend SimpleRequest;
948948

949-
bool evaluate(Evaluator &evaluator, ClassDecl *classDecl) const;
949+
bool evaluate(Evaluator &evaluator, ClassDecl *classDecl,
950+
ModuleDecl *M, ResilienceExpansion expansion) const;
950951

951952
public:
952953
// Caching

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ SWIFT_REQUEST(TypeChecker, CanBeAsyncHandlerRequest, bool(FuncDecl *),
9999
Cached, NoLocationInfo)
100100
SWIFT_REQUEST(TypeChecker, IsActorRequest, bool(NominalTypeDecl *),
101101
Cached, NoLocationInfo)
102-
SWIFT_REQUEST(TypeChecker, IsDefaultActorRequest, bool(ClassDecl *),
102+
SWIFT_REQUEST(TypeChecker, IsDefaultActorRequest,
103+
bool(ClassDecl *, ModuleDecl *, ResilienceExpansion),
103104
Cached, NoLocationInfo)
104105
SWIFT_REQUEST(TypeChecker, GlobalActorInstanceRequest,
105106
VarDecl *(NominalTypeDecl *),

include/swift/Runtime/Concurrency.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,14 @@ void swift_defaultActor_initialize(DefaultActor *actor);
512512
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
513513
void swift_defaultActor_destroy(DefaultActor *actor);
514514

515+
/// Deallocate an instance of a default actor.
516+
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
517+
void swift_defaultActor_deallocate(DefaultActor *actor);
518+
519+
/// Deallocate an instance of what might be a default actor.
520+
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
521+
void swift_defaultActor_deallocateResilient(HeapObject *actor);
522+
515523
/// Enqueue a job on the default actor implementation.
516524
///
517525
/// The job must be ready to run. Notably, if it's a task, that

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ FUNCTION(TaskSwitchFunc,
15951595
swift_task_switch, SwiftAsyncCC,
15961596
ConcurrencyAvailability,
15971597
RETURNS(VoidTy),
1598-
ARGS(SwiftContextPtrTy, Int8PtrTy, SwiftExecutorPtrTy),
1598+
ARGS(SwiftContextPtrTy, Int8PtrTy, ExecutorFirstTy, ExecutorSecondTy),
15991599
ATTRS(NoUnwind))
16001600

16011601
// AsyncTask *swift_continuation_init(AsyncContext *continuationContext,
@@ -1636,7 +1636,7 @@ FUNCTION(ContinuationThrowingResumeWithError,
16361636
FUNCTION(TaskGetCurrentExecutor,
16371637
swift_task_getCurrentExecutor, SwiftCC,
16381638
ConcurrencyAvailability,
1639-
RETURNS(IntPtrTy),
1639+
RETURNS(SwiftExecutorTy),
16401640
ARGS(),
16411641
ATTRS(NoUnwind, ArgMemOnly))
16421642

@@ -1656,6 +1656,22 @@ FUNCTION(DefaultActorDestroy,
16561656
ARGS(RefCountedPtrTy),
16571657
ATTRS(NoUnwind))
16581658

1659+
// void swift_defaultActor_deallocate(DefaultActor *actor);
1660+
FUNCTION(DefaultActorDeallocate,
1661+
swift_defaultActor_deallocate, SwiftCC,
1662+
ConcurrencyAvailability,
1663+
RETURNS(VoidTy),
1664+
ARGS(RefCountedPtrTy),
1665+
ATTRS(NoUnwind))
1666+
1667+
// void swift_defaultActor_deallocateResilient(HeapObject *actor);
1668+
FUNCTION(DefaultActorDeallocateResilient,
1669+
swift_defaultActor_deallocateResilient, SwiftCC,
1670+
ConcurrencyAvailability,
1671+
RETURNS(VoidTy),
1672+
ARGS(RefCountedPtrTy),
1673+
ATTRS(NoUnwind))
1674+
16591675
//// void swift_taskGroup_initialize(AsyncTask *task, TaskGroup *group);
16601676
//FUNCTION(TaskGroupInitialize,
16611677
// swift_taskGroup_initialize, SwiftCC,

lib/AST/Decl.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4251,12 +4251,29 @@ GetDestructorRequest::evaluate(Evaluator &evaluator, ClassDecl *CD) const {
42514251
}
42524252

42534253
bool ClassDecl::isDefaultActor() const {
4254+
return isDefaultActor(getModuleContext(), ResilienceExpansion::Minimal);
4255+
}
4256+
4257+
bool ClassDecl::isDefaultActor(ModuleDecl *M,
4258+
ResilienceExpansion expansion) const {
42544259
auto mutableThis = const_cast<ClassDecl *>(this);
42554260
return evaluateOrDefault(getASTContext().evaluator,
4256-
IsDefaultActorRequest{mutableThis},
4261+
IsDefaultActorRequest{mutableThis, M,
4262+
expansion},
42574263
false);
42584264
}
42594265

4266+
const ClassDecl *ClassDecl::getRootActorClass() const {
4267+
if (!isActor()) return nullptr;
4268+
auto cur = this;
4269+
while (true) {
4270+
auto super = cur->getSuperclassDecl();
4271+
if (!super || !super->isActor())
4272+
return cur;
4273+
cur = super;
4274+
}
4275+
}
4276+
42604277
bool ClassDecl::hasMissingDesignatedInitializers() const {
42614278
return evaluateOrDefault(
42624279
getASTContext().evaluator,
@@ -8056,7 +8073,12 @@ bool ClassDecl::hasExplicitCustomActorMethods() const {
80568073
}
80578074

80588075
bool ClassDecl::isRootDefaultActor() const {
8059-
if (!isDefaultActor()) return false;
8076+
return isRootDefaultActor(getModuleContext(), ResilienceExpansion::Minimal);
8077+
}
8078+
8079+
bool ClassDecl::isRootDefaultActor(ModuleDecl *M,
8080+
ResilienceExpansion expansion) const {
8081+
if (!isDefaultActor(M, expansion)) return false;
80608082
auto superclass = getSuperclassDecl();
80618083
return (!superclass || superclass->isNSObject());
80628084
}

lib/AST/Type.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3150,7 +3150,7 @@ static bool canSubstituteTypeInto(Type ty, const DeclContext *dc,
31503150
case OpaqueSubstitutionKind::SubstituteNonResilientModule:
31513151
// Can't access types that are not public from a different module.
31523152
if (dc->getParentModule() == typeDecl->getDeclContext()->getParentModule())
3153-
return true;
3153+
return typeDecl->getEffectiveAccess() > AccessLevel::FilePrivate;
31543154

31553155
return typeDecl->getEffectiveAccess() > AccessLevel::Internal;
31563156
}

0 commit comments

Comments
 (0)