Skip to content

Commit 0ffa29f

Browse files
authored
[clang][modules] Timestamp PCM files when writing (#112452)
Clang uses timestamp files to track the last time an implicitly-built PCM file was verified to be up-to-date with regard to its inputs. With `-fbuild-session-{file,timestamp}=` and `-fmodules-validate-once-per-build-session` this reduces the number of times a PCM file is checked per "build session". The behavior I'm seeing with the current scheme is that when lots of Clang instances wait for the same PCM to be built, they race to validate it as soon as the file lock gets released, causing lots of concurrent IO. This patch makes it so that the timestamp is written by the same Clang instance responsible for building the PCM while still holding the lock. This makes it so that whenever a PCM file gets compiled, it's never re-validated in the same build session. I believe this is as sound as the current scheme. One thing to be aware of is that there might be a time interval between accessing input file N and writing the timestamp file, where changes to input files 0..<N would not result in a rebuild. Since this is the case current scheme too, I'm not too concerned about that. I've seen this speed up `clang-scan-deps` by ~27%.
1 parent d985197 commit 0ffa29f

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LLVM_CLANG_SERIALIZATION_MODULEFILE_H
1616

1717
#include "clang/Basic/FileManager.h"
18+
#include "clang/Basic/LLVM.h"
1819
#include "clang/Basic/Module.h"
1920
#include "clang/Basic/SourceLocation.h"
2021
#include "clang/Serialization/ASTBitCodes.h"
@@ -144,8 +145,8 @@ class ModuleFile {
144145
/// The base directory of the module.
145146
std::string BaseDirectory;
146147

147-
std::string getTimestampFilename() const {
148-
return FileName + ".timestamp";
148+
static std::string getTimestampFilename(StringRef FileName) {
149+
return (FileName + ".timestamp").str();
149150
}
150151

151152
/// The original source file name that was used to build the

clang/lib/Serialization/ASTCommon.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
#include "clang/AST/DeclObjC.h"
1616
#include "clang/Basic/IdentifierTable.h"
1717
#include "clang/Serialization/ASTDeserializationListener.h"
18+
#include "clang/Serialization/ModuleFile.h"
1819
#include "llvm/Support/DJB.h"
20+
#include "llvm/Support/FileSystem.h"
21+
#include "llvm/Support/raw_ostream.h"
1922

2023
using namespace clang;
2124

@@ -503,3 +506,15 @@ bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
503506
return false;
504507
return isa<TagDecl, FieldDecl>(D);
505508
}
509+
510+
void serialization::updateModuleTimestamp(StringRef ModuleFilename) {
511+
// Overwrite the timestamp file contents so that file's mtime changes.
512+
std::error_code EC;
513+
llvm::raw_fd_ostream OS(ModuleFile::getTimestampFilename(ModuleFilename), EC,
514+
llvm::sys::fs::OF_TextWithCRLF);
515+
if (EC)
516+
return;
517+
OS << "Timestamp file\n";
518+
OS.close();
519+
OS.clear_error(); // Avoid triggering a fatal error.
520+
}

clang/lib/Serialization/ASTCommon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "clang/AST/ASTContext.h"
1717
#include "clang/AST/DeclFriend.h"
18+
#include "clang/Basic/LLVM.h"
1819
#include "clang/Serialization/ASTBitCodes.h"
1920

2021
namespace clang {
@@ -100,6 +101,8 @@ inline bool isPartOfPerModuleInitializer(const Decl *D) {
100101
return false;
101102
}
102103

104+
void updateModuleTimestamp(StringRef ModuleFilename);
105+
103106
} // namespace serialization
104107

105108
} // namespace clang

clang/lib/Serialization/ASTReader.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4416,19 +4416,6 @@ bool ASTReader::isGlobalIndexUnavailable() const {
44164416
!hasGlobalIndex() && TriedLoadingGlobalIndex;
44174417
}
44184418

4419-
static void updateModuleTimestamp(ModuleFile &MF) {
4420-
// Overwrite the timestamp file contents so that file's mtime changes.
4421-
std::string TimestampFilename = MF.getTimestampFilename();
4422-
std::error_code EC;
4423-
llvm::raw_fd_ostream OS(TimestampFilename, EC,
4424-
llvm::sys::fs::OF_TextWithCRLF);
4425-
if (EC)
4426-
return;
4427-
OS << "Timestamp file\n";
4428-
OS.close();
4429-
OS.clear_error(); // Avoid triggering a fatal error.
4430-
}
4431-
44324419
/// Given a cursor at the start of an AST file, scan ahead and drop the
44334420
/// cursor into the start of the given block ID, returning false on success and
44344421
/// true on failure.
@@ -4707,7 +4694,7 @@ ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
47074694
ImportedModule &M = Loaded[I];
47084695
if (M.Mod->Kind == MK_ImplicitModule &&
47094696
M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
4710-
updateModuleTimestamp(*M.Mod);
4697+
updateModuleTimestamp(M.Mod->FileName);
47114698
}
47124699
}
47134700

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4905,6 +4905,12 @@ ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef, StringRef OutputFile,
49054905
this->BaseDirectory.clear();
49064906

49074907
WritingAST = false;
4908+
4909+
if (WritingModule && SemaRef.PP.getHeaderSearchInfo()
4910+
.getHeaderSearchOpts()
4911+
.ModulesValidateOncePerBuildSession)
4912+
updateModuleTimestamp(OutputFile);
4913+
49084914
if (ShouldCacheASTInMemory) {
49094915
// Construct MemoryBuffer and update buffer manager.
49104916
ModuleCache.addBuiltPCM(OutputFile,

clang/lib/Serialization/ModuleManager.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ ModuleManager::addModule(StringRef FileName, ModuleKind Type,
170170
NewModule->InputFilesValidationTimestamp = 0;
171171

172172
if (NewModule->Kind == MK_ImplicitModule) {
173-
std::string TimestampFilename = NewModule->getTimestampFilename();
173+
std::string TimestampFilename =
174+
ModuleFile::getTimestampFilename(NewModule->FileName);
174175
llvm::vfs::Status Status;
175176
// A cached stat value would be fine as well.
176177
if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))

0 commit comments

Comments
 (0)