Skip to content

Commit 36f2f46

Browse files
authored
Merge pull request #77345 from hamishknight/opaque-cache
[AST] Use cached deserialized decl in `getOpaqueResultTypeDecl`
2 parents 03845ac + 152812c commit 36f2f46

File tree

7 files changed

+70
-47
lines changed

7 files changed

+70
-47
lines changed

include/swift/AST/Decl.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2758,6 +2758,10 @@ class ValueDecl : public Decl {
27582758
/// output a null pointer.
27592759
unsigned noDynamicallyReplacedDecl : 1;
27602760

2761+
/// Whether the OpaqueResultTypeRequest request was evaluated and produced
2762+
/// a null pointer.
2763+
unsigned noOpaqueResultType : 1;
2764+
27612765
/// Whether the "isFinal" bit has been computed yet.
27622766
unsigned isFinalComputed : 1;
27632767

@@ -2785,7 +2789,7 @@ class ValueDecl : public Decl {
27852789
friend class InterfaceTypeRequest;
27862790
friend class CheckRedeclarationRequest;
27872791
friend class ActorIsolationRequest;
2788-
friend class DynamicallyReplacedDeclRequest;
2792+
friend class OpaqueResultTypeRequest;
27892793
friend class ApplyAccessNoteRequest;
27902794

27912795
friend class Decl;

include/swift/AST/TypeCheckRequests.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,21 +2295,22 @@ class IsABICompatibleOverrideRequest
22952295

22962296
/// Builds an opaque result type for a declaration.
22972297
class OpaqueResultTypeRequest
2298-
: public SimpleRequest<OpaqueResultTypeRequest,
2299-
OpaqueTypeDecl *(ValueDecl *),
2300-
RequestFlags::Cached> {
2298+
: public SimpleRequest<
2299+
OpaqueResultTypeRequest, OpaqueTypeDecl *(ValueDecl *),
2300+
RequestFlags::SeparatelyCached | RequestFlags::SplitCached> {
23012301
public:
23022302
using SimpleRequest::SimpleRequest;
23032303

23042304
private:
23052305
friend SimpleRequest;
23062306

2307-
OpaqueTypeDecl *
2308-
evaluate(Evaluator &evaluator, ValueDecl *VD) const;
2307+
OpaqueTypeDecl *evaluate(Evaluator &evaluator, ValueDecl *VD) const;
23092308

23102309
public:
2311-
// Caching.
2310+
// Split caching.
23122311
bool isCached() const { return true; }
2312+
std::optional<OpaqueTypeDecl *> getCachedResult() const;
2313+
void cacheResult(OpaqueTypeDecl *result) const;
23132314
};
23142315

23152316
/// Determines if a function declaration is 'static'.

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ SWIFT_REQUEST(TypeChecker, OpaqueReadOwnershipRequest,
262262
NoLocationInfo)
263263
SWIFT_REQUEST(TypeChecker, OpaqueResultTypeRequest,
264264
OpaqueTypeDecl *(ValueDecl *),
265-
Cached, NoLocationInfo)
265+
SeparatelyCached | SplitCached, NoLocationInfo)
266266
SWIFT_REQUEST(TypeChecker, OperatorPrecedenceGroupRequest,
267267
PrecedenceGroupDecl *(PrecedenceGroupDecl *),
268268
Cached, NoLocationInfo)

lib/AST/Decl.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3887,21 +3887,6 @@ TypeRepr *ValueDecl::getOpaqueResultTypeRepr() const {
38873887
}
38883888

38893889
OpaqueTypeDecl *ValueDecl::getOpaqueResultTypeDecl() const {
3890-
if (getOpaqueResultTypeRepr() == nullptr) {
3891-
if (!isa<VarDecl>(this) &&
3892-
!isa<FuncDecl>(this) &&
3893-
!isa<SubscriptDecl>(this))
3894-
return nullptr;
3895-
auto file = cast<FileUnit>(getDeclContext()->getModuleScopeContext());
3896-
// Don't look up when the decl is from source, otherwise a cycle will happen.
3897-
if (file->getKind() == FileUnitKind::SerializedAST) {
3898-
Mangle::ASTMangler mangler;
3899-
auto name = mangler.mangleOpaqueTypeDecl(this);
3900-
return file->lookupOpaqueResultType(name);
3901-
}
3902-
return nullptr;
3903-
}
3904-
39053890
return evaluateOrDefault(getASTContext().evaluator,
39063891
OpaqueResultTypeRequest{const_cast<ValueDecl *>(this)},
39073892
nullptr);

lib/AST/TypeCheckRequests.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,28 @@ void DynamicallyReplacedDeclRequest::cacheResult(ValueDecl *result) const {
385385
decl->getASTContext().evaluator.cacheNonEmptyOutput(*this, std::move(result));
386386
}
387387

388+
//----------------------------------------------------------------------------//
389+
// OpaqueResultTypeRequest caching.
390+
//----------------------------------------------------------------------------//
391+
392+
std::optional<OpaqueTypeDecl *>
393+
OpaqueResultTypeRequest::getCachedResult() const {
394+
auto *decl = std::get<0>(getStorage());
395+
if (decl->LazySemanticInfo.noOpaqueResultType)
396+
return std::optional(nullptr);
397+
398+
return decl->getASTContext().evaluator.getCachedNonEmptyOutput(*this);
399+
}
400+
401+
void OpaqueResultTypeRequest::cacheResult(OpaqueTypeDecl *result) const {
402+
auto *decl = std::get<0>(getStorage());
403+
if (!result) {
404+
decl->LazySemanticInfo.noOpaqueResultType = 1;
405+
return;
406+
}
407+
decl->getASTContext().evaluator.cacheNonEmptyOutput(*this, std::move(result));
408+
}
409+
388410
//----------------------------------------------------------------------------//
389411
// ApplyAccessNoteRequest computation.
390412
//----------------------------------------------------------------------------//

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ OpaqueTypeDecl *
4242
OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
4343
ValueDecl *originatingDecl) const {
4444
auto *repr = originatingDecl->getOpaqueResultTypeRepr();
45-
assert(repr && "Declaration does not have an opaque result type");
45+
if (!repr)
46+
return nullptr;
47+
4648
auto *dc = originatingDecl->getInnermostDeclContext();
4749
auto &ctx = dc->getASTContext();
4850

lib/Serialization/Deserialization.cpp

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3936,14 +3936,17 @@ class DeclDeserializer {
39363936
if (var->hasStorage())
39373937
AddAttribute(new (ctx) HasStorageAttr(/*isImplicit:*/true));
39383938

3939-
if (opaqueReturnTypeID) {
3940-
auto opaqueReturnType = MF.getDeclChecked(opaqueReturnTypeID);
3941-
if (!opaqueReturnType)
3942-
return opaqueReturnType.takeError();
3943-
3944-
ctx.evaluator.cacheOutput(
3945-
OpaqueResultTypeRequest{var},
3946-
cast<OpaqueTypeDecl>(opaqueReturnType.get()));
3939+
{
3940+
OpaqueTypeDecl *opaqueDecl = nullptr;
3941+
if (opaqueReturnTypeID) {
3942+
auto opaqueReturnType = MF.getDeclChecked(opaqueReturnTypeID);
3943+
if (!opaqueReturnType)
3944+
return opaqueReturnType.takeError();
3945+
3946+
opaqueDecl = cast<OpaqueTypeDecl>(opaqueReturnType.get());
3947+
}
3948+
ctx.evaluator.cacheOutput(OpaqueResultTypeRequest{var},
3949+
std::move(opaqueDecl));
39473950
}
39483951

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

4360-
if (opaqueReturnTypeID) {
4361-
auto declOrError = MF.getDeclChecked(opaqueReturnTypeID);
4362-
if (!declOrError)
4363-
return declOrError.takeError();
4363+
{
4364+
OpaqueTypeDecl *opaqueDecl = nullptr;
4365+
if (opaqueReturnTypeID) {
4366+
auto declOrError = MF.getDeclChecked(opaqueReturnTypeID);
4367+
if (!declOrError)
4368+
return declOrError.takeError();
43644369

4365-
ctx.evaluator.cacheOutput(
4366-
OpaqueResultTypeRequest{fn},
4367-
cast<OpaqueTypeDecl>(declOrError.get()));
4370+
opaqueDecl = cast<OpaqueTypeDecl>(declOrError.get());
4371+
}
4372+
ctx.evaluator.cacheOutput(OpaqueResultTypeRequest{fn},
4373+
std::move(opaqueDecl));
43684374
}
43694375

43704376
if (!isAccessor)
@@ -5204,16 +5210,19 @@ class DeclDeserializer {
52045210
subscript->setOverriddenDecl(cast_or_null<SubscriptDecl>(overridden.get()));
52055211
if (subscript->getOverriddenDecl())
52065212
AddAttribute(new (ctx) OverrideAttr(SourceLoc()));
5207-
5208-
if (opaqueReturnTypeID) {
5209-
Decl *opaqueReturnType;
5210-
UNWRAP(MF.getDeclChecked(opaqueReturnTypeID), opaqueReturnType);
52115213

5212-
ctx.evaluator.cacheOutput(
5213-
OpaqueResultTypeRequest{subscript},
5214-
cast<OpaqueTypeDecl>(opaqueReturnType));
5214+
{
5215+
OpaqueTypeDecl *opaqueDecl = nullptr;
5216+
if (opaqueReturnTypeID) {
5217+
Decl *opaqueReturnType;
5218+
UNWRAP(MF.getDeclChecked(opaqueReturnTypeID), opaqueReturnType);
5219+
5220+
opaqueDecl = cast<OpaqueTypeDecl>(opaqueReturnType);
5221+
}
5222+
ctx.evaluator.cacheOutput(OpaqueResultTypeRequest{subscript},
5223+
std::move(opaqueDecl));
52155224
}
5216-
5225+
52175226
return subscript;
52185227
}
52195228

0 commit comments

Comments
 (0)