From 936ea7a69c8ac05e468b23f3a7aa9ff5660ebc31 Mon Sep 17 00:00:00 2001 From: Qiongsi Wu Date: Wed, 6 Aug 2025 15:39:37 -0700 Subject: [PATCH] [clang][Dependency Scanning] Move Module Timestamp Update After Compilation Finishes (#151774) When two threads are accessing the same `pcm`, it is possible that the reading thread sees the timestamp update, while the file on disk is not updated. This PR moves timestamp update from `writeAST` to `compileModuleAndReadASTImpl`, so we only update the timestamp after the file has been committed to disk. rdar://152097193 (cherry picked from commit 09dbdf651470bb4c9e5b81986a47f7c495285fbe) --- clang/lib/Frontend/CompilerInstance.cpp | 8 ++++++++ clang/lib/Serialization/ASTWriter.cpp | 5 ----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index 18af2fed00d01..35af53e9e695d 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -1617,6 +1617,14 @@ static bool compileModuleAndReadASTImpl(CompilerInstance &ImportingInstance, return false; } + // The module is built successfully, we can update its timestamp now. + if (ImportingInstance.getPreprocessor() + .getHeaderSearchInfo() + .getHeaderSearchOpts() + .ModulesValidateOncePerBuildSession) { + ImportingInstance.getModuleCache().updateModuleTimestamp(ModuleFileName); + } + return readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc, Module, ModuleFileName, /*OutOfDate=*/nullptr, /*Missing=*/nullptr); diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index dd1d35c15a60a..cee5b2ba262fe 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -5031,11 +5031,6 @@ ASTWriter::WriteAST(llvm::PointerUnion Subject, WritingAST = false; - if (WritingModule && PPRef.getHeaderSearchInfo() - .getHeaderSearchOpts() - .ModulesValidateOncePerBuildSession) - ModCache.updateModuleTimestamp(OutputFile); - if (ShouldCacheASTInMemory) { // Construct MemoryBuffer and update buffer manager. ModCache.getInMemoryModuleCache().addBuiltPCM(