Skip to content

Commit ad75aa3

Browse files
Merge pull request #2222 from swiftwasm/maxd/main-merge
Resolve conflicts with the `main` branch
2 parents 4792d68 + 9a6ed2f commit ad75aa3

File tree

176 files changed

+5733
-2796
lines changed

Some content is hidden

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

176 files changed

+5733
-2796
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
| Version | Released | Toolchain |
88
| :--------------------- | :--------- | :---------- |
9+
| [Swift 5.4](#swift-54) | | |
910
| [Swift 5.3](#swift-53) | 2020-09-16 | Xcode 12.0 |
1011
| [Swift 5.2](#swift-52) | 2020-03-24 | Xcode 11.4 |
1112
| [Swift 5.1](#swift-51) | 2019-09-20 | Xcode 11.0 |
@@ -24,8 +25,8 @@ CHANGELOG
2425

2526
</details>
2627

27-
Swift Next
28-
----------
28+
Swift 5.4
29+
---------
2930

3031
* [SR-10069][]:
3132

docs/ABI/Mangling.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ types where the metadata itself has unknown layout.)
220220
global ::= entity // some identifiable thing
221221
global ::= from-type to-type generic-signature? 'TR' // reabstraction thunk
222222
global ::= from-type to-type generic-signature? 'TR' // reabstraction thunk
223-
global ::= impl-function-type 'Tz' // objc-to-swift-async completion handler block implementation
223+
global ::= impl-function-type type 'Tz' // objc-to-swift-async completion handler block implementation
224+
global ::= impl-function-type type 'TZ' // objc-to-swift-async completion handler block implementation (predefined by runtime)
224225
global ::= from-type to-type self-type generic-signature? 'Ty' // reabstraction thunk with dynamic 'Self' capture
225226
global ::= from-type to-type generic-signature? 'Tr' // obsolete mangling for reabstraction thunk
226227
global ::= entity generic-signature? type type* 'TK' // key path getter

include/swift/ABI/MetadataKind.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ METADATAKIND(HeapGenericLocalVariable,
8585
METADATAKIND(ErrorObject,
8686
1 | MetadataKindIsNonType | MetadataKindIsRuntimePrivate)
8787

88-
/// A heap-allocated simple task.
89-
METADATAKIND(SimpleTask,
88+
/// A heap-allocated task.
89+
METADATAKIND(Task,
9090
2 | MetadataKindIsNonType | MetadataKindIsRuntimePrivate)
9191

9292
// getEnumeratedMetadataKind assumes that all the enumerated values here

include/swift/ABI/Task.h

Lines changed: 150 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "swift/Basic/RelativePointer.h"
2121
#include "swift/ABI/HeapObject.h"
22+
#include "swift/ABI/Metadata.h"
2223
#include "swift/ABI/MetadataValues.h"
2324
#include "swift/Runtime/Config.h"
2425
#include "swift/Basic/STLExtras.h"
@@ -29,6 +30,8 @@ class AsyncTask;
2930
class AsyncContext;
3031
class Executor;
3132
class Job;
33+
struct OpaqueValue;
34+
struct SwiftError;
3235
class TaskStatusRecord;
3336

3437
/// An ExecutorRef isn't necessarily just a pointer to an executor
@@ -86,6 +89,13 @@ class AsyncFunctionPointer {
8689

8790
/// A schedulable job.
8891
class alignas(2 * alignof(void*)) Job {
92+
protected:
93+
// Indices into SchedulerPrivate, for use by the runtime.
94+
enum {
95+
/// The next waiting task link, an AsyncTask that is waiting on a future.
96+
NextWaitingTaskIndex = 0,
97+
};
98+
8999
public:
90100
// Reserved for the use of the scheduler.
91101
void *SchedulerPrivate[2];
@@ -230,19 +240,142 @@ class AsyncTask : public HeapObject, public Job {
230240
}
231241
};
232242

233-
bool isFuture() const { return Flags.task_isFuture(); }
234-
235243
bool hasChildFragment() const { return Flags.task_isChildTask(); }
236244
ChildFragment *childFragment() {
237245
assert(hasChildFragment());
238246
return reinterpret_cast<ChildFragment*>(this + 1);
239247
}
240248

241-
// TODO: Future fragment
249+
class FutureFragment {
250+
public:
251+
/// Describes the status of the future.
252+
///
253+
/// Futures always begin in the "Executing" state, and will always
254+
/// make a single state change to either Success or Error.
255+
enum class Status : uintptr_t {
256+
/// The future is executing or ready to execute. The storage
257+
/// is not accessible.
258+
Executing = 0,
259+
260+
/// The future has completed with result (of type \c resultType).
261+
Success,
262+
263+
/// The future has completed by throwing an error (an \c Error
264+
/// existential).
265+
Error,
266+
};
267+
268+
/// An item within the wait queue, which includes the status and the
269+
/// head of the list of tasks.
270+
struct WaitQueueItem {
271+
/// Mask used for the low status bits in a wait queue item.
272+
static const uintptr_t statusMask = 0x03;
273+
274+
uintptr_t storage;
275+
276+
Status getStatus() const {
277+
return static_cast<Status>(storage & statusMask);
278+
}
279+
280+
AsyncTask *getTask() const {
281+
return reinterpret_cast<AsyncTask *>(storage & ~statusMask);
282+
}
283+
284+
static WaitQueueItem get(Status status, AsyncTask *task) {
285+
return WaitQueueItem{
286+
reinterpret_cast<uintptr_t>(task) | static_cast<uintptr_t>(status)};
287+
}
288+
};
289+
290+
private:
291+
/// Queue containing all of the tasks that are waiting in `get()`.
292+
///
293+
/// The low bits contain the status, the rest of the pointer is the
294+
/// AsyncTask.
295+
std::atomic<WaitQueueItem> waitQueue;
296+
297+
/// The type of the result that will be produced by the future.
298+
const Metadata *resultType;
299+
300+
// Trailing storage for the result itself. The storage will be uninitialized,
301+
// contain an instance of \c resultType, or contaon an an \c Error.
302+
303+
friend class AsyncTask;
304+
305+
public:
306+
explicit FutureFragment(const Metadata *resultType)
307+
: waitQueue(WaitQueueItem::get(Status::Executing, nullptr)),
308+
resultType(resultType) { }
309+
310+
/// Destroy the storage associated with the future.
311+
void destroy();
312+
313+
/// Retrieve a pointer to the storage of result.
314+
OpaqueValue *getStoragePtr() {
315+
return reinterpret_cast<OpaqueValue *>(
316+
reinterpret_cast<char *>(this) + storageOffset(resultType));
317+
}
318+
319+
/// Retrieve the error.
320+
SwiftError *&getError() {
321+
return *reinterpret_cast<SwiftError **>(
322+
reinterpret_cast<char *>(this) + storageOffset(resultType));
323+
}
324+
325+
/// Compute the offset of the storage from the base of the future
326+
/// fragment.
327+
static size_t storageOffset(const Metadata *resultType) {
328+
size_t offset = sizeof(FutureFragment);
329+
size_t alignment =
330+
std::max(resultType->vw_alignment(), alignof(SwiftError *));
331+
return (offset + alignment - 1) & ~(alignment - 1);
332+
}
333+
334+
/// Determine the size of the future fragment given a particular future
335+
/// result type.
336+
static size_t fragmentSize(const Metadata *resultType) {
337+
return storageOffset(resultType) +
338+
std::max(resultType->vw_size(), sizeof(SwiftError *));
339+
}
340+
};
341+
342+
bool isFuture() const { return Flags.task_isFuture(); }
343+
344+
FutureFragment *futureFragment() {
345+
assert(isFuture());
346+
if (hasChildFragment()) {
347+
return reinterpret_cast<FutureFragment *>(
348+
reinterpret_cast<ChildFragment*>(this + 1) + 1);
349+
}
350+
351+
return reinterpret_cast<FutureFragment *>(this + 1);
352+
}
353+
354+
/// Wait for this future to complete.
355+
///
356+
/// \returns the status of the future. If this result is
357+
/// \c Executing, then \c waitingTask has been added to the
358+
/// wait queue and will be scheduled when the future completes. Otherwise,
359+
/// the future has completed and can be queried.
360+
FutureFragment::Status waitFuture(AsyncTask *waitingTask);
361+
362+
/// Complete this future.
363+
///
364+
/// Upon completion, any waiting tasks will be scheduled on the given
365+
/// executor.
366+
void completeFuture(AsyncContext *context, ExecutorRef executor);
242367

243368
static bool classof(const Job *job) {
244369
return job->isAsyncTask();
245370
}
371+
372+
private:
373+
/// Access the next waiting task, which establishes a singly linked list of
374+
/// tasks that are waiting on a future.
375+
AsyncTask *&getNextWaitingTask() {
376+
return reinterpret_cast<AsyncTask *&>(
377+
SchedulerPrivate[NextWaitingTaskIndex]);
378+
}
246379
};
247380

248381
// The compiler will eventually assume these.
@@ -327,6 +460,20 @@ class YieldingAsyncContext : public AsyncContext {
327460
}
328461
};
329462

463+
/// An asynchronous context within a task that describes a general "Future".
464+
/// task.
465+
///
466+
/// This type matches the ABI of a function `<T> () async throws -> T`, which
467+
/// is the type used by `Task.runDetached` and `Task.group.add` to create
468+
/// futures.
469+
class FutureAsyncContext : public AsyncContext {
470+
public:
471+
SwiftError *errorResult = nullptr;
472+
OpaqueValue *indirectResult;
473+
474+
using AsyncContext::AsyncContext;
475+
};
476+
330477
} // end namespace swift
331478

332479
#endif

include/swift/AST/ASTContext.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,14 @@ class ASTContext final {
640640
/// if applicable.
641641
const Decl *getSwiftDeclForExportedClangDecl(const clang::Decl *decl);
642642

643+
/// General conversion method from Swift types -> Clang types.
644+
///
645+
/// HACK: This method is only intended to be called from a specific place in
646+
/// IRGen. For converting function types, strongly prefer using one of the
647+
/// other methods instead, instead of manually iterating over parameters
648+
/// and results.
649+
const clang::Type *getClangTypeForIRGen(Type ty);
650+
643651
/// Determine whether the given Swift type is representable in a
644652
/// given foreign language.
645653
ForeignRepresentationInfo
@@ -901,6 +909,13 @@ class ASTContext final {
901909
/// \returns The requested module, or NULL if the module cannot be found.
902910
ModuleDecl *getModule(ImportPath::Module ModulePath);
903911

912+
/// Attempts to load the matching overlay module for the given clang
913+
/// module into this ASTContext.
914+
///
915+
/// \returns The Swift overlay module corresponding to the given Clang module,
916+
/// or NULL if the overlay module cannot be found.
917+
ModuleDecl *getOverlayModule(const FileUnit *ClangModule);
918+
904919
ModuleDecl *getModuleByName(StringRef ModuleName);
905920

906921
ModuleDecl *getModuleByIdentifier(Identifier ModuleID);

include/swift/AST/ASTMangler.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ class ASTMangler : public Mangler {
155155
Type SelfType,
156156
ModuleDecl *Module);
157157

158+
/// Mangle a completion handler block implementation function, used for importing ObjC
159+
/// APIs as async.
160+
///
161+
/// - If `predefined` is true, this mangles the symbol name of the completion handler
162+
/// predefined in the Swift runtime for the given type signature.
163+
std::string mangleObjCAsyncCompletionHandlerImpl(CanSILFunctionType BlockType,
164+
CanType ResultType,
165+
bool predefined);
166+
158167
/// Mangle the derivative function (JVP/VJP) for the given:
159168
/// - Mangled original function name.
160169
/// - Derivative function kind.

include/swift/AST/Decl.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ class alignas(1 << DeclAlignInBits) Decl {
391391
SWIFT_INLINE_BITFIELD(SubscriptDecl, VarDecl, 2,
392392
StaticSpelling : 2
393393
);
394-
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+8+1+1+1+1+1+1,
394+
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+8+1+1+1+1+1+1+1,
395395
/// \see AbstractFunctionDecl::BodyKind
396396
BodyKind : 3,
397397

@@ -415,7 +415,11 @@ class alignas(1 << DeclAlignInBits) Decl {
415415
Synthesized : 1,
416416

417417
/// Whether this member's body consists of a single expression.
418-
HasSingleExpressionBody : 1
418+
HasSingleExpressionBody : 1,
419+
420+
/// Whether peeking into this function detected nested type declarations.
421+
/// This is set when skipping over the decl at parsing.
422+
HasNestedTypeDeclarations : 1
419423
);
420424

421425
SWIFT_INLINE_BITFIELD(FuncDecl, AbstractFunctionDecl, 1+1+2+1+1+2+1,
@@ -5544,6 +5548,7 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
55445548
Bits.AbstractFunctionDecl.Throws = Throws;
55455549
Bits.AbstractFunctionDecl.Synthesized = false;
55465550
Bits.AbstractFunctionDecl.HasSingleExpressionBody = false;
5551+
Bits.AbstractFunctionDecl.HasNestedTypeDeclarations = false;
55475552
}
55485553

55495554
void setBodyKind(BodyKind K) {
@@ -5690,6 +5695,16 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
56905695
setBody(S, BodyKind::Parsed);
56915696
}
56925697

5698+
/// Was there a nested type declaration detected when parsing this
5699+
/// function was skipped?
5700+
bool hasNestedTypeDeclarations() const {
5701+
return Bits.AbstractFunctionDecl.HasNestedTypeDeclarations;
5702+
}
5703+
5704+
void setHasNestedTypeDeclarations(bool value) {
5705+
Bits.AbstractFunctionDecl.HasNestedTypeDeclarations = value;
5706+
}
5707+
56935708
/// Note that parsing for the body was delayed.
56945709
///
56955710
/// The function should return the body statement and a flag indicating

include/swift/AST/FineGrainedDependencies.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class BiIndexedTwoStageMap {
351351
///
352352
/// \Note The returned graph should not be escaped from the callback.
353353
bool withReferenceDependencies(
354-
llvm::PointerUnion<ModuleDecl *, SourceFile *> MSF,
354+
llvm::PointerUnion<const ModuleDecl *, const SourceFile *> MSF,
355355
const DependencyTracker &depTracker, StringRef outputPath,
356356
bool alsoEmitDotFile, llvm::function_ref<bool(SourceFileDepGraph &&)>);
357357

include/swift/Basic/FunctionBodySkipping.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ enum class FunctionBodySkipping : uint8_t {
2323
None,
2424
/// Only non-inlinable function bodies should be skipped.
2525
NonInlinable,
26+
/// Only non-inlinable functions bodies without type definitions should
27+
/// be skipped.
28+
NonInlinableWithoutTypes,
2629
/// All function bodies should be skipped, where not otherwise required
2730
/// for type inference.
2831
All

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ NODE(NominalTypeDescriptor)
160160
NODE(NonObjCAttribute)
161161
NODE(Number)
162162
NODE(ObjCAsyncCompletionHandlerImpl)
163+
NODE(PredefinedObjCAsyncCompletionHandlerImpl)
163164
NODE(ObjCAttribute)
164165
NODE(ObjCBlock)
165166
NODE(EscapingObjCBlock)

include/swift/Frontend/InputFile.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,17 @@ class InputFile final {
121121
// FrontendInputsAndOutputs. They merely make the call sites
122122
// a bit shorter. Add more forwarding methods as needed.
123123

124-
std::string dependenciesFilePath() const {
124+
StringRef getDependenciesFilePath() const {
125125
return getPrimarySpecificPaths().SupplementaryOutputs.DependenciesFilePath;
126126
}
127-
std::string loadedModuleTracePath() const {
127+
StringRef getLoadedModuleTracePath() const {
128128
return getPrimarySpecificPaths().SupplementaryOutputs.LoadedModuleTracePath;
129129
}
130-
std::string serializedDiagnosticsPath() const {
130+
StringRef getSerializedDiagnosticsPath() const {
131131
return getPrimarySpecificPaths().SupplementaryOutputs
132132
.SerializedDiagnosticsPath;
133133
}
134-
std::string fixItsOutputPath() const {
134+
StringRef getFixItsOutputPath() const {
135135
return getPrimarySpecificPaths().SupplementaryOutputs.FixItsOutputPath;
136136
}
137137
};

0 commit comments

Comments
 (0)