Skip to content

Commit ab9176f

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents 7af6a24 + 16fbfec commit ab9176f

23 files changed

+194
-583
lines changed

include/swift/AST/AvailabilityDomain.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ class AvailabilityDomain final {
153153

154154
/// If `decl` represents an availability domain, returns the corresponding
155155
/// `AvailabilityDomain` value. Otherwise, returns `std::nullopt`.
156-
static std::optional<AvailabilityDomain> forCustom(ValueDecl *decl,
157-
const ASTContext &ctx);
156+
static std::optional<AvailabilityDomain> forCustom(ValueDecl *decl);
158157

159158
static AvailabilityDomain forCustom(const CustomAvailabilityDomain *domain) {
160159
return AvailabilityDomain(domain);

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,6 +2925,10 @@ class ValueDecl : public Decl {
29252925

29262926
/// Whether we've evaluated the ApplyAccessNoteRequest.
29272927
unsigned accessNoteApplied : 1;
2928+
2929+
/// Whether the AvailabilityDomainForDeclRequest request was evaluated and
2930+
/// yielded no availability domain.
2931+
unsigned noAvailabilityDomain : 1;
29282932
} LazySemanticInfo = { };
29292933

29302934
friend class DynamicallyReplacedDeclRequest;
@@ -2938,6 +2942,7 @@ class ValueDecl : public Decl {
29382942
friend class ActorIsolationRequest;
29392943
friend class OpaqueResultTypeRequest;
29402944
friend class ApplyAccessNoteRequest;
2945+
friend class AvailabilityDomainForDeclRequest;
29412946

29422947
friend class Decl;
29432948
SourceLoc getLocFromSource() const { return NameLoc; }

include/swift/AST/TypeCheckRequests.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5424,6 +5424,26 @@ class ModuleHasTypeCheckerPerformanceHacksEnabledRequest
54245424
bool isCached() const { return true; }
54255425
};
54265426

5427+
class AvailabilityDomainForDeclRequest
5428+
: public SimpleRequest<AvailabilityDomainForDeclRequest,
5429+
std::optional<AvailabilityDomain>(ValueDecl *),
5430+
RequestFlags::Cached | RequestFlags::SplitCached> {
5431+
public:
5432+
using SimpleRequest::SimpleRequest;
5433+
5434+
private:
5435+
friend SimpleRequest;
5436+
5437+
// Evaluation.
5438+
std::optional<AvailabilityDomain> evaluate(Evaluator &evaluator,
5439+
ValueDecl *decl) const;
5440+
5441+
public:
5442+
bool isCached() const { return true; }
5443+
std::optional<std::optional<AvailabilityDomain>> getCachedResult() const;
5444+
void cacheResult(std::optional<AvailabilityDomain> domain) const;
5445+
};
5446+
54275447
#define SWIFT_TYPEID_ZONE TypeChecker
54285448
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
54295449
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,7 @@ SWIFT_REQUEST(TypeChecker, BindExtensionsForIDEInspectionRequest,
648648
SWIFT_REQUEST(TypeChecker, ModuleHasTypeCheckerPerformanceHacksEnabledRequest,
649649
bool(const ModuleDecl *),
650650
Cached, NoLocationInfo)
651+
652+
SWIFT_REQUEST(TypeChecker, AvailabilityDomainForDeclRequest,
653+
std::optional<AvailabilityDomain>(ValueDecl *),
654+
Cached | SplitCached, NoLocationInfo)

include/swift/Basic/Features.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,6 @@ EXPERIMENTAL_FEATURE(CoroutineAccessorsUnwindOnCallerError, false)
502502

503503
EXPERIMENTAL_FEATURE(AddressableParameters, true)
504504
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(AddressableTypes, true)
505-
EXPERIMENTAL_FEATURE(AddressableInterop, true)
506505

507506
/// Allow custom availability domains to be defined and referenced.
508507
EXPERIMENTAL_FEATURE(CustomAvailability, true)

include/swift/Runtime/Concurrent.h

Lines changed: 42 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -593,51 +593,6 @@ struct ConcurrentReadableHashMap {
593593
}
594594
}
595595

596-
// Common implementation for `getOrInsert` and `GetOrInsertManyScope`
597-
template <class KeyTy, typename Call>
598-
void getOrInsertExternallyLocked(KeyTy key, const Call &call) {
599-
auto indices = IndexStorage{Indices.load(std::memory_order_relaxed)};
600-
auto indicesCapacityLog2 = indices.getCapacityLog2();
601-
auto elementCount = ElementCount.load(std::memory_order_relaxed);
602-
auto *elements = Elements.load(std::memory_order_relaxed);
603-
auto *elementsPtr = elements ? elements->data() : nullptr;
604-
605-
606-
auto found = this->find(key, indices, elementCount, elementsPtr);
607-
if (found.first) {
608-
call(found.first, false);
609-
return;
610-
}
611-
612-
auto indicesCapacity = 1UL << indicesCapacityLog2;
613-
614-
// The number of slots in use is elementCount + 1, since the capacity also
615-
// takes a slot.
616-
auto emptyCount = indicesCapacity - (elementCount + 1);
617-
auto proportion = indicesCapacity / emptyCount;
618-
if (proportion >= ResizeProportion) {
619-
indices = resize(indices, indicesCapacityLog2, elementsPtr);
620-
found = find(key, indices, elementCount, elementsPtr);
621-
assert(!found.first && "Shouldn't suddenly find the key after rehashing");
622-
}
623-
624-
if (!elements || elementCount >= elements->Capacity) {
625-
elements = resize(elements, elementCount);
626-
}
627-
auto *element = &elements->data()[elementCount];
628-
629-
// Order matters: fill out the element, then update the count,
630-
// then update the index.
631-
bool keep = call(element, true);
632-
if (keep) {
633-
assert(hash_value(key) == hash_value(*element) &&
634-
"Element must have the same hash code as its key.");
635-
ElementCount.store(elementCount + 1, std::memory_order_release);
636-
indices.storeIndexAt(&Indices, elementCount + 1, found.second,
637-
std::memory_order_release);
638-
}
639-
}
640-
641596
public:
642597
// Implicitly trivial constructor/destructor.
643598
ConcurrentReadableHashMap() = default;
@@ -729,48 +684,6 @@ struct ConcurrentReadableHashMap {
729684
return Snapshot(this, indices, elementsPtr, elementCount);
730685
}
731686

732-
/// A wrapper that allows performing several `getOrInsert` operations under
733-
/// the same lock.
734-
class GetOrInsertManyScope {
735-
GetOrInsertManyScope() = delete;
736-
GetOrInsertManyScope(const GetOrInsertManyScope &) = delete;
737-
GetOrInsertManyScope &operator=(const GetOrInsertManyScope &) = delete;
738-
GetOrInsertManyScope(GetOrInsertManyScope &&) = delete;
739-
GetOrInsertManyScope &operator=(GetOrInsertManyScope &&) = delete;
740-
741-
ConcurrentReadableHashMap &Map;
742-
743-
public:
744-
GetOrInsertManyScope(ConcurrentReadableHashMap &map) : Map(map) {
745-
Map.WriterLock.lock();
746-
}
747-
748-
~GetOrInsertManyScope() {
749-
Map.deallocateFreeListIfSafe();
750-
Map.WriterLock.unlock();
751-
}
752-
753-
/// Get an element by key, or insert a new element for that key if one is
754-
/// not already present. Invoke `call` with the pointer to the element.
755-
///
756-
/// `call` is passed the following parameters:
757-
/// - `element`: the pointer to the element corresponding to `key`
758-
/// - `created`: true if the element is newly created, false if it already
759-
/// exists
760-
/// `call` returns a `bool`. When `created` is `true`, the return values
761-
/// mean:
762-
/// - `true` the new entry is to be kept
763-
/// - `false` indicates that the new entry is discarded
764-
/// If the new entry is kept, then the new element MUST be initialized, and
765-
/// have a hash value that matches the hash value of `key`.
766-
///
767-
/// The return value is ignored when `created` is `false`.
768-
template <class KeyTy, typename Call>
769-
void getOrInsert(KeyTy key, const Call &call) {
770-
Map.getOrInsertExternallyLocked(key, call);
771-
}
772-
};
773-
774687
/// Get an element by key, or insert a new element for that key if one is not
775688
/// already present. Invoke `call` with the pointer to the element. BEWARE:
776689
/// `call` is invoked with the internal writer lock held, keep work to a
@@ -790,7 +703,48 @@ struct ConcurrentReadableHashMap {
790703
template <class KeyTy, typename Call>
791704
void getOrInsert(KeyTy key, const Call &call) {
792705
typename MutexTy::ScopedLock guard(WriterLock);
793-
getOrInsertExternallyLocked(key, call);
706+
707+
auto indices = IndexStorage{Indices.load(std::memory_order_relaxed)};
708+
auto indicesCapacityLog2 = indices.getCapacityLog2();
709+
auto elementCount = ElementCount.load(std::memory_order_relaxed);
710+
auto *elements = Elements.load(std::memory_order_relaxed);
711+
auto *elementsPtr = elements ? elements->data() : nullptr;
712+
713+
auto found = this->find(key, indices, elementCount, elementsPtr);
714+
if (found.first) {
715+
call(found.first, false);
716+
deallocateFreeListIfSafe();
717+
return;
718+
}
719+
720+
auto indicesCapacity = 1UL << indicesCapacityLog2;
721+
722+
// The number of slots in use is elementCount + 1, since the capacity also
723+
// takes a slot.
724+
auto emptyCount = indicesCapacity - (elementCount + 1);
725+
auto proportion = indicesCapacity / emptyCount;
726+
if (proportion >= ResizeProportion) {
727+
indices = resize(indices, indicesCapacityLog2, elementsPtr);
728+
found = find(key, indices, elementCount, elementsPtr);
729+
assert(!found.first && "Shouldn't suddenly find the key after rehashing");
730+
}
731+
732+
if (!elements || elementCount >= elements->Capacity) {
733+
elements = resize(elements, elementCount);
734+
}
735+
auto *element = &elements->data()[elementCount];
736+
737+
// Order matters: fill out the element, then update the count,
738+
// then update the index.
739+
bool keep = call(element, true);
740+
if (keep) {
741+
assert(hash_value(key) == hash_value(*element) &&
742+
"Element must have the same hash code as its key.");
743+
ElementCount.store(elementCount + 1, std::memory_order_release);
744+
indices.storeIndexAt(&Indices, elementCount + 1, found.second,
745+
std::memory_order_release);
746+
}
747+
794748
deallocateFreeListIfSafe();
795749
}
796750

lib/AST/AvailabilityDomain.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ getCustomDomainKind(clang::FeatureAvailKind featureAvailKind) {
3939
}
4040

4141
static const CustomAvailabilityDomain *
42-
customDomainForClangDecl(ValueDecl *decl, const ASTContext &ctx) {
42+
customDomainForClangDecl(ValueDecl *decl) {
4343
auto *clangDecl = decl->getClangDecl();
4444
ASSERT(clangDecl);
4545

@@ -57,6 +57,7 @@ customDomainForClangDecl(ValueDecl *decl, const ASTContext &ctx) {
5757
if (featureInfo.second.Kind == clang::FeatureAvailKind::None)
5858
return nullptr;
5959

60+
auto &ctx = decl->getASTContext();
6061
FuncDecl *predicate = nullptr;
6162
if (featureInfo.second.Kind == clang::FeatureAvailKind::Dynamic)
6263
predicate =
@@ -68,12 +69,13 @@ customDomainForClangDecl(ValueDecl *decl, const ASTContext &ctx) {
6869
}
6970

7071
std::optional<AvailabilityDomain>
71-
AvailabilityDomain::forCustom(ValueDecl *decl, const ASTContext &ctx) {
72+
AvailabilityDomainForDeclRequest::evaluate(Evaluator &evaluator,
73+
ValueDecl *decl) const {
7274
if (!decl)
7375
return std::nullopt;
7476

7577
if (decl->hasClangNode()) {
76-
if (auto *customDomain = customDomainForClangDecl(decl, ctx))
78+
if (auto *customDomain = customDomainForClangDecl(decl))
7779
return AvailabilityDomain::forCustom(customDomain);
7880
} else {
7981
// FIXME: [availability] Handle Swift availability domains decls.
@@ -82,6 +84,15 @@ AvailabilityDomain::forCustom(ValueDecl *decl, const ASTContext &ctx) {
8284
return std::nullopt;
8385
}
8486

87+
std::optional<AvailabilityDomain>
88+
AvailabilityDomain::forCustom(ValueDecl *decl) {
89+
if (!decl)
90+
return std::nullopt;
91+
92+
return evaluateOrDefault(decl->getASTContext().evaluator,
93+
AvailabilityDomainForDeclRequest{decl}, {});
94+
}
95+
8596
std::optional<AvailabilityDomain>
8697
AvailabilityDomain::builtinDomainForString(StringRef string,
8798
const DeclContext *declContext) {

lib/AST/FeatureSet.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ static bool usesFeatureAddressableTypes(Decl *d) {
270270
return false;
271271
}
272272

273-
UNINTERESTING_FEATURE(AddressableInterop)
274273
UNINTERESTING_FEATURE(IsolatedAny2)
275274
UNINTERESTING_FEATURE(GlobalActorIsolatedTypesUsability)
276275
UNINTERESTING_FEATURE(ObjCImplementation)

lib/AST/TypeCheckRequests.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,3 +2837,28 @@ void SemanticAvailableAttrRequest::cacheResult(
28372837
if (!value)
28382838
attr->setInvalid();
28392839
}
2840+
2841+
//----------------------------------------------------------------------------//
2842+
// AvailabilityDomainForDeclRequest computation.
2843+
//----------------------------------------------------------------------------//
2844+
2845+
std::optional<std::optional<AvailabilityDomain>>
2846+
AvailabilityDomainForDeclRequest::getCachedResult() const {
2847+
auto decl = std::get<0>(getStorage());
2848+
2849+
if (decl->LazySemanticInfo.noAvailabilityDomain)
2850+
return std::optional<AvailabilityDomain>();
2851+
return decl->getASTContext().evaluator.getCachedNonEmptyOutput(*this);
2852+
}
2853+
2854+
void AvailabilityDomainForDeclRequest::cacheResult(
2855+
std::optional<AvailabilityDomain> domain) const {
2856+
auto decl = std::get<0>(getStorage());
2857+
2858+
if (!domain) {
2859+
decl->LazySemanticInfo.noAvailabilityDomain = 1;
2860+
return;
2861+
}
2862+
2863+
decl->getASTContext().evaluator.cacheNonEmptyOutput(*this, std::move(domain));
2864+
}

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4087,7 +4087,7 @@ void ClangModuleUnit::lookupAvailabilityDomains(
40874087
if (!imported)
40884088
return;
40894089

4090-
auto customDomain = AvailabilityDomain::forCustom(imported, ctx);
4090+
auto customDomain = AvailabilityDomain::forCustom(imported);
40914091
ASSERT(customDomain);
40924092
results.push_back(*customDomain);
40934093
}

0 commit comments

Comments
 (0)