Skip to content

Commit 67cf190

Browse files
authored
Merge pull request #66428 from DougGregor/remove-lazy-member-lookup-5.9
[5.9] Eliminate non-lazy name lookup
2 parents 223596b + b0f387b commit 67cf190

21 files changed

+46
-294
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,6 @@ namespace swift {
333333
/// Flags for developers
334334
///
335335

336-
/// Enable named lazy member loading.
337-
bool NamedLazyMemberLoading = true;
338-
339336
/// Whether to record request references for incremental builds.
340337
bool RecordRequestReferences = true;
341338

lib/AST/NameLookup.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,9 @@ class swift::MemberLookupTable : public ASTAllocated<swift::MemberLookupTable> {
12181218
/// Lookup table mapping names to the set of declarations with that name.
12191219
LookupTable Lookup;
12201220

1221+
/// List of containers that have lazily-loaded members
1222+
llvm::SmallVector<ExtensionDecl *, 2> ExtensionsWithLazyMembers;
1223+
12211224
/// The set of names of lazily-loaded members that the lookup table has a
12221225
/// complete accounting of with respect to all known extensions of its
12231226
/// parent nominal type.
@@ -1246,6 +1249,14 @@ class swift::MemberLookupTable : public ASTAllocated<swift::MemberLookupTable> {
12461249
/// Add the given members to the lookup table.
12471250
void addMembers(DeclRange members);
12481251

1252+
void addExtensionWithLazyMembers(ExtensionDecl *ext) {
1253+
ExtensionsWithLazyMembers.push_back(ext);
1254+
}
1255+
1256+
ArrayRef<ExtensionDecl *> getExtensionsWithLazyMembers() const {
1257+
return ExtensionsWithLazyMembers;
1258+
}
1259+
12491260
/// Returns \c true if the lookup table has a complete accounting of the
12501261
/// given name.
12511262
bool isLazilyComplete(DeclBaseName name) const {
@@ -1435,10 +1446,11 @@ void NominalTypeDecl::addedExtension(ExtensionDecl *ext) {
14351446
auto *table = LookupTable.getPointer();
14361447
assert(table);
14371448

1438-
if (ext->hasLazyMembers()) {
1449+
if (ext->wasDeserialized() || ext->hasClangNode()) {
14391450
table->addMembers(ext->getCurrentMembersWithoutLoading());
14401451
table->clearLazilyCompleteCache();
14411452
table->clearLazilyCompleteForMacroExpansionCache();
1453+
table->addExtensionWithLazyMembers(ext);
14421454
} else {
14431455
table->addMembers(ext->getMembers());
14441456
}
@@ -1534,6 +1546,9 @@ populateLookupTableEntryFromLazyIDCLoader(ASTContext &ctx,
15341546
MemberLookupTable &LookupTable,
15351547
DeclBaseName name,
15361548
IterableDeclContext *IDC) {
1549+
if (!IDC->hasLazyMembers())
1550+
return;
1551+
15371552
auto ci = ctx.getOrCreateLazyIterableContextData(IDC,
15381553
/*lazyLoader=*/nullptr);
15391554
auto res = ci->loader->loadNamedMembers(IDC, name, ci->memberData);
@@ -1553,7 +1568,7 @@ populateLookupTableEntryFromExtensions(ASTContext &ctx,
15531568
assert(!table.isLazilyComplete(name) &&
15541569
"Should not be searching extensions for complete name!");
15551570

1556-
for (auto e : nominal->getExtensions()) {
1571+
for (auto e : table.getExtensionsWithLazyMembers()) {
15571572
// If there's no lazy members to look at, all the members of this extension
15581573
// are present in the lookup table.
15591574
if (!e->hasLazyMembers()) {
@@ -1789,6 +1804,7 @@ void NominalTypeDecl::prepareLookupTable() {
17891804
// LazyMemberLoader::loadNamedMembers().
17901805
if (e->wasDeserialized() || e->hasClangNode()) {
17911806
table->addMembers(e->getCurrentMembersWithoutLoading());
1807+
table->addExtensionWithLazyMembers(e);
17921808
continue;
17931809
}
17941810

@@ -1848,21 +1864,14 @@ DirectLookupRequest::evaluate(Evaluator &evaluator,
18481864
const auto flags = desc.Options;
18491865
auto *decl = desc.DC;
18501866

1851-
// We only use NamedLazyMemberLoading when a user opts-in and we have
1852-
// not yet loaded all the members into the IDC list in the first place.
18531867
ASTContext &ctx = decl->getASTContext();
1854-
const bool useNamedLazyMemberLoading = (ctx.LangOpts.NamedLazyMemberLoading &&
1855-
decl->hasLazyMembers());
18561868
const bool includeAttrImplements =
18571869
flags.contains(NominalTypeDecl::LookupDirectFlags::IncludeAttrImplements);
18581870
const bool excludeMacroExpansions =
18591871
flags.contains(NominalTypeDecl::LookupDirectFlags::ExcludeMacroExpansions);
18601872

18611873
LLVM_DEBUG(llvm::dbgs() << decl->getNameStr() << ".lookupDirect("
18621874
<< name << ")"
1863-
<< ", hasLazyMembers()=" << decl->hasLazyMembers()
1864-
<< ", useNamedLazyMemberLoading="
1865-
<< useNamedLazyMemberLoading
18661875
<< ", excludeMacroExpansions="
18671876
<< excludeMacroExpansions
18681877
<< "\n");
@@ -1875,15 +1884,7 @@ DirectLookupRequest::evaluate(Evaluator &evaluator,
18751884
decl->prepareExtensions();
18761885

18771886
auto &Table = *decl->getLookupTable();
1878-
if (!useNamedLazyMemberLoading) {
1879-
// Make sure we have the complete list of members (in this nominal and in
1880-
// all extensions).
1881-
(void)decl->getMembers();
1882-
1883-
for (auto E : decl->getExtensions())
1884-
(void)E->getMembers();
1885-
1886-
} else if (!Table.isLazilyComplete(name.getBaseName())) {
1887+
if (!Table.isLazilyComplete(name.getBaseName())) {
18871888
DeclBaseName baseName(name.getBaseName());
18881889

18891890
if (isa_and_nonnull<clang::NamespaceDecl>(decl->getClangDecl())) {

lib/ClangImporter/ClangImporter.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4992,7 +4992,8 @@ DeclAttributes cloneImportedAttributes(ValueDecl *decl, ASTContext &context) {
49924992
return attrs;
49934993
}
49944994

4995-
ValueDecl *cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext) {
4995+
static ValueDecl *
4996+
cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext) {
49964997
if (auto fn = dyn_cast<FuncDecl>(decl)) {
49974998
// TODO: function templates are specialized during type checking so to
49984999
// support these we need to tell Swift to type check the synthesized bodies.
@@ -6282,16 +6283,23 @@ Decl *ClangImporter::importDeclDirectly(const clang::NamedDecl *decl) {
62826283
return Impl.importDecl(decl, Impl.CurrentVersion);
62836284
}
62846285

6285-
ValueDecl *ClangImporter::importBaseMemberDecl(ValueDecl *decl,
6286-
DeclContext *newContext) {
6286+
ValueDecl *ClangImporter::Implementation::importBaseMemberDecl(
6287+
ValueDecl *decl, DeclContext *newContext) {
62876288
// Make sure we don't clone the decl again for this class, as that would
62886289
// result in multiple definitions of the same symbol.
62896290
std::pair<ValueDecl *, DeclContext *> key = {decl, newContext};
6290-
if (!Impl.clonedBaseMembers.count(key)) {
6291+
auto known = clonedBaseMembers.find(key);
6292+
if (known == clonedBaseMembers.end()) {
62916293
ValueDecl *cloned = cloneBaseMemberDecl(decl, newContext);
6292-
Impl.clonedBaseMembers[key] = cloned;
6294+
known = clonedBaseMembers.insert({key, cloned}).first;
62936295
}
6294-
return Impl.clonedBaseMembers[key];
6296+
6297+
return known->second;
6298+
}
6299+
6300+
ValueDecl *ClangImporter::importBaseMemberDecl(ValueDecl *decl,
6301+
DeclContext *newContext) {
6302+
return Impl.importBaseMemberDecl(decl, newContext);
62956303
}
62966304

62976305
void ClangImporter::diagnoseTopLevelValue(const DeclName &name) {

lib/ClangImporter/ImportDecl.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3570,6 +3570,12 @@ namespace {
35703570
if (!dc)
35713571
return nullptr;
35723572

3573+
// While importing the DeclContext, we might have imported the decl
3574+
// itself.
3575+
auto known = Impl.importDeclCached(decl, getVersion());
3576+
if (known.has_value())
3577+
return known.value();
3578+
35733579
// TODO: do we want to emit a diagnostic here?
35743580
// Types that are marked as foreign references cannot be stored by value.
35753581
if (auto recordType =
@@ -8750,8 +8756,6 @@ static void loadAllMembersOfSuperclassIfNeeded(ClassDecl *CD) {
87508756
E->loadAllMembers();
87518757
}
87528758

8753-
ValueDecl *cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext);
8754-
87558759
void ClangImporter::Implementation::loadAllMembersOfRecordDecl(
87568760
NominalTypeDecl *swiftDecl, const clang::RecordDecl *clangRecord) {
87578761
// Import all of the members.
@@ -8782,7 +8786,7 @@ void ClangImporter::Implementation::loadAllMembersOfRecordDecl(
87828786
// This means we found a member in a C++ record's base class.
87838787
if (swiftDecl->getClangDecl() != clangRecord) {
87848788
// So we need to clone the member into the derived class.
8785-
if (auto newDecl = cloneBaseMemberDecl(cast<ValueDecl>(member), swiftDecl)) {
8789+
if (auto newDecl = importBaseMemberDecl(cast<ValueDecl>(member), swiftDecl)) {
87868790
swiftDecl->addMember(newDecl);
87878791
}
87888792
continue;

lib/ClangImporter/ImporterImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,14 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
642642
llvm::MapVector<std::pair<NominalTypeDecl *, Type>,
643643
std::pair<FuncDecl *, FuncDecl *>> cxxSubscripts;
644644

645+
private:
645646
// Keep track of the decls that were already cloned for this specific class.
646647
llvm::DenseMap<std::pair<ValueDecl *, DeclContext *>, ValueDecl *>
647648
clonedBaseMembers;
648649

650+
ValueDecl *importBaseMemberDecl(ValueDecl *decl, DeclContext *newContext);
651+
652+
public:
649653
// Cache for already-specialized function templates and any thunks they may
650654
// have.
651655
llvm::DenseMap<clang::FunctionDecl *, ValueDecl *>

lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
649649
/*default*/ false);
650650
Opts.UseClangFunctionTypes |= Args.hasArg(OPT_use_clang_function_types);
651651

652-
Opts.NamedLazyMemberLoading &= !Args.hasArg(OPT_disable_named_lazy_member_loading);
653-
654652
if (Args.hasArg(OPT_emit_fine_grained_dependency_sourcefile_dot_files))
655653
Opts.EmitFineGrainedDependencySourcefileDotFiles = true;
656654

test/ClangImporter/attr-swift_name-errors.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -I %S/Inputs/custom-modules -typecheck %s -disable-named-lazy-member-loading \
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -I %S/Inputs/custom-modules -typecheck %s \
22
// RUN: -verify -verify-additional-file %S/Inputs/custom-modules/ConflictingNames.h -verify-ignore-unknown
33

44
// REQUIRES: objc_interop

test/ClangImporter/attr-swift_name.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t.mcp)
2-
// RUN: not %target-swift-frontend(mock-sdk: %clang-importer-sdk) -I %S/Inputs/custom-modules -Xcc -w -typecheck %s -module-cache-path %t.mcp -disable-named-lazy-member-loading 2>&1 | %FileCheck %s
2+
// RUN: not %target-swift-frontend(mock-sdk: %clang-importer-sdk) -I %S/Inputs/custom-modules -Xcc -w -typecheck %s -module-cache-path %t.mcp 2>&1 | %FileCheck %s
33

44
// REQUIRES: objc_interop
55

@@ -26,12 +26,6 @@ func test(_ i: Int) {
2626
// CHECK: note: please report this issue to the owners of 'ObjCIRExtras'
2727
// CHECK-NOT: warning:
2828

29-
// CHECK: warning: too few parameters in swift_name attribute (expected 2; got 1)
30-
// CHECK: + (instancetype)testW:(id)x out:(id *)outObject SWIFT_NAME(ww(_:));
31-
// CHECK-NOT: warning:
32-
// CHECK: note: please report this issue to the owners of 'ObjCIRExtras'
33-
// CHECK-NOT: warning:
34-
3529
// CHECK: warning: cycle detected while resolving 'CircularName' in swift_name attribute for 'CircularName'
3630
// CHECK: SWIFT_NAME(CircularName.Inner) @interface CircularName : NSObject @end
3731
// CHECK-NOT: {{warning|note}}:

test/NameLookup/named_lazy_member_loading_objc_category.swift

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/NameLookup/named_lazy_member_loading_objc_interface.swift

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)