Skip to content

[6.0] Requestify LifetimeDependenceInfo #73004

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
merged 1 commit into from
Apr 16, 2024
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
8 changes: 1 addition & 7 deletions SwiftCompilerSources/Sources/SIL/FunctionConvention.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,8 @@ extension FunctionConvention {
return nil
}

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

public var description: String {
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7301,6 +7301,7 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {

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

protected:
// If a function has a body at all, we have either a parsed body AST node or
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -7880,7 +7880,7 @@ ERROR(lifetime_dependence_invalid_return_type, none,
ERROR(lifetime_dependence_cannot_infer_ambiguous_candidate, none,
"cannot infer lifetime dependence %0, multiple parameters qualifiy as a candidate", (StringRef))
ERROR(lifetime_dependence_cannot_infer_no_candidates, none,
"cannot infer lifetime dependence %0, no parameters found that are "
"cannot infer lifetime dependence %0, no parameters found that are either "
"~Escapable or Escapable with a borrowing ownership", (StringRef))
ERROR(lifetime_dependence_ctor_non_self_or_nil_return, none,
"expected nil or self as return values in an initializer with "
Expand Down
32 changes: 12 additions & 20 deletions include/swift/AST/LifetimeDependence.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,26 @@ class LifetimeDependenceSpecifier {
class LifetimeDependenceInfo {
IndexSubset *inheritLifetimeParamIndices;
IndexSubset *scopeLifetimeParamIndices;
bool isExplicit;

static LifetimeDependenceInfo getForParamIndex(AbstractFunctionDecl *afd,
unsigned index,
LifetimeDependenceKind kind);

/// Builds LifetimeDependenceInfo from a swift decl
static std::optional<LifetimeDependenceInfo>
fromTypeRepr(AbstractFunctionDecl *afd);

/// Infer LifetimeDependenceInfo
static std::optional<LifetimeDependenceInfo> infer(AbstractFunctionDecl *afd);

public:
LifetimeDependenceInfo()
: inheritLifetimeParamIndices(nullptr),
scopeLifetimeParamIndices(nullptr), isExplicit(false) {}
scopeLifetimeParamIndices(nullptr) {}
LifetimeDependenceInfo(IndexSubset *inheritLifetimeParamIndices,
IndexSubset *scopeLifetimeParamIndices,
bool isExplicit = false)
IndexSubset *scopeLifetimeParamIndices)
: inheritLifetimeParamIndices(inheritLifetimeParamIndices),
scopeLifetimeParamIndices(scopeLifetimeParamIndices),
isExplicit(isExplicit) {
scopeLifetimeParamIndices(scopeLifetimeParamIndices) {
assert(!empty());
assert(!inheritLifetimeParamIndices ||
!inheritLifetimeParamIndices->isEmpty());
Expand All @@ -162,8 +166,6 @@ class LifetimeDependenceInfo {
scopeLifetimeParamIndices == nullptr;
}

bool isExplicitlySpecified() const { return isExplicit; }

bool hasInheritLifetimeParamIndices() const {
return inheritLifetimeParamIndices != nullptr;
}
Expand Down Expand Up @@ -191,27 +193,17 @@ class LifetimeDependenceInfo {
/// Builds LifetimeDependenceInfo from a swift decl, either from the explicit
/// lifetime dependence specifiers or by inference based on types and
/// ownership modifiers.
static std::optional<LifetimeDependenceInfo>
get(AbstractFunctionDecl *decl, Type resultType, bool allowIndex = false);
static std::optional<LifetimeDependenceInfo> get(AbstractFunctionDecl *decl);

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

/// Builds LifetimeDependenceInfo from a swift decl
static std::optional<LifetimeDependenceInfo>
fromTypeRepr(AbstractFunctionDecl *afd, Type resultType, bool allowIndex);

/// Builds LifetimeDependenceInfo from SIL
static std::optional<LifetimeDependenceInfo>
fromTypeRepr(LifetimeDependentReturnTypeRepr *lifetimeDependentRepr,
SmallVectorImpl<SILParameterInfo> &params, bool hasSelfParam,
DeclContext *dc);

/// Infer LifetimeDependenceInfo
static std::optional<LifetimeDependenceInfo> infer(AbstractFunctionDecl *afd,
Type resultType);
SmallVectorImpl<SILParameterInfo> &params, DeclContext *dc);
};

} // namespace swift
Expand Down
19 changes: 19 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -4866,6 +4866,25 @@ class ImportDeclRequest
bool isCached() const { return true; }
};

class LifetimeDependenceInfoRequest
: public SimpleRequest<LifetimeDependenceInfoRequest,
std::optional<LifetimeDependenceInfo>(
AbstractFunctionDecl *),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

std::optional<LifetimeDependenceInfo>
evaluate(Evaluator &evaluator, AbstractFunctionDecl *AFD) const;

public:
// Caching.
bool isCached() const { return true; }
};

#define SWIFT_TYPEID_ZONE TypeChecker
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
#include "swift/Basic/DefineTypeIDZone.h"
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,5 @@ SWIFT_REQUEST(TypeChecker, ImportDeclRequest,
std::optional<AttributedImport<ImportedModule>>(
const SourceFile *sf, const ModuleDecl *mod),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, LifetimeDependenceInfoRequest,
LifetimeDependenceInfo(AbstractFunctionDecl *), Cached, NoLocationInfo)
6 changes: 3 additions & 3 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3089,7 +3089,7 @@ void ASTMangler::appendFunctionSignature(AnyFunctionType *fn,
if (afd->hasImplicitSelfDecl()) {
auto lifetimeDependenceKind =
fn->getLifetimeDependenceInfo().getLifetimeDependenceOnParam(
/*paramIndex*/ 0);
/*selfIndex*/ afd->getParameters()->size());
if (lifetimeDependenceKind) {
appendLifetimeDependenceKind(*lifetimeDependenceKind,
/*isSelfDependence*/ true);
Expand Down Expand Up @@ -3178,7 +3178,7 @@ void ASTMangler::appendFunctionInputType(
Identifier(), type,
getParameterFlagsForMangling(param.getParameterFlags(),
defaultSpecifier),
lifetimeDependenceInfo.getLifetimeDependenceOnParam(/*paramIndex*/ 1),
lifetimeDependenceInfo.getLifetimeDependenceOnParam(/*paramIndex*/ 0),
sig, nullptr);
break;
}
Expand All @@ -3190,7 +3190,7 @@ void ASTMangler::appendFunctionInputType(

default:
bool isFirstParam = true;
unsigned paramIndex = 1; /* 0 is reserved for self*/
unsigned paramIndex = 0;
for (auto &param : params) {
// Note that we pass `nullptr` as the `forDecl` argument, since the type
// of the input is no longer directly the type of the declaration, so we
Expand Down
12 changes: 12 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10020,6 +10020,18 @@ void AbstractFunctionDecl::addDerivativeFunctionConfiguration(
DerivativeFunctionConfigs->insert(config);
}

std::optional<LifetimeDependenceInfo>
AbstractFunctionDecl::getLifetimeDependenceInfo() const {
if (!isa<FuncDecl>(this) && !isa<ConstructorDecl>(this)) {
return std::nullopt;
}

return evaluateOrDefault(
getASTContext().evaluator,
LifetimeDependenceInfoRequest{const_cast<AbstractFunctionDecl *>(this)},
std::nullopt);
}

void FuncDecl::setResultInterfaceType(Type type) {
getASTContext().evaluator.cacheOutput(ResultTypeRequest{this},
std::move(type));
Expand Down
Loading