@@ -282,7 +282,16 @@ static void discoverCrosssImportOverlayDependencies(
282282
283283 // Record the dummy main module's direct dependencies. The dummy main module
284284 // only directly depend on these newly discovered overlay modules.
285- cache.recordDependencies (dummyMainName, dummyMainDependencies);
285+ if (cache.findDependencies (dummyMainName,
286+ ModuleDependenciesKind::SwiftTextual)) {
287+ cache.updateDependencies (
288+ std::make_pair (dummyMainName.str (),
289+ ModuleDependenciesKind::SwiftTextual),
290+ dummyMainDependencies);
291+ } else {
292+ cache.recordDependencies (dummyMainName, dummyMainDependencies);
293+ }
294+
286295 llvm::SetVector<ModuleDependencyID, std::vector<ModuleDependencyID>,
287296 std::set<ModuleDependencyID>>
288297 allModules;
@@ -1100,17 +1109,30 @@ identifyMainModuleDependencies(CompilerInstance &instance) {
11001109
11011110} // namespace
11021111
1103- static void serializeDependencyCache (DiagnosticEngine &diags,
1104- const std::string &path,
1112+ static void serializeDependencyCache (CompilerInstance &instance,
11051113 const ModuleDependenciesCache &cache) {
1114+ const FrontendOptions &opts = instance.getInvocation ().getFrontendOptions ();
1115+ ASTContext &Context = instance.getASTContext ();
1116+ auto savePath = opts.SerializedDependencyScannerCachePath ;
11061117 module_dependency_cache_serialization::writeInterModuleDependenciesCache (
1107- diags, path, cache);
1118+ Context.Diags , savePath, cache);
1119+ if (opts.EmitDependencyScannerCacheRemarks ) {
1120+ Context.Diags .diagnose (SourceLoc (), diag::remark_save_cache, savePath);
1121+ }
11081122}
11091123
1110- static void deserializeDependencyCache (const std::string &path ,
1124+ static void deserializeDependencyCache (CompilerInstance &instance ,
11111125 ModuleDependenciesCache &cache) {
1112- module_dependency_cache_serialization::readInterModuleDependenciesCache (
1113- path, cache);
1126+ const FrontendOptions &opts = instance.getInvocation ().getFrontendOptions ();
1127+ ASTContext &Context = instance.getASTContext ();
1128+ auto loadPath = opts.SerializedDependencyScannerCachePath ;
1129+ if (module_dependency_cache_serialization::readInterModuleDependenciesCache (
1130+ loadPath, cache)) {
1131+ Context.Diags .diagnose (SourceLoc (), diag::warn_scaner_deserialize_failed,
1132+ loadPath);
1133+ } else if (opts.EmitDependencyScannerCacheRemarks ) {
1134+ Context.Diags .diagnose (SourceLoc (), diag::remark_reuse_cache, loadPath);
1135+ }
11141136}
11151137
11161138bool swift::dependencies::scanDependencies (CompilerInstance &instance) {
@@ -1119,22 +1141,32 @@ bool swift::dependencies::scanDependencies(CompilerInstance &instance) {
11191141 std::string path = opts.InputsAndOutputs .getSingleOutputFilename ();
11201142 std::error_code EC;
11211143 llvm::raw_fd_ostream out (path, EC, llvm::sys::fs::F_None);
1122-
1123- // `-scan-dependencies` invocations use a single new instance
1124- // of a module cache
1125- ModuleDependenciesCache cache;
11261144 if (out.has_error () || EC) {
11271145 Context.Diags .diagnose (SourceLoc (), diag::error_opening_output, path,
11281146 EC.message ());
11291147 out.clear_error ();
11301148 return true ;
11311149 }
11321150
1133- // Execute scan, and write JSON output to the output stream
1151+ // `-scan-dependencies` invocations use a single new instance
1152+ // of a module cache
1153+ ModuleDependenciesCache cache;
1154+
1155+ if (opts.ReuseDependencyScannerCache )
1156+ deserializeDependencyCache (instance, cache);
1157+
1158+ // Execute scan
11341159 auto dependenciesOrErr = performModuleScan (instance, cache);
1160+
1161+ // Serialize the dependency cache if -serialize-dependency-scan-cache
1162+ // is specified
1163+ if (opts.SerializeDependencyScannerCache )
1164+ serializeDependencyCache (instance, cache);
1165+
11351166 if (dependenciesOrErr.getError ())
11361167 return true ;
11371168 auto dependencies = std::move (*dependenciesOrErr);
1169+
11381170 // Write out the JSON description.
11391171 writeJSON (out, dependencies);
11401172 // This process succeeds regardless of whether any errors occurred.
@@ -1253,7 +1285,19 @@ swift::dependencies::performModuleScan(CompilerInstance &instance,
12531285 std::set<ModuleDependencyID>> allModules;
12541286
12551287 allModules.insert ({mainModuleName.str (), mainDependencies.getKind ()});
1256- cache.recordDependencies (mainModuleName, std::move (mainDependencies));
1288+
1289+ // We may be re-using an instance of the cache which already contains
1290+ // an entry for this module.
1291+ if (cache.findDependencies (mainModuleName,
1292+ ModuleDependenciesKind::SwiftTextual)) {
1293+ cache.updateDependencies (
1294+ std::make_pair (mainModuleName.str (),
1295+ ModuleDependenciesKind::SwiftTextual),
1296+ std::move (mainDependencies));
1297+ } else {
1298+ cache.recordDependencies (mainModuleName, std::move (mainDependencies));
1299+ }
1300+
12571301 auto &ctx = instance.getASTContext ();
12581302 auto ModuleCachePath = getModuleCachePathFromClang (
12591303 ctx.getClangModuleLoader ()->getClangInstance ());
@@ -1295,15 +1339,17 @@ swift::dependencies::performModuleScan(CompilerInstance &instance,
12951339 ModuleDependenciesCache loadedCache;
12961340 if (FEOpts.TestDependencyScannerCacheSerialization ) {
12971341 llvm::SmallString<128 > buffer;
1298- auto EC = llvm::sys::fs::createTemporaryFile (" depscan_cache" , " moddepcache" , buffer);
1342+ auto EC = llvm::sys::fs::createTemporaryFile (" depscan_cache" , " moddepcache" ,
1343+ buffer);
12991344 if (EC) {
1300- instance.getDiags ().diagnose (SourceLoc (),
1301- diag::error_unable_to_make_temporary_file,
1302- EC.message ());
1345+ instance.getDiags ().diagnose (
1346+ SourceLoc (), diag::error_unable_to_make_temporary_file, EC.message ());
13031347 } else {
13041348 std::string path = buffer.str ().str ();
1305- serializeDependencyCache (instance.getDiags (), path, cache);
1306- deserializeDependencyCache (path, loadedCache);
1349+ module_dependency_cache_serialization::writeInterModuleDependenciesCache (
1350+ instance.getDiags (), path, cache);
1351+ module_dependency_cache_serialization::readInterModuleDependenciesCache (
1352+ path, loadedCache);
13071353 resultCache = &loadedCache;
13081354 }
13091355 }
0 commit comments