Skip to content

Commit 0cff65f

Browse files
committed
[Support] Remove output file checks from LockFileManager (llvm#130395)
Currently, `LockFileManager` assumes the owner of the lock file creates an output file. This is problematic for at least three reasons: 1. It is orthogonal to the main purpose of this class - mutual exclusion. This makes creating an alternative implementation more complicated than it needs to be. 2. Some clients (like the upstream `AMDGPUSplitModule.cpp` file) assume the output file is not necessary. The owner of the lock file does not write the file expected by `LockFileManager` and the processes waiting for the non-owned lock file to be unlocked therefore assume the owner has died. This means that the work gets repeated by each waiting process, serially. 3. The documentation makes it sound like successfully waiting for a non-owned lock file guarantees the output file to be present on the file system. Implicitly-built modules rely on this. However, the module file may disappear between `LockFileManager` performing the check and the compiler loading the module (for example due to module cache pruning with short intervals, or intervention from outside of Clang). The compiler assumes this cannot happen, and fails the build if it does. This PR solves this situation by removing the check, reflecting that in the `LockFileManager` documentation, and fixing the time-of-check time-of-use bug in implicit modules. (cherry picked from commit f90aa41)
1 parent 7135e91 commit 0cff65f

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ static bool readASTAfterCompileModule(CompilerInstance &ImportingInstance,
15571557
SourceLocation ImportLoc,
15581558
SourceLocation ModuleNameLoc,
15591559
Module *Module, StringRef ModuleFileName,
1560-
bool *OutOfDate) {
1560+
bool *OutOfDate, bool *Missing) {
15611561
DiagnosticsEngine &Diags = ImportingInstance.getDiagnostics();
15621562

15631563
unsigned ModuleLoadCapabilities = ASTReader::ARR_Missing;
@@ -1578,6 +1578,12 @@ static bool readASTAfterCompileModule(CompilerInstance &ImportingInstance,
15781578
return false;
15791579
}
15801580

1581+
// The caller wants to handle missing module files.
1582+
if (Missing && ReadResult == ASTReader::Missing) {
1583+
*Missing = true;
1584+
return false;
1585+
}
1586+
15811587
// The ASTReader didn't diagnose the error, so conservatively report it.
15821588
if (ReadResult == ASTReader::Missing || !Diags.hasErrorOccurred())
15831589
Diags.Report(ModuleNameLoc, diag::err_module_not_built)
@@ -1603,7 +1609,7 @@ static bool compileModuleAndReadASTImpl(CompilerInstance &ImportingInstance,
16031609

16041610
return readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc,
16051611
Module, ModuleFileName,
1606-
/*OutOfDate=*/nullptr);
1612+
/*OutOfDate=*/nullptr, /*Missing=*/nullptr);
16071613
}
16081614

16091615
/// Compile a module in a separate compiler instance and read the AST,
@@ -1668,15 +1674,17 @@ static bool compileModuleAndReadASTBehindLock(
16681674

16691675
// Read the module that was just written by someone else.
16701676
bool OutOfDate = false;
1677+
bool Missing = false;
16711678
if (readASTAfterCompileModule(ImportingInstance, ImportLoc, ModuleNameLoc,
1672-
Module, ModuleFileName, &OutOfDate))
1679+
Module, ModuleFileName, &OutOfDate, &Missing))
16731680
return true;
1674-
if (!OutOfDate)
1681+
if (!OutOfDate && !Missing)
16751682
return false;
16761683

1677-
// The module may be out of date in the presence of file system races,
1678-
// or if one of its imports depends on header search paths that are not
1679-
// consistent with this ImportingInstance. Try again...
1684+
// The module may be missing or out of date in the presence of file system
1685+
// races. It may also be out of date if one of its imports depends on header
1686+
// search paths that are not consistent with this ImportingInstance.
1687+
// Try again...
16801688
}
16811689
}
16821690

llvm/include/llvm/Support/LockFileManager.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
namespace llvm {
1717
class StringRef;
1818

19-
/// Class that manages the creation of a lock file to aid
20-
/// implicit coordination between different processes.
19+
/// Class that manages the creation of a lock file to aid implicit coordination
20+
/// between different processes.
2121
///
22-
/// The implicit coordination works by creating a ".lock" file alongside
23-
/// the file that we're coordinating for, using the atomicity of the file
24-
/// system to ensure that only a single process can create that ".lock" file.
25-
/// When the lock file is removed, the owning process has finished the
26-
/// operation.
22+
/// The implicit coordination works by creating a ".lock" file, using the
23+
/// atomicity of the file system to ensure that only a single process can create
24+
/// that ".lock" file. When the lock file is removed, the owning process has
25+
/// finished the operation.
2726
class LockFileManager {
2827
public:
2928
/// Describes the state of a lock file.

llvm/lib/Support/LockFileManager.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,8 @@ LockFileManager::waitForUnlock(const unsigned MaxSeconds) {
307307
while (Backoff.waitForNextAttempt()) {
308308
// FIXME: implement event-based waiting
309309
if (sys::fs::access(LockFileName.c_str(), sys::fs::AccessMode::Exist) ==
310-
errc::no_such_file_or_directory) {
311-
// If the original file wasn't created, somone thought the lock was dead.
312-
if (!sys::fs::exists(FileName))
313-
return Res_OwnerDied;
310+
errc::no_such_file_or_directory)
314311
return Res_Success;
315-
}
316312

317313
// If the process owning the lock died without cleaning up, just bail out.
318314
if (!processStillExecuting((*Owner).first, (*Owner).second))

0 commit comments

Comments
 (0)