Skip to content

Commit abda4e8

Browse files
committed
[Dependency Scanning] Key into a global (shared) dependency scanning cache via context hash
Introduces a concept of a dependency scanning action context hash, which is used to select an instance of a global dependency scanning cache which gets re-used across dependency scanning actions.
1 parent e3f572d commit abda4e8

14 files changed

+148
-88
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,12 @@ class IRGenOptions {
550550
return llvm::hash_value(0);
551551
}
552552

553+
/// Return a hash code of any components from these options that should
554+
/// contribute to a Swift Dependency Scanning hash.
555+
llvm::hash_code getModuleScanningHashComponents() const {
556+
return llvm::hash_value(0);
557+
}
558+
553559
bool hasMultipleIRGenThreads() const { return !UseSingleModuleLLVMEmission && NumThreads > 1; }
554560
bool shouldPerformIRGenerationInParallel() const { return !UseSingleModuleLLVMEmission && NumThreads != 0; }
555561
bool hasMultipleIGMs() const { return hasMultipleIRGenThreads(); }

include/swift/AST/ModuleDependencies.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,8 @@ using ModuleDependenciesKindRefMap =
501501
/// ensure that the returned cached dependency was one that can be found in the
502502
/// current scanning action's filesystem view.
503503
class GlobalModuleDependenciesCache {
504-
/// Global cache contents specific to a target-triple specified on a scanner invocation
505-
struct TargetSpecificGlobalCacheState {
504+
/// Global cache contents specific to a specific scanner invocation context
505+
struct ContextSpecificGlobalCacheState {
506506
/// All cached module dependencies, in the order in which they were
507507
/// encountered.
508508
std::vector<ModuleDependencyID> AllModules;
@@ -526,13 +526,13 @@ class GlobalModuleDependenciesCache {
526526

527527
/// A map from a String representing the target triple of a scanner invocation to the corresponding
528528
/// cached dependencies discovered so far when using this triple.
529-
llvm::StringMap<std::unique_ptr<TargetSpecificGlobalCacheState>> TargetSpecificCacheMap;
529+
llvm::StringMap<std::unique_ptr<ContextSpecificGlobalCacheState>> ContextSpecificCacheMap;
530530

531-
/// The current target triple cache configuration
532-
Optional<std::string> CurrentTriple;
531+
/// The current context hash configuration
532+
Optional<std::string> CurrentContextHash;
533533

534-
/// The triples used by scanners using this cache, in the order in which they were used
535-
std::vector<std::string> AllTriples;
534+
/// The context hashes used by scanners using this cache, in the order in which they were used
535+
std::vector<std::string> AllContextHashes;
536536

537537
/// Retrieve the dependencies map that corresponds to the given dependency
538538
/// kind.
@@ -548,10 +548,10 @@ class GlobalModuleDependenciesCache {
548548
operator=(const GlobalModuleDependenciesCache &) = delete;
549549
virtual ~GlobalModuleDependenciesCache() {}
550550

551-
void configureForTriple(std::string triple);
551+
void configureForContextHash(std::string scanningContextHash);
552552

553-
const std::vector<std::string>& getAllTriples() const {
554-
return AllTriples;
553+
const std::vector<std::string>& getAllContextHashes() const {
554+
return AllContextHashes;
555555
}
556556

557557
private:
@@ -570,11 +570,11 @@ class GlobalModuleDependenciesCache {
570570
Optional<ModuleDependencies>
571571
findDependencies(StringRef moduleName, ModuleLookupSpecifics details) const;
572572

573-
/// Return a pointer to the target-specific cache state of the current triple configuration.
574-
TargetSpecificGlobalCacheState* getCurrentCache() const;
573+
/// Return a pointer to the context-specific cache state of the current scanning action.
574+
ContextSpecificGlobalCacheState* getCurrentCache() const;
575575

576-
/// Return a pointer to the target-specific cache state of the specified triple configuration.
577-
TargetSpecificGlobalCacheState* getCacheForTriple(StringRef triple) const;
576+
/// Return a pointer to the cache state of the specified context hash.
577+
ContextSpecificGlobalCacheState* getCacheForScanningContextHash(StringRef scanningContextHash) const;
578578

579579
public:
580580
/// Look for module dependencies for a module with the given name.
@@ -600,9 +600,9 @@ class GlobalModuleDependenciesCache {
600600

601601
/// Reference the list of all module dependencies that are not source-based modules
602602
/// (i.e. interface dependencies, binary dependencies, clang dependencies).
603-
const std::vector<ModuleDependencyID> &getAllNonSourceModules(StringRef triple) const {
604-
auto targetSpecificCache = getCacheForTriple(triple);
605-
return targetSpecificCache->AllModules;
603+
const std::vector<ModuleDependencyID> &getAllNonSourceModules(StringRef scanningContextHash) const {
604+
auto contextSpecificCache = getCacheForScanningContextHash(scanningContextHash);
605+
return contextSpecificCache->AllModules;
606606
}
607607

608608
/// Return the list of all source-based modules discovered by this cache

include/swift/AST/SILOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ class SILOptions {
281281
return llvm::hash_value(0);
282282
}
283283

284+
/// Return a hash code of any components from these options that should
285+
/// contribute to a Swift Dependency Scanning hash.
286+
llvm::hash_code getModuleScanningHashComponents() const {
287+
return llvm::hash_value(0);
288+
}
289+
284290
bool shouldOptimize() const {
285291
return OptMode > OptimizationMode::NoOptimization;
286292
}

include/swift/AST/SearchPathOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,12 @@ class SearchPathOptions {
421421
RuntimeLibraryImportPaths.end()),
422422
DisableModulesValidateSystemDependencies);
423423
}
424+
425+
/// Return a hash code of any components from these options that should
426+
/// contribute to a Swift Dependency Scanning hash.
427+
llvm::hash_code getModuleScanningHashComponents() const {
428+
return getPCHHashComponents();
429+
}
424430
};
425431
}
426432

include/swift/Basic/DiagnosticOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ class DiagnosticOptions {
8686
// Nothing here that contributes anything significant when emitting the PCH.
8787
return llvm::hash_value(0);
8888
}
89+
90+
/// Return a hash code of any components from these options that should
91+
/// contribute to a Swift Dependency Scanning hash.
92+
llvm::hash_code getModuleScanningHashComponents() const {
93+
return llvm::hash_value(0);
94+
}
8995
};
9096

9197
} // end namespace swift

include/swift/Basic/LangOptions.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,12 @@ namespace swift {
628628
return llvm::hash_combine(Target.str(), OS.str());
629629
}
630630

631+
/// Return a hash code of any components from these options that should
632+
/// contribute to a Swift Dependency Scanning hash.
633+
llvm::hash_code getModuleScanningHashComponents() const {
634+
return getPCHHashComponents();
635+
}
636+
631637
private:
632638
llvm::SmallVector<std::pair<PlatformConditionKind, std::string>, 6>
633639
PlatformConditionValues;
@@ -832,6 +838,12 @@ namespace swift {
832838
EnableClangSPI);
833839
}
834840

841+
/// Return a hash code of any components from these options that should
842+
/// contribute to a Swift Dependency Scanning hash.
843+
llvm::hash_code getModuleScanningHashComponents() const {
844+
return getPCHHashComponents();
845+
}
846+
835847
std::vector<std::string> getRemappedExtraArgs(
836848
std::function<std::string(StringRef)> pathRemapCallback) const;
837849
};

include/swift/DependencyScan/SerializedModuleDependencyCacheFormat.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ using llvm::BCRecordLayout;
3535
using llvm::BCVBR;
3636

3737
/// Every .moddepcache file begins with these 4 bytes, for easy identification.
38-
const unsigned char MODULE_DEPENDENCY_CACHE_FORMAT_SIGNATURE[] = {'I', 'M', 'D',
39-
'C'};
40-
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MAJOR = 2;
38+
const unsigned char MODULE_DEPENDENCY_CACHE_FORMAT_SIGNATURE[] = {'I', 'M', 'D','C'};
39+
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MAJOR = 3;
4140
/// Increment this on every change.
4241
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MINOR = 0;
4342

@@ -56,7 +55,7 @@ using IdentifierIDArryField = llvm::BCArray<IdentifierIDField>;
5655

5756
/// Identifiers used to refer to the above arrays
5857
using FileIDArrayIDField = IdentifierIDField;
59-
using TripleIDField = IdentifierIDField;
58+
using ContextHashIDField = IdentifierIDField;
6059
using DependencyIDArrayIDField = IdentifierIDField;
6160
using FlagIDArrayIDField = IdentifierIDField;
6261

@@ -118,7 +117,7 @@ using IdentifierArrayLayout =
118117
using ModuleInfoLayout =
119118
BCRecordLayout<MODULE_NODE, // ID
120119
IdentifierIDField, // module name
121-
TripleIDField, // target triple
120+
ContextHashIDField, //
122121
DependencyIDArrayIDField // directDependencies
123122
>;
124123

include/swift/Frontend/Frontend.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ class CompilerInvocation {
345345
/// in generating a cached PCH file for the bridging header.
346346
std::string getPCHHash() const;
347347

348+
/// Retrieve a module hash string that is suitable for uniquely
349+
/// identifying the conditions under which the current module is built,
350+
/// from the perspective of a dependency scanning action.
351+
std::string getModuleScanningHash() const;
352+
348353
/// Retrieve the stdlib kind to implicitly import.
349354
ImplicitStdlibKind getImplicitStdlibKind() const {
350355
if (FrontendOpts.InputMode == FrontendOptions::ParseInputMode::SIL) {

include/swift/Frontend/FrontendOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,12 @@ class FrontendOptions {
430430
return llvm::hash_value(0);
431431
}
432432

433+
/// Return a hash code of any components from these options that should
434+
/// contribute to a Swift Dependency Scanning hash.
435+
llvm::hash_code getModuleScanningHashComponents() const {
436+
return llvm::hash_value(0);
437+
}
438+
433439
StringRef determineFallbackModuleName() const;
434440

435441
bool isCompilingExactlyOneSwiftFile() const {

lib/AST/ModuleDependencies.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -234,20 +234,20 @@ GlobalModuleDependenciesCache::GlobalModuleDependenciesCache()
234234
/* SharedFS */ nullptr,
235235
/* ReuseFileManager */ false,
236236
/* OptimizeArgs */ false) {}
237-
GlobalModuleDependenciesCache::TargetSpecificGlobalCacheState *
237+
GlobalModuleDependenciesCache::ContextSpecificGlobalCacheState *
238238
GlobalModuleDependenciesCache::getCurrentCache() const {
239-
assert(CurrentTriple.has_value() &&
239+
assert(CurrentContextHash.has_value() &&
240240
"Global Module Dependencies Cache not configured with Triple.");
241-
return getCacheForTriple(CurrentTriple.value());
241+
return getCacheForScanningContextHash(CurrentContextHash.value());
242242
}
243243

244-
GlobalModuleDependenciesCache::TargetSpecificGlobalCacheState *
245-
GlobalModuleDependenciesCache::getCacheForTriple(StringRef triple) const {
246-
auto targetSpecificCache = TargetSpecificCacheMap.find(triple);
247-
assert(targetSpecificCache != TargetSpecificCacheMap.end() &&
248-
"Global Module Dependencies Cache not configured with Triple-specific "
244+
GlobalModuleDependenciesCache::ContextSpecificGlobalCacheState *
245+
GlobalModuleDependenciesCache::getCacheForScanningContextHash(StringRef scanningContextHash) const {
246+
auto contextSpecificCache = ContextSpecificCacheMap.find(scanningContextHash);
247+
assert(contextSpecificCache != ContextSpecificCacheMap.end() &&
248+
"Global Module Dependencies Cache not configured with context-specific "
249249
"state.");
250-
return targetSpecificCache->getValue().get();
250+
return contextSpecificCache->getValue().get();
251251
}
252252

253253
llvm::StringMap<ModuleDependenciesVector> &
@@ -333,24 +333,24 @@ moduleContainedInImportPathSet(const ModuleDependencies &module,
333333
return false;
334334
}
335335

336-
void GlobalModuleDependenciesCache::configureForTriple(std::string triple) {
337-
auto knownTriple = TargetSpecificCacheMap.find(triple);
338-
if (knownTriple != TargetSpecificCacheMap.end()) {
339-
// Set the current triple and leave the rest as-is
340-
CurrentTriple = triple;
336+
void GlobalModuleDependenciesCache::configureForContextHash(std::string scanningContextHash) {
337+
auto knownContext = ContextSpecificCacheMap.find(scanningContextHash);
338+
if (knownContext != ContextSpecificCacheMap.end()) {
339+
// Set the current context and leave the rest as-is
340+
CurrentContextHash = scanningContextHash;
341341
} else {
342342
// First time scanning with this triple, initialize target-specific state.
343-
std::unique_ptr<TargetSpecificGlobalCacheState> targetSpecificCache =
344-
std::make_unique<TargetSpecificGlobalCacheState>();
343+
std::unique_ptr<ContextSpecificGlobalCacheState> contextSpecificCache =
344+
std::make_unique<ContextSpecificGlobalCacheState>();
345345
for (auto kind = ModuleDependenciesKind::FirstKind;
346346
kind != ModuleDependenciesKind::LastKind; ++kind) {
347-
targetSpecificCache->ModuleDependenciesMap.insert(
347+
contextSpecificCache->ModuleDependenciesMap.insert(
348348
{kind, llvm::StringMap<ModuleDependenciesVector>()});
349349
}
350350

351-
TargetSpecificCacheMap.insert({triple, std::move(targetSpecificCache)});
352-
CurrentTriple = triple;
353-
AllTriples.push_back(triple);
351+
ContextSpecificCacheMap.insert({scanningContextHash, std::move(contextSpecificCache)});
352+
CurrentContextHash = scanningContextHash;
353+
AllContextHashes.push_back(scanningContextHash);
354354
}
355355
}
356356

lib/DependencyScan/DependencyScanningTool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ DependencyScanningTool::initScannerForAction(
205205
auto instanceOrErr = initCompilerInstanceForScan(Command);
206206
if (instanceOrErr.getError())
207207
return instanceOrErr;
208-
SharedCache->configureForTriple((*instanceOrErr)->getInvocation()
209-
.getLangOptions().Target.str());
208+
SharedCache->configureForContextHash((*instanceOrErr)->getInvocation()
209+
.getModuleScanningHash());
210210
return instanceOrErr;
211211
}
212212

0 commit comments

Comments
 (0)