Skip to content

Commit 8499640

Browse files
authored
Merge pull request #72143 from rjmccall/builtin-create-task
Unify and simplify the task-creation builtins
2 parents c705a62 + 02ddc29 commit 8499640

28 files changed

+1196
-582
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBuiltin.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ extension BuiltinInst : OnoneSimplifyable {
4444
.AllocVector,
4545
.IsPOD:
4646
optimizeArgumentToThinMetatype(argument: 0, context)
47-
case .CreateAsyncTask:
48-
// In embedded Swift, CreateAsyncTask needs a thin metatype
49-
if context.options.enableEmbeddedSwift {
50-
optimizeArgumentToThinMetatype(argument: 1, context)
51-
}
5247
case .ICMP_EQ:
5348
constantFoldIntegerEquality(isEqual: true, context)
5449
case .ICMP_NE:

include/swift/AST/ASTSynthesis.h

Lines changed: 152 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/Types.h"
1717
#include "swift/AST/ASTContext.h"
1818
#include "swift/AST/Decl.h"
19+
#include "swift/AST/Expr.h"
1920
#include "swift/AST/GenericParamList.h"
2021
#include "swift/AST/ParameterList.h"
2122

@@ -51,6 +52,7 @@ enum SingletonTypeSynthesizer {
5152
_rawUnsafeContinuation,
5253
_void,
5354
_word,
55+
_swiftInt, // Swift.Int
5456
_serialExecutor, // the '_Concurrency.SerialExecutor' protocol
5557
_taskExecutor, // the '_Concurrency.TaskExecutor' protocol
5658
_actor, // the '_Concurrency.Actor' protocol
@@ -70,6 +72,7 @@ inline Type synthesizeType(SynthesisContext &SC,
7072
case _void: return SC.Context.TheEmptyTupleType;
7173
case _word: return BuiltinIntegerType::get(BuiltinIntegerWidth::pointer(),
7274
SC.Context);
75+
case _swiftInt: return SC.Context.getIntType();
7376
case _serialExecutor:
7477
return SC.Context.getProtocol(KnownProtocolKind::SerialExecutor)
7578
->getDeclaredInterfaceType();
@@ -88,6 +91,11 @@ inline Type synthesizeType(SynthesisContext &SC,
8891
}
8992
}
9093

94+
enum RepresentationSynthesizer {
95+
_thin,
96+
_thick
97+
};
98+
9199
/// A synthesizer which generates an integer type.
92100
struct IntegerTypeSynthesizer {
93101
unsigned BitWidth;
@@ -131,6 +139,23 @@ Type synthesizeType(SynthesisContext &SC,
131139
return MetatypeType::get(synthesizeType(SC, M.Sub));
132140
}
133141

142+
template <class S>
143+
struct RepMetatypeTypeSynthesizer {
144+
S Sub;
145+
RepresentationSynthesizer Rep;
146+
};
147+
template <class S>
148+
constexpr RepMetatypeTypeSynthesizer<S>
149+
_metatype(S sub, RepresentationSynthesizer rep ) {
150+
return {sub, rep};
151+
}
152+
template <class S>
153+
Type synthesizeType(SynthesisContext &SC,
154+
const RepMetatypeTypeSynthesizer<S> &M) {
155+
auto instanceType = synthesizeType(SC, M.Sub);
156+
return MetatypeType::get(instanceType, synthesizeMetatypeRepresentation(M.Rep));
157+
}
158+
134159
/// A synthesizer which generates an existential type from a requirement type.
135160
template <class S>
136161
struct ExistentialTypeSynthesizer {
@@ -146,6 +171,16 @@ Type synthesizeType(SynthesisContext &SC,
146171
return ExistentialType::get(synthesizeType(SC, M.Sub));
147172
}
148173

174+
MetatypeRepresentation
175+
inline synthesizeMetatypeRepresentation(RepresentationSynthesizer rep) {
176+
switch (rep) {
177+
case _thin: return MetatypeRepresentation::Thin;
178+
case _thick: return MetatypeRepresentation::Thick;
179+
// TOOD: maybe add _objc?
180+
}
181+
llvm_unreachable("bad kind");
182+
}
183+
149184
/// A synthesizer which generates an existential metatype type.
150185
template <class S>
151186
struct ExistentialMetatypeTypeSynthesizer {
@@ -160,6 +195,22 @@ Type synthesizeType(SynthesisContext &SC,
160195
const ExistentialMetatypeTypeSynthesizer<S> &M) {
161196
return ExistentialMetatypeType::get(synthesizeType(SC, M.Sub));
162197
}
198+
template <class S>
199+
struct RepExistentialMetatypeTypeSynthesizer {
200+
S Sub;
201+
RepresentationSynthesizer Rep;
202+
};
203+
template <class S>
204+
constexpr RepExistentialMetatypeTypeSynthesizer<S>
205+
_existentialMetatype(S sub, RepresentationSynthesizer rep) {
206+
return {sub, rep};
207+
}
208+
template <class S>
209+
Type synthesizeType(SynthesisContext &SC,
210+
const RepExistentialMetatypeTypeSynthesizer<S> &M) {
211+
return ExistentialMetatypeType::get(synthesizeType(SC, M.Sub),
212+
synthesizeMetatypeRepresentation(M.Rep));
213+
}
163214

164215
/// A synthesizer that generates a MoveOnly wrapper of a type.
165216
template <class S>
@@ -249,10 +300,12 @@ Type synthesizeType(SynthesisContext &SC,
249300

250301
/// Synthesize parameter declarations.
251302
template <class S>
252-
ParamDecl *synthesizeParamDecl(SynthesisContext &SC, const S &s) {
303+
ParamDecl *synthesizeParamDecl(SynthesisContext &SC, const S &s,
304+
const char *label = nullptr) {
305+
auto argLabelIdent = (label ? SC.Context.getIdentifier(label) : Identifier());
253306
auto type = synthesizeType(SC, s);
254307
auto PD = new (SC.Context) ParamDecl(SourceLoc(), SourceLoc(),
255-
Identifier(), SourceLoc(),
308+
argLabelIdent, SourceLoc(),
256309
Identifier(), SC.DC);
257310
PD->setSpecifier(ParamSpecifier::Default);
258311
PD->setInterfaceType(type);
@@ -280,8 +333,9 @@ constexpr SpecifiedParamSynthesizer<G> _inout(G sub) {
280333
}
281334
template <class S>
282335
ParamDecl *synthesizeParamDecl(SynthesisContext &SC,
283-
const SpecifiedParamSynthesizer<S> &s) {
284-
auto param = synthesizeParamDecl(SC, s.sub);
336+
const SpecifiedParamSynthesizer<S> &s,
337+
const char *label = nullptr) {
338+
auto param = synthesizeParamDecl(SC, s.sub, label);
285339
param->setSpecifier(s.specifier);
286340
return param;
287341
}
@@ -295,6 +349,61 @@ FunctionType::Param synthesizeParamType(SynthesisContext &SC,
295349
return param.withFlags(flags);
296350
}
297351

352+
template <class S>
353+
void synthesizeDefaultArgument(SynthesisContext &SC, const S &s,
354+
ParamDecl *param) {
355+
synthesizeDefaultArgumentFromExpr(SC, s, param);
356+
}
357+
template <class S>
358+
void synthesizeDefaultArgumentFromExpr(SynthesisContext &SC, const S &s,
359+
ParamDecl *param) {
360+
// FIXME: this works except that we tend to crash in diagnostics trying
361+
// to render the default argument if you mess up the call.
362+
auto expr = synthesizeExpr(SC, s);
363+
param->setDefaultArgumentKind(DefaultArgumentKind::Normal);
364+
param->setDefaultExpr(expr, /*type checked*/ false);
365+
}
366+
367+
/// Default arguments.
368+
template <class S, class A>
369+
struct DefaultedSynthesizer { S sub; A arg; };
370+
template <class S, class A>
371+
constexpr DefaultedSynthesizer<S, A> _defaulted(S sub, A arg) {
372+
return {sub, arg};
373+
}
374+
template <class S, class A>
375+
ParamDecl *synthesizeParamDecl(SynthesisContext &SC,
376+
const DefaultedSynthesizer<S, A> &s,
377+
const char *label = nullptr) {
378+
auto param = synthesizeParamDecl(SC, s.sub, label);
379+
synthesizeDefaultArgument(SC, s.arg, param);
380+
return param;
381+
}
382+
template <class S, class A>
383+
FunctionType::Param synthesizeParamType(SynthesisContext &SC,
384+
const DefaultedSynthesizer<S, A> &s) {
385+
return synthesizeParamType(s.sub);
386+
}
387+
388+
/// Labels.
389+
template <class S>
390+
struct LabelSynthesizer { const char *label; S sub; };
391+
template <class S>
392+
constexpr LabelSynthesizer<S> _label(const char *label, S sub) {
393+
return {label, sub};
394+
}
395+
template <class S>
396+
ParamDecl *synthesizeParamDecl(SynthesisContext &SC,
397+
const LabelSynthesizer<S> &s) {
398+
return synthesizeParamDecl(SC, s.sub, s.label);
399+
}
400+
template <class S>
401+
FunctionType::Param synthesizeParamType(SynthesisContext &SC,
402+
const LabelSynthesizer<S> &s) {
403+
auto label = SC.Context.getIdentifier(s.label);
404+
return synthesizeParamType(SC, s.sub).withLabel(label);
405+
}
406+
298407
/// Synthesize a parameter list.
299408
template <class... Params>
300409
struct ParameterListSynthesizer {
@@ -341,22 +450,21 @@ void synthesizeParameterTypes(SynthesisContext &SC,
341450
}
342451

343452
/// Synthesize function ExtInfo.
344-
enum FunctionRepresentationSynthesizer {
345-
_thin,
346-
_thick
347-
};
348453
template <class S> struct ThrowsSynthesizer { S sub; };
349454
template <class S> struct AsyncSynthesizer { S sub; };
350455
template <class S> struct NoescapeSynthesizer { S sub; };
456+
template <class S> struct SendableModSynthesizer { S sub; };
351457
template <class S>
352458
constexpr ThrowsSynthesizer<S> _throws(S sub) { return {sub}; }
353459
template <class S>
354460
constexpr AsyncSynthesizer<S> _async(S sub) { return {sub}; }
355461
template <class S>
356462
constexpr NoescapeSynthesizer<S> _noescape(S sub) { return {sub}; }
463+
template <class S>
464+
constexpr SendableModSynthesizer<S> _sendable(S sub) { return {sub}; }
357465

358466
inline ASTExtInfo synthesizeExtInfo(SynthesisContext &SC,
359-
FunctionRepresentationSynthesizer kind) {
467+
RepresentationSynthesizer kind) {
360468
switch (kind) {
361469
case _thin: return ASTExtInfo().withRepresentation(
362470
FunctionTypeRepresentation::Thin);
@@ -379,6 +487,11 @@ ASTExtInfo synthesizeExtInfo(SynthesisContext &SC,
379487
const NoescapeSynthesizer<S> &s) {
380488
return synthesizeExtInfo(SC, s.sub).withNoEscape();
381489
}
490+
template <class S>
491+
ASTExtInfo synthesizeExtInfo(SynthesisContext &SC,
492+
const SendableModSynthesizer<S> &s) {
493+
return synthesizeExtInfo(SC, s.sub).withConcurrent();
494+
}
382495

383496
/// Synthesize a function type.
384497
template <class ExtInfoS, class ResultS, class ParamsS>
@@ -414,6 +527,36 @@ Type synthesizeType(SynthesisContext &SC, const OptionalSynthesizer<S> &s) {
414527
return OptionalType::get(synthesizeType(SC, s.sub));
415528
}
416529

530+
/// Expressions.
531+
enum SingletonExprSynthesizer {
532+
_nil
533+
};
534+
inline Expr *synthesizeExpr(SynthesisContext &SC, SingletonExprSynthesizer s) {
535+
switch (s) {
536+
case _nil:
537+
return new (SC.Context) NilLiteralExpr(SourceLoc(), /*implicit*/true);
538+
}
539+
llvm_unreachable("bad singleton kind");
540+
}
541+
inline void synthesizeDefaultArgument(SynthesisContext &SC,
542+
SingletonExprSynthesizer s,
543+
ParamDecl *param) {
544+
switch (s) {
545+
case _nil: {
546+
auto expr = synthesizeExpr(SC, s);
547+
param->setDefaultArgumentKind(DefaultArgumentKind::NilLiteral);
548+
param->setDefaultExpr(expr, /*type checked*/ false);
549+
return;
550+
}
551+
552+
/*
553+
default:
554+
synthesizeDefaultArgumentFromExpr(SC, s, param);
555+
return;
556+
*/
557+
}
558+
}
559+
417560
} // end namespace swift
418561

419562
#endif

0 commit comments

Comments
 (0)