Skip to content

Commit 21bf781

Browse files
authored
Merge pull request swiftlang#32049 from xymus/spi-unique-imports
[ModuleInterface] Print the SPI attribute only once per group and import
2 parents 8c201f0 + 8fefdec commit 21bf781

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
@@ -646,8 +646,9 @@ class ModuleDecl : public DeclContext, public TypeDecl {
646646

647647
/// Find all SPI names imported from \p importedModule by this module,
648648
/// collecting the identifiers in \p spiGroups.
649-
void lookupImportedSPIGroups(const ModuleDecl *importedModule,
650-
SmallVectorImpl<Identifier> &spiGroups) const;
649+
void lookupImportedSPIGroups(
650+
const ModuleDecl *importedModule,
651+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const;
651652

652653
/// \sa getImportedModules
653654
enum class ImportFilterKind {

include/swift/AST/SourceFile.h

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

344345
// Is \p targetDecl accessible as an explictly imported SPI from this file?
345346
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
@@ -368,8 +368,9 @@ class SerializedASTFile final : public LoadedFile {
368368
SmallVectorImpl<AbstractFunctionDecl *> &results) const override;
369369

370370
virtual void
371-
lookupImportedSPIGroups(const ModuleDecl *importedModule,
372-
SmallVectorImpl<Identifier> &spiGroups) const override;
371+
lookupImportedSPIGroups(
372+
const ModuleDecl *importedModule,
373+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const override;
373374

374375
Optional<CommentInfo> getCommentForDecl(const Decl *D) const override;
375376

lib/AST/Module.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,9 @@ void ModuleDecl::lookupObjCMethods(
648648
FORWARD(lookupObjCMethods, (selector, results));
649649
}
650650

651-
void ModuleDecl::lookupImportedSPIGroups(const ModuleDecl *importedModule,
652-
SmallVectorImpl<Identifier> &spiGroups) const {
651+
void ModuleDecl::lookupImportedSPIGroups(
652+
const ModuleDecl *importedModule,
653+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const {
653654
FORWARD(lookupImportedSPIGroups, (importedModule, spiGroups));
654655
}
655656

@@ -1933,31 +1934,29 @@ bool SourceFile::isImportedImplementationOnly(const ModuleDecl *module) const {
19331934
return !imports.isImportedBy(module, getParentModule());
19341935
}
19351936

1936-
void SourceFile::lookupImportedSPIGroups(const ModuleDecl *importedModule,
1937-
SmallVectorImpl<Identifier> &spiGroups) const {
1937+
void SourceFile::lookupImportedSPIGroups(
1938+
const ModuleDecl *importedModule,
1939+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const {
19381940
for (auto &import : *Imports) {
19391941
if (import.importOptions.contains(ImportFlags::SPIAccessControl) &&
19401942
importedModule == import.module.importedModule) {
19411943
auto importedSpis = import.spiGroups;
1942-
spiGroups.append(importedSpis.begin(), importedSpis.end());
1944+
spiGroups.insert(importedSpis.begin(), importedSpis.end());
19431945
}
19441946
}
19451947
}
19461948

19471949
bool SourceFile::isImportedAsSPI(const ValueDecl *targetDecl) const {
19481950
auto targetModule = targetDecl->getModuleContext();
1949-
SmallVector<Identifier, 4> importedSPIGroups;
1951+
llvm::SmallSetVector<Identifier, 4> importedSPIGroups;
19501952
lookupImportedSPIGroups(targetModule, importedSPIGroups);
19511953
if (importedSPIGroups.empty()) return false;
19521954

19531955
auto declSPIGroups = targetDecl->getSPIGroups();
19541956

1955-
// Note: If we reach a point where there are many SPI imports or SPI groups
1956-
// on decls we could optimize this further by using a set.
1957-
for (auto importedSPI : importedSPIGroups)
1958-
for (auto declSPI : declSPIGroups)
1959-
if (importedSPI == declSPI)
1960-
return true;
1957+
for (auto declSPI : declSPIGroups)
1958+
if (importedSPIGroups.count(declSPI))
1959+
return true;
19611960

19621961
return false;
19631962
}

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
@@ -2663,13 +2663,14 @@ void ModuleFile::lookupObjCMethods(
26632663
}
26642664
}
26652665

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

lib/Serialization/ModuleFile.h

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

813813
/// Find all SPI names imported from \p importedModule by this module,
814814
/// collecting the identifiers in \p spiGroups.
815-
void lookupImportedSPIGroups(const ModuleDecl *importedModule,
816-
SmallVectorImpl<Identifier> &spiGroups) const;
815+
void lookupImportedSPIGroups(
816+
const ModuleDecl *importedModule,
817+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const;
817818

818819
/// Reports all link-time dependencies.
819820
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
@@ -1176,8 +1176,9 @@ void SerializedASTFile::lookupObjCMethods(
11761176
File.lookupObjCMethods(selector, results);
11771177
}
11781178

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

0 commit comments

Comments
 (0)