Skip to content

Commit 4b440a1

Browse files
authored
Merge pull request #72916 from meg-gupta/lifetimedepreq
Requestify LifetimeDependenceInfo
2 parents 3bb25de + afd2052 commit 4b440a1

30 files changed

+191
-211
lines changed

SwiftCompilerSources/Sources/SIL/FunctionConvention.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,8 @@ extension FunctionConvention {
271271
return nil
272272
}
273273

274-
// In Sema's LifetimeDependenceInfo, 'self' is always index zero,
275-
// whether it exists or not. In SILFunctionType, 'self' is the
276-
// last parameter if it exists.
277274
private func bridgedIndex(parameterIndex: Int) -> Int {
278-
if hasSelfParam, parameterIndex == (paramCount - 1) {
279-
return 0
280-
}
281-
return parameterIndex + 1
275+
return parameterIndex
282276
}
283277

284278
public var description: String {

include/swift/AST/Decl.h

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

73027302
/// Add the given derivative function configuration.
73037303
void addDerivativeFunctionConfiguration(const AutoDiffConfig &config);
7304+
std::optional<LifetimeDependenceInfo> getLifetimeDependenceInfo() const;
73047305

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

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7898,7 +7898,7 @@ ERROR(lifetime_dependence_invalid_return_type, none,
78987898
ERROR(lifetime_dependence_cannot_infer_ambiguous_candidate, none,
78997899
"cannot infer lifetime dependence %0, multiple parameters qualifiy as a candidate", (StringRef))
79007900
ERROR(lifetime_dependence_cannot_infer_no_candidates, none,
7901-
"cannot infer lifetime dependence %0, no parameters found that are "
7901+
"cannot infer lifetime dependence %0, no parameters found that are either "
79027902
"~Escapable or Escapable with a borrowing ownership", (StringRef))
79037903
ERROR(lifetime_dependence_ctor_non_self_or_nil_return, none,
79047904
"expected nil or self as return values in an initializer with "

include/swift/AST/LifetimeDependence.h

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,26 @@ class LifetimeDependenceSpecifier {
133133
class LifetimeDependenceInfo {
134134
IndexSubset *inheritLifetimeParamIndices;
135135
IndexSubset *scopeLifetimeParamIndices;
136-
bool isExplicit;
137136

138137
static LifetimeDependenceInfo getForParamIndex(AbstractFunctionDecl *afd,
139138
unsigned index,
140139
LifetimeDependenceKind kind);
141140

141+
/// Builds LifetimeDependenceInfo from a swift decl
142+
static std::optional<LifetimeDependenceInfo>
143+
fromTypeRepr(AbstractFunctionDecl *afd);
144+
145+
/// Infer LifetimeDependenceInfo
146+
static std::optional<LifetimeDependenceInfo> infer(AbstractFunctionDecl *afd);
147+
142148
public:
143149
LifetimeDependenceInfo()
144150
: inheritLifetimeParamIndices(nullptr),
145-
scopeLifetimeParamIndices(nullptr), isExplicit(false) {}
151+
scopeLifetimeParamIndices(nullptr) {}
146152
LifetimeDependenceInfo(IndexSubset *inheritLifetimeParamIndices,
147-
IndexSubset *scopeLifetimeParamIndices,
148-
bool isExplicit = false)
153+
IndexSubset *scopeLifetimeParamIndices)
149154
: inheritLifetimeParamIndices(inheritLifetimeParamIndices),
150-
scopeLifetimeParamIndices(scopeLifetimeParamIndices),
151-
isExplicit(isExplicit) {
155+
scopeLifetimeParamIndices(scopeLifetimeParamIndices) {
152156
assert(!empty());
153157
assert(!inheritLifetimeParamIndices ||
154158
!inheritLifetimeParamIndices->isEmpty());
@@ -162,8 +166,6 @@ class LifetimeDependenceInfo {
162166
scopeLifetimeParamIndices == nullptr;
163167
}
164168

165-
bool isExplicitlySpecified() const { return isExplicit; }
166-
167169
bool hasInheritLifetimeParamIndices() const {
168170
return inheritLifetimeParamIndices != nullptr;
169171
}
@@ -191,27 +193,17 @@ class LifetimeDependenceInfo {
191193
/// Builds LifetimeDependenceInfo from a swift decl, either from the explicit
192194
/// lifetime dependence specifiers or by inference based on types and
193195
/// ownership modifiers.
194-
static std::optional<LifetimeDependenceInfo>
195-
get(AbstractFunctionDecl *decl, Type resultType, bool allowIndex = false);
196+
static std::optional<LifetimeDependenceInfo> get(AbstractFunctionDecl *decl);
196197

197198
/// Builds LifetimeDependenceInfo from the bitvectors passes as parameters.
198199
static LifetimeDependenceInfo
199200
get(ASTContext &ctx, const SmallBitVector &inheritLifetimeIndices,
200201
const SmallBitVector &scopeLifetimeIndices);
201202

202-
/// Builds LifetimeDependenceInfo from a swift decl
203-
static std::optional<LifetimeDependenceInfo>
204-
fromTypeRepr(AbstractFunctionDecl *afd, Type resultType, bool allowIndex);
205-
206203
/// Builds LifetimeDependenceInfo from SIL
207204
static std::optional<LifetimeDependenceInfo>
208205
fromTypeRepr(LifetimeDependentReturnTypeRepr *lifetimeDependentRepr,
209-
SmallVectorImpl<SILParameterInfo> &params, bool hasSelfParam,
210-
DeclContext *dc);
211-
212-
/// Infer LifetimeDependenceInfo
213-
static std::optional<LifetimeDependenceInfo> infer(AbstractFunctionDecl *afd,
214-
Type resultType);
206+
SmallVectorImpl<SILParameterInfo> &params, DeclContext *dc);
215207
};
216208

217209
} // namespace swift

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/ASTMangler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3094,7 +3094,7 @@ void ASTMangler::appendFunctionSignature(AnyFunctionType *fn,
30943094
if (afd->hasImplicitSelfDecl()) {
30953095
auto lifetimeDependenceKind =
30963096
fn->getLifetimeDependenceInfo().getLifetimeDependenceOnParam(
3097-
/*paramIndex*/ 0);
3097+
/*selfIndex*/ afd->getParameters()->size());
30983098
if (lifetimeDependenceKind) {
30993099
appendLifetimeDependenceKind(*lifetimeDependenceKind,
31003100
/*isSelfDependence*/ true);
@@ -3183,7 +3183,7 @@ void ASTMangler::appendFunctionInputType(
31833183
Identifier(), type,
31843184
getParameterFlagsForMangling(param.getParameterFlags(),
31853185
defaultSpecifier),
3186-
lifetimeDependenceInfo.getLifetimeDependenceOnParam(/*paramIndex*/ 1),
3186+
lifetimeDependenceInfo.getLifetimeDependenceOnParam(/*paramIndex*/ 0),
31873187
sig, nullptr);
31883188
break;
31893189
}
@@ -3195,7 +3195,7 @@ void ASTMangler::appendFunctionInputType(
31953195

31963196
default:
31973197
bool isFirstParam = true;
3198-
unsigned paramIndex = 1; /* 0 is reserved for self*/
3198+
unsigned paramIndex = 0;
31993199
for (auto &param : params) {
32003200
// Note that we pass `nullptr` as the `forDecl` argument, since the type
32013201
// of the input is no longer directly the type of the declaration, so we

lib/AST/Decl.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10020,6 +10020,18 @@ void AbstractFunctionDecl::addDerivativeFunctionConfiguration(
1002010020
DerivativeFunctionConfigs->insert(config);
1002110021
}
1002210022

10023+
std::optional<LifetimeDependenceInfo>
10024+
AbstractFunctionDecl::getLifetimeDependenceInfo() const {
10025+
if (!isa<FuncDecl>(this) && !isa<ConstructorDecl>(this)) {
10026+
return std::nullopt;
10027+
}
10028+
10029+
return evaluateOrDefault(
10030+
getASTContext().evaluator,
10031+
LifetimeDependenceInfoRequest{const_cast<AbstractFunctionDecl *>(this)},
10032+
std::nullopt);
10033+
}
10034+
1002310035
void FuncDecl::setResultInterfaceType(Type type) {
1002410036
getASTContext().evaluator.cacheOutput(ResultTypeRequest{this},
1002510037
std::move(type));

0 commit comments

Comments
 (0)