Skip to content

Commit 9b88288

Browse files
committed
[SIL] Add SILFunctionType flag for async.
1 parent 4145ef7 commit 9b88288

37 files changed

+153
-88
lines changed

include/swift/AST/Attr.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ TYPE_ATTR(noescape)
5353
TYPE_ATTR(escaping)
5454
TYPE_ATTR(differentiable)
5555
TYPE_ATTR(noDerivative)
56+
TYPE_ATTR(async)
5657

5758
// SIL-specific attributes
5859
TYPE_ATTR(block_storage)

include/swift/AST/Types.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,12 @@ class alignas(1 << TypeAlignInBits) TypeBase {
362362
ID : 32
363363
);
364364

365-
SWIFT_INLINE_BITFIELD(SILFunctionType, TypeBase, NumSILExtInfoBits+1+3+1+2+1+1,
365+
SWIFT_INLINE_BITFIELD(SILFunctionType, TypeBase, NumSILExtInfoBits+1+3+1+1+2+1+1,
366366
ExtInfoBits : NumSILExtInfoBits,
367367
HasClangTypeInfo : 1,
368368
CalleeConvention : 3,
369369
HasErrorResult : 1,
370+
IsAsync : 1,
370371
CoroutineKind : 2,
371372
HasInvocationSubs : 1,
372373
HasPatternSubs : 1
@@ -3979,7 +3980,7 @@ class SILFunctionType final
39793980
+ 1);
39803981
}
39813982

3982-
SILFunctionType(GenericSignature genericSig, ExtInfo ext,
3983+
SILFunctionType(GenericSignature genericSig, ExtInfo ext, bool isAsync,
39833984
SILCoroutineKind coroutineKind,
39843985
ParameterConvention calleeConvention,
39853986
ArrayRef<SILParameterInfo> params,
@@ -3993,7 +3994,8 @@ class SILFunctionType final
39933994

39943995
public:
39953996
static CanSILFunctionType
3996-
get(GenericSignature genericSig, ExtInfo ext, SILCoroutineKind coroutineKind,
3997+
get(GenericSignature genericSig, ExtInfo ext, bool isAsync,
3998+
SILCoroutineKind coroutineKind,
39973999
ParameterConvention calleeConvention,
39984000
ArrayRef<SILParameterInfo> interfaceParams,
39994001
ArrayRef<SILYieldInfo> interfaceYields,
@@ -4046,6 +4048,8 @@ class SILFunctionType final
40464048
return SILCoroutineKind(Bits.SILFunctionType.CoroutineKind);
40474049
}
40484050

4051+
bool isAsync() const { return Bits.SILFunctionType.IsAsync; }
4052+
40494053
/// Return the array of all the yields.
40504054
ArrayRef<SILYieldInfo> getYields() const {
40514055
return const_cast<SILFunctionType *>(this)->getMutableYields();

include/swift/SIL/SILFunction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,8 @@ class SILFunction
501501
IsWithoutActuallyEscapingThunk = val;
502502
}
503503

504+
bool isAsync() const { return LoweredType->isAsync(); }
505+
504506
/// Returns the calling convention used by this entry point.
505507
SILFunctionTypeRepresentation getRepresentation() const {
506508
return getLoweredFunctionType()->getRepresentation();

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,6 +3283,7 @@ void SILFunctionType::Profile(
32833283
SILFunctionType::SILFunctionType(
32843284
GenericSignature genericSig,
32853285
ExtInfo ext,
3286+
bool isAsync,
32863287
SILCoroutineKind coroutineKind,
32873288
ParameterConvention calleeConvention,
32883289
ArrayRef<SILParameterInfo> params,
@@ -3308,6 +3309,7 @@ SILFunctionType::SILFunctionType(
33083309
"Bits were dropped!");
33093310
static_assert(SILExtInfoBuilder::NumMaskBits == NumSILExtInfoBits,
33103311
"ExtInfo and SILFunctionTypeBitfields must agree on bit size");
3312+
Bits.SILFunctionType.IsAsync = isAsync;
33113313
Bits.SILFunctionType.CoroutineKind = unsigned(coroutineKind);
33123314
NumParameters = params.size();
33133315
if (coroutineKind == SILCoroutineKind::None) {
@@ -3451,7 +3453,7 @@ CanSILBlockStorageType SILBlockStorageType::get(CanType captureType) {
34513453

34523454
CanSILFunctionType SILFunctionType::get(
34533455
GenericSignature genericSig,
3454-
ExtInfo ext, SILCoroutineKind coroutineKind,
3456+
ExtInfo ext, bool isAsync, SILCoroutineKind coroutineKind,
34553457
ParameterConvention callee,
34563458
ArrayRef<SILParameterInfo> params,
34573459
ArrayRef<SILYieldInfo> yields,
@@ -3518,7 +3520,7 @@ CanSILFunctionType SILFunctionType::get(
35183520
}
35193521

35203522
auto fnType =
3521-
new (mem) SILFunctionType(genericSig, ext, coroutineKind, callee,
3523+
new (mem) SILFunctionType(genericSig, ext, isAsync, coroutineKind, callee,
35223524
params, yields, normalResults, errorResult,
35233525
patternSubs, invocationSubs,
35243526
ctx, properties, witnessMethodConformance);

lib/AST/ASTDemangler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,8 @@ Type ASTBuilder::createImplFunctionType(
558558
auto conv = getResultConvention(errorResult->getConvention());
559559
funcErrorResult.emplace(type, conv);
560560
}
561-
return SILFunctionType::get(genericSig, einfo, funcCoroutineKind,
561+
return SILFunctionType::get(genericSig, einfo,
562+
/*isAsync*/ false, funcCoroutineKind,
562563
funcCalleeConvention, funcParams, funcYields,
563564
funcResults, funcErrorResult,
564565
SubstitutionMap(), SubstitutionMap(), Ctx);

lib/AST/ASTPrinter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4249,6 +4249,12 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
42494249
llvm_unreachable("bad convention");
42504250
}
42514251

4252+
void printSILAsyncAttr(bool isAsync) {
4253+
if (isAsync) {
4254+
Printer << "@async ";
4255+
}
4256+
}
4257+
42524258
void printCalleeConvention(ParameterConvention conv) {
42534259
switch (conv) {
42544260
case ParameterConvention::Direct_Unowned:
@@ -4271,6 +4277,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
42714277

42724278
void visitSILFunctionType(SILFunctionType *T) {
42734279
printSILCoroutineKind(T->getCoroutineKind());
4280+
printSILAsyncAttr(T->isAsync());
42744281
printFunctionExtInfo(T->getASTContext(), T->getExtInfo(),
42754282
T->getWitnessMethodConformanceOrInvalid());
42764283
printCalleeConvention(T->getCalleeConvention());

lib/AST/Type.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4378,6 +4378,7 @@ case TypeKind::Id:
43784378
return SILFunctionType::get(
43794379
fnTy->getInvocationGenericSignature(),
43804380
fnTy->getExtInfo(),
4381+
fnTy->isAsync(),
43814382
fnTy->getCoroutineKind(),
43824383
fnTy->getCalleeConvention(),
43834384
transInterfaceParams,
@@ -5371,7 +5372,7 @@ SILFunctionType::withInvocationSubstitutions(SubstitutionMap subs) const {
53715372
assert(!subs || CanGenericSignature(subs.getGenericSignature())
53725373
== getInvocationGenericSignature());
53735374
return SILFunctionType::get(getInvocationGenericSignature(),
5374-
getExtInfo(), getCoroutineKind(),
5375+
getExtInfo(), isAsync(), getCoroutineKind(),
53755376
getCalleeConvention(),
53765377
getParameters(), getYields(), getResults(),
53775378
getOptionalErrorResult(),
@@ -5389,7 +5390,7 @@ SILFunctionType::withPatternSubstitutions(SubstitutionMap subs) const {
53895390
assert(!subs || CanGenericSignature(subs.getGenericSignature())
53905391
== getPatternGenericSignature());
53915392
return SILFunctionType::get(getInvocationGenericSignature(),
5392-
getExtInfo(), getCoroutineKind(),
5393+
getExtInfo(), isAsync(), getCoroutineKind(),
53935394
getCalleeConvention(),
53945395
getParameters(), getYields(), getResults(),
53955396
getOptionalErrorResult(),
@@ -5408,7 +5409,7 @@ SILFunctionType::withPatternSpecialization(CanGenericSignature sig,
54085409
assert(!subs || CanGenericSignature(subs.getGenericSignature())
54095410
== getSubstGenericSignature());
54105411
return SILFunctionType::get(sig,
5411-
getExtInfo(), getCoroutineKind(),
5412+
getExtInfo(), isAsync(), getCoroutineKind(),
54125413
getCalleeConvention(),
54135414
getParameters(), getYields(), getResults(),
54145415
getOptionalErrorResult(),

lib/AST/TypeRepr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ void AttributedTypeRepr::printAttrs(ASTPrinter &Printer,
171171
Printer.printStructurePost(PrintStructureKind::BuiltinAttribute);
172172
Printer << " ";
173173
}
174+
175+
if (hasAttr(TAK_async))
176+
Printer.printSimpleAttr("@async") << " ";
174177
}
175178

176179
IdentTypeRepr *IdentTypeRepr::create(ASTContext &C,

lib/IRGen/GenProto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3056,7 +3056,7 @@ GenericTypeRequirements::GenericTypeRequirements(IRGenModule &IGM,
30563056
// Construct a representative function type.
30573057
auto generics = ncGenerics.getCanonicalSignature();
30583058
auto fnType = SILFunctionType::get(generics, SILFunctionType::ExtInfo(),
3059-
SILCoroutineKind::None,
3059+
/*isAsync*/ false, SILCoroutineKind::None,
30603060
/*callee*/ ParameterConvention::Direct_Unowned,
30613061
/*params*/ {}, /*yields*/ {},
30623062
/*results*/ {}, /*error*/ None,

lib/IRGen/LoadableByAddress.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ LargeSILTypeMapper::getNewSILFunctionType(GenericEnvironment *env,
297297
auto newFnType = SILFunctionType::get(
298298
fnType->getInvocationGenericSignature(),
299299
fnType->getExtInfo(),
300+
fnType->isAsync(),
300301
fnType->getCoroutineKind(),
301302
fnType->getCalleeConvention(),
302303
newParams,
@@ -2361,6 +2362,7 @@ static bool rewriteFunctionReturn(StructLoweringState &pass) {
23612362
auto NewTy = SILFunctionType::get(
23622363
loweredTy->getSubstGenericSignature(),
23632364
loweredTy->getExtInfo(),
2365+
loweredTy->isAsync(),
23642366
loweredTy->getCoroutineKind(),
23652367
loweredTy->getCalleeConvention(),
23662368
loweredTy->getParameters(),

lib/SIL/IR/SILBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ SILType SILBuilder::getPartialApplyResultType(
107107

108108
auto appliedFnType = SILFunctionType::get(nullptr,
109109
extInfo,
110+
FTI->isAsync(),
110111
FTI->getCoroutineKind(),
111112
calleeConvention,
112113
newParams,

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ CanSILFunctionType SILFunctionType::getUnsubstitutedType(SILModule &M) const {
9494
: CanGenericSignature();
9595
return SILFunctionType::get(signature,
9696
getExtInfo(),
97+
isAsync(),
9798
getCoroutineKind(),
9899
getCalleeConvention(),
99100
params, yields, results, errorResult,
@@ -287,11 +288,11 @@ SILFunctionType::getWithDifferentiability(DifferentiabilityKind kind,
287288
}
288289
auto newExtInfo =
289290
getExtInfo().intoBuilder().withDifferentiabilityKind(kind).build();
290-
return get(getInvocationGenericSignature(), newExtInfo, getCoroutineKind(),
291-
getCalleeConvention(), newParameters, getYields(), newResults,
292-
getOptionalErrorResult(), getPatternSubstitutions(),
293-
getInvocationSubstitutions(), getASTContext(),
294-
getWitnessMethodConformanceOrInvalid());
291+
return get(getInvocationGenericSignature(), newExtInfo, isAsync(),
292+
getCoroutineKind(), getCalleeConvention(), newParameters,
293+
getYields(), newResults, getOptionalErrorResult(),
294+
getPatternSubstitutions(), getInvocationSubstitutions(),
295+
getASTContext(), getWitnessMethodConformanceOrInvalid());
295296
}
296297

297298
CanSILFunctionType SILFunctionType::getWithoutDifferentiability() {
@@ -311,9 +312,9 @@ CanSILFunctionType SILFunctionType::getWithoutDifferentiability() {
311312
newResults.push_back(result.getWithDifferentiability(
312313
SILResultDifferentiability::DifferentiableOrNotApplicable));
313314
return SILFunctionType::get(
314-
getInvocationGenericSignature(), nondiffExtInfo, getCoroutineKind(),
315-
getCalleeConvention(), newParams, getYields(), newResults,
316-
getOptionalErrorResult(), getPatternSubstitutions(),
315+
getInvocationGenericSignature(), nondiffExtInfo, isAsync(),
316+
getCoroutineKind(), getCalleeConvention(), newParams, getYields(),
317+
newResults, getOptionalErrorResult(), getPatternSubstitutions(),
317318
getInvocationSubstitutions(), getASTContext());
318319
}
319320

@@ -502,9 +503,9 @@ static CanSILFunctionType getAutoDiffDifferentialType(
502503
llvm::makeArrayRef(substConformances));
503504
}
504505
return SILFunctionType::get(
505-
GenericSignature(), SILFunctionType::ExtInfo(), SILCoroutineKind::None,
506-
ParameterConvention::Direct_Guaranteed, differentialParams, {},
507-
differentialResults, None, substitutions,
506+
GenericSignature(), SILFunctionType::ExtInfo(), /*isAsync*/ false,
507+
SILCoroutineKind::None, ParameterConvention::Direct_Guaranteed,
508+
differentialParams, {}, differentialResults, None, substitutions,
508509
/*invocationSubstitutions*/ SubstitutionMap(), ctx);
509510
}
510511

@@ -681,9 +682,9 @@ static CanSILFunctionType getAutoDiffPullbackType(
681682
llvm::makeArrayRef(substConformances));
682683
}
683684
return SILFunctionType::get(
684-
GenericSignature(), SILFunctionType::ExtInfo(), SILCoroutineKind::None,
685-
ParameterConvention::Direct_Guaranteed, pullbackParams, {},
686-
pullbackResults, None, substitutions,
685+
GenericSignature(), SILFunctionType::ExtInfo(), /*isAsync*/ false,
686+
SILCoroutineKind::None, ParameterConvention::Direct_Guaranteed,
687+
pullbackParams, {}, pullbackResults, None, substitutions,
687688
/*invocationSubstitutions*/ SubstitutionMap(), ctx);
688689
}
689690

@@ -737,7 +738,7 @@ static SILFunctionType *getConstrainedAutoDiffOriginalFunctionType(
737738
constrainedInvocationGenSig->areAllParamsConcrete()
738739
? GenericSignature()
739740
: constrainedInvocationGenSig,
740-
original->getExtInfo(), original->getCoroutineKind(),
741+
original->getExtInfo(), original->isAsync(), original->getCoroutineKind(),
741742
original->getCalleeConvention(), newParameters, original->getYields(),
742743
newResults, original->getOptionalErrorResult(),
743744
/*patternSubstitutions*/ SubstitutionMap(),
@@ -828,6 +829,7 @@ CanSILFunctionType SILFunctionType::getAutoDiffDerivativeFunctionType(
828829
// cache and return.
829830
cachedResult = SILFunctionType::get(
830831
constrainedOriginalFnTy->getSubstGenericSignature(), extInfo,
832+
constrainedOriginalFnTy->isAsync(),
831833
constrainedOriginalFnTy->getCoroutineKind(),
832834
constrainedOriginalFnTy->getCalleeConvention(), newParameters,
833835
constrainedOriginalFnTy->getYields(), newResults,
@@ -913,9 +915,9 @@ CanSILFunctionType SILFunctionType::getAutoDiffTransposeFunctionType(
913915
for (auto &res : getResults())
914916
newParameters.push_back(getParameterInfoForOriginalResult(res));
915917
return SILFunctionType::get(
916-
getInvocationGenericSignature(), getExtInfo(), getCoroutineKind(),
917-
getCalleeConvention(), newParameters, getYields(), newResults,
918-
getOptionalErrorResult(), getPatternSubstitutions(),
918+
getInvocationGenericSignature(), getExtInfo(), isAsync(),
919+
getCoroutineKind(), getCalleeConvention(), newParameters, getYields(),
920+
newResults, getOptionalErrorResult(), getPatternSubstitutions(),
919921
/*invocationSubstitutions*/ {}, getASTContext());
920922
}
921923

@@ -989,7 +991,8 @@ Lowering::adjustFunctionType(CanSILFunctionType type,
989991
return type;
990992

991993
return SILFunctionType::get(type->getInvocationGenericSignature(),
992-
extInfo, type->getCoroutineKind(), callee,
994+
extInfo, type->isAsync(),
995+
type->getCoroutineKind(), callee,
993996
type->getParameters(), type->getYields(),
994997
type->getResults(),
995998
type->getOptionalErrorResult(),
@@ -1016,9 +1019,9 @@ CanSILFunctionType SILFunctionType::getWithExtInfo(ExtInfo newExt) {
10161019
: Lowering::DefaultThickCalleeConvention)
10171020
: ParameterConvention::Direct_Unowned);
10181021

1019-
return get(getInvocationGenericSignature(), newExt, getCoroutineKind(),
1020-
calleeConvention, getParameters(), getYields(), getResults(),
1021-
getOptionalErrorResult(), getPatternSubstitutions(),
1022+
return get(getInvocationGenericSignature(), newExt, isAsync(),
1023+
getCoroutineKind(), calleeConvention, getParameters(), getYields(),
1024+
getResults(), getOptionalErrorResult(), getPatternSubstitutions(),
10221025
getInvocationSubstitutions(), getASTContext(),
10231026
getWitnessMethodConformanceOrInvalid());
10241027
}
@@ -2166,7 +2169,8 @@ static CanSILFunctionType getSILFunctionType(
21662169
}
21672170
}
21682171

2169-
return SILFunctionType::get(genericSig, silExtInfo, coroutineKind,
2172+
return SILFunctionType::get(genericSig, silExtInfo,
2173+
substFnInterfaceType->isAsync(), coroutineKind,
21702174
calleeConvention, inputs, yields,
21712175
results, errorResult,
21722176
substitutions, SubstitutionMap(),
@@ -3756,7 +3760,7 @@ class SILTypeSubstituter :
37563760
? origType->getInvocationGenericSignature()
37573761
: nullptr;
37583762

3759-
return SILFunctionType::get(genericSig, extInfo,
3763+
return SILFunctionType::get(genericSig, extInfo, origType->isAsync(),
37603764
origType->getCoroutineKind(),
37613765
origType->getCalleeConvention(), substParams,
37623766
substYields, substResults, substErrorResult,

lib/SIL/IR/SILType.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,10 @@ TypeBase::replaceSubstitutedSILFunctionTypesWithUnsubstituted(SILModule &M) cons
671671

672672
if (!didChange)
673673
return sft;
674-
674+
675675
return SILFunctionType::get(sft->getInvocationGenericSignature(),
676-
sft->getExtInfo(), sft->getCoroutineKind(),
676+
sft->getExtInfo(), sft->isAsync(),
677+
sft->getCoroutineKind(),
677678
sft->getCalleeConvention(),
678679
newParams, newYields, newResults,
679680
newErrorResult,

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,6 +3003,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
30033003

30043004
auto fnTy = SILFunctionType::get(nullptr,
30053005
methodTy->getExtInfo(),
3006+
methodTy->isAsync(),
30063007
methodTy->getCoroutineKind(),
30073008
methodTy->getCalleeConvention(),
30083009
dynParams,

lib/SILGen/SILGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ SILGenModule::getKeyPathProjectionCoroutine(bool isReadAccess,
419419
/*clangFunctionType*/ nullptr)
420420
.build();
421421

422-
auto functionTy = SILFunctionType::get(sig, extInfo,
422+
auto functionTy = SILFunctionType::get(sig, extInfo, /*isAsync*/ false,
423423
SILCoroutineKind::YieldOnce,
424424
ParameterConvention::Direct_Unowned,
425425
params,
@@ -482,7 +482,7 @@ SILFunction *SILGenModule::emitTopLevelFunction(SILLocation Loc) {
482482
};
483483

484484
CanSILFunctionType topLevelType = SILFunctionType::get(nullptr, extInfo,
485-
SILCoroutineKind::None,
485+
/*isAsync*/ false, SILCoroutineKind::None,
486486
ParameterConvention::Direct_Unowned,
487487
params, /*yields*/ {},
488488
SILResultInfo(Int32Ty,

lib/SILGen/SILGenBridging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ ManagedValue SILGenFunction::emitFuncToBlock(SILLocation loc,
578578
}
579579

580580
auto invokeTy = SILFunctionType::get(
581-
genericSig, extInfo, SILCoroutineKind::None,
581+
genericSig, extInfo, /*isAsync*/ false, SILCoroutineKind::None,
582582
ParameterConvention::Direct_Unowned, params,
583583
/*yields*/ {}, blockInterfaceTy->getResults(),
584584
blockInterfaceTy->getOptionalErrorResult(),

0 commit comments

Comments
 (0)