Skip to content

Commit 1698beb

Browse files
authored
[clang][modules][deps] Optimize in-process timestamping of PCMs (#137363)
In the past, timestamps used for `-fmodules-validate-once-per-build-session` were found to be a source of contention in the dependency scanner ([D149802](https://reviews.llvm.org/D149802), #112452). This PR is yet another attempt to optimize these. We now make use of the new `ModuleCache` interface to implement the in-process version in terms of atomic `std::time_t` variables rather the mtime attribute on `.timestamp` files.
1 parent b8461ac commit 1698beb

File tree

12 files changed

+109
-47
lines changed

12 files changed

+109
-47
lines changed

clang/include/clang/Serialization/ModuleCache.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "clang/Basic/LLVM.h"
1313
#include "llvm/ADT/IntrusiveRefCntPtr.h"
1414

15+
#include <ctime>
16+
1517
namespace llvm {
1618
class AdvisoryLock;
1719
} // namespace llvm
@@ -31,11 +33,23 @@ class ModuleCache : public RefCountedBase<ModuleCache> {
3133
virtual std::unique_ptr<llvm::AdvisoryLock>
3234
getLock(StringRef ModuleFilename) = 0;
3335

36+
// TODO: Abstract away timestamps with isUpToDate() and markUpToDate().
37+
// TODO: Consider exposing a "validation lock" API to prevent multiple clients
38+
// concurrently noticing an out-of-date module file and validating its inputs.
39+
40+
/// Returns the timestamp denoting the last time inputs of the module file
41+
/// were validated.
42+
virtual std::time_t getModuleTimestamp(StringRef ModuleFilename) = 0;
43+
44+
/// Updates the timestamp denoting the last time inputs of the module file
45+
/// were validated.
46+
virtual void updateModuleTimestamp(StringRef ModuleFilename) = 0;
47+
3448
/// Returns this process's view of the module cache.
3549
virtual InMemoryModuleCache &getInMemoryModuleCache() = 0;
3650
virtual const InMemoryModuleCache &getInMemoryModuleCache() const = 0;
3751

38-
// TODO: Virtualize writing/reading PCM files, timestamping, pruning, etc.
52+
// TODO: Virtualize writing/reading PCM files, pruning, etc.
3953

4054
virtual ~ModuleCache() = default;
4155
};

clang/include/clang/Tooling/DependencyScanning/DependencyScanningService.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h"
1313
#include "clang/Tooling/DependencyScanning/InProcessModuleCache.h"
1414
#include "llvm/ADT/BitmaskEnum.h"
15+
#include "llvm/Support/Chrono.h"
1516

1617
namespace clang {
1718
namespace tooling {
@@ -84,7 +85,9 @@ class DependencyScanningService {
8485
DependencyScanningService(
8586
ScanningMode Mode, ScanningOutputFormat Format,
8687
ScanningOptimizations OptimizeArgs = ScanningOptimizations::Default,
87-
bool EagerLoadModules = false, bool TraceVFS = false);
88+
bool EagerLoadModules = false, bool TraceVFS = false,
89+
std::time_t BuildSessionTimestamp =
90+
llvm::sys::toTimeT(std::chrono::system_clock::now()));
8891

8992
ScanningMode getMode() const { return Mode; }
9093

@@ -100,7 +103,9 @@ class DependencyScanningService {
100103
return SharedCache;
101104
}
102105

103-
ModuleCacheMutexes &getModuleCacheMutexes() { return ModCacheMutexes; }
106+
ModuleCacheEntries &getModuleCacheEntries() { return ModCacheEntries; }
107+
108+
std::time_t getBuildSessionTimestamp() const { return BuildSessionTimestamp; }
104109

105110
private:
106111
const ScanningMode Mode;
@@ -113,8 +118,10 @@ class DependencyScanningService {
113118
const bool TraceVFS;
114119
/// The global file system cache.
115120
DependencyScanningFilesystemSharedCache SharedCache;
116-
/// The global module cache mutexes.
117-
ModuleCacheMutexes ModCacheMutexes;
121+
/// The global module cache entries.
122+
ModuleCacheEntries ModCacheEntries;
123+
/// The build session timestamp.
124+
std::time_t BuildSessionTimestamp;
118125
};
119126

120127
} // end namespace dependencies

clang/include/clang/Tooling/DependencyScanning/InProcessModuleCache.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818
namespace clang {
1919
namespace tooling {
2020
namespace dependencies {
21-
struct ModuleCacheMutexes {
21+
struct ModuleCacheEntry {
22+
std::shared_mutex CompilationMutex;
23+
std::atomic<std::time_t> Timestamp = 0;
24+
};
25+
26+
struct ModuleCacheEntries {
2227
std::mutex Mutex;
23-
llvm::StringMap<std::unique_ptr<std::shared_mutex>> Map;
28+
llvm::StringMap<std::unique_ptr<ModuleCacheEntry>> Map;
2429
};
2530

2631
IntrusiveRefCntPtr<ModuleCache>
27-
makeInProcessModuleCache(ModuleCacheMutexes &Mutexes);
32+
makeInProcessModuleCache(ModuleCacheEntries &Entries);
2833
} // namespace dependencies
2934
} // namespace tooling
3035
} // namespace clang

clang/lib/Serialization/ASTCommon.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,3 @@ bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
510510
return false;
511511
return isa<TagDecl, FieldDecl>(D);
512512
}
513-
514-
void serialization::updateModuleTimestamp(StringRef ModuleFilename) {
515-
// Overwrite the timestamp file contents so that file's mtime changes.
516-
std::error_code EC;
517-
llvm::raw_fd_ostream OS(ModuleFile::getTimestampFilename(ModuleFilename), EC,
518-
llvm::sys::fs::OF_TextWithCRLF);
519-
if (EC)
520-
return;
521-
OS << "Timestamp file\n";
522-
OS.close();
523-
OS.clear_error(); // Avoid triggering a fatal error.
524-
}

clang/lib/Serialization/ASTCommon.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ inline bool isPartOfPerModuleInitializer(const Decl *D) {
100100
return false;
101101
}
102102

103-
void updateModuleTimestamp(StringRef ModuleFilename);
104-
105103
} // namespace serialization
106104

107105
} // namespace clang

clang/lib/Serialization/ASTReader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4952,7 +4952,8 @@ ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
49524952
ImportedModule &M = Loaded[I];
49534953
if (M.Mod->Kind == MK_ImplicitModule &&
49544954
M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
4955-
updateModuleTimestamp(M.Mod->FileName);
4955+
getModuleManager().getModuleCache().updateModuleTimestamp(
4956+
M.Mod->FileName);
49564957
}
49574958
}
49584959

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5394,7 +5394,7 @@ ASTWriter::WriteAST(llvm::PointerUnion<Sema *, Preprocessor *> Subject,
53945394
if (WritingModule && PPRef.getHeaderSearchInfo()
53955395
.getHeaderSearchOpts()
53965396
.ModulesValidateOncePerBuildSession)
5397-
updateModuleTimestamp(OutputFile);
5397+
ModCache.updateModuleTimestamp(OutputFile);
53985398

53995399
if (ShouldCacheASTInMemory) {
54005400
// Construct MemoryBuffer and update buffer manager.

clang/lib/Serialization/ModuleCache.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "clang/Serialization/ModuleCache.h"
1010

1111
#include "clang/Serialization/InMemoryModuleCache.h"
12+
#include "clang/Serialization/ModuleFile.h"
1213
#include "llvm/Support/FileSystem.h"
1314
#include "llvm/Support/LockFileManager.h"
1415
#include "llvm/Support/Path.h"
@@ -32,6 +33,28 @@ class CrossProcessModuleCache : public ModuleCache {
3233
return std::make_unique<llvm::LockFileManager>(ModuleFilename);
3334
}
3435

36+
std::time_t getModuleTimestamp(StringRef ModuleFilename) override {
37+
std::string TimestampFilename =
38+
serialization::ModuleFile::getTimestampFilename(ModuleFilename);
39+
llvm::sys::fs::file_status Status;
40+
if (llvm::sys::fs::status(ModuleFilename, Status) != std::error_code{})
41+
return 0;
42+
return llvm::sys::toTimeT(Status.getLastModificationTime());
43+
}
44+
45+
void updateModuleTimestamp(StringRef ModuleFilename) override {
46+
// Overwrite the timestamp file contents so that file's mtime changes.
47+
std::error_code EC;
48+
llvm::raw_fd_ostream OS(
49+
serialization::ModuleFile::getTimestampFilename(ModuleFilename), EC,
50+
llvm::sys::fs::OF_TextWithCRLF);
51+
if (EC)
52+
return;
53+
OS << "Timestamp file\n";
54+
OS.close();
55+
OS.clear_error(); // Avoid triggering a fatal error.
56+
}
57+
3558
InMemoryModuleCache &getInMemoryModuleCache() override { return InMemory; }
3659
const InMemoryModuleCache &getInMemoryModuleCache() const override {
3760
return InMemory;

clang/lib/Serialization/ModuleManager.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,9 @@ ModuleManager::addModule(StringRef FileName, ModuleKind Type,
174174
NewModule->ImportLoc = ImportLoc;
175175
NewModule->InputFilesValidationTimestamp = 0;
176176

177-
if (NewModule->Kind == MK_ImplicitModule) {
178-
std::string TimestampFilename =
179-
ModuleFile::getTimestampFilename(NewModule->FileName);
180-
llvm::vfs::Status Status;
181-
// A cached stat value would be fine as well.
182-
if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
183-
NewModule->InputFilesValidationTimestamp =
184-
llvm::sys::toTimeT(Status.getLastModificationTime());
185-
}
177+
if (NewModule->Kind == MK_ImplicitModule)
178+
NewModule->InputFilesValidationTimestamp =
179+
ModCache->getModuleTimestamp(NewModule->FileName);
186180

187181
// Load the contents of the module
188182
if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {

clang/lib/Tooling/DependencyScanning/DependencyScanningService.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ using namespace dependencies;
1414

1515
DependencyScanningService::DependencyScanningService(
1616
ScanningMode Mode, ScanningOutputFormat Format,
17-
ScanningOptimizations OptimizeArgs, bool EagerLoadModules, bool TraceVFS)
17+
ScanningOptimizations OptimizeArgs, bool EagerLoadModules, bool TraceVFS,
18+
std::time_t BuildSessionTimestamp)
1819
: Mode(Mode), Format(Format), OptimizeArgs(OptimizeArgs),
19-
EagerLoadModules(EagerLoadModules), TraceVFS(TraceVFS) {}
20+
EagerLoadModules(EagerLoadModules), TraceVFS(TraceVFS),
21+
BuildSessionTimestamp(BuildSessionTimestamp) {}

0 commit comments

Comments
 (0)