llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Michael Spencer (Bigcheese)

<details>
<summary>Changes</summary>

In some cases `SourceManager::translateFile` ends up spending a lot of time 
loading source locations from PCMs, significantly slowing down module scanning. 
The actual requirements are that we use local FileIDs for module maps so that 
we can compute affecting module maps, and that there is only one FileID per 
module map per compiler instance. This patch simply records the FileEntry to 
FileID mapping and reuses it when needed.

---
Full diff: https://github.com/llvm/llvm-project/pull/176288.diff


2 Files Affected:

- (modified) clang/include/clang/Lex/ModuleMap.h (+4) 
- (modified) clang/lib/Lex/ModuleMap.cpp (+8-6) 


``````````diff
diff --git a/clang/include/clang/Lex/ModuleMap.h 
b/clang/include/clang/Lex/ModuleMap.h
index d6a1e14958f13..6eba7cad0890b 100644
--- a/clang/include/clang/Lex/ModuleMap.h
+++ b/clang/include/clang/Lex/ModuleMap.h
@@ -266,6 +266,10 @@ class ModuleMap {
   llvm::DenseMap<const FileEntry *, const modulemap::ModuleMapFile *>
       ParsedModuleMap;
 
+  /// Each CompilerInstance needs its own FileID for each module map, but there
+  /// should only ever be one for each.
+  llvm::DenseMap<const FileEntry *, FileID> ModuleMapLocalFileID;
+
   std::vector<std::unique_ptr<modulemap::ModuleMapFile>> ParsedModuleMaps;
 
   /// Map from top level module name to a list of ModuleDecls in the order they
diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index b8202ea11be36..ea1ed9b48b101 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -1332,12 +1332,13 @@ bool ModuleMap::parseModuleMapFile(FileEntryRef File, 
bool IsSystem,
 
   // If the module map file wasn't already entered, do so now.
   if (ID.isInvalid()) {
-    ID = SourceMgr.translateFile(File);
-    if (ID.isInvalid() || SourceMgr.isLoadedFileID(ID)) {
+    FileID &LocalFID = ModuleMapLocalFileID[File];
+    if (LocalFID.isInvalid()) {
       auto FileCharacter =
           IsSystem ? SrcMgr::C_System_ModuleMap : SrcMgr::C_User_ModuleMap;
-      ID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter);
+      LocalFID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter);
     }
+    ID = LocalFID;
   }
 
   std::optional<llvm::MemoryBufferRef> Buffer = SourceMgr.getBufferOrNone(ID);
@@ -2247,16 +2248,17 @@ bool ModuleMap::parseAndLoadModuleMapFile(FileEntryRef 
File, bool IsSystem,
 
   // If the module map file wasn't already entered, do so now.
   if (ID.isInvalid()) {
-    ID = SourceMgr.translateFile(File);
     // TODO: The way we compute affecting module maps requires this to be a
     //       local FileID. This should be changed to reuse loaded FileIDs when
     //       available, and change the way that affecting module maps are
     //       computed to not require this.
-    if (ID.isInvalid() || SourceMgr.isLoadedFileID(ID)) {
+    FileID &LocalFID = ModuleMapLocalFileID[File];
+    if (LocalFID.isInvalid()) {
       auto FileCharacter =
           IsSystem ? SrcMgr::C_System_ModuleMap : SrcMgr::C_User_ModuleMap;
-      ID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter);
+      LocalFID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter);
     }
+    ID = LocalFID;
   }
 
   assert(Target && "Missing target information");

``````````

</details>


https://github.com/llvm/llvm-project/pull/176288
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to