Skip to content

Commit b0fbefa

Browse files
authored
Merge pull request #7206 from apple/jan_svoboda/stable-20221013-scanner-perf-cherry-pick
[clang][deps] Cherry-pick the 1st batch of performance improvements
2 parents 0f296f6 + 5c7f289 commit b0fbefa

20 files changed

+278
-170
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,6 +2443,10 @@ defm modules_search_all : BoolFOption<"modules-search-all",
24432443
defm implicit_modules : BoolFOption<"implicit-modules",
24442444
LangOpts<"ImplicitModules">, DefaultTrue,
24452445
NegFlag<SetFalse, [CC1Option]>, PosFlag<SetTrue>, BothFlags<[NoXarchOption,CoreOption]>>;
2446+
def fno_modules_check_relocated : Joined<["-"], "fno-modules-check-relocated">,
2447+
Group<f_Group>, Flags<[CC1Option]>,
2448+
HelpText<"Skip checks for relocated modules when loading PCM files">,
2449+
MarshallingInfoNegativeFlag<PreprocessorOpts<"ModulesCheckRelocated">>;
24462450
def fretain_comments_from_system_headers : Flag<["-"], "fretain-comments-from-system-headers">, Group<f_Group>, Flags<[CC1Option]>,
24472451
MarshallingInfoFlag<LangOpts<"RetainCommentsFromSystemHeaders">>;
24482452
def fmodule_header : Flag <["-"], "fmodule-header">, Group<f_Group>,

clang/include/clang/Lex/Preprocessor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,7 @@ class Preprocessor {
13771377

13781378
/// Return true if this header has already been included.
13791379
bool alreadyIncluded(const FileEntry *File) const {
1380+
HeaderInfo.getFileInfo(File);
13801381
return IncludedFiles.count(File);
13811382
}
13821383

clang/include/clang/Lex/PreprocessorOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class PreprocessorOptions {
8080
std::vector<std::string> Includes;
8181
std::vector<std::string> MacroIncludes;
8282

83+
/// Perform extra checks when loading PCM files for mutable file systems.
84+
bool ModulesCheckRelocated = true;
85+
8386
/// Initialize the preprocessor with the compiler and target specific
8487
/// predefines.
8588
bool UsePredefines = true;

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 2 additions & 3 deletions
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.
@@ -707,8 +707,7 @@ enum ASTRecordTypes {
707707
/// Record code for \#pragma float_control options.
708708
FLOAT_CONTROL_PRAGMA_OPTIONS = 65,
709709

710-
/// Record code for included files.
711-
PP_INCLUDED_FILES = 66,
710+
/// ID 66 used to be the list of included files.
712711

713712
/// Record code for an unterminated \#pragma clang assume_nonnull begin
714713
/// recorded in a preamble.

clang/include/clang/Serialization/ASTReader.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,6 @@ class ASTReader
13791379
llvm::Error ReadSourceManagerBlock(ModuleFile &F);
13801380
llvm::BitstreamCursor &SLocCursorForID(int ID);
13811381
SourceLocation getImportLocation(ModuleFile *F);
1382-
void readIncludedFiles(ModuleFile &F, StringRef Blob, Preprocessor &PP);
13831382
ASTReadResult ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
13841383
const ModuleFile *ImportedBy,
13851384
unsigned ClientLoadCapabilities);

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,6 @@ class ASTWriter : public ASTDeserializationListener,
500500
void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts);
501501
void WriteSourceManagerBlock(SourceManager &SourceMgr,
502502
const Preprocessor &PP);
503-
void writeIncludedFiles(raw_ostream &Out, const Preprocessor &PP);
504503
void WritePreprocessor(const Preprocessor &PP, bool IsModule);
505504
void WriteHeaderSearch(const HeaderSearch &HS);
506505
void WritePreprocessorDetail(PreprocessingRecord &PPRec,

clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,10 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
261261
#undef ANALYZER_OPTION
262262
#undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
263263

264-
// Create an array of all -analyzer-config command line options. Sort it in
265-
// the constructor.
266-
std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = {
264+
bool isUnknownAnalyzerConfig(llvm::StringRef Name) {
265+
static std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = []() {
266+
// Create an array of all -analyzer-config command line options.
267+
std::vector<llvm::StringLiteral> AnalyzerConfigCmdFlags = {
267268
#define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
268269
SHALLOW_VAL, DEEP_VAL) \
269270
ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, SHALLOW_VAL)
@@ -274,10 +275,11 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
274275
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
275276
#undef ANALYZER_OPTION
276277
#undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
277-
};
278-
279-
bool isUnknownAnalyzerConfig(StringRef Name) const {
280-
assert(llvm::is_sorted(AnalyzerConfigCmdFlags));
278+
};
279+
// FIXME: Sort this at compile-time when we get constexpr sort (C++20).
280+
llvm::sort(AnalyzerConfigCmdFlags);
281+
return AnalyzerConfigCmdFlags;
282+
}();
281283

282284
return !std::binary_search(AnalyzerConfigCmdFlags.begin(),
283285
AnalyzerConfigCmdFlags.end(), Name);
@@ -293,9 +295,7 @@ class AnalyzerOptions : public RefCountedBase<AnalyzerOptions> {
293295
AnalyzerDisplayProgress(false), eagerlyAssumeBinOpBifurcation(false),
294296
TrimGraph(false), visualizeExplodedGraphWithGraphViz(false),
295297
UnoptimizedCFG(false), PrintStats(false), NoRetryExhausted(false),
296-
AnalyzerWerror(false) {
297-
llvm::sort(AnalyzerConfigCmdFlags);
298-
}
298+
AnalyzerWerror(false) {}
299299

300300
/// Interprets an option's string value as a boolean. The "true" string is
301301
/// interpreted as true and the "false" string is interpreted as false.

clang/lib/Lex/HeaderSearch.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,9 +1735,6 @@ HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
17351735

17361736
Module *HeaderSearch::loadFrameworkModule(StringRef Name, DirectoryEntryRef Dir,
17371737
bool IsSystem) {
1738-
if (Module *Module = ModMap.findModule(Name))
1739-
return Module;
1740-
17411738
// Try to load a module map file.
17421739
switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
17431740
case LMM_InvalidModuleMap:
@@ -1746,10 +1743,10 @@ Module *HeaderSearch::loadFrameworkModule(StringRef Name, DirectoryEntryRef Dir,
17461743
ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
17471744
break;
17481745

1749-
case LMM_AlreadyLoaded:
17501746
case LMM_NoDirectory:
17511747
return nullptr;
17521748

1749+
case LMM_AlreadyLoaded:
17531750
case LMM_NewlyLoaded:
17541751
break;
17551752
}

clang/lib/Lex/ModuleMap.cpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ Module *ModuleMap::findModule(StringRef Name) const {
810810
if (Known != Modules.end()) {
811811
Module *M = Known->getValue();
812812
// Notify callbacks that we found a module map for the module.
813-
if (!M->DefinitionLoc.isInvalid())
813+
if (M->DefinitionLoc.isValid() && !M->IsFromModuleFile)
814814
for (const auto &Cb : Callbacks)
815815
Cb->moduleMapFoundForModule(
816816
**getContainingModuleMapFile(M), M,
@@ -1265,6 +1265,20 @@ void ModuleMap::resolveHeaderDirectives(
12651265
Mod->UnresolvedHeaders.swap(NewHeaders);
12661266
}
12671267

1268+
// FIXME: duplicates code in PPDirectives
1269+
static bool isForModuleBuilding(const Module *M, StringRef CurrentModule,
1270+
StringRef ModuleName) {
1271+
StringRef TopLevelName = M->getTopLevelModuleName();
1272+
1273+
// When building framework Foo, we wanna make sure that Foo *and* Foo_Private
1274+
// are textually included and no modules are built for both.
1275+
if (M->getTopLevelModule()->IsFramework && CurrentModule == ModuleName &&
1276+
!CurrentModule.endswith("_Private") && TopLevelName.endswith("_Private"))
1277+
TopLevelName = TopLevelName.drop_back(8);
1278+
1279+
return TopLevelName == CurrentModule;
1280+
}
1281+
12681282
void ModuleMap::addHeader(Module *Mod, Module::Header Header,
12691283
ModuleHeaderRole Role, bool Imported) {
12701284
KnownHeader KH(Mod, Role);
@@ -1280,7 +1294,7 @@ void ModuleMap::addHeader(Module *Mod, Module::Header Header,
12801294
Mod->Headers[headerRoleToKind(Role)].push_back(Header);
12811295

12821296
bool isCompilingModuleHeader =
1283-
LangOpts.isCompilingModule() && Mod->getTopLevelModule() == SourceModule;
1297+
isForModuleBuilding(Mod, LangOpts.CurrentModule, LangOpts.ModuleName);
12841298
if (!Imported || isCompilingModuleHeader) {
12851299
// When we import HeaderFileInfo, the external source is expected to
12861300
// set the isModuleHeader flag itself.
@@ -2040,18 +2054,37 @@ void ModuleMapParser::parseModuleDecl() {
20402054
Module *ShadowingModule = nullptr;
20412055
if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
20422056
// We might see a (re)definition of a module that we already have a
2043-
// definition for in two cases:
2057+
// definition for in four cases:
20442058
// - If we loaded one definition from an AST file and we've just found a
20452059
// corresponding definition in a module map file, or
2046-
bool LoadedFromASTFile = Existing->DefinitionLoc.isInvalid();
2060+
bool LoadedFromASTFile = Existing->IsFromModuleFile;
2061+
// - If we previously inferred this module from different module map file.
2062+
bool Inferred = Existing->IsInferred;
2063+
// - If we're building a framework that vends a module map, we might've
2064+
// previously seen the one in intermediate products and now the system
2065+
// one.
2066+
// FIXME: If we're parsing module map file that looks like this:
2067+
// framework module FW { ... }
2068+
// module FW.Sub { ... }
2069+
// We can't check the framework qualifier, since it's not attached to
2070+
// the definition of Sub. Checking that qualifier on \c Existing is
2071+
// not correct either, since we might've previously seen:
2072+
// module FW { ... }
2073+
// module FW.Sub { ... }
2074+
// We should enforce consistency of redefinitions so that we can rely
2075+
// that \c Existing is part of a framework iff the redefinition of FW
2076+
// we have just skipped had it too. Once we do that, stop checking
2077+
// the local framework qualifier and only rely on \c Existing.
2078+
bool PartOfFramework = Framework || Existing->isPartOfFramework();
20472079
// - If we're building a (preprocessed) module and we've just loaded the
20482080
// module map file from which it was created.
20492081
bool ParsedAsMainInput =
20502082
Map.LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap &&
20512083
Map.LangOpts.CurrentModule == ModuleName &&
20522084
SourceMgr.getDecomposedLoc(ModuleNameLoc).first !=
20532085
SourceMgr.getDecomposedLoc(Existing->DefinitionLoc).first;
2054-
if (!ActiveModule && (LoadedFromASTFile || ParsedAsMainInput)) {
2086+
if (LoadedFromASTFile || Inferred || PartOfFramework || ParsedAsMainInput) {
2087+
ActiveModule = PreviousActiveModule;
20552088
// Skip the module definition.
20562089
skipUntil(MMToken::RBrace);
20572090
if (Tok.is(MMToken::RBrace))

clang/lib/Sema/SemaModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc,
269269
// module map defining it already.
270270
if (auto *M = Map.findModule(ModuleName)) {
271271
Diag(Path[0].second, diag::err_module_redefinition) << ModuleName;
272-
if (M->DefinitionLoc.isValid())
272+
if (M->DefinitionLoc.isValid() && !M->IsFromModuleFile)
273273
Diag(M->DefinitionLoc, diag::note_prev_module_definition);
274274
else if (Optional<FileEntryRef> FE = M->getASTFile())
275-
Diag(M->DefinitionLoc, diag::note_prev_module_definition_from_ast_file)
275+
Diag(SourceLocation(), diag::note_prev_module_definition_from_ast_file)
276276
<< FE->getName();
277277
Mod = M;
278278
break;

0 commit comments

Comments
 (0)