Skip to content

Commit 9d884bf

Browse files
committed
AST: Adopt split caching for RenamedDeclRequest.
In the common case where there is no renamed decl for the attribute, just set a few bits in inline storage for `AvailableAttr`.
1 parent 5de7211 commit 9d884bf

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

include/swift/AST/Attr.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,17 @@ class DeclAttribute : public AttributeBase {
147147
Value : 32
148148
);
149149

150-
SWIFT_INLINE_BITFIELD(AvailableAttr, DeclAttribute, 8+8+1+1,
150+
SWIFT_INLINE_BITFIELD(AvailableAttr, DeclAttribute, 8+8+1+1+1+1,
151151
/// A `PlatformKind` value.
152152
Platform : 8,
153153

154154
/// A `PlatformAgnosticAvailabilityKind` value.
155155
PlatformAgnostic : 8,
156156

157+
/// State storage for `RenamedDeclRequest`.
158+
HasComputedRenamedDecl : 1,
159+
HasRenamedDecl : 1,
160+
157161
/// Whether this attribute was spelled `@_spi_available`.
158162
IsSPI : 1,
159163

@@ -862,6 +866,22 @@ class AvailableAttr : public DeclAttribute {
862866
static bool classof(const DeclAttribute *DA) {
863867
return DA->getKind() == DeclAttrKind::Available;
864868
}
869+
870+
bool hasCachedRenamedDecl() const {
871+
return Bits.AvailableAttr.HasRenamedDecl;
872+
}
873+
874+
private:
875+
friend class RenamedDeclRequest;
876+
877+
bool hasComputedRenamedDecl() const {
878+
return Bits.AvailableAttr.HasComputedRenamedDecl;
879+
}
880+
881+
void setComputedRenamedDecl(bool hasRenamedDecl) {
882+
Bits.AvailableAttr.HasComputedRenamedDecl = true;
883+
Bits.AvailableAttr.HasRenamedDecl = hasRenamedDecl;
884+
}
865885
};
866886

867887
/// Indicates that the given declaration is visible to Objective-C.

include/swift/AST/TypeCheckRequests.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4085,8 +4085,9 @@ class ConditionalRequirementsRequest
40854085

40864086
class RenamedDeclRequest
40874087
: public SimpleRequest<RenamedDeclRequest,
4088-
ValueDecl *(const ValueDecl *, const AvailableAttr *),
4089-
RequestFlags::Cached> {
4088+
ValueDecl *(const ValueDecl *,
4089+
const AvailableAttr *),
4090+
RequestFlags::Cached | RequestFlags::SplitCached> {
40904091
public:
40914092
using SimpleRequest::SimpleRequest;
40924093

@@ -4098,6 +4099,8 @@ class RenamedDeclRequest
40984099

40994100
public:
41004101
bool isCached() const { return true; }
4102+
std::optional<ValueDecl *> getCachedResult() const;
4103+
void cacheResult(ValueDecl *value) const;
41014104
};
41024105

41034106
using AvailableAttrDeclPair = std::pair<const AvailableAttr *, const Decl *>;

lib/AST/Attr.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,8 @@ AvailableAttr::AvailableAttr(
22362236
INIT_VER_TUPLE(Obsoleted), ObsoletedRange(ObsoletedRange) {
22372237
Bits.AvailableAttr.Platform = static_cast<uint8_t>(Platform);
22382238
Bits.AvailableAttr.PlatformAgnostic = static_cast<uint8_t>(PlatformAgnostic);
2239+
Bits.AvailableAttr.HasComputedRenamedDecl = false;
2240+
Bits.AvailableAttr.HasRenamedDecl = false;
22392241
Bits.AvailableAttr.IsSPI = IsSPI;
22402242

22412243
if (IsForEmbedded) {

lib/AST/TypeCheckRequests.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,34 @@ void ResolveRawLayoutTypeRequest::cacheResult(Type value) const {
16221622
}
16231623
}
16241624

1625+
//----------------------------------------------------------------------------//
1626+
// RenamedDeclRequest computation.
1627+
//----------------------------------------------------------------------------//
1628+
1629+
std::optional<ValueDecl *> RenamedDeclRequest::getCachedResult() const {
1630+
auto decl = std::get<0>(getStorage());
1631+
auto attr = std::get<1>(getStorage());
1632+
1633+
if (attr->hasComputedRenamedDecl()) {
1634+
if (attr->hasCachedRenamedDecl())
1635+
return decl->getASTContext().evaluator.getCachedNonEmptyOutput(*this);
1636+
1637+
return nullptr;
1638+
}
1639+
1640+
return std::nullopt;
1641+
}
1642+
1643+
void RenamedDeclRequest::cacheResult(ValueDecl *value) const {
1644+
auto decl = std::get<0>(getStorage());
1645+
auto attr = const_cast<AvailableAttr *>(std::get<1>(getStorage()));
1646+
1647+
attr->setComputedRenamedDecl(value != nullptr);
1648+
if (value)
1649+
decl->getASTContext().evaluator.cacheNonEmptyOutput(*this,
1650+
std::move(value));
1651+
}
1652+
16251653
//----------------------------------------------------------------------------//
16261654
// TypeCheckSourceFileRequest computation.
16271655
//----------------------------------------------------------------------------//

0 commit comments

Comments
 (0)