Skip to content

[AST] Use cached deserialized decl in getOpaqueResultTypeDecl #77345

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
Nov 5, 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
6 changes: 5 additions & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2758,6 +2758,10 @@ class ValueDecl : public Decl {
/// output a null pointer.
unsigned noDynamicallyReplacedDecl : 1;

/// Whether the OpaqueResultTypeRequest request was evaluated and produced
/// a null pointer.
unsigned noOpaqueResultType : 1;

/// Whether the "isFinal" bit has been computed yet.
unsigned isFinalComputed : 1;

Expand Down Expand Up @@ -2785,7 +2789,7 @@ class ValueDecl : public Decl {
friend class InterfaceTypeRequest;
friend class CheckRedeclarationRequest;
friend class ActorIsolationRequest;
friend class DynamicallyReplacedDeclRequest;
friend class OpaqueResultTypeRequest;
friend class ApplyAccessNoteRequest;

friend class Decl;
Expand Down
13 changes: 7 additions & 6 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -2295,21 +2295,22 @@ class IsABICompatibleOverrideRequest

/// Builds an opaque result type for a declaration.
class OpaqueResultTypeRequest
: public SimpleRequest<OpaqueResultTypeRequest,
OpaqueTypeDecl *(ValueDecl *),
RequestFlags::Cached> {
: public SimpleRequest<
OpaqueResultTypeRequest, OpaqueTypeDecl *(ValueDecl *),
RequestFlags::SeparatelyCached | RequestFlags::SplitCached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

OpaqueTypeDecl *
evaluate(Evaluator &evaluator, ValueDecl *VD) const;
OpaqueTypeDecl *evaluate(Evaluator &evaluator, ValueDecl *VD) const;

public:
// Caching.
// Split caching.
bool isCached() const { return true; }
std::optional<OpaqueTypeDecl *> getCachedResult() const;
void cacheResult(OpaqueTypeDecl *result) const;
};

/// Determines if a function declaration is 'static'.
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ SWIFT_REQUEST(TypeChecker, OpaqueReadOwnershipRequest,
NoLocationInfo)
SWIFT_REQUEST(TypeChecker, OpaqueResultTypeRequest,
OpaqueTypeDecl *(ValueDecl *),
Cached, NoLocationInfo)
SeparatelyCached | SplitCached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, OperatorPrecedenceGroupRequest,
PrecedenceGroupDecl *(PrecedenceGroupDecl *),
Cached, NoLocationInfo)
Expand Down
15 changes: 0 additions & 15 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3887,21 +3887,6 @@ TypeRepr *ValueDecl::getOpaqueResultTypeRepr() const {
}

OpaqueTypeDecl *ValueDecl::getOpaqueResultTypeDecl() const {
if (getOpaqueResultTypeRepr() == nullptr) {
if (!isa<VarDecl>(this) &&
!isa<FuncDecl>(this) &&
!isa<SubscriptDecl>(this))
return nullptr;
auto file = cast<FileUnit>(getDeclContext()->getModuleScopeContext());
// Don't look up when the decl is from source, otherwise a cycle will happen.
if (file->getKind() == FileUnitKind::SerializedAST) {
Mangle::ASTMangler mangler;
auto name = mangler.mangleOpaqueTypeDecl(this);
return file->lookupOpaqueResultType(name);
}
return nullptr;
}

return evaluateOrDefault(getASTContext().evaluator,
OpaqueResultTypeRequest{const_cast<ValueDecl *>(this)},
nullptr);
Expand Down
22 changes: 22 additions & 0 deletions lib/AST/TypeCheckRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,28 @@ void DynamicallyReplacedDeclRequest::cacheResult(ValueDecl *result) const {
decl->getASTContext().evaluator.cacheNonEmptyOutput(*this, std::move(result));
}

//----------------------------------------------------------------------------//
// OpaqueResultTypeRequest caching.
//----------------------------------------------------------------------------//

std::optional<OpaqueTypeDecl *>
OpaqueResultTypeRequest::getCachedResult() const {
auto *decl = std::get<0>(getStorage());
if (decl->LazySemanticInfo.noOpaqueResultType)
return std::optional(nullptr);

return decl->getASTContext().evaluator.getCachedNonEmptyOutput(*this);
}

void OpaqueResultTypeRequest::cacheResult(OpaqueTypeDecl *result) const {
auto *decl = std::get<0>(getStorage());
if (!result) {
decl->LazySemanticInfo.noOpaqueResultType = 1;
return;
}
decl->getASTContext().evaluator.cacheNonEmptyOutput(*this, std::move(result));
}

//----------------------------------------------------------------------------//
// ApplyAccessNoteRequest computation.
//----------------------------------------------------------------------------//
Expand Down
4 changes: 3 additions & 1 deletion lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ OpaqueTypeDecl *
OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
ValueDecl *originatingDecl) const {
auto *repr = originatingDecl->getOpaqueResultTypeRepr();
assert(repr && "Declaration does not have an opaque result type");
if (!repr)
return nullptr;

auto *dc = originatingDecl->getInnermostDeclContext();
auto &ctx = dc->getASTContext();

Expand Down
55 changes: 32 additions & 23 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3936,14 +3936,17 @@ class DeclDeserializer {
if (var->hasStorage())
AddAttribute(new (ctx) HasStorageAttr(/*isImplicit:*/true));

if (opaqueReturnTypeID) {
auto opaqueReturnType = MF.getDeclChecked(opaqueReturnTypeID);
if (!opaqueReturnType)
return opaqueReturnType.takeError();

ctx.evaluator.cacheOutput(
OpaqueResultTypeRequest{var},
cast<OpaqueTypeDecl>(opaqueReturnType.get()));
{
OpaqueTypeDecl *opaqueDecl = nullptr;
if (opaqueReturnTypeID) {
auto opaqueReturnType = MF.getDeclChecked(opaqueReturnTypeID);
if (!opaqueReturnType)
return opaqueReturnType.takeError();

opaqueDecl = cast<OpaqueTypeDecl>(opaqueReturnType.get());
}
ctx.evaluator.cacheOutput(OpaqueResultTypeRequest{var},
std::move(opaqueDecl));
}

// If this is a lazy property, record its backing storage.
Expand Down Expand Up @@ -4357,14 +4360,17 @@ class DeclDeserializer {
fn->setIsObjC(isObjC);
fn->setForcedStaticDispatch(hasForcedStaticDispatch);

if (opaqueReturnTypeID) {
auto declOrError = MF.getDeclChecked(opaqueReturnTypeID);
if (!declOrError)
return declOrError.takeError();
{
OpaqueTypeDecl *opaqueDecl = nullptr;
if (opaqueReturnTypeID) {
auto declOrError = MF.getDeclChecked(opaqueReturnTypeID);
if (!declOrError)
return declOrError.takeError();

ctx.evaluator.cacheOutput(
OpaqueResultTypeRequest{fn},
cast<OpaqueTypeDecl>(declOrError.get()));
opaqueDecl = cast<OpaqueTypeDecl>(declOrError.get());
}
ctx.evaluator.cacheOutput(OpaqueResultTypeRequest{fn},
std::move(opaqueDecl));
}

if (!isAccessor)
Expand Down Expand Up @@ -5204,16 +5210,19 @@ class DeclDeserializer {
subscript->setOverriddenDecl(cast_or_null<SubscriptDecl>(overridden.get()));
if (subscript->getOverriddenDecl())
AddAttribute(new (ctx) OverrideAttr(SourceLoc()));

if (opaqueReturnTypeID) {
Decl *opaqueReturnType;
UNWRAP(MF.getDeclChecked(opaqueReturnTypeID), opaqueReturnType);

ctx.evaluator.cacheOutput(
OpaqueResultTypeRequest{subscript},
cast<OpaqueTypeDecl>(opaqueReturnType));
{
OpaqueTypeDecl *opaqueDecl = nullptr;
if (opaqueReturnTypeID) {
Decl *opaqueReturnType;
UNWRAP(MF.getDeclChecked(opaqueReturnTypeID), opaqueReturnType);

opaqueDecl = cast<OpaqueTypeDecl>(opaqueReturnType);
}
ctx.evaluator.cacheOutput(OpaqueResultTypeRequest{subscript},
std::move(opaqueDecl));
}

return subscript;
}

Expand Down