Skip to content

Commit 5b72c42

Browse files
committed
[clang][deps] Optimize in-process timestamping of PCMs
1 parent 2bc6f9d commit 5b72c42

File tree

11 files changed

+97
-44
lines changed

11 files changed

+97
-44
lines changed

clang/include/clang/Serialization/ModuleCache.h

Lines changed: 11 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,19 @@ class ModuleCache : public RefCountedBase<ModuleCache> {
3133
virtual std::unique_ptr<llvm::AdvisoryLock>
3234
getLock(StringRef ModuleFilename) = 0;
3335

36+
/// Returns the timestamp denoting the last time inputs of the module file
37+
/// were validated.
38+
virtual std::time_t getModuleTimestamp(StringRef ModuleFilename) = 0;
39+
40+
/// Updates the timestamp denoting the last time inputs of the module file
41+
/// were validated.
42+
virtual void updateModuleTimestamp(StringRef ModuleFilename) = 0;
43+
3444
/// Returns this process's view of the module cache.
3545
virtual InMemoryModuleCache &getInMemoryModuleCache() = 0;
3646
virtual const InMemoryModuleCache &getInMemoryModuleCache() const = 0;
3747

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

4050
virtual ~ModuleCache() = default;
4151
};

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class DependencyScanningService {
100100
return SharedCache;
101101
}
102102

103-
ModuleCacheMutexes &getModuleCacheMutexes() { return ModCacheMutexes; }
103+
ModuleCacheEntries &getModuleCacheEntries() { return ModCacheEntries; }
104104

105105
private:
106106
const ScanningMode Mode;
@@ -113,8 +113,8 @@ class DependencyScanningService {
113113
const bool TraceVFS;
114114
/// The global file system cache.
115115
DependencyScanningFilesystemSharedCache SharedCache;
116-
/// The global module cache mutexes.
117-
ModuleCacheMutexes ModCacheMutexes;
116+
/// The global module cache entries.
117+
ModuleCacheEntries ModCacheEntries;
118118
};
119119

120120
} // 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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3102,6 +3102,9 @@ ASTReader::ReadControlBlock(ModuleFile &F,
31023102
// files.
31033103

31043104
unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
3105+
// FIXME: F.InputFilesValidationTimestamp comes from std::chrono, but
3106+
// HSOpts.BuildSessionTimestamp comes from the FS. They may not be
3107+
// comparable.
31053108
if (HSOpts.ModulesValidateOncePerBuildSession &&
31063109
F.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp &&
31073110
F.Kind == MK_ImplicitModule)
@@ -4932,9 +4935,13 @@ ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
49324935
// timestamp files are up-to-date in this build session.
49334936
for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
49344937
ImportedModule &M = Loaded[I];
4938+
// FIXME: F.InputFilesValidationTimestamp comes from std::chrono, but
4939+
// HSOpts.BuildSessionTimestamp comes from the FS. They may not be
4940+
// comparable.
49354941
if (M.Mod->Kind == MK_ImplicitModule &&
49364942
M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
4937-
updateModuleTimestamp(M.Mod->FileName);
4943+
getModuleManager().getModuleCache().updateModuleTimestamp(
4944+
M.Mod->FileName);
49384945
}
49394946
}
49404947

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 {};
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/DependencyScanningWorker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ class DependencyScanningAction : public tooling::ToolAction {
411411
Scanned = true;
412412

413413
// Create a compiler instance to handle the actual work.
414-
auto ModCache = makeInProcessModuleCache(Service.getModuleCacheMutexes());
414+
auto ModCache = makeInProcessModuleCache(Service.getModuleCacheEntries());
415415
ScanInstanceStorage.emplace(std::move(PCHContainerOps), ModCache.get());
416416
CompilerInstance &ScanInstance = *ScanInstanceStorage;
417417
ScanInstance.setInvocation(std::move(Invocation));

clang/lib/Tooling/DependencyScanning/InProcessModuleCache.cpp

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "clang/Serialization/InMemoryModuleCache.h"
1212
#include "llvm/Support/AdvisoryLock.h"
13+
#include "llvm/Support/Chrono.h"
1314

1415
#include <mutex>
1516

@@ -50,7 +51,7 @@ class ReaderWriterLock : public llvm::AdvisoryLock {
5051
};
5152

5253
class InProcessModuleCache : public ModuleCache {
53-
ModuleCacheMutexes &Mutexes;
54+
ModuleCacheEntries &Entries;
5455

5556
// TODO: If we changed the InMemoryModuleCache API and relied on strict
5657
// context hash, we could probably create more efficient thread-safe
@@ -59,19 +60,46 @@ class InProcessModuleCache : public ModuleCache {
5960
InMemoryModuleCache InMemory;
6061

6162
public:
62-
InProcessModuleCache(ModuleCacheMutexes &Mutexes) : Mutexes(Mutexes) {}
63+
InProcessModuleCache(ModuleCacheEntries &Entries) : Entries(Entries) {}
6364

6465
void prepareForGetLock(StringRef Filename) override {}
6566

6667
std::unique_ptr<llvm::AdvisoryLock> getLock(StringRef Filename) override {
67-
auto &Mtx = [&]() -> std::shared_mutex & {
68-
std::lock_guard<std::mutex> Lock(Mutexes.Mutex);
69-
auto &Mutex = Mutexes.Map[Filename];
70-
if (!Mutex)
71-
Mutex = std::make_unique<std::shared_mutex>();
72-
return *Mutex;
68+
auto &CompilationMutex = [&]() -> std::shared_mutex & {
69+
std::lock_guard Lock(Entries.Mutex);
70+
auto &Entry = Entries.Map[Filename];
71+
if (!Entry)
72+
Entry = std::make_unique<ModuleCacheEntry>();
73+
return Entry->CompilationMutex;
7374
}();
74-
return std::make_unique<ReaderWriterLock>(Mtx);
75+
return std::make_unique<ReaderWriterLock>(CompilationMutex);
76+
}
77+
78+
std::time_t getModuleTimestamp(StringRef Filename) override {
79+
auto &Timestamp = [&]() -> std::atomic<std::time_t> & {
80+
std::lock_guard Lock(Entries.Mutex);
81+
auto &Entry = Entries.Map[Filename];
82+
if (!Entry)
83+
Entry = std::make_unique<ModuleCacheEntry>();
84+
return Entry->Timestamp;
85+
}();
86+
87+
return Timestamp.load();
88+
}
89+
90+
void updateModuleTimestamp(StringRef Filename) override {
91+
// Note: This essentially replaces FS contention with mutex contention.
92+
auto &Timestamp = [&]() -> std::atomic<std::time_t> & {
93+
std::lock_guard Lock(Entries.Mutex);
94+
auto &Entry = Entries.Map[Filename];
95+
if (!Entry)
96+
Entry = std::make_unique<ModuleCacheEntry>();
97+
return Entry->Timestamp;
98+
}();
99+
100+
std::time_t Expected = 0;
101+
std::time_t Now = llvm::sys::toTimeT(std::chrono::system_clock::now());
102+
Timestamp.compare_exchange_weak(Expected, Now);
75103
}
76104

77105
InMemoryModuleCache &getInMemoryModuleCache() override { return InMemory; }
@@ -82,6 +110,6 @@ class InProcessModuleCache : public ModuleCache {
82110
} // namespace
83111

84112
IntrusiveRefCntPtr<ModuleCache>
85-
dependencies::makeInProcessModuleCache(ModuleCacheMutexes &Mutexes) {
86-
return llvm::makeIntrusiveRefCnt<InProcessModuleCache>(Mutexes);
113+
dependencies::makeInProcessModuleCache(ModuleCacheEntries &Entries) {
114+
return llvm::makeIntrusiveRefCnt<InProcessModuleCache>(Entries);
87115
}

0 commit comments

Comments
 (0)