ilyakuteev created this revision.
ilyakuteev added reviewers: aprantl, dblaikie, rsmith, dexonsmith.
ilyakuteev requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Problem:
PCM file includes references to all module maps used in compilation which 
created PCM. This problem leads to PCM-rebuilds in distributed compilations as 
some module maps could be missing in isolated compilation. (For example in our 
distributed build system we create a temp folder for every compilation with 
only modules and headers that are needed for that particular command).

Solution:
Add only affecting module map files to a PCM-file.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D106876

Files:
  clang/include/clang/Serialization/ASTWriter.h
  clang/lib/Serialization/ASTWriter.cpp

Index: clang/lib/Serialization/ASTWriter.cpp
===================================================================
--- clang/lib/Serialization/ASTWriter.cpp
+++ clang/lib/Serialization/ASTWriter.cpp
@@ -149,6 +149,48 @@

 namespace {

+std::string ModuleMapFilePathForModule(ModuleMap &Map, Module* GivenModule) {
+  if (GivenModule->PresumedModuleMapFile.empty()) {
+    auto *ModuleMapFile = Map.getModuleMapFileForUniquing(GivenModule);
+    if (ModuleMapFile) {
+      return std::string(ModuleMapFile->getName());
+    } else {
+      return std::string();
+    }
+  } else {
+    return GivenModule->PresumedModuleMapFile;
+  }
+}
+
+std::set<std::string> GetAllModulemaps(ModuleMap &Map, Module *RootModule) {
+  std::set<std::string> ModuleMaps {};
+  std::set<Module*> ProcessedModules;
+  std::set<Module*> ModulesToProcess {RootModule};
+  while (!ModulesToProcess.empty()) {
+    auto CurrentModule = ModulesToProcess.begin();
+    ProcessedModules.insert(*CurrentModule);
+
+    const std::string CurrentModuleMapFile =
+        ModuleMapFilePathForModule(Map, *CurrentModule);
+    if (CurrentModuleMapFile.empty()) {
+      ModulesToProcess.erase(CurrentModule);
+      continue;
+    }
+
+    ModuleMaps.insert(CurrentModuleMapFile);
+
+    for (auto *ImportedModule : (*CurrentModule)->Imports) {
+      if (!ImportedModule ||
+          ProcessedModules.find(ImportedModule) != ProcessedModules.end()) {
+        continue;
+      }
+      ModulesToProcess.insert(ImportedModule);
+    }
+    ModulesToProcess.erase(CurrentModule);
+  }
+  return ModuleMaps;
+}
+
 class ASTTypeWriter {
   ASTWriter &Writer;
   ASTWriter::RecordData Record;
@@ -1396,9 +1438,15 @@
     Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
   }

+  std::set<std::string> AffectingModulemaps;
+  if (WritingModule) {
+    AffectingModulemaps = GetAllModulemaps(
+        PP.getHeaderSearchInfo().getModuleMap(), WritingModule);
+  }
+
   WriteInputFiles(Context.SourceMgr,
                   PP.getHeaderSearchInfo().getHeaderSearchOpts(),
-                  PP.getLangOpts().Modules);
+                  AffectingModulemaps);
   Stream.ExitBlock();
 }

@@ -1418,7 +1466,7 @@

 void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
                                 HeaderSearchOptions &HSOpts,
-                                bool Modules) {
+                                std::set<std::string>& AffectingModuleMaps) {
   using namespace llvm;

   Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
@@ -1458,6 +1506,15 @@
     if (!Cache->OrigEntry)
       continue;

+    if (isModuleMap(File.getFileCharacteristic()) &&
+        !isSystem(File.getFileCharacteristic()) &&
+        !AffectingModuleMaps.empty() &&
+        AffectingModuleMaps.find(std::string(Cache->OrigEntry->getName())) ==
+           AffectingModuleMaps.end()) {
+      // Do not emit modulemaps that do not affect current module.
+      continue;
+    }
+
     InputFileEntry Entry;
     Entry.File = Cache->OrigEntry;
     Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
@@ -1971,11 +2028,15 @@
     Record.push_back(SLoc->getOffset() - 2);
     if (SLoc->isFile()) {
       const SrcMgr::FileInfo &File = SLoc->getFile();
+      const SrcMgr::ContentCache *Content = &File.getContentCache();
+      if (Content->OrigEntry && InputFileIDs[Content->OrigEntry] == 0) {
+        // Do not emit files that were not listed as inputs.
+        continue;
+      }
       AddSourceLocation(File.getIncludeLoc(), Record);
       Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
       Record.push_back(File.hasLineDirectives());

-      const SrcMgr::ContentCache *Content = &File.getContentCache();
       bool EmitBlob = false;
       if (Content->OrigEntry) {
         assert(Content->OrigEntry == Content->ContentsEntry &&
Index: clang/include/clang/Serialization/ASTWriter.h
===================================================================
--- clang/include/clang/Serialization/ASTWriter.h
+++ clang/include/clang/Serialization/ASTWriter.h
@@ -475,7 +475,7 @@
   createSignature(StringRef AllBytes, StringRef ASTBlockBytes);

   void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
-                       bool Modules);
+                       std::set<std::string>& AffectingModuleMaps);
   void WriteSourceManagerBlock(SourceManager &SourceMgr,
                                const Preprocessor &PP);
   void WritePreprocessor(const Preprocessor &PP, bool IsModule);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to