Skip to content

Commit 72b2219

Browse files
committed
Revert "[clang][modules] Timestamp-less validation API (llvm#139987)"
This reverts commit 7a24238. Even after 175f8a4, the Modules/fmodules-validate-once-per-build-session.c test is not fixed on the clang-armv8-quick build bot. (Failure occurs on line 114.)
1 parent 10d198b commit 72b2219

File tree

14 files changed

+60
-55
lines changed

14 files changed

+60
-55
lines changed

clang-tools-extra/clangd/ModulesBuilder.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ bool IsModuleFileUpToDate(PathRef ModuleFilePath,
207207
Preprocessor PP(PPOpts, *Diags, LangOpts, SourceMgr, HeaderInfo,
208208
ModuleLoader);
209209

210-
IntrusiveRefCntPtr<ModuleCache> ModCache =
211-
createCrossProcessModuleCache(HSOpts.BuildSessionTimestamp);
210+
IntrusiveRefCntPtr<ModuleCache> ModCache = createCrossProcessModuleCache();
212211
PCHContainerOperations PCHOperations;
213212
ASTReader Reader(PP, *ModCache, /*ASTContext=*/nullptr,
214213
PCHOperations.getRawReader(), {});

clang/include/clang/Serialization/ModuleCache.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ class ModuleCache : public RefCountedBase<ModuleCache> {
3333
virtual std::unique_ptr<llvm::AdvisoryLock>
3434
getLock(StringRef ModuleFilename) = 0;
3535

36+
// TODO: Abstract away timestamps with isUpToDate() and markUpToDate().
3637
// TODO: Consider exposing a "validation lock" API to prevent multiple clients
3738
// concurrently noticing an out-of-date module file and validating its inputs.
3839

39-
/// Checks whether the inputs of the module file were marked as validated.
40-
virtual bool isMarkedUpToDate(StringRef ModuleFilename) = 0;
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;
4143

42-
/// Marks the inputs of the module file as validated.
43-
virtual void markUpToDate(StringRef ModuleFilename) = 0;
44+
/// Updates the timestamp denoting the last time inputs of the module file
45+
/// were validated.
46+
virtual void updateModuleTimestamp(StringRef ModuleFilename) = 0;
4447

4548
/// Returns this process's view of the module cache.
4649
virtual InMemoryModuleCache &getInMemoryModuleCache() = 0;
@@ -55,8 +58,7 @@ class ModuleCache : public RefCountedBase<ModuleCache> {
5558
/// operated on by multiple processes. This instance must be used across all
5659
/// \c CompilerInstance instances participating in building modules for single
5760
/// translation unit in order to share the same \c InMemoryModuleCache.
58-
IntrusiveRefCntPtr<ModuleCache>
59-
createCrossProcessModuleCache(std::time_t BuildSessionTimestamp);
61+
IntrusiveRefCntPtr<ModuleCache> createCrossProcessModuleCache();
6062
} // namespace clang
6163

6264
#endif

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,11 @@ class ModuleFile {
270270
// system input files reside at [NumUserInputFiles, InputFilesLoaded.size()).
271271
unsigned NumUserInputFiles = 0;
272272

273-
/// Specifies whether the input files have been validated (i.e. checked
274-
/// whether they are up-to-date).
275-
bool InputFilesValidated = false;
273+
/// If non-zero, specifies the time when we last validated input
274+
/// files. Zero means we never validated them.
275+
///
276+
/// The time is specified in seconds since the start of the Epoch.
277+
uint64_t InputFilesValidationTimestamp = 0;
276278

277279
// === Source Locations ===
278280

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

Lines changed: 8 additions & 1 deletion
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

@@ -102,6 +105,8 @@ class DependencyScanningService {
102105

103106
ModuleCacheEntries &getModuleCacheEntries() { return ModCacheEntries; }
104107

108+
std::time_t getBuildSessionTimestamp() const { return BuildSessionTimestamp; }
109+
105110
private:
106111
const ScanningMode Mode;
107112
const ScanningOutputFormat Format;
@@ -115,6 +120,8 @@ class DependencyScanningService {
115120
DependencyScanningFilesystemSharedCache SharedCache;
116121
/// The global module cache entries.
117122
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace tooling {
2020
namespace dependencies {
2121
struct ModuleCacheEntry {
2222
std::shared_mutex CompilationMutex;
23-
std::atomic<bool> UpToDate = false;
23+
std::atomic<std::time_t> Timestamp = 0;
2424
};
2525

2626
struct ModuleCacheEntries {

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,9 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
829829
AST->SourceMgr = new SourceManager(AST->getDiagnostics(),
830830
AST->getFileManager(),
831831
UserFilesAreVolatile);
832+
AST->ModCache = createCrossProcessModuleCache();
832833
AST->HSOpts = std::make_unique<HeaderSearchOptions>(HSOpts);
833834
AST->HSOpts->ModuleFormat = std::string(PCHContainerRdr.getFormats().front());
834-
AST->ModCache =
835-
createCrossProcessModuleCache(AST->HSOpts->BuildSessionTimestamp);
836835
AST->HeaderInfo.reset(new HeaderSearch(AST->getHeaderSearchOpts(),
837836
AST->getSourceManager(),
838837
AST->getDiagnostics(),
@@ -1549,8 +1548,7 @@ ASTUnit::create(std::shared_ptr<CompilerInvocation> CI,
15491548
AST->UserFilesAreVolatile = UserFilesAreVolatile;
15501549
AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
15511550
UserFilesAreVolatile);
1552-
AST->ModCache = createCrossProcessModuleCache(
1553-
AST->Invocation->getHeaderSearchOpts().BuildSessionTimestamp);
1551+
AST->ModCache = createCrossProcessModuleCache();
15541552

15551553
return AST;
15561554
}
@@ -1747,8 +1745,6 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
17471745
AST->IncludeBriefCommentsInCodeCompletion
17481746
= IncludeBriefCommentsInCodeCompletion;
17491747
AST->Invocation = std::move(CI);
1750-
AST->ModCache = createCrossProcessModuleCache(
1751-
AST->Invocation->getHeaderSearchOpts().BuildSessionTimestamp);
17521748
AST->FileSystemOpts = FileMgr->getFileSystemOpts();
17531749
AST->FileMgr = FileMgr;
17541750
AST->UserFilesAreVolatile = UserFilesAreVolatile;
@@ -1838,6 +1834,7 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCommandLine(
18381834
AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS);
18391835
AST->StorePreamblesInMemory = StorePreamblesInMemory;
18401836
AST->PreambleStoragePath = PreambleStoragePath;
1837+
AST->ModCache = createCrossProcessModuleCache();
18411838
AST->OnlyLocalDecls = OnlyLocalDecls;
18421839
AST->CaptureDiagnostics = CaptureDiagnostics;
18431840
AST->TUKind = TUKind;
@@ -1846,8 +1843,6 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCommandLine(
18461843
= IncludeBriefCommentsInCodeCompletion;
18471844
AST->UserFilesAreVolatile = UserFilesAreVolatile;
18481845
AST->Invocation = CI;
1849-
AST->ModCache = createCrossProcessModuleCache(
1850-
AST->Invocation->getHeaderSearchOpts().BuildSessionTimestamp);
18511846
AST->SkipFunctionBodies = SkipFunctionBodies;
18521847
if (ForSerialization)
18531848
AST->WriterData.reset(new ASTWriterData(*AST->ModCache));
@@ -2383,6 +2378,7 @@ bool ASTUnit::serialize(raw_ostream &OS) {
23832378

23842379
SmallString<128> Buffer;
23852380
llvm::BitstreamWriter Stream(Buffer);
2381+
IntrusiveRefCntPtr<ModuleCache> ModCache = createCrossProcessModuleCache();
23862382
ASTWriter Writer(Stream, Buffer, *ModCache, {});
23872383
return serializeUnit(Writer, Buffer, getSema(), OS);
23882384
}

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ CompilerInstance::CompilerInstance(
7272
ModuleCache *ModCache)
7373
: ModuleLoader(/*BuildingModule=*/ModCache),
7474
Invocation(std::move(Invocation)),
75-
ModCache(ModCache ? ModCache
76-
: createCrossProcessModuleCache(
77-
getHeaderSearchOpts().BuildSessionTimestamp)),
75+
ModCache(ModCache ? ModCache : createCrossProcessModuleCache()),
7876
ThePCHContainerOperations(std::move(PCHContainerOps)) {
7977
assert(this->Invocation && "Invocation must not be null");
8078
}

clang/lib/Serialization/ASTReader.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,7 +3103,8 @@ ASTReader::ReadControlBlock(ModuleFile &F,
31033103

31043104
unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;
31053105
if (HSOpts.ModulesValidateOncePerBuildSession &&
3106-
F.InputFilesValidated && F.Kind == MK_ImplicitModule)
3106+
F.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp &&
3107+
F.Kind == MK_ImplicitModule)
31073108
N = ForceValidateUserInputs ? NumUserInputs : 0;
31083109

31093110
for (unsigned I = 0; I < N; ++I) {
@@ -4949,8 +4950,10 @@ ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
49494950
// timestamp files are up-to-date in this build session.
49504951
for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
49514952
ImportedModule &M = Loaded[I];
4952-
if (M.Mod->Kind == MK_ImplicitModule && !M.Mod->InputFilesValidated)
4953-
getModuleManager().getModuleCache().markUpToDate(M.Mod->FileName);
4953+
if (M.Mod->Kind == MK_ImplicitModule &&
4954+
M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)
4955+
getModuleManager().getModuleCache().updateModuleTimestamp(
4956+
M.Mod->FileName);
49544957
}
49554958
}
49564959

clang/lib/Serialization/ASTWriter.cpp

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

53965396
if (ShouldCacheASTInMemory) {
53975397
// Construct MemoryBuffer and update buffer manager.

clang/lib/Serialization/ModuleCache.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ namespace {
2020
class CrossProcessModuleCache : public ModuleCache {
2121
InMemoryModuleCache InMemory;
2222

23-
std::time_t BuildSessionTimestamp;
24-
2523
public:
26-
explicit CrossProcessModuleCache(std::time_t BuildSessionTimestamp)
27-
: BuildSessionTimestamp(BuildSessionTimestamp) {}
28-
2924
void prepareForGetLock(StringRef ModuleFilename) override {
3025
// FIXME: Do this in LockFileManager and only if the directory doesn't
3126
// exist.
@@ -38,17 +33,16 @@ class CrossProcessModuleCache : public ModuleCache {
3833
return std::make_unique<llvm::LockFileManager>(ModuleFilename);
3934
}
4035

41-
bool isMarkedUpToDate(StringRef ModuleFilename) override {
36+
std::time_t getModuleTimestamp(StringRef ModuleFilename) override {
4237
std::string TimestampFilename =
4338
serialization::ModuleFile::getTimestampFilename(ModuleFilename);
4439
llvm::sys::fs::file_status Status;
4540
if (llvm::sys::fs::status(ModuleFilename, Status) != std::error_code{})
46-
return false;
47-
return llvm::sys::toTimeT(Status.getLastModificationTime()) >
48-
BuildSessionTimestamp;
41+
return 0;
42+
return llvm::sys::toTimeT(Status.getLastModificationTime());
4943
}
5044

51-
void markUpToDate(StringRef ModuleFilename) override {
45+
void updateModuleTimestamp(StringRef ModuleFilename) override {
5246
// Overwrite the timestamp file contents so that file's mtime changes.
5347
std::error_code EC;
5448
llvm::raw_fd_ostream OS(
@@ -68,8 +62,6 @@ class CrossProcessModuleCache : public ModuleCache {
6862
};
6963
} // namespace
7064

71-
IntrusiveRefCntPtr<ModuleCache>
72-
clang::createCrossProcessModuleCache(std::time_t BuildSessionTimestamp) {
73-
return llvm::makeIntrusiveRefCnt<CrossProcessModuleCache>(
74-
BuildSessionTimestamp);
65+
IntrusiveRefCntPtr<ModuleCache> clang::createCrossProcessModuleCache() {
66+
return llvm::makeIntrusiveRefCnt<CrossProcessModuleCache>();
7567
}

clang/lib/Serialization/ModuleManager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ ModuleManager::addModule(StringRef FileName, ModuleKind Type,
174174
NewModule->Index = Chain.size();
175175
NewModule->FileName = FileName.str();
176176
NewModule->ImportLoc = ImportLoc;
177-
NewModule->InputFilesValidated = false;
177+
NewModule->InputFilesValidationTimestamp = 0;
178178

179179
if (NewModule->Kind == MK_ImplicitModule)
180-
NewModule->InputFilesValidated =
181-
ModCache->isMarkedUpToDate(NewModule->FileName);
180+
NewModule->InputFilesValidationTimestamp =
181+
ModCache->getModuleTimestamp(NewModule->FileName);
182182

183183
// Load the contents of the module
184184
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) {}

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ class DependencyScanningAction : public tooling::ToolAction {
428428
ScanInstance.getPreprocessorOpts().AllowPCHWithDifferentModulesCachePath =
429429
true;
430430

431+
if (ScanInstance.getHeaderSearchOpts().ModulesValidateOncePerBuildSession)
432+
ScanInstance.getHeaderSearchOpts().BuildSessionTimestamp =
433+
Service.getBuildSessionTimestamp();
434+
431435
ScanInstance.getFrontendOpts().GenerateGlobalModuleIndex = false;
432436
ScanInstance.getFrontendOpts().UseGlobalModuleIndex = false;
433437
// This will prevent us compiling individual modules asynchronously since

clang/lib/Tooling/DependencyScanning/InProcessModuleCache.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,29 @@ class InProcessModuleCache : public ModuleCache {
7575
return std::make_unique<ReaderWriterLock>(CompilationMutex);
7676
}
7777

78-
bool isMarkedUpToDate(StringRef Filename) override {
79-
auto &IsUpToDate = [&]() -> std::atomic<bool> & {
78+
std::time_t getModuleTimestamp(StringRef Filename) override {
79+
auto &Timestamp = [&]() -> std::atomic<std::time_t> & {
8080
std::lock_guard<std::mutex> Lock(Entries.Mutex);
8181
auto &Entry = Entries.Map[Filename];
8282
if (!Entry)
8383
Entry = std::make_unique<ModuleCacheEntry>();
84-
return Entry->UpToDate;
84+
return Entry->Timestamp;
8585
}();
8686

87-
return IsUpToDate;
87+
return Timestamp.load();
8888
}
8989

90-
void markUpToDate(StringRef Filename) override {
90+
void updateModuleTimestamp(StringRef Filename) override {
9191
// Note: This essentially replaces FS contention with mutex contention.
92-
auto &IsUpToDate = [&]() -> std::atomic<bool> & {
92+
auto &Timestamp = [&]() -> std::atomic<std::time_t> & {
9393
std::lock_guard<std::mutex> Lock(Entries.Mutex);
9494
auto &Entry = Entries.Map[Filename];
9595
if (!Entry)
9696
Entry = std::make_unique<ModuleCacheEntry>();
97-
return Entry->UpToDate;
97+
return Entry->Timestamp;
9898
}();
9999

100-
IsUpToDate = true;
100+
Timestamp.store(llvm::sys::toTimeT(std::chrono::system_clock::now()));
101101
}
102102

103103
InMemoryModuleCache &getInMemoryModuleCache() override { return InMemory; }

0 commit comments

Comments
 (0)