Skip to content

Commit fcd51e0

Browse files
committed
Revert "[clang][modules] Lazily load by name lookups in module maps (llvm#132853)"
This is causing a slowdown in SourceKit completion requests. The slowdown is caused by the call to `SourceManager::translateFile` that was added to avoid allocating separate source location space for each module map in each `CompilerInstance`. rdar://162196108 This reverts commit 32fb8c5.
1 parent eb374f8 commit fcd51e0

File tree

20 files changed

+136
-457
lines changed

20 files changed

+136
-457
lines changed

clang-tools-extra/modularize/ModularizeUtilities.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ std::error_code ModularizeUtilities::loadModuleMap(
287287
Target.get(), *HeaderInfo));
288288

289289
// Parse module.modulemap file into module map.
290-
if (ModMap->parseAndLoadModuleMapFile(ModuleMapEntry, false, Dir)) {
290+
if (ModMap->loadModuleMapFile(ModuleMapEntry, false, Dir)) {
291291
return std::error_code(1, std::generic_category());
292292
}
293293

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ def ModuleImport : DiagGroup<"module-import">;
629629
def ModuleConflict : DiagGroup<"module-conflict">;
630630
def ModuleFileExtension : DiagGroup<"module-file-extension">;
631631
def ModuleIncludeDirectiveTranslation : DiagGroup<"module-include-translation">;
632-
def ModuleMap : DiagGroup<"module-map">;
633632
def IndexStore : DiagGroup<"index-store">;
634633
def RoundTripCC1Args : DiagGroup<"round-trip-cc1-args">;
635634
def NewlineEOF : DiagGroup<"newline-eof">;

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -848,12 +848,6 @@ def err_pp_encounter_nonreproducible: Error<
848848
"encountered non-reproducible token, caching failed">;
849849

850850
// Module map parsing
851-
def remark_mmap_parse : Remark<
852-
"parsing modulemap '%0'">, ShowInSystemHeader, InGroup<ModuleMap>;
853-
def remark_mmap_load : Remark<
854-
"loading modulemap '%0'">, ShowInSystemHeader, InGroup<ModuleMap>;
855-
def remark_mmap_load_module : Remark<
856-
"loading parsed module '%0'">, ShowInSystemHeader, InGroup<ModuleMap>;
857851
def err_mmap_unknown_token : Error<"skipping stray token">;
858852
def err_mmap_expected_module : Error<"expected module declaration">;
859853
def err_mmap_expected_module_name : Error<"expected module name">;

clang/include/clang/Lex/HeaderSearch.h

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -332,27 +332,13 @@ class HeaderSearch {
332332
/// The mapping between modules and headers.
333333
mutable ModuleMap ModMap;
334334

335-
struct ModuleMapDirectoryState {
336-
OptionalFileEntryRef ModuleMapFile;
337-
enum {
338-
Parsed,
339-
Loaded,
340-
Invalid,
341-
} Status;
342-
};
343-
344335
/// Describes whether a given directory has a module map in it.
345-
llvm::DenseMap<const DirectoryEntry *, ModuleMapDirectoryState>
346-
DirectoryModuleMap;
336+
llvm::DenseMap<const DirectoryEntry *, bool> DirectoryHasModuleMap;
347337

348338
/// Set of module map files we've already loaded, and a flag indicating
349339
/// whether they were valid or not.
350340
llvm::DenseMap<const FileEntry *, bool> LoadedModuleMaps;
351341

352-
/// Set of module map files we've already parsed, and a flag indicating
353-
/// whether they were valid or not.
354-
llvm::DenseMap<const FileEntry *, bool> ParsedModuleMaps;
355-
356342
// A map of discovered headers with their associated include file name.
357343
llvm::DenseMap<const FileEntry *, llvm::SmallString<64>> IncludeNames;
358344

@@ -447,6 +433,11 @@ class HeaderSearch {
447433
/// Retrieve the path to the module cache.
448434
StringRef getModuleCachePath() const { return ModuleCachePath; }
449435

436+
/// Consider modules when including files from this directory.
437+
void setDirectoryHasModuleMap(const DirectoryEntry* Dir) {
438+
DirectoryHasModuleMap[Dir] = true;
439+
}
440+
450441
/// Forget everything we know about headers so far.
451442
void ClearFileInfo() {
452443
FileInfo.clear();
@@ -722,10 +713,9 @@ class HeaderSearch {
722713
/// used to resolve paths within the module (this is required when
723714
/// building the module from preprocessed source).
724715
/// \returns true if an error occurred, false otherwise.
725-
bool parseAndLoadModuleMapFile(FileEntryRef File, bool IsSystem,
726-
FileID ID = FileID(),
727-
unsigned *Offset = nullptr,
728-
StringRef OriginalModuleMapFile = StringRef());
716+
bool loadModuleMapFile(FileEntryRef File, bool IsSystem, FileID ID = FileID(),
717+
unsigned *Offset = nullptr,
718+
StringRef OriginalModuleMapFile = StringRef());
729719

730720
/// Collect the set of all known, top-level modules.
731721
///
@@ -925,31 +915,26 @@ class HeaderSearch {
925915
size_t getTotalMemory() const;
926916

927917
private:
928-
/// Describes what happened when we tried to load or parse a module map file.
929-
enum ModuleMapResult {
930-
/// The module map file had already been processed.
931-
MMR_AlreadyProcessed,
918+
/// Describes what happened when we tried to load a module map file.
919+
enum LoadModuleMapResult {
920+
/// The module map file had already been loaded.
921+
LMM_AlreadyLoaded,
932922

933-
/// The module map file was processed by this invocation.
934-
MMR_NewlyProcessed,
923+
/// The module map file was loaded by this invocation.
924+
LMM_NewlyLoaded,
935925

936926
/// There is was directory with the given name.
937-
MMR_NoDirectory,
927+
LMM_NoDirectory,
938928

939929
/// There was either no module map file or the module map file was
940930
/// invalid.
941-
MMR_InvalidModuleMap
931+
LMM_InvalidModuleMap
942932
};
943933

944-
ModuleMapResult parseAndLoadModuleMapFileImpl(FileEntryRef File,
945-
bool IsSystem,
946-
DirectoryEntryRef Dir,
947-
FileID ID = FileID(),
948-
unsigned *Offset = nullptr);
949-
950-
ModuleMapResult parseModuleMapFileImpl(FileEntryRef File, bool IsSystem,
951-
DirectoryEntryRef Dir,
952-
FileID ID = FileID());
934+
LoadModuleMapResult loadModuleMapFileImpl(FileEntryRef File, bool IsSystem,
935+
DirectoryEntryRef Dir,
936+
FileID ID = FileID(),
937+
unsigned *Offset = nullptr);
953938

954939
/// Try to load the module map file in the given directory.
955940
///
@@ -960,8 +945,8 @@ class HeaderSearch {
960945
///
961946
/// \returns The result of attempting to load the module map file from the
962947
/// named directory.
963-
ModuleMapResult parseAndLoadModuleMapFile(StringRef DirName, bool IsSystem,
964-
bool IsFramework);
948+
LoadModuleMapResult loadModuleMapFile(StringRef DirName, bool IsSystem,
949+
bool IsFramework);
965950

966951
/// Try to load the module map file in the given directory.
967952
///
@@ -971,13 +956,8 @@ class HeaderSearch {
971956
///
972957
/// \returns The result of attempting to load the module map file from the
973958
/// named directory.
974-
ModuleMapResult parseAndLoadModuleMapFile(DirectoryEntryRef Dir,
975-
bool IsSystem, bool IsFramework);
976-
977-
ModuleMapResult parseModuleMapFile(StringRef DirName, bool IsSystem,
978-
bool IsFramework);
979-
ModuleMapResult parseModuleMapFile(DirectoryEntryRef Dir, bool IsSystem,
980-
bool IsFramework);
959+
LoadModuleMapResult loadModuleMapFile(DirectoryEntryRef Dir, bool IsSystem,
960+
bool IsFramework);
981961
};
982962

983963
/// Apply the header search options to get given HeaderSearch object.

clang/include/clang/Lex/ModuleMap.h

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "clang/Basic/LangOptions.h"
1919
#include "clang/Basic/Module.h"
2020
#include "clang/Basic/SourceLocation.h"
21-
#include "clang/Lex/ModuleMapFile.h"
2221
#include "llvm/ADT/ArrayRef.h"
2322
#include "llvm/ADT/DenseMap.h"
2423
#include "llvm/ADT/DenseSet.h"
@@ -271,18 +270,6 @@ class ModuleMap {
271270
/// Describes whether we haved loaded a particular file as a module
272271
/// map.
273272
llvm::DenseMap<const FileEntry *, bool> LoadedModuleMap;
274-
llvm::DenseMap<const FileEntry *, const modulemap::ModuleMapFile *>
275-
ParsedModuleMap;
276-
277-
std::vector<std::unique_ptr<modulemap::ModuleMapFile>> ParsedModuleMaps;
278-
279-
/// Map from top level module name to a list of ModuleDecls in the order they
280-
/// were discovered. This allows handling shadowing correctly and diagnosing
281-
/// redefinitions.
282-
llvm::StringMap<SmallVector<std::pair<const modulemap::ModuleMapFile *,
283-
const modulemap::ModuleDecl *>,
284-
1>>
285-
ParsedModules;
286273

287274
/// Resolve the given export declaration into an actual export
288275
/// declaration.
@@ -499,8 +486,6 @@ class ModuleMap {
499486
/// \returns The named module, if known; otherwise, returns null.
500487
Module *findModule(StringRef Name) const;
501488

502-
Module *findOrLoadModule(StringRef Name);
503-
504489
Module *findOrInferSubmodule(Module *Parent, StringRef Name);
505490

506491
/// Retrieve a module with the given name using lexical name lookup,
@@ -716,11 +701,6 @@ class ModuleMap {
716701
void addHeader(Module *Mod, Module::Header Header,
717702
ModuleHeaderRole Role, bool Imported = false);
718703

719-
/// Parse a module map without creating `clang::Module` instances.
720-
bool parseModuleMapFile(FileEntryRef File, bool IsSystem,
721-
DirectoryEntryRef Dir, FileID ID = FileID(),
722-
SourceLocation ExternModuleLoc = SourceLocation());
723-
724704
/// Load the given module map file, and record any modules we
725705
/// encounter.
726706
///
@@ -741,11 +721,10 @@ class ModuleMap {
741721
/// that caused us to load this module map file, if any.
742722
///
743723
/// \returns true if an error occurred, false otherwise.
744-
bool
745-
parseAndLoadModuleMapFile(FileEntryRef File, bool IsSystem,
746-
DirectoryEntryRef HomeDir, FileID ID = FileID(),
747-
unsigned *Offset = nullptr,
748-
SourceLocation ExternModuleLoc = SourceLocation());
724+
bool loadModuleMapFile(FileEntryRef File, bool IsSystem,
725+
DirectoryEntryRef HomeDir, FileID ID = FileID(),
726+
unsigned *Offset = nullptr,
727+
SourceLocation ExternModuleLoc = SourceLocation());
749728

750729
/// Dump the contents of the module map, for debugging purposes.
751730
void dump();

clang/include/clang/Lex/ModuleMapFile.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,8 @@ using TopLevelDecl = std::variant<ModuleDecl, ExternModuleDecl>;
133133
/// This holds many reference types (StringRef, SourceLocation, etc.) whose
134134
/// lifetimes are bound by the SourceManager and FileManager used.
135135
struct ModuleMapFile {
136-
/// The FileID used to parse this module map. This is always a local ID.
137-
FileID ID;
138-
139-
/// The directory in which the module map was discovered. Declarations in
140-
/// the module map are relative to this directory.
141-
OptionalDirectoryEntryRef Dir;
142-
143136
/// Beginning of the file, used for moduleMapFileRead callback.
144137
SourceLocation Start;
145-
146-
bool IsSystem;
147138
std::vector<TopLevelDecl> Decls;
148139

149140
void dump(llvm::raw_ostream &out) const;

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,13 +599,13 @@ struct ReadModuleNames : ASTReaderListener {
599599
ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap();
600600
for (const std::string &LoadedModule : LoadedModules)
601601
MM.cacheModuleLoad(*PP.getIdentifierInfo(LoadedModule),
602-
MM.findOrLoadModule(LoadedModule));
602+
MM.findModule(LoadedModule));
603603
LoadedModules.clear();
604604
}
605605

606606
void markAllUnavailable() {
607607
for (const std::string &LoadedModule : LoadedModules) {
608-
if (Module *M = PP.getHeaderSearchInfo().getModuleMap().findOrLoadModule(
608+
if (Module *M = PP.getHeaderSearchInfo().getModuleMap().findModule(
609609
LoadedModule)) {
610610
M->HasIncompatibleModuleFile = true;
611611

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,8 @@ static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
627627
}
628628

629629
// Load the module map file.
630-
if (HS.parseAndLoadModuleMapFile(*ModuleMap, IsSystem, ModuleMapID, &Offset,
631-
PresumedModuleMapFile))
630+
if (HS.loadModuleMapFile(*ModuleMap, IsSystem, ModuleMapID, &Offset,
631+
PresumedModuleMapFile))
632632
return true;
633633

634634
if (SrcMgr.getBufferOrFake(ModuleMapID).getBufferSize() == Offset)
@@ -1248,8 +1248,8 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
12481248
// If we were asked to load any module map files, do so now.
12491249
for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
12501250
if (auto File = CI.getFileManager().getOptionalFileRef(Filename))
1251-
CI.getPreprocessor().getHeaderSearchInfo().parseAndLoadModuleMapFile(
1252-
*File, /*IsSystem*/ false);
1251+
CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
1252+
*File, /*IsSystem*/false);
12531253
else
12541254
CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
12551255
}

0 commit comments

Comments
 (0)