Skip to content

[SIL] Move from SILFunction to SILFunctionType flag for async. #33554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/AST/Attr.def
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ TYPE_ATTR(noescape)
TYPE_ATTR(escaping)
TYPE_ATTR(differentiable)
TYPE_ATTR(noDerivative)
TYPE_ATTR(async)

// SIL-specific attributes
TYPE_ATTR(block_storage)
Expand Down
10 changes: 7 additions & 3 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,12 @@ class alignas(1 << TypeAlignInBits) TypeBase {
ID : 32
);

SWIFT_INLINE_BITFIELD(SILFunctionType, TypeBase, NumSILExtInfoBits+1+3+1+2+1+1,
SWIFT_INLINE_BITFIELD(SILFunctionType, TypeBase, NumSILExtInfoBits+1+3+1+1+2+1+1,
ExtInfoBits : NumSILExtInfoBits,
HasClangTypeInfo : 1,
CalleeConvention : 3,
HasErrorResult : 1,
IsAsync : 1,
CoroutineKind : 2,
HasInvocationSubs : 1,
HasPatternSubs : 1
Expand Down Expand Up @@ -3979,7 +3980,7 @@ class SILFunctionType final
+ 1);
}

SILFunctionType(GenericSignature genericSig, ExtInfo ext,
SILFunctionType(GenericSignature genericSig, ExtInfo ext, bool isAsync,
SILCoroutineKind coroutineKind,
ParameterConvention calleeConvention,
ArrayRef<SILParameterInfo> params,
Expand All @@ -3993,7 +3994,8 @@ class SILFunctionType final

public:
static CanSILFunctionType
get(GenericSignature genericSig, ExtInfo ext, SILCoroutineKind coroutineKind,
get(GenericSignature genericSig, ExtInfo ext, bool isAsync,
SILCoroutineKind coroutineKind,
ParameterConvention calleeConvention,
ArrayRef<SILParameterInfo> interfaceParams,
ArrayRef<SILYieldInfo> interfaceYields,
Expand Down Expand Up @@ -4046,6 +4048,8 @@ class SILFunctionType final
return SILCoroutineKind(Bits.SILFunctionType.CoroutineKind);
}

bool isAsync() const { return Bits.SILFunctionType.IsAsync; }

/// Return the array of all the yields.
ArrayRef<SILYieldInfo> getYields() const {
return const_cast<SILFunctionType *>(this)->getMutableYields();
Expand Down
7 changes: 1 addition & 6 deletions include/swift/SIL/SILFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,6 @@ class SILFunction
/// that it may have unboxed capture (i.e. @inout_aliasable parameters).
unsigned IsWithoutActuallyEscapingThunk : 1;

/// True if this function is an async function.
unsigned IsAsync : 1;

/// If != OptimizationMode::NotSet, the optimization mode specified with an
/// function attribute.
unsigned OptMode : NumOptimizationModeBits;
Expand Down Expand Up @@ -504,9 +501,7 @@ class SILFunction
IsWithoutActuallyEscapingThunk = val;
}

bool isAsync() const { return IsAsync; }

void setAsync(bool val = true) { IsAsync = val; }
bool isAsync() const { return LoweredType->isAsync(); }

/// Returns the calling convention used by this entry point.
SILFunctionTypeRepresentation getRepresentation() const {
Expand Down
6 changes: 4 additions & 2 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3283,6 +3283,7 @@ void SILFunctionType::Profile(
SILFunctionType::SILFunctionType(
GenericSignature genericSig,
ExtInfo ext,
bool isAsync,
SILCoroutineKind coroutineKind,
ParameterConvention calleeConvention,
ArrayRef<SILParameterInfo> params,
Expand All @@ -3308,6 +3309,7 @@ SILFunctionType::SILFunctionType(
"Bits were dropped!");
static_assert(SILExtInfoBuilder::NumMaskBits == NumSILExtInfoBits,
"ExtInfo and SILFunctionTypeBitfields must agree on bit size");
Bits.SILFunctionType.IsAsync = isAsync;
Bits.SILFunctionType.CoroutineKind = unsigned(coroutineKind);
NumParameters = params.size();
if (coroutineKind == SILCoroutineKind::None) {
Expand Down Expand Up @@ -3451,7 +3453,7 @@ CanSILBlockStorageType SILBlockStorageType::get(CanType captureType) {

CanSILFunctionType SILFunctionType::get(
GenericSignature genericSig,
ExtInfo ext, SILCoroutineKind coroutineKind,
ExtInfo ext, bool isAsync, SILCoroutineKind coroutineKind,
ParameterConvention callee,
ArrayRef<SILParameterInfo> params,
ArrayRef<SILYieldInfo> yields,
Expand Down Expand Up @@ -3518,7 +3520,7 @@ CanSILFunctionType SILFunctionType::get(
}

auto fnType =
new (mem) SILFunctionType(genericSig, ext, coroutineKind, callee,
new (mem) SILFunctionType(genericSig, ext, isAsync, coroutineKind, callee,
params, yields, normalResults, errorResult,
patternSubs, invocationSubs,
ctx, properties, witnessMethodConformance);
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,8 @@ Type ASTBuilder::createImplFunctionType(
auto conv = getResultConvention(errorResult->getConvention());
funcErrorResult.emplace(type, conv);
}
return SILFunctionType::get(genericSig, einfo, funcCoroutineKind,
return SILFunctionType::get(genericSig, einfo,
/*isAsync*/ false, funcCoroutineKind,
funcCalleeConvention, funcParams, funcYields,
funcResults, funcErrorResult,
SubstitutionMap(), SubstitutionMap(), Ctx);
Expand Down
7 changes: 7 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4249,6 +4249,12 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
llvm_unreachable("bad convention");
}

void printSILAsyncAttr(bool isAsync) {
if (isAsync) {
Printer << "@async ";
}
}

void printCalleeConvention(ParameterConvention conv) {
switch (conv) {
case ParameterConvention::Direct_Unowned:
Expand All @@ -4271,6 +4277,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {

void visitSILFunctionType(SILFunctionType *T) {
printSILCoroutineKind(T->getCoroutineKind());
printSILAsyncAttr(T->isAsync());
printFunctionExtInfo(T->getASTContext(), T->getExtInfo(),
T->getWitnessMethodConformanceOrInvalid());
printCalleeConvention(T->getCalleeConvention());
Expand Down
7 changes: 4 additions & 3 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4378,6 +4378,7 @@ case TypeKind::Id:
return SILFunctionType::get(
fnTy->getInvocationGenericSignature(),
fnTy->getExtInfo(),
fnTy->isAsync(),
fnTy->getCoroutineKind(),
fnTy->getCalleeConvention(),
transInterfaceParams,
Expand Down Expand Up @@ -5371,7 +5372,7 @@ SILFunctionType::withInvocationSubstitutions(SubstitutionMap subs) const {
assert(!subs || CanGenericSignature(subs.getGenericSignature())
== getInvocationGenericSignature());
return SILFunctionType::get(getInvocationGenericSignature(),
getExtInfo(), getCoroutineKind(),
getExtInfo(), isAsync(), getCoroutineKind(),
getCalleeConvention(),
getParameters(), getYields(), getResults(),
getOptionalErrorResult(),
Expand All @@ -5389,7 +5390,7 @@ SILFunctionType::withPatternSubstitutions(SubstitutionMap subs) const {
assert(!subs || CanGenericSignature(subs.getGenericSignature())
== getPatternGenericSignature());
return SILFunctionType::get(getInvocationGenericSignature(),
getExtInfo(), getCoroutineKind(),
getExtInfo(), isAsync(), getCoroutineKind(),
getCalleeConvention(),
getParameters(), getYields(), getResults(),
getOptionalErrorResult(),
Expand All @@ -5408,7 +5409,7 @@ SILFunctionType::withPatternSpecialization(CanGenericSignature sig,
assert(!subs || CanGenericSignature(subs.getGenericSignature())
== getSubstGenericSignature());
return SILFunctionType::get(sig,
getExtInfo(), getCoroutineKind(),
getExtInfo(), isAsync(), getCoroutineKind(),
getCalleeConvention(),
getParameters(), getYields(), getResults(),
getOptionalErrorResult(),
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/TypeRepr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ void AttributedTypeRepr::printAttrs(ASTPrinter &Printer,
Printer.printStructurePost(PrintStructureKind::BuiltinAttribute);
Printer << " ";
}

if (hasAttr(TAK_async))
Printer.printSimpleAttr("@async") << " ";
}

IdentTypeRepr *IdentTypeRepr::create(ASTContext &C,
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3056,7 +3056,7 @@ GenericTypeRequirements::GenericTypeRequirements(IRGenModule &IGM,
// Construct a representative function type.
auto generics = ncGenerics.getCanonicalSignature();
auto fnType = SILFunctionType::get(generics, SILFunctionType::ExtInfo(),
SILCoroutineKind::None,
/*isAsync*/ false, SILCoroutineKind::None,
/*callee*/ ParameterConvention::Direct_Unowned,
/*params*/ {}, /*yields*/ {},
/*results*/ {}, /*error*/ None,
Expand Down
2 changes: 2 additions & 0 deletions lib/IRGen/LoadableByAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ LargeSILTypeMapper::getNewSILFunctionType(GenericEnvironment *env,
auto newFnType = SILFunctionType::get(
fnType->getInvocationGenericSignature(),
fnType->getExtInfo(),
fnType->isAsync(),
fnType->getCoroutineKind(),
fnType->getCalleeConvention(),
newParams,
Expand Down Expand Up @@ -2361,6 +2362,7 @@ static bool rewriteFunctionReturn(StructLoweringState &pass) {
auto NewTy = SILFunctionType::get(
loweredTy->getSubstGenericSignature(),
loweredTy->getExtInfo(),
loweredTy->isAsync(),
loweredTy->getCoroutineKind(),
loweredTy->getCalleeConvention(),
loweredTy->getParameters(),
Expand Down
1 change: 1 addition & 0 deletions lib/SIL/IR/SILBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ SILType SILBuilder::getPartialApplyResultType(

auto appliedFnType = SILFunctionType::get(nullptr,
extInfo,
FTI->isAsync(),
FTI->getCoroutineKind(),
calleeConvention,
newParams,
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/IR/SILFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ SILFunction::SILFunction(SILModule &Module, SILLinkage Linkage, StringRef Name,
ExactSelfClass(isExactSelfClass),
Inlined(false), Zombie(false), HasOwnership(true),
WasDeserializedCanonical(false), IsWithoutActuallyEscapingThunk(false),
IsAsync(false), OptMode(unsigned(OptimizationMode::NotSet)),
OptMode(unsigned(OptimizationMode::NotSet)),
EffectsKindAttr(unsigned(E)) {
assert(!Transparent || !IsDynamicReplaceable);
validateSubclassScope(classSubclassScope, isThunk, nullptr);
Expand Down
4 changes: 0 additions & 4 deletions lib/SIL/IR/SILFunctionBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,6 @@ SILFunction *SILFunctionBuilder::getOrCreateFunction(
}
addFunctionAttributes(F, decl->getAttrs(), mod, getOrCreateDeclaration,
constant);

if (auto *funcDecl = dyn_cast<AbstractFunctionDecl>(decl)) {
F->setAsync(funcDecl->hasAsync());
}
}

return F;
Expand Down
52 changes: 28 additions & 24 deletions lib/SIL/IR/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ CanSILFunctionType SILFunctionType::getUnsubstitutedType(SILModule &M) const {
: CanGenericSignature();
return SILFunctionType::get(signature,
getExtInfo(),
isAsync(),
getCoroutineKind(),
getCalleeConvention(),
params, yields, results, errorResult,
Expand Down Expand Up @@ -287,11 +288,11 @@ SILFunctionType::getWithDifferentiability(DifferentiabilityKind kind,
}
auto newExtInfo =
getExtInfo().intoBuilder().withDifferentiabilityKind(kind).build();
return get(getInvocationGenericSignature(), newExtInfo, getCoroutineKind(),
getCalleeConvention(), newParameters, getYields(), newResults,
getOptionalErrorResult(), getPatternSubstitutions(),
getInvocationSubstitutions(), getASTContext(),
getWitnessMethodConformanceOrInvalid());
return get(getInvocationGenericSignature(), newExtInfo, isAsync(),
getCoroutineKind(), getCalleeConvention(), newParameters,
getYields(), newResults, getOptionalErrorResult(),
getPatternSubstitutions(), getInvocationSubstitutions(),
getASTContext(), getWitnessMethodConformanceOrInvalid());
}

CanSILFunctionType SILFunctionType::getWithoutDifferentiability() {
Expand All @@ -311,9 +312,9 @@ CanSILFunctionType SILFunctionType::getWithoutDifferentiability() {
newResults.push_back(result.getWithDifferentiability(
SILResultDifferentiability::DifferentiableOrNotApplicable));
return SILFunctionType::get(
getInvocationGenericSignature(), nondiffExtInfo, getCoroutineKind(),
getCalleeConvention(), newParams, getYields(), newResults,
getOptionalErrorResult(), getPatternSubstitutions(),
getInvocationGenericSignature(), nondiffExtInfo, isAsync(),
getCoroutineKind(), getCalleeConvention(), newParams, getYields(),
newResults, getOptionalErrorResult(), getPatternSubstitutions(),
getInvocationSubstitutions(), getASTContext());
}

Expand Down Expand Up @@ -502,9 +503,9 @@ static CanSILFunctionType getAutoDiffDifferentialType(
llvm::makeArrayRef(substConformances));
}
return SILFunctionType::get(
GenericSignature(), SILFunctionType::ExtInfo(), SILCoroutineKind::None,
ParameterConvention::Direct_Guaranteed, differentialParams, {},
differentialResults, None, substitutions,
GenericSignature(), SILFunctionType::ExtInfo(), /*isAsync*/ false,
SILCoroutineKind::None, ParameterConvention::Direct_Guaranteed,
differentialParams, {}, differentialResults, None, substitutions,
/*invocationSubstitutions*/ SubstitutionMap(), ctx);
}

Expand Down Expand Up @@ -681,9 +682,9 @@ static CanSILFunctionType getAutoDiffPullbackType(
llvm::makeArrayRef(substConformances));
}
return SILFunctionType::get(
GenericSignature(), SILFunctionType::ExtInfo(), SILCoroutineKind::None,
ParameterConvention::Direct_Guaranteed, pullbackParams, {},
pullbackResults, None, substitutions,
GenericSignature(), SILFunctionType::ExtInfo(), /*isAsync*/ false,
SILCoroutineKind::None, ParameterConvention::Direct_Guaranteed,
pullbackParams, {}, pullbackResults, None, substitutions,
/*invocationSubstitutions*/ SubstitutionMap(), ctx);
}

Expand Down Expand Up @@ -737,7 +738,7 @@ static SILFunctionType *getConstrainedAutoDiffOriginalFunctionType(
constrainedInvocationGenSig->areAllParamsConcrete()
? GenericSignature()
: constrainedInvocationGenSig,
original->getExtInfo(), original->getCoroutineKind(),
original->getExtInfo(), original->isAsync(), original->getCoroutineKind(),
original->getCalleeConvention(), newParameters, original->getYields(),
newResults, original->getOptionalErrorResult(),
/*patternSubstitutions*/ SubstitutionMap(),
Expand Down Expand Up @@ -828,6 +829,7 @@ CanSILFunctionType SILFunctionType::getAutoDiffDerivativeFunctionType(
// cache and return.
cachedResult = SILFunctionType::get(
constrainedOriginalFnTy->getSubstGenericSignature(), extInfo,
constrainedOriginalFnTy->isAsync(),
constrainedOriginalFnTy->getCoroutineKind(),
constrainedOriginalFnTy->getCalleeConvention(), newParameters,
constrainedOriginalFnTy->getYields(), newResults,
Expand Down Expand Up @@ -913,9 +915,9 @@ CanSILFunctionType SILFunctionType::getAutoDiffTransposeFunctionType(
for (auto &res : getResults())
newParameters.push_back(getParameterInfoForOriginalResult(res));
return SILFunctionType::get(
getInvocationGenericSignature(), getExtInfo(), getCoroutineKind(),
getCalleeConvention(), newParameters, getYields(), newResults,
getOptionalErrorResult(), getPatternSubstitutions(),
getInvocationGenericSignature(), getExtInfo(), isAsync(),
getCoroutineKind(), getCalleeConvention(), newParameters, getYields(),
newResults, getOptionalErrorResult(), getPatternSubstitutions(),
/*invocationSubstitutions*/ {}, getASTContext());
}

Expand Down Expand Up @@ -989,7 +991,8 @@ Lowering::adjustFunctionType(CanSILFunctionType type,
return type;

return SILFunctionType::get(type->getInvocationGenericSignature(),
extInfo, type->getCoroutineKind(), callee,
extInfo, type->isAsync(),
type->getCoroutineKind(), callee,
type->getParameters(), type->getYields(),
type->getResults(),
type->getOptionalErrorResult(),
Expand All @@ -1016,9 +1019,9 @@ CanSILFunctionType SILFunctionType::getWithExtInfo(ExtInfo newExt) {
: Lowering::DefaultThickCalleeConvention)
: ParameterConvention::Direct_Unowned);

return get(getInvocationGenericSignature(), newExt, getCoroutineKind(),
calleeConvention, getParameters(), getYields(), getResults(),
getOptionalErrorResult(), getPatternSubstitutions(),
return get(getInvocationGenericSignature(), newExt, isAsync(),
getCoroutineKind(), calleeConvention, getParameters(), getYields(),
getResults(), getOptionalErrorResult(), getPatternSubstitutions(),
getInvocationSubstitutions(), getASTContext(),
getWitnessMethodConformanceOrInvalid());
}
Expand Down Expand Up @@ -2166,7 +2169,8 @@ static CanSILFunctionType getSILFunctionType(
}
}

return SILFunctionType::get(genericSig, silExtInfo, coroutineKind,
return SILFunctionType::get(genericSig, silExtInfo,
substFnInterfaceType->isAsync(), coroutineKind,
calleeConvention, inputs, yields,
results, errorResult,
substitutions, SubstitutionMap(),
Expand Down Expand Up @@ -3756,7 +3760,7 @@ class SILTypeSubstituter :
? origType->getInvocationGenericSignature()
: nullptr;

return SILFunctionType::get(genericSig, extInfo,
return SILFunctionType::get(genericSig, extInfo, origType->isAsync(),
origType->getCoroutineKind(),
origType->getCalleeConvention(), substParams,
substYields, substResults, substErrorResult,
Expand Down
3 changes: 0 additions & 3 deletions lib/SIL/IR/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2598,9 +2598,6 @@ void SILFunction::print(SILPrintContext &PrintCtx) const {
if (isWithoutActuallyEscapingThunk())
OS << "[without_actually_escaping] ";

if (isAsync())
OS << "[async] ";

switch (getSpecialPurpose()) {
case SILFunction::Purpose::None:
break;
Expand Down
5 changes: 3 additions & 2 deletions lib/SIL/IR/SILType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,10 @@ TypeBase::replaceSubstitutedSILFunctionTypesWithUnsubstituted(SILModule &M) cons

if (!didChange)
return sft;

return SILFunctionType::get(sft->getInvocationGenericSignature(),
sft->getExtInfo(), sft->getCoroutineKind(),
sft->getExtInfo(), sft->isAsync(),
sft->getCoroutineKind(),
sft->getCalleeConvention(),
newParams, newYields, newResults,
newErrorResult,
Expand Down
Loading