Skip to content

Commit cf1da48

Browse files
authored
Merge pull request #1895 from swiftwasm/main
[pull] swiftwasm from main
2 parents a1f2fef + 3972a13 commit cf1da48

35 files changed

+793
-66
lines changed

include/swift/AST/ASTContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,13 @@ class ASTContext final {
499499
/// Retrieve the type Swift.Never.
500500
CanType getNeverType() const;
501501

502-
#define KNOWN_OBJC_TYPE_DECL(MODULE, NAME, DECL_CLASS) \
502+
#define KNOWN_SDK_TYPE_DECL(MODULE, NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
503503
/** Retrieve the declaration of MODULE.NAME. */ \
504504
DECL_CLASS *get##NAME##Decl() const; \
505505
\
506506
/** Retrieve the type of MODULE.NAME. */ \
507507
Type get##NAME##Type() const;
508-
#include "swift/AST/KnownObjCTypes.def"
508+
#include "swift/AST/KnownSDKTypes.def"
509509

510510
// Declare accessors for the known declarations.
511511
#define FUNC_DECL(Name, Id) \

include/swift/AST/DiagnosticsDriver.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,7 @@ WARNING(warn_drv_darwin_sdk_invalid_settings, none,
190190
"SDK settings were ignored because 'SDKSettings.json' could not be parsed",
191191
())
192192

193+
REMARK(remark_forwarding_to_new_driver, none, "new Swift driver will be used", ())
194+
193195
#define UNDEFINE_DIAGNOSTIC_MACROS
194196
#include "DefineDiagnosticMacros.h"

include/swift/AST/KnownObjCTypes.def

Lines changed: 0 additions & 37 deletions
This file was deleted.

include/swift/AST/KnownSDKTypes.def

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===--- KnownSDKTypes.def - Common SDK types -----------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This xmacro generates code for common SDK types the
14+
// compiler has special knowledge of.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef KNOWN_SDK_TYPE_DECL
19+
/// KNOWN_SDK_TYPE_DECL(MODULE, NAME, DECL_CLASS, NUM_GENERIC_ARGS)
20+
///
21+
/// The macro is expanded for each known SDK type. MODULE is
22+
/// bound to the name of the module the type comes from. NAME is bound to the
23+
/// unqualified name of the type. DECL_CLASS is bound to the Decl subclass it
24+
/// is expected to be an instance of. NUM_GENERIC_ARGS is the number of generic
25+
/// parameters the decl ought to have.
26+
#define KNOWN_SDK_TYPE_DECL(MODULE, NAME, DECL_CLASS, NUM_GENERIC_ARGS)
27+
#endif
28+
29+
KNOWN_SDK_TYPE_DECL(Foundation, NSCopying, ProtocolDecl, 0)
30+
KNOWN_SDK_TYPE_DECL(Foundation, NSError, ClassDecl, 0)
31+
KNOWN_SDK_TYPE_DECL(Foundation, NSNumber, ClassDecl, 0)
32+
KNOWN_SDK_TYPE_DECL(Foundation, NSValue, ClassDecl, 0)
33+
34+
KNOWN_SDK_TYPE_DECL(ObjectiveC, NSObject, ClassDecl, 0)
35+
KNOWN_SDK_TYPE_DECL(ObjectiveC, Selector, StructDecl, 0)
36+
KNOWN_SDK_TYPE_DECL(ObjectiveC, ObjCBool, StructDecl, 0)
37+
38+
// TODO(async): These might move to the stdlib module when concurrency is
39+
// standardized
40+
KNOWN_SDK_TYPE_DECL(Concurrency, UnsafeContinuation, NominalTypeDecl, 1)
41+
KNOWN_SDK_TYPE_DECL(Concurrency, UnsafeThrowingContinuation, NominalTypeDecl, 1)
42+
43+
#undef KNOWN_SDK_TYPE_DECL

include/swift/SIL/SILBuilder.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,20 @@ class SILBuilder {
19341934
getSILDebugLocation(Loc), Operand, Index));
19351935
}
19361936

1937+
GetAsyncContinuationInst *createGetAsyncContinuation(SILLocation Loc,
1938+
SILType ContinuationTy) {
1939+
return insert(new (getModule()) GetAsyncContinuationInst(getSILDebugLocation(Loc),
1940+
ContinuationTy));
1941+
}
1942+
1943+
GetAsyncContinuationAddrInst *createGetAsyncContinuationAddr(SILLocation Loc,
1944+
SILValue Operand,
1945+
SILType ContinuationTy) {
1946+
return insert(new (getModule()) GetAsyncContinuationAddrInst(getSILDebugLocation(Loc),
1947+
Operand,
1948+
ContinuationTy));
1949+
}
1950+
19371951
//===--------------------------------------------------------------------===//
19381952
// Terminator SILInstruction Creation Methods
19391953
//===--------------------------------------------------------------------===//
@@ -1964,7 +1978,17 @@ class SILBuilder {
19641978
YieldInst::create(getSILDebugLocation(loc), yieldedValues,
19651979
resumeBB, unwindBB, getFunction()));
19661980
}
1967-
1981+
1982+
AwaitAsyncContinuationInst *createAwaitAsyncContinuation(SILLocation loc,
1983+
SILValue continuation,
1984+
SILBasicBlock *resumeBB,
1985+
SILBasicBlock *errorBB) {
1986+
return insertTerminator(
1987+
new (getModule()) AwaitAsyncContinuationInst(getSILDebugLocation(loc),
1988+
continuation,
1989+
resumeBB, errorBB));
1990+
}
1991+
19681992
CondBranchInst *
19691993
createCondBranch(SILLocation Loc, SILValue Cond, SILBasicBlock *Target1,
19701994
SILBasicBlock *Target2,

include/swift/SIL/SILCloner.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2928,6 +2928,41 @@ void SILCloner<ImplClass>::visitDifferentiabilityWitnessFunctionInst(
29282928
Inst->getWitnessKind(), Inst->getWitness()));
29292929
}
29302930

2931+
template <typename ImplClass>
2932+
void SILCloner<ImplClass>
2933+
::visitGetAsyncContinuationInst(GetAsyncContinuationInst *Inst) {
2934+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
2935+
recordClonedInstruction(Inst,
2936+
getBuilder().createGetAsyncContinuation(
2937+
getOpLocation(Inst->getLoc()),
2938+
getOpType(Inst->getType())));
2939+
}
2940+
2941+
template <typename ImplClass>
2942+
void SILCloner<ImplClass>
2943+
::visitGetAsyncContinuationAddrInst(GetAsyncContinuationAddrInst *Inst) {
2944+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
2945+
recordClonedInstruction(Inst,
2946+
getBuilder().createGetAsyncContinuationAddr(
2947+
getOpLocation(Inst->getLoc()),
2948+
getOpValue(Inst->getOperand()),
2949+
getOpType(Inst->getType())));
2950+
}
2951+
2952+
template <typename ImplClass>
2953+
void SILCloner<ImplClass>
2954+
::visitAwaitAsyncContinuationInst(AwaitAsyncContinuationInst *Inst) {
2955+
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
2956+
recordClonedInstruction(Inst,
2957+
getBuilder().createAwaitAsyncContinuation(
2958+
getOpLocation(Inst->getLoc()),
2959+
getOpValue(Inst->getOperand()),
2960+
getOpBasicBlock(Inst->getResumeBB()),
2961+
Inst->getErrorBB()
2962+
? getOpBasicBlock(Inst->getErrorBB())
2963+
: nullptr));
2964+
}
2965+
29312966
} // end namespace swift
29322967

29332968
#endif

include/swift/SIL/SILInstruction.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3039,6 +3039,60 @@ class KeyPathPattern final
30393039
}
30403040
};
30413041

3042+
/// Accesses the continuation for an async task, to prepare a primitive suspend operation.
3043+
/// The continuation must be consumed by an AwaitAsyncContinuation instruction locally,
3044+
/// and must dynamically be resumed exactly once during the program's ensuing execution.
3045+
class GetAsyncContinuationInst final
3046+
: public InstructionBase<SILInstructionKind::GetAsyncContinuationInst,
3047+
SingleValueInstruction>
3048+
{
3049+
friend SILBuilder;
3050+
3051+
GetAsyncContinuationInst(SILDebugLocation Loc,
3052+
SILType ContinuationTy)
3053+
: InstructionBase(Loc, ContinuationTy)
3054+
{}
3055+
3056+
public:
3057+
3058+
/// Get the type of the value the async task receives on a resume.
3059+
CanType getFormalResumeType() const;
3060+
SILType getLoweredResumeType() const;
3061+
3062+
/// True if the continuation can be used to resume the task by throwing an error.
3063+
bool throws() const;
3064+
3065+
ArrayRef<Operand> getAllOperands() const { return {}; }
3066+
MutableArrayRef<Operand> getAllOperands() { return {}; }
3067+
};
3068+
3069+
/// Accesses the continuation for an async task, to prepare a primitive suspend operation.
3070+
/// The continuation must be consumed by an AwaitAsyncContinuation instruction locally,
3071+
/// and must dynamically be resumed exactly once during the program's ensuing execution.
3072+
///
3073+
/// This variation of the instruction additionally takes an operand for the address of the
3074+
/// buffer that receives the incoming value when the continuation is resumed.
3075+
class GetAsyncContinuationAddrInst final
3076+
: public UnaryInstructionBase<SILInstructionKind::GetAsyncContinuationAddrInst,
3077+
SingleValueInstruction>
3078+
{
3079+
friend SILBuilder;
3080+
GetAsyncContinuationAddrInst(SILDebugLocation Loc,
3081+
SILValue Operand,
3082+
SILType ContinuationTy)
3083+
: UnaryInstructionBase(Loc, Operand, ContinuationTy)
3084+
{}
3085+
3086+
public:
3087+
3088+
/// Get the type of the value the async task receives on a resume.
3089+
CanType getFormalResumeType() const;
3090+
SILType getLoweredResumeType() const;
3091+
3092+
/// True if the continuation can be used to resume the task by throwing an error.
3093+
bool throws() const;
3094+
};
3095+
30423096
/// Instantiates a key path object.
30433097
class KeyPathInst final
30443098
: public InstructionBase<SILInstructionKind::KeyPathInst,
@@ -7231,6 +7285,7 @@ class TermInst : public NonValueInstruction {
72317285
case TermKind::DynamicMethodBranchInst:
72327286
case TermKind::CheckedCastAddrBranchInst:
72337287
case TermKind::CheckedCastValueBranchInst:
7288+
case TermKind::AwaitAsyncContinuationInst:
72347289
return false;
72357290
case TermKind::SwitchEnumInst:
72367291
case TermKind::CheckedCastBranchInst:
@@ -7326,6 +7381,52 @@ class UnwindInst
73267381
MutableArrayRef<Operand> getAllOperands() { return {}; }
73277382
};
73287383

7384+
/// Suspend execution of an async task until
7385+
/// essentially just a funny kind of return).
7386+
class AwaitAsyncContinuationInst final
7387+
: public UnaryInstructionBase<SILInstructionKind::AwaitAsyncContinuationInst,
7388+
TermInst>
7389+
{
7390+
friend SILBuilder;
7391+
7392+
std::array<SILSuccessor, 2> Successors;
7393+
7394+
AwaitAsyncContinuationInst(SILDebugLocation Loc, SILValue Continuation,
7395+
SILBasicBlock *resumeBB,
7396+
SILBasicBlock *errorBBOrNull)
7397+
: UnaryInstructionBase(Loc, Continuation),
7398+
Successors{{{this}, {this}}}
7399+
{
7400+
Successors[0] = resumeBB;
7401+
if (errorBBOrNull)
7402+
Successors[1] = errorBBOrNull;
7403+
}
7404+
7405+
public:
7406+
/// Returns the basic block to which control is transferred when the task is
7407+
/// resumed normally.
7408+
///
7409+
/// This basic block should take an argument of the continuation's resume type,
7410+
/// unless the continuation is formed by a \c GetAsyncContinuationAddrInst
7411+
/// that binds a specific memory location to receive the resume value.
7412+
SILBasicBlock *getResumeBB() const { return Successors[0].getBB(); }
7413+
7414+
/// Returns the basic block to which control is transferred when the task is
7415+
/// resumed in an error state, or `nullptr` if the continuation does not support
7416+
/// failure.
7417+
///
7418+
/// This basic block should take an argument of Error type.
7419+
SILBasicBlock *getErrorBB() const {
7420+
return Successors[1].getBB();
7421+
}
7422+
7423+
SuccessorListTy getSuccessors() {
7424+
if (getErrorBB())
7425+
return Successors;
7426+
return SuccessorListTy(Successors.data(), 1);
7427+
}
7428+
};
7429+
73297430
/// YieldInst - Yield control temporarily to the caller of this coroutine.
73307431
///
73317432
/// This is a terminator because the caller can abort the coroutine,

include/swift/SIL/SILNodes.def

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,16 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
713713
SINGLE_VALUE_INST(DifferentiabilityWitnessFunctionInst,
714714
differentiability_witness_function,
715715
SingleValueInstruction, None, DoesNotRelease)
716+
717+
// Async
718+
// TODO: The side effects declarations on this instruction could likely
719+
// be tightened, though we want to be careful that passes that try to do
720+
// code motion or eliminate this instruction don't do so without awareness of
721+
// its structural requirements.
722+
SINGLE_VALUE_INST(GetAsyncContinuationInst, get_async_continuation,
723+
SingleValueInstruction, MayHaveSideEffects, MayRelease)
724+
SINGLE_VALUE_INST(GetAsyncContinuationAddrInst, get_async_continuation_addr,
725+
SingleValueInstruction, MayHaveSideEffects, MayRelease)
716726

717727
// Key paths
718728
// TODO: The only "side effect" is potentially retaining the returned key path
@@ -750,6 +760,8 @@ ABSTRACT_INST(TermInst, SILInstruction)
750760
TermInst, MayRead, DoesNotRelease)
751761
TERMINATOR(DynamicMethodBranchInst, dynamic_method_br,
752762
TermInst, None, DoesNotRelease)
763+
TERMINATOR(AwaitAsyncContinuationInst, await_async_continuation,
764+
TermInst, MayHaveSideEffects, MayRelease)
753765
DYNAMICCAST_TERMINATOR(CheckedCastBranchInst, checked_cast_br,
754766
TermInst, None, DoesNotRelease)
755767
DYNAMICCAST_TERMINATOR(CheckedCastAddrBranchInst, checked_cast_addr_br,

include/swift/SILOptimizer/Utils/SCCVisitor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ class SCCVisitor {
116116
Operands.push_back(CBI->getFalseArgs()[Index]);
117117
return;
118118
}
119+
120+
case TermKind::AwaitAsyncContinuationInst: {
121+
auto *AACI = cast<AwaitAsyncContinuationInst>(Term);
122+
Operands.push_back(AACI->getOperand());
123+
return;
124+
}
119125

120126
case TermKind::SwitchEnumInst:
121127
case TermKind::SwitchEnumAddrInst:

lib/AST/ASTContext.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ struct ASTContext::Implementation {
191191
DECL_CLASS *NAME##Decl = nullptr;
192192
#include "swift/AST/KnownStdlibTypes.def"
193193

194-
#define KNOWN_OBJC_TYPE_DECL(MODULE, NAME, DECL_CLASS) \
194+
#define KNOWN_SDK_TYPE_DECL(MODULE, NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
195195
/** The declaration of MODULE.NAME. */ \
196196
DECL_CLASS *NAME##Decl = nullptr;
197-
#include "swift/AST/KnownObjCTypes.def"
197+
#include "swift/AST/KnownSDKTypes.def"
198198

199199
/// The declaration of '+' function for two RangeReplaceableCollection.
200200
FuncDecl *PlusFunctionOnRangeReplaceableCollection = nullptr;
@@ -894,7 +894,7 @@ CanType ASTContext::getNeverType() const {
894894
return neverDecl->getDeclaredInterfaceType()->getCanonicalType();
895895
}
896896

897-
#define KNOWN_OBJC_TYPE_DECL(MODULE, NAME, DECLTYPE) \
897+
#define KNOWN_SDK_TYPE_DECL(MODULE, NAME, DECLTYPE, GENERIC_ARGS) \
898898
DECLTYPE *ASTContext::get##NAME##Decl() const { \
899899
if (!getImpl().NAME##Decl) { \
900900
if (ModuleDecl *M = getLoadedModule(Id_##MODULE)) { \
@@ -905,7 +905,8 @@ DECLTYPE *ASTContext::get##NAME##Decl() const { \
905905
decls); \
906906
if (decls.size() == 1 && isa<DECLTYPE>(decls[0])) { \
907907
auto decl = cast<DECLTYPE>(decls[0]); \
908-
if (isa<ProtocolDecl>(decl) || decl->getGenericParams() == nullptr) { \
908+
if (isa<ProtocolDecl>(decl) \
909+
|| (bool)decl->getGenericParams() == (bool)GENERIC_ARGS) { \
909910
getImpl().NAME##Decl = decl; \
910911
} \
911912
} \
@@ -922,7 +923,7 @@ Type ASTContext::get##NAME##Type() const { \
922923
return decl->getDeclaredInterfaceType(); \
923924
}
924925

925-
#include "swift/AST/KnownObjCTypes.def"
926+
#include "swift/AST/KnownSDKTypes.def"
926927

927928
ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
928929
// Check whether we've already looked for and cached this protocol.

0 commit comments

Comments
 (0)