diff --git a/include/swift/AST/Module.h b/include/swift/AST/Module.h index bcfabeda77447..e4f878f1742ef 100644 --- a/include/swift/AST/Module.h +++ b/include/swift/AST/Module.h @@ -244,8 +244,11 @@ class ModuleDecl : public DeclContext, public TypeDecl { declaredCrossImports; std::unique_ptr Cache; + +public: SourceLookupCache &getSourceLookupCache() const; +private: /// Tracks the file that will generate the module's entry point, either /// because it contains a class marked with \@UIApplicationMain /// or \@NSApplicationMain, or because it is a script file. diff --git a/include/swift/AST/SourceFile.h b/include/swift/AST/SourceFile.h index fa3bee62f5245..ec776ae072490 100644 --- a/include/swift/AST/SourceFile.h +++ b/include/swift/AST/SourceFile.h @@ -210,11 +210,7 @@ class SourceFile final : public FileUnit { public: /// Appends the given declaration to the end of the top-level decls list. Do /// not add any additional uses of this function. - void addTopLevelDecl(Decl *d) { - // Force decl parsing if we haven't already. - (void)getTopLevelDecls(); - Decls->push_back(d); - } + void addTopLevelDecl(Decl *d); /// Prepends a declaration to the top-level decls list. /// @@ -394,8 +390,6 @@ class SourceFile final : public FileUnit { void cacheVisibleDecls(SmallVectorImpl &&globals) const; const SmallVectorImpl &getCachedVisibleDecls() const; - void addVisibleDecl(ValueDecl *decl); - virtual void lookupValue(DeclName name, NLKind lookupKind, SmallVectorImpl &result) const override; diff --git a/lib/AST/Module.cpp b/lib/AST/Module.cpp index d071cab0d6843..c779298e4255b 100644 --- a/lib/AST/Module.cpp +++ b/lib/AST/Module.cpp @@ -156,7 +156,10 @@ class swift::SourceLookupCache { } }; +public: ValueDeclMap TopLevelValues; + +private: ValueDeclMap ClassMembers; bool MemberCachePopulated = false; @@ -216,6 +219,8 @@ class swift::SourceLookupCache { DeclName name, SmallVectorImpl &results); + void addTopLevelDecl(ValueDecl *decl); + SmallVector AllVisibleValues; }; SourceLookupCache &SourceFile::getCache() const { @@ -322,6 +327,10 @@ SourceLookupCache::SourceLookupCache(const ModuleDecl &M) { } } +void SourceLookupCache::addTopLevelDecl(ValueDecl *decl) { + TopLevelValues.add(decl); +} + void SourceLookupCache::lookupValue(DeclName Name, NLKind LookupKind, SmallVectorImpl &Result) { auto I = TopLevelValues.find(Name); @@ -2232,9 +2241,15 @@ SourceFile::getCachedVisibleDecls() const { return getCache().AllVisibleValues; } -void SourceFile::addVisibleDecl(ValueDecl *decl) { - Decls->push_back(decl); - getCache().AllVisibleValues.push_back(decl); +void SourceFile::addTopLevelDecl(Decl *d) { + // Force decl parsing if we haven't already. + (void)getTopLevelDecls(); + Decls->push_back(d); + // Update caches. + if (auto *valueDecl = dyn_cast(d)) { + getCache().TopLevelValues.add(valueDecl); + getParentModule()->getSourceLookupCache().TopLevelValues.add(valueDecl); + } } static void performAutoImport( diff --git a/lib/SILOptimizer/Utils/Differentiation/LinearMapInfo.cpp b/lib/SILOptimizer/Utils/Differentiation/LinearMapInfo.cpp index bb0032fa00069..555e7392e9c5f 100644 --- a/lib/SILOptimizer/Utils/Differentiation/LinearMapInfo.cpp +++ b/lib/SILOptimizer/Utils/Differentiation/LinearMapInfo.cpp @@ -159,7 +159,7 @@ EnumDecl *LinearMapInfo::createBranchingTraceDecl( computeAccessLevel(branchingTraceDecl, original->getEffectiveSymbolLinkage()); branchingTraceDecl->getInterfaceType(); assert(branchingTraceDecl->hasInterfaceType()); - file.addVisibleDecl(branchingTraceDecl); + file.addTopLevelDecl(branchingTraceDecl); // Add basic block enum cases. for (auto *predBB : originalBB->getPredecessorBlocks()) { auto bbId = "bb" + std::to_string(predBB->getDebugID()); @@ -233,7 +233,7 @@ LinearMapInfo::createLinearMapStruct(SILBasicBlock *originalBB, computeAccessLevel(linearMapStruct, original->getEffectiveSymbolLinkage()); linearMapStruct->getInterfaceType(); assert(linearMapStruct->hasInterfaceType()); - file.addVisibleDecl(linearMapStruct); + file.addTopLevelDecl(linearMapStruct); return linearMapStruct; } diff --git a/test/AutoDiff/compiler_crashers/tf1232-autodiff-generated-declaration-mangling.swift b/test/AutoDiff/compiler_crashers_fixed/tf1232-autodiff-generated-declaration-mangling.swift similarity index 96% rename from test/AutoDiff/compiler_crashers/tf1232-autodiff-generated-declaration-mangling.swift rename to test/AutoDiff/compiler_crashers_fixed/tf1232-autodiff-generated-declaration-mangling.swift index 2189f4f454934..61bc8ac3abdcc 100644 --- a/test/AutoDiff/compiler_crashers/tf1232-autodiff-generated-declaration-mangling.swift +++ b/test/AutoDiff/compiler_crashers_fixed/tf1232-autodiff-generated-declaration-mangling.swift @@ -1,4 +1,4 @@ -// RUN: not %target-build-swift -g %s +// RUN: %target-build-swift -g %s // REQUIRES: asserts // TF-1232: IRGenDebugInfo crash due to lack of proper mangling for