Skip to content

Commit 709676c

Browse files
committed
[Dependency Scanning] Make GlobalModuleDependenciesCache aware of the current scanning action's target triple
And only resolve cached dependencies that came from scanning actions with the same target triple. This change means that the `GlobalModuleDependenciesCache` must be configured with a specific target triple for every scannig action, and it will only resolve previously-found dependencies from previous scannig actions using the exact same triple. Furthermore, the `GlobalModuleDependenciesCache` separately tracks source-file-based module dependencies as those represent main Swift modules of previous scanning actions, and we must be able to resolve those regardless of the target triple. Resolves rdar://83105455
1 parent e64a404 commit 709676c

File tree

8 files changed

+582
-391
lines changed

8 files changed

+582
-391
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 106 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class Identifier;
3838
enum class ModuleDependenciesKind : int8_t {
3939
FirstKind,
4040
SwiftInterface = FirstKind,
41-
SwiftSource,
4241
SwiftBinary,
4342
// Placeholder dependencies are a kind of dependencies used only by the
4443
// dependency scanner. They are swift modules that the scanner will not be
@@ -64,7 +63,8 @@ enum class ModuleDependenciesKind : int8_t {
6463
// of all targets, individually, have been computed.
6564
SwiftPlaceholder,
6665
Clang,
67-
LastKind = Clang + 1
66+
SwiftSource,
67+
LastKind = SwiftSource + 1
6868
};
6969

7070
struct ModuleDependenciesKindHash {
@@ -98,6 +98,25 @@ class ModuleDependenciesStorageBase {
9898
std::vector<std::string> moduleDependencies;
9999
};
100100

101+
struct CommonSwiftTextualModuleDependencyDetails {
102+
CommonSwiftTextualModuleDependencyDetails(ArrayRef<StringRef> extraPCMArgs)
103+
: extraPCMArgs(extraPCMArgs.begin(), extraPCMArgs.end()) {}
104+
105+
/// To build a PCM to be used by this Swift module, we need to append these
106+
/// arguments to the generic PCM build arguments reported from the dependency
107+
/// graph.
108+
const std::vector<std::string> extraPCMArgs;
109+
110+
/// Bridging header file, if there is one.
111+
Optional<std::string> bridgingHeaderFile;
112+
113+
/// Source files on which the bridging header depends.
114+
std::vector<std::string> bridgingSourceFiles;
115+
116+
/// (Clang) modules on which the bridging header depends.
117+
std::vector<std::string> bridgingModuleDependencies;
118+
};
119+
101120
/// Describes the dependencies of a Swift module described by an Swift interface file.
102121
///
103122
/// This class is mostly an implementation detail for \c ModuleDependencies.
@@ -114,28 +133,14 @@ class SwiftInterfaceModuleDependenciesStorage :
114133
/// interface.
115134
const std::vector<std::string> buildCommandLine;
116135

117-
/// To build a PCM to be used by this Swift module, we need to append these
118-
/// arguments to the generic PCM build arguments reported from the dependency
119-
/// graph.
120-
const std::vector<std::string> extraPCMArgs;
121-
122136
/// The hash value that will be used for the generated module
123137
const std::string contextHash;
124138

125139
/// A flag that indicates this dependency is a framework
126140
const bool isFramework;
127141

128-
/// Bridging header file, if there is one.
129-
Optional<std::string> bridgingHeaderFile;
130-
131-
/// Swift source files that are part of the Swift module, when known.
132-
std::vector<std::string> sourceFiles;
133-
134-
/// Source files on which the bridging header depends.
135-
std::vector<std::string> bridgingSourceFiles;
136-
137-
/// (Clang) modules on which the bridging header depends.
138-
std::vector<std::string> bridgingModuleDependencies;
142+
/// Details common to Swift textual (interface or source) modules
143+
CommonSwiftTextualModuleDependencyDetails textualModuleDetails;
139144

140145
SwiftInterfaceModuleDependenciesStorage(
141146
const std::string swiftInterfaceFile,
@@ -149,8 +154,9 @@ class SwiftInterfaceModuleDependenciesStorage :
149154
compiledModuleCandidates(compiledModuleCandidates.begin(),
150155
compiledModuleCandidates.end()),
151156
buildCommandLine(buildCommandLine.begin(), buildCommandLine.end()),
152-
extraPCMArgs(extraPCMArgs.begin(), extraPCMArgs.end()),
153-
contextHash(contextHash), isFramework(isFramework) { }
157+
contextHash(contextHash), isFramework(isFramework),
158+
textualModuleDetails(extraPCMArgs)
159+
{}
154160

155161
ModuleDependenciesStorageBase *clone() const override {
156162
return new SwiftInterfaceModuleDependenciesStorage(*this);
@@ -167,36 +173,26 @@ class SwiftInterfaceModuleDependenciesStorage :
167173
class SwiftSourceModuleDependenciesStorage :
168174
public ModuleDependenciesStorageBase {
169175
public:
170-
/// To build a PCM to be used by this Swift module, we need to append these
171-
/// arguments to the generic PCM build arguments reported from the dependency
172-
/// graph.
173-
const std::vector<std::string> extraPCMArgs;
174-
175-
/// Bridging header file, if there is one.
176-
Optional<std::string> bridgingHeaderFile;
177176

178177
/// Swift source files that are part of the Swift module, when known.
179178
std::vector<std::string> sourceFiles;
180179

181-
/// Source files on which the bridging header depends.
182-
std::vector<std::string> bridgingSourceFiles;
180+
/// Details common to Swift textual (interface or source) modules
181+
CommonSwiftTextualModuleDependencyDetails textualModuleDetails;
183182

184-
/// (Clang) modules on which the bridging header depends.
185-
std::vector<std::string> bridgingModuleDependencies;
183+
SwiftSourceModuleDependenciesStorage(
184+
ArrayRef<StringRef> extraPCMArgs
185+
) : ModuleDependenciesStorageBase(ModuleDependenciesKind::SwiftSource),
186+
textualModuleDetails(extraPCMArgs) {}
186187

187-
SwiftSourceModuleDependenciesStorage(
188-
ArrayRef<StringRef> extraPCMArgs
189-
) : ModuleDependenciesStorageBase(ModuleDependenciesKind::SwiftSource),
190-
extraPCMArgs(extraPCMArgs.begin(), extraPCMArgs.end()) {}
191-
192-
ModuleDependenciesStorageBase *clone() const override {
193-
return new SwiftSourceModuleDependenciesStorage(*this);
194-
}
188+
ModuleDependenciesStorageBase *clone() const override {
189+
return new SwiftSourceModuleDependenciesStorage(*this);
190+
}
195191

196-
static bool classof(const ModuleDependenciesStorageBase *base) {
197-
return base->dependencyKind == ModuleDependenciesKind::SwiftSource;
198-
}
199-
};
192+
static bool classof(const ModuleDependenciesStorageBase *base) {
193+
return base->dependencyKind == ModuleDependenciesKind::SwiftSource;
194+
}
195+
};
200196

201197
/// Describes the dependencies of a pre-built Swift module (with no .swiftinterface).
202198
///
@@ -462,6 +458,14 @@ class ModuleDependencies {
462458

463459
using ModuleDependencyID = std::pair<std::string, ModuleDependenciesKind>;
464460
using ModuleDependenciesVector = llvm::SmallVector<ModuleDependencies, 1>;
461+
using ModuleDependenciesKindMap =
462+
std::unordered_map<ModuleDependenciesKind,
463+
llvm::StringMap<ModuleDependenciesVector>,
464+
ModuleDependenciesKindHash>;
465+
using ModuleDependenciesKindRefMap =
466+
std::unordered_map<ModuleDependenciesKind,
467+
llvm::StringMap<const ModuleDependencies *>,
468+
ModuleDependenciesKindHash>;
465469

466470
/// A cache describing the set of module dependencies that has been queried
467471
/// thus far. This cache records/stores the actual Dependency values and can be
@@ -473,18 +477,35 @@ using ModuleDependenciesVector = llvm::SmallVector<ModuleDependencies, 1>;
473477
/// ensure that the returned cached dependency was one that can be found in the
474478
/// current scanning action's filesystem view.
475479
class GlobalModuleDependenciesCache {
476-
/// All cached module dependencies, in the order in which they were
477-
/// encountered.
478-
std::vector<ModuleDependencyID> AllModules;
479-
480-
/// Dependencies for modules that have already been computed.
481-
/// This maps a dependency kind to a map of a module's name to a vector of Dependency objects,
482-
/// which correspond to instances of the same module that may have been found
483-
/// in different sets of search paths.
484-
std::unordered_map<ModuleDependenciesKind,
485-
llvm::StringMap<ModuleDependenciesVector>,
486-
ModuleDependenciesKindHash>
487-
ModuleDependenciesKindMap;
480+
/// Global cache contents specific to a target-triple specified on a scanner invocation
481+
struct TargetSpecificGlobalCacheState {
482+
/// All cached module dependencies, in the order in which they were
483+
/// encountered.
484+
std::vector<ModuleDependencyID> AllModules;
485+
486+
/// Dependencies for modules that have already been computed.
487+
/// This maps a dependency kind to a map of a module's name to a vector of Dependency objects,
488+
/// which correspond to instances of the same module that may have been found
489+
/// in different sets of search paths.
490+
ModuleDependenciesKindMap ModuleDependenciesMap;
491+
};
492+
493+
/// All cached Swift source module dependencies, in the order in which they were encountered
494+
std::vector<ModuleDependencyID> AllSourceModules;
495+
496+
/// Dependencies for all Swift source-based modules discovered. Each one is the main
497+
/// module of a prior invocation of the scanner.
498+
llvm::StringMap<ModuleDependencies> SwiftSourceModuleDependenciesMap;
499+
500+
/// A map from a String representing the target triple of a scanner invocation to the corresponding
501+
/// cached dependencies discovered so far when using this triple.
502+
llvm::StringMap<std::unique_ptr<TargetSpecificGlobalCacheState>> TargetSpecificCacheMap;
503+
504+
/// The current target triple cache configuration
505+
Optional<std::string> CurrentTriple;
506+
507+
/// The triples used by scanners using this cache, in the order in which they were used
508+
std::vector<std::string> AllTriples;
488509

489510
/// Additional information needed for Clang dependency scanning.
490511
ClangModuleDependenciesCacheImpl *clangImpl = nullptr;
@@ -506,13 +527,19 @@ class GlobalModuleDependenciesCache {
506527
getDependenciesMap(ModuleDependenciesKind kind) const;
507528

508529
public:
509-
GlobalModuleDependenciesCache();
530+
GlobalModuleDependenciesCache() {};
510531
GlobalModuleDependenciesCache(const GlobalModuleDependenciesCache &) = delete;
511532
GlobalModuleDependenciesCache &
512533
operator=(const GlobalModuleDependenciesCache &) = delete;
513534

514535
virtual ~GlobalModuleDependenciesCache() { destroyClangImpl(); }
515536

537+
void configureForTriple(std::string triple);
538+
539+
const std::vector<std::string>& getAllTriples() const {
540+
return AllTriples;
541+
}
542+
516543
private:
517544
/// Enforce clients not being allowed to query this cache directly, it must be
518545
/// wrapped in an instance of `ModuleDependenciesCache`.
@@ -542,6 +569,12 @@ class GlobalModuleDependenciesCache {
542569
Optional<ModuleDependencies>
543570
findDependencies(StringRef moduleName, ModuleLookupSpecifics details) const;
544571

572+
/// Return a pointer to the target-specific cache state of the current triple configuration.
573+
TargetSpecificGlobalCacheState* getCurrentCache() const;
574+
575+
/// Return a pointer to the target-specific cache state of the specified triple configuration.
576+
TargetSpecificGlobalCacheState* getCacheForTriple(StringRef triple) const;
577+
545578
public:
546579
/// Look for module dependencies for a module with the given name.
547580
/// This method has a deliberately-obtuse name to indicate that it is not to
@@ -552,6 +585,10 @@ class GlobalModuleDependenciesCache {
552585
findAllDependenciesIrrespectiveOfSearchPaths(
553586
StringRef moduleName, Optional<ModuleDependenciesKind> kind) const;
554587

588+
/// Look for source-based module dependency details
589+
Optional<ModuleDependencies>
590+
findSourceModuleDependency(StringRef moduleName) const;
591+
555592
/// Record dependencies for the given module.
556593
const ModuleDependencies *recordDependencies(StringRef moduleName,
557594
ModuleDependencies dependencies);
@@ -560,9 +597,16 @@ class GlobalModuleDependenciesCache {
560597
const ModuleDependencies *updateDependencies(ModuleDependencyID moduleID,
561598
ModuleDependencies dependencies);
562599

563-
/// Reference the list of all module dependencies.
564-
const std::vector<ModuleDependencyID> &getAllModules() const {
565-
return AllModules;
600+
/// Reference the list of all module dependencies that are not source-based modules
601+
/// (i.e. interface dependencies, binary dependencies, clang dependencies).
602+
const std::vector<ModuleDependencyID> &getAllNonSourceModules(StringRef triple) const {
603+
auto targetSpecificCache = getCacheForTriple(triple);
604+
return targetSpecificCache->AllModules;
605+
}
606+
607+
/// Return the list of all source-based modules discovered by this cache
608+
const std::vector<ModuleDependencyID> &getAllSourceModules() const {
609+
return AllSourceModules;
566610
}
567611
};
568612

@@ -578,10 +622,7 @@ class ModuleDependenciesCache {
578622

579623
/// References to data in `globalCache` for dependencies accimulated during
580624
/// the current scanning action.
581-
std::unordered_map<ModuleDependenciesKind,
582-
llvm::StringMap<const ModuleDependencies *>,
583-
ModuleDependenciesKindHash>
584-
ModuleDependenciesKindMap;
625+
ModuleDependenciesKindRefMap ModuleDependenciesMap;
585626

586627
/// Retrieve the dependencies map that corresponds to the given dependency
587628
/// kind.
@@ -651,9 +692,8 @@ class ModuleDependenciesCache {
651692
void updateDependencies(ModuleDependencyID moduleID,
652693
ModuleDependencies dependencies);
653694

654-
/// Reference the list of all module dependencies.
655-
const std::vector<ModuleDependencyID> &getAllModules() const {
656-
return globalCache.getAllModules();
695+
const std::vector<ModuleDependencyID> &getAllSourceModules() const {
696+
return globalCache.getAllSourceModules();
657697
}
658698
};
659699

include/swift/DependencyScan/SerializedModuleDependencyCacheFormat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ using IdentifierIDArryField = llvm::BCArray<IdentifierIDField>;
5757

5858
/// Identifiers used to refer to the above arrays
5959
using FileIDArrayIDField = IdentifierIDField;
60+
using TripleIDField = IdentifierIDField;
6061
using DependencyIDArrayIDField = IdentifierIDField;
6162
using FlagIDArrayIDField = IdentifierIDField;
6263

@@ -118,6 +119,7 @@ using IdentifierArrayLayout =
118119
using ModuleInfoLayout =
119120
BCRecordLayout<MODULE_NODE, // ID
120121
IdentifierIDField, // module name
122+
TripleIDField, // target triple
121123
DependencyIDArrayIDField // directDependencies
122124
>;
123125

0 commit comments

Comments
 (0)