Skip to content

Commit 8fefdec

Browse files
committed
Use a SetVector when looking up the SPI attributes on imports
Using a SetVector fixes an issue where many source files imported the same SPI group from the same module, the emitted private textual interfaces superfluously repeated the `@_spi` attribute on the import. rdar://problem/63681845
1 parent 9c08c24 commit 8fefdec

File tree

11 files changed

+35
-30
lines changed

11 files changed

+35
-30
lines changed

include/swift/AST/FileUnit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ class FileUnit : public DeclContext {
107107
/// Find all SPI names imported from \p importedModule by this module,
108108
/// collecting the identifiers in \p spiGroups.
109109
virtual void lookupImportedSPIGroups(
110-
const ModuleDecl *importedModule,
111-
SmallVectorImpl<Identifier> &spiGroups) const {};
110+
const ModuleDecl *importedModule,
111+
SmallSetVector<Identifier, 4> &spiGroups) const {};
112112

113113
protected:
114114
/// Look up an operator declaration. Do not call directly, use

include/swift/AST/Module.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,9 @@ class ModuleDecl : public DeclContext, public TypeDecl {
663663

664664
/// Find all SPI names imported from \p importedModule by this module,
665665
/// collecting the identifiers in \p spiGroups.
666-
void lookupImportedSPIGroups(const ModuleDecl *importedModule,
667-
SmallVectorImpl<Identifier> &spiGroups) const;
666+
void lookupImportedSPIGroups(
667+
const ModuleDecl *importedModule,
668+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const;
668669

669670
/// \sa getImportedModules
670671
enum class ImportFilterKind {

include/swift/AST/SourceFile.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,9 @@ class SourceFile final : public FileUnit {
343343
/// Find all SPI names imported from \p importedModule by this file,
344344
/// collecting the identifiers in \p spiGroups.
345345
virtual void
346-
lookupImportedSPIGroups(const ModuleDecl *importedModule,
347-
SmallVectorImpl<Identifier> &spiGroups) const override;
346+
lookupImportedSPIGroups(
347+
const ModuleDecl *importedModule,
348+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const override;
348349

349350
// Is \p targetDecl accessible as an explictly imported SPI from this file?
350351
bool isImportedAsSPI(const ValueDecl *targetDecl) const;

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,9 @@ class SerializedASTFile final : public LoadedFile {
365365
SmallVectorImpl<AbstractFunctionDecl *> &results) const override;
366366

367367
virtual void
368-
lookupImportedSPIGroups(const ModuleDecl *importedModule,
369-
SmallVectorImpl<Identifier> &spiGroups) const override;
368+
lookupImportedSPIGroups(
369+
const ModuleDecl *importedModule,
370+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const override;
370371

371372
Optional<CommentInfo> getCommentForDecl(const Decl *D) const override;
372373

lib/AST/Module.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,9 @@ void ModuleDecl::lookupObjCMethods(
658658
FORWARD(lookupObjCMethods, (selector, results));
659659
}
660660

661-
void ModuleDecl::lookupImportedSPIGroups(const ModuleDecl *importedModule,
662-
SmallVectorImpl<Identifier> &spiGroups) const {
661+
void ModuleDecl::lookupImportedSPIGroups(
662+
const ModuleDecl *importedModule,
663+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const {
663664
FORWARD(lookupImportedSPIGroups, (importedModule, spiGroups));
664665
}
665666

@@ -2183,31 +2184,29 @@ bool SourceFile::isImportedImplementationOnly(const ModuleDecl *module) const {
21832184
return !imports.isImportedBy(module, getParentModule());
21842185
}
21852186

2186-
void SourceFile::lookupImportedSPIGroups(const ModuleDecl *importedModule,
2187-
SmallVectorImpl<Identifier> &spiGroups) const {
2187+
void SourceFile::lookupImportedSPIGroups(
2188+
const ModuleDecl *importedModule,
2189+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const {
21882190
for (auto &import : *Imports) {
21892191
if (import.importOptions.contains(ImportFlags::SPIAccessControl) &&
21902192
importedModule == import.module.importedModule) {
21912193
auto importedSpis = import.spiGroups;
2192-
spiGroups.append(importedSpis.begin(), importedSpis.end());
2194+
spiGroups.insert(importedSpis.begin(), importedSpis.end());
21932195
}
21942196
}
21952197
}
21962198

21972199
bool SourceFile::isImportedAsSPI(const ValueDecl *targetDecl) const {
21982200
auto targetModule = targetDecl->getModuleContext();
2199-
SmallVector<Identifier, 4> importedSPIGroups;
2201+
llvm::SmallSetVector<Identifier, 4> importedSPIGroups;
22002202
lookupImportedSPIGroups(targetModule, importedSPIGroups);
22012203
if (importedSPIGroups.empty()) return false;
22022204

22032205
auto declSPIGroups = targetDecl->getSPIGroups();
22042206

2205-
// Note: If we reach a point where there are many SPI imports or SPI groups
2206-
// on decls we could optimize this further by using a set.
2207-
for (auto importedSPI : importedSPIGroups)
2208-
for (auto declSPI : declSPIGroups)
2209-
if (importedSPI == declSPI)
2210-
return true;
2207+
for (auto declSPI : declSPIGroups)
2208+
if (importedSPIGroups.count(declSPI))
2209+
return true;
22112210

22122211
return false;
22132212
}

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static void printImports(raw_ostream &out,
130130

131131
// SPI attribute on imports
132132
if (Opts.PrintSPIs) {
133-
SmallVector<Identifier, 4> spis;
133+
llvm::SmallSetVector<Identifier, 4> spis;
134134
M->lookupImportedSPIGroups(importedModule, spis);
135135
for (auto spiName : spis)
136136
out << "@_spi(" << spiName << ") ";

lib/Serialization/ModuleFile.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2664,13 +2664,14 @@ void ModuleFile::lookupObjCMethods(
26642664
}
26652665
}
26662666

2667-
void ModuleFile::lookupImportedSPIGroups(const ModuleDecl *importedModule,
2668-
SmallVectorImpl<Identifier> &spiGroups) const {
2667+
void ModuleFile::lookupImportedSPIGroups(
2668+
const ModuleDecl *importedModule,
2669+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const {
26692670
for (auto &dep : Dependencies) {
26702671
auto depSpis = dep.spiGroups;
26712672
if (dep.Import.hasValue() && dep.Import->importedModule == importedModule &&
26722673
!depSpis.empty()) {
2673-
spiGroups.append(depSpis.begin(), depSpis.end());
2674+
spiGroups.insert(depSpis.begin(), depSpis.end());
26742675
}
26752676
}
26762677
}

lib/Serialization/ModuleFile.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,9 @@ class ModuleFile
816816

817817
/// Find all SPI names imported from \p importedModule by this module,
818818
/// collecting the identifiers in \p spiGroups.
819-
void lookupImportedSPIGroups(const ModuleDecl *importedModule,
820-
SmallVectorImpl<Identifier> &spiGroups) const;
819+
void lookupImportedSPIGroups(
820+
const ModuleDecl *importedModule,
821+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const;
821822

822823
/// Reports all link-time dependencies.
823824
void collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const;

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ void Serializer::writeInputBlock(const SerializationOptions &options) {
10821082
else
10831083
stableImportControl = ImportControl::ImplementationOnly;
10841084

1085-
SmallVector<Identifier, 4> spis;
1085+
llvm::SmallSetVector<Identifier, 4> spis;
10861086
M->lookupImportedSPIGroups(import.importedModule, spis);
10871087

10881088
ImportedModule.emit(ScratchRecord,

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,8 +1175,9 @@ void SerializedASTFile::lookupObjCMethods(
11751175
File.lookupObjCMethods(selector, results);
11761176
}
11771177

1178-
void SerializedASTFile::lookupImportedSPIGroups(const ModuleDecl *importedModule,
1179-
SmallVectorImpl<Identifier> &spiGroups) const {
1178+
void SerializedASTFile::lookupImportedSPIGroups(
1179+
const ModuleDecl *importedModule,
1180+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const {
11801181
File.lookupImportedSPIGroups(importedModule, spiGroups);
11811182
}
11821183

0 commit comments

Comments
 (0)