Skip to content

Commit bfa6c57

Browse files
committed
Requestify LifetimeDependenceInfo
Query and cache lifetime dependence info via evaluator requests
1 parent 2ee26d9 commit bfa6c57

File tree

6 files changed

+63
-55
lines changed

6 files changed

+63
-55
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7299,6 +7299,7 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
72997299

73007300
/// Add the given derivative function configuration.
73017301
void addDerivativeFunctionConfiguration(const AutoDiffConfig &config);
7302+
std::optional<LifetimeDependenceInfo> getLifetimeDependenceInfo() const;
73027303

73037304
protected:
73047305
// If a function has a body at all, we have either a parsed body AST node or

include/swift/AST/TypeCheckRequests.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4866,6 +4866,25 @@ class ImportDeclRequest
48664866
bool isCached() const { return true; }
48674867
};
48684868

4869+
class LifetimeDependenceInfoRequest
4870+
: public SimpleRequest<LifetimeDependenceInfoRequest,
4871+
std::optional<LifetimeDependenceInfo>(
4872+
AbstractFunctionDecl *),
4873+
RequestFlags::Cached> {
4874+
public:
4875+
using SimpleRequest::SimpleRequest;
4876+
4877+
private:
4878+
friend SimpleRequest;
4879+
4880+
std::optional<LifetimeDependenceInfo>
4881+
evaluate(Evaluator &evaluator, AbstractFunctionDecl *AFD) const;
4882+
4883+
public:
4884+
// Caching.
4885+
bool isCached() const { return true; }
4886+
};
4887+
48694888
#define SWIFT_TYPEID_ZONE TypeChecker
48704889
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
48714890
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,5 @@ SWIFT_REQUEST(TypeChecker, ImportDeclRequest,
566566
std::optional<AttributedImport<ImportedModule>>(
567567
const SourceFile *sf, const ModuleDecl *mod),
568568
Cached, NoLocationInfo)
569+
SWIFT_REQUEST(TypeChecker, LifetimeDependenceInfoRequest,
570+
LifetimeDependenceInfo(AbstractFunctionDecl *), Cached, NoLocationInfo)

lib/AST/Decl.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10007,6 +10007,18 @@ void AbstractFunctionDecl::addDerivativeFunctionConfiguration(
1000710007
DerivativeFunctionConfigs->insert(config);
1000810008
}
1000910009

10010+
std::optional<LifetimeDependenceInfo>
10011+
AbstractFunctionDecl::getLifetimeDependenceInfo() const {
10012+
if (!isa<FuncDecl>(this) && !isa<ConstructorDecl>(this)) {
10013+
return std::nullopt;
10014+
}
10015+
10016+
return evaluateOrDefault(
10017+
getASTContext().evaluator,
10018+
LifetimeDependenceInfoRequest{const_cast<AbstractFunctionDecl *>(this)},
10019+
std::nullopt);
10020+
}
10021+
1001010022
void FuncDecl::setResultInterfaceType(Type type) {
1001110023
getASTContext().evaluator.cacheOutput(ResultTypeRequest{this},
1001210024
std::move(type));

lib/Sema/TypeCheckDecl.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,7 +2550,7 @@ InterfaceTypeRequest::evaluate(Evaluator &eval, ValueDecl *D) const {
25502550
resultTy = TupleType::getEmpty(AFD->getASTContext());
25512551
}
25522552

2553-
std::optional<LifetimeDependenceInfo> lifetimeDependenceInfo;
2553+
auto lifetimeDependenceInfo = AFD->getLifetimeDependenceInfo();
25542554

25552555
// (Args...) -> Result
25562556
Type funcTy;
@@ -2571,7 +2571,6 @@ InterfaceTypeRequest::evaluate(Evaluator &eval, ValueDecl *D) const {
25712571
infoBuilder = infoBuilder.withTransferringResult();
25722572
}
25732573

2574-
lifetimeDependenceInfo = LifetimeDependenceInfo::get(AFD, resultTy);
25752574
if (lifetimeDependenceInfo.has_value()) {
25762575
infoBuilder =
25772576
infoBuilder.withLifetimeDependenceInfo(*lifetimeDependenceInfo);
@@ -3098,3 +3097,18 @@ ImplicitKnownProtocolConformanceRequest::evaluate(Evaluator &evaluator,
30983097
llvm_unreachable("non-implicitly derived KnownProtocol");
30993098
}
31003099
}
3100+
3101+
std::optional<LifetimeDependenceInfo>
3102+
LifetimeDependenceInfoRequest::evaluate(Evaluator &evaluator,
3103+
AbstractFunctionDecl *decl) const {
3104+
Type resultTy;
3105+
if (auto fn = dyn_cast<FuncDecl>(decl)) {
3106+
resultTy = fn->getResultInterfaceType();
3107+
} else if (auto ctor = dyn_cast<ConstructorDecl>(decl)) {
3108+
resultTy = ctor->getResultInterfaceType();
3109+
} else {
3110+
return std::nullopt;
3111+
}
3112+
3113+
return LifetimeDependenceInfo::get(decl, resultTy);
3114+
}

lib/Serialization/Deserialization.cpp

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,14 +3644,12 @@ class DeclDeserializer {
36443644
assert(bodyParams && "missing parameters for constructor");
36453645
ctor->setParameters(bodyParams);
36463646

3647-
SmallVector<LifetimeDependenceSpecifier> specifierList;
3648-
if (MF.maybeReadLifetimeDependenceSpecifier(specifierList,
3649-
bodyParams->size())) {
3650-
auto SelfType = ctor->getDeclaredInterfaceType();
3651-
auto typeRepr = new (ctx) FixedTypeRepr(SelfType, SourceLoc());
3652-
auto lifetimeTypeRepr =
3653-
LifetimeDependentReturnTypeRepr::create(ctx, typeRepr, specifierList);
3654-
ctor->setDeserializedResultTypeLoc(TypeLoc(lifetimeTypeRepr, SelfType));
3647+
auto lifetimeDependenceInfo =
3648+
MF.maybeReadLifetimeDependenceInfo(bodyParams->size());
3649+
3650+
if (lifetimeDependenceInfo.has_value()) {
3651+
ctx.evaluator.cacheOutput(LifetimeDependenceInfoRequest{ctor},
3652+
std::move(lifetimeDependenceInfo.value()));
36553653
}
36563654

36573655
if (auto errorConvention = MF.maybeReadForeignErrorConvention())
@@ -4222,13 +4220,13 @@ class DeclDeserializer {
42224220

42234221
ParameterList *paramList = MF.readParameterList();
42244222
fn->setParameters(paramList);
4225-
SmallVector<LifetimeDependenceSpecifier> specifierList;
4226-
if (MF.maybeReadLifetimeDependenceSpecifier(specifierList,
4227-
paramList->size())) {
4228-
auto typeRepr = new (ctx) FixedTypeRepr(resultType, SourceLoc());
4229-
auto lifetimeTypeRepr =
4230-
LifetimeDependentReturnTypeRepr::create(ctx, typeRepr, specifierList);
4231-
fn->setDeserializedResultTypeLoc(TypeLoc(lifetimeTypeRepr, resultType));
4223+
4224+
auto lifetimeDependenceInfo =
4225+
MF.maybeReadLifetimeDependenceInfo(paramList->size());
4226+
4227+
if (lifetimeDependenceInfo.has_value()) {
4228+
ctx.evaluator.cacheOutput(LifetimeDependenceInfoRequest{fn},
4229+
std::move(lifetimeDependenceInfo.value()));
42324230
}
42334231

42344232
if (auto errorConvention = MF.maybeReadForeignErrorConvention())
@@ -8829,41 +8827,3 @@ ModuleFile::maybeReadLifetimeDependenceInfo(unsigned numParams) {
88298827
? IndexSubset::get(ctx, scopeLifetimeParamIndices)
88308828
: nullptr);
88318829
}
8832-
8833-
bool ModuleFile::maybeReadLifetimeDependenceSpecifier(
8834-
SmallVectorImpl<LifetimeDependenceSpecifier> &specifierList,
8835-
unsigned numDeclParams) {
8836-
using namespace decls_block;
8837-
8838-
SmallVector<uint64_t, 8> scratch;
8839-
if (!maybeReadLifetimeDependenceRecord(scratch)) {
8840-
return false;
8841-
}
8842-
8843-
bool hasInheritLifetimeParamIndices;
8844-
bool hasScopeLifetimeParamIndices;
8845-
ArrayRef<uint64_t> lifetimeDependenceData;
8846-
LifetimeDependenceLayout::readRecord(scratch, hasInheritLifetimeParamIndices,
8847-
hasScopeLifetimeParamIndices,
8848-
lifetimeDependenceData);
8849-
8850-
unsigned startIndex = 0;
8851-
auto pushData = [&](ParsedLifetimeDependenceKind kind) {
8852-
for (unsigned i = 0; i < numDeclParams + 1; i++) {
8853-
if (lifetimeDependenceData[startIndex + i]) {
8854-
specifierList.push_back(
8855-
LifetimeDependenceSpecifier::getOrderedLifetimeDependenceSpecifier(
8856-
SourceLoc(), kind, i));
8857-
}
8858-
}
8859-
startIndex += numDeclParams + 1;
8860-
};
8861-
8862-
if (hasInheritLifetimeParamIndices) {
8863-
pushData(ParsedLifetimeDependenceKind::Inherit);
8864-
}
8865-
if (hasScopeLifetimeParamIndices) {
8866-
pushData(ParsedLifetimeDependenceKind::Scope);
8867-
}
8868-
return true;
8869-
}

0 commit comments

Comments
 (0)