Skip to content

Commit b4ee045

Browse files
committed
[clang][modules] Serialize Module::DefinitionLoc
This is a prep patch for avoiding the quadratic number of calls to `HeaderSearch::lookupModule()` in `ASTReader` for each (transitively) loaded PCM file. (Specifically in the context of `clang-scan-deps`). This patch explicitly serializes `Module::DefinitionLoc` so that we can stop relying on it being filled by the module map parser. This change also required change to the module map parser, where we used the absence of `DefinitionLoc` to determine whether a file came from a PCM file. We also need to make sure we consider the "containing" module map affecting when writing a PCM, so that it's not stripped during serialization, which ensures `DefinitionLoc` still ends up pointing to the correct offset. This is intended to be a NFC change. Reviewed By: benlangmuir Differential Revision: https://reviews.llvm.org/D150292 (cherry picked from commit abcf7ce)
1 parent 089d50d commit b4ee045

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace serialization {
4141
/// Version 4 of AST files also requires that the version control branch and
4242
/// revision match exactly, since there is no backward compatibility of
4343
/// AST files at this time.
44-
const unsigned VERSION_MAJOR = 26;
44+
const unsigned VERSION_MAJOR = 27;
4545

4646
/// AST file minor version number supported by this version of
4747
/// Clang.

clang/lib/Lex/ModuleMap.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,18 +2040,20 @@ void ModuleMapParser::parseModuleDecl() {
20402040
Module *ShadowingModule = nullptr;
20412041
if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
20422042
// We might see a (re)definition of a module that we already have a
2043-
// definition for in two cases:
2043+
// definition for in three cases:
20442044
// - If we loaded one definition from an AST file and we've just found a
20452045
// corresponding definition in a module map file, or
2046-
bool LoadedFromASTFile = Existing->DefinitionLoc.isInvalid();
2046+
bool LoadedFromASTFile = Existing->IsFromModuleFile;
2047+
// - If we previously inferred this module from different module map file.
2048+
bool Inferred = Existing->IsInferred;
20472049
// - If we're building a (preprocessed) module and we've just loaded the
20482050
// module map file from which it was created.
20492051
bool ParsedAsMainInput =
20502052
Map.LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap &&
20512053
Map.LangOpts.CurrentModule == ModuleName &&
20522054
SourceMgr.getDecomposedLoc(ModuleNameLoc).first !=
20532055
SourceMgr.getDecomposedLoc(Existing->DefinitionLoc).first;
2054-
if (!ActiveModule && (LoadedFromASTFile || ParsedAsMainInput)) {
2056+
if (!ActiveModule && (LoadedFromASTFile || Inferred || ParsedAsMainInput)) {
20552057
// Skip the module definition.
20562058
skipUntil(MMToken::RBrace);
20572059
if (Tok.is(MMToken::RBrace))

clang/lib/Serialization/ASTReader.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5583,7 +5583,7 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
55835583
// Factor this out into a separate constant to make it easier to resolve
55845584
// merge conflicts.
55855585
static const unsigned NUM_SWIFT_SPECIFIC_FIELDS = 1;
5586-
if (Record.size() < 12 + NUM_SWIFT_SPECIFIC_FIELDS)
5586+
if (Record.size() < 13 + NUM_SWIFT_SPECIFIC_FIELDS)
55875587
return llvm::createStringError(std::errc::illegal_byte_sequence,
55885588
"malformed module definition");
55895589

@@ -5592,6 +5592,7 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
55925592
SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
55935593
SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
55945594
Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5595+
SourceLocation DefinitionLoc = ReadSourceLocation(F, Record[Idx++]);
55955596

55965597
// SWIFT-SPECIFIC FIELDS HERE. Handling them separately helps avoid merge
55975598
// conflicts. See also NUM_SWIFT_SPECIFIC_FIELDS above.
@@ -5617,8 +5618,7 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
56175618
ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
56185619
.first;
56195620

5620-
// FIXME: set the definition loc for CurrentModule, or call
5621-
// ModMap.setInferredModuleAllowedBy()
5621+
// FIXME: Call ModMap.setInferredModuleAllowedBy()
56225622

56235623
SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
56245624
if (GlobalIndex >= SubmodulesLoaded.size() ||
@@ -5649,6 +5649,7 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
56495649
}
56505650

56515651
CurrentModule->Kind = Kind;
5652+
CurrentModule->DefinitionLoc = DefinitionLoc;
56525653
CurrentModule->Signature = F.Signature;
56535654
CurrentModule->IsFromModuleFile = true;
56545655
CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ std::set<const FileEntry *> GetAffectingModuleMaps(const Preprocessor &PP,
200200
CB(F);
201201
FileID FID = SourceMgr.translateFile(F);
202202
SourceLocation Loc = SourceMgr.getIncludeLoc(FID);
203-
while (Loc.isValid()) {
203+
// The include location of inferred module maps can point into the header
204+
// file that triggered the inferring. Cut off the walk if that's the case.
205+
while (Loc.isValid() && isModuleMap(SourceMgr.getFileCharacteristic(Loc))) {
204206
FID = SourceMgr.getFileID(Loc);
205207
CB(*SourceMgr.getFileEntryRefForID(FID));
206208
Loc = SourceMgr.getIncludeLoc(FID);
@@ -209,11 +211,18 @@ std::set<const FileEntry *> GetAffectingModuleMaps(const Preprocessor &PP,
209211

210212
auto ProcessModuleOnce = [&](const Module *M) {
211213
for (const Module *Mod = M; Mod; Mod = Mod->Parent)
212-
if (ProcessedModules.insert(Mod).second)
214+
if (ProcessedModules.insert(Mod).second) {
215+
auto Insert = [&](FileEntryRef F) { ModuleMaps.insert(F); };
216+
// The containing module map is affecting, because it's being pointed
217+
// into by Module::DefinitionLoc.
218+
if (auto ModuleMapFile = MM.getContainingModuleMapFile(Mod))
219+
ForIncludeChain(*ModuleMapFile, Insert);
220+
// For inferred modules, the module map that allowed inferring is not in
221+
// the include chain of the virtual containing module map file. It did
222+
// affect the compilation, though.
213223
if (auto ModuleMapFile = MM.getModuleMapFileForUniquing(Mod))
214-
ForIncludeChain(*ModuleMapFile, [&](FileEntryRef F) {
215-
ModuleMaps.insert(F);
216-
});
224+
ForIncludeChain(*ModuleMapFile, Insert);
225+
}
217226
};
218227

219228
for (const Module *CurrentModule : ModulesToProcess) {
@@ -2690,6 +2699,7 @@ void ASTWriter::WriteSubmodules(Module *WritingModule) {
26902699
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
26912700
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
26922701
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Kind
2702+
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Definition location
26932703

26942704
// SWIFT-SPECIFIC FIELDS HERE. Handling them separately helps avoid merge
26952705
// conflicts.
@@ -2795,12 +2805,16 @@ void ASTWriter::WriteSubmodules(Module *WritingModule) {
27952805
ParentID = SubmoduleIDs[Mod->Parent];
27962806
}
27972807

2808+
uint64_t DefinitionLoc =
2809+
SourceLocationEncoding::encode(getAdjustedLocation(Mod->DefinitionLoc));
2810+
27982811
// Emit the definition of the block.
27992812
{
28002813
RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
28012814
ID,
28022815
ParentID,
28032816
(RecordData::value_type)Mod->Kind,
2817+
DefinitionLoc,
28042818

28052819
// SWIFT-SPECIFIC FIELDS HERE.
28062820
// Handling them separately helps

0 commit comments

Comments
 (0)