clang 23.0.0git
InProcessModuleCache.cpp
Go to the documentation of this file.
1//===- InProcessModuleCache.cpp - Implicit Module Cache ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
12#include "llvm/Support/AdvisoryLock.h"
13#include "llvm/Support/Chrono.h"
14#include "llvm/Support/Error.h"
15#include "llvm/Support/IOSandbox.h"
16
17using namespace clang;
18using namespace dependencies;
19
20namespace {
21class ReaderWriterLock : public llvm::AdvisoryLock {
22 ModuleCacheEntry &Entry;
23 std::optional<unsigned> OwnedGeneration;
24
25public:
26 ReaderWriterLock(ModuleCacheEntry &Entry) : Entry(Entry) {}
27
28 Expected<bool> tryLock() override {
29 std::lock_guard<std::mutex> Lock(Entry.Mutex);
30 if (Entry.Locked)
31 return false;
32 Entry.Locked = true;
33 OwnedGeneration = Entry.Generation;
34 return true;
35 }
36
37 llvm::WaitForUnlockResult
38 waitForUnlockFor(std::chrono::seconds MaxSeconds) override {
39 assert(!OwnedGeneration);
40 std::unique_lock<std::mutex> Lock(Entry.Mutex);
41 unsigned CurrentGeneration = Entry.Generation;
42 bool Success = Entry.CondVar.wait_for(Lock, MaxSeconds, [&] {
43 // We check not only Locked, but also Generation to break the wait in case
44 // of unsafeUnlock() and successful tryLock().
45 return !Entry.Locked || Entry.Generation != CurrentGeneration;
46 });
47 return Success ? llvm::WaitForUnlockResult::Success
48 : llvm::WaitForUnlockResult::Timeout;
49 }
50
51 std::error_code unsafeUnlock() override {
52 {
53 std::lock_guard<std::mutex> Lock(Entry.Mutex);
54 Entry.Generation += 1;
55 Entry.Locked = false;
56 }
57 Entry.CondVar.notify_all();
58 return {};
59 }
60
61 ~ReaderWriterLock() override {
62 if (OwnedGeneration) {
63 {
64 std::lock_guard<std::mutex> Lock(Entry.Mutex);
65 // Avoid stomping over the state managed by someone else after
66 // unsafeUnlock() and successful tryLock().
67 if (*OwnedGeneration == Entry.Generation)
68 Entry.Locked = false;
69 }
70 Entry.CondVar.notify_all();
71 }
72 }
73};
74
75class InProcessModuleCache : public ModuleCache {
76 ModuleCacheEntries &Entries;
77
78 // TODO: If we changed the InMemoryModuleCache API and relied on strict
79 // context hash, we could probably create more efficient thread-safe
80 // implementation of the InMemoryModuleCache such that it doesn't need to be
81 // recreated for each translation unit.
82 InMemoryModuleCache InMemory;
83
84public:
85 InProcessModuleCache(ModuleCacheEntries &Entries) : Entries(Entries) {}
86
87 std::unique_ptr<llvm::AdvisoryLock> getLock(StringRef Filename) override {
88 auto &Entry = [&]() -> ModuleCacheEntry & {
89 std::lock_guard<std::mutex> Lock(Entries.Mutex);
90 auto &Entry = Entries.Map[Filename];
91 if (!Entry)
92 Entry = std::make_unique<ModuleCacheEntry>();
93 return *Entry;
94 }();
95 return std::make_unique<ReaderWriterLock>(Entry);
96 }
97
98 std::time_t getModuleTimestamp(StringRef Filename) override {
99 auto &Timestamp = [&]() -> std::atomic<std::time_t> & {
100 std::lock_guard<std::mutex> Lock(Entries.Mutex);
101 auto &Entry = Entries.Map[Filename];
102 if (!Entry)
103 Entry = std::make_unique<ModuleCacheEntry>();
104 return Entry->Timestamp;
105 }();
106
107 return Timestamp.load();
108 }
109
110 void updateModuleTimestamp(StringRef Filename) override {
111 // Note: This essentially replaces FS contention with mutex contention.
112 auto &Timestamp = [&]() -> std::atomic<std::time_t> & {
113 std::lock_guard<std::mutex> Lock(Entries.Mutex);
114 auto &Entry = Entries.Map[Filename];
115 if (!Entry)
116 Entry = std::make_unique<ModuleCacheEntry>();
117 return Entry->Timestamp;
118 }();
119
120 Timestamp.store(llvm::sys::toTimeT(std::chrono::system_clock::now()));
121 }
122
123 void maybePrune(StringRef Path, time_t PruneInterval,
124 time_t PruneAfter) override {
125 // FIXME: This only needs to be ran once per build, not in every
126 // compilation. Call it once per service.
127 maybePruneImpl(Path, PruneInterval, PruneAfter);
128 }
129
130 InMemoryModuleCache &getInMemoryModuleCache() override { return InMemory; }
131 const InMemoryModuleCache &getInMemoryModuleCache() const override {
132 return InMemory;
133 }
134
135 std::error_code write(StringRef Path, llvm::MemoryBufferRef Buffer) override {
136 // This is a compiler-internal input/output, let's bypass the sandbox.
137 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
138
139 // FIXME: This could use an in-memory cache to avoid IO, and only write to
140 // disk at the end of the scan.
141 return writeImpl(Path, Buffer);
142 }
143
144 Expected<std::unique_ptr<llvm::MemoryBuffer>>
145 read(StringRef FileName, off_t &Size, time_t &ModTime) override {
146 // This is a compiler-internal input/output, let's bypass the sandbox.
147 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
148
149 // FIXME: This only needs to go to disk once per build, not in every
150 // compilation. Introduce in-memory cache.
151 return readImpl(FileName, Size, ModTime);
152 }
153};
154} // namespace
155
156std::shared_ptr<ModuleCache>
158 return std::make_shared<InProcessModuleCache>(Entries);
159}
The module cache used for compiling modules implicitly.
Definition ModuleCache.h:30
std::shared_ptr< ModuleCache > makeInProcessModuleCache(ModuleCacheEntries &Entries)
The JSON file list parser is used to communicate input to InstallAPI.
Expected< std::unique_ptr< llvm::MemoryBuffer > > readImpl(StringRef FileName, off_t &Size, time_t &ModTime)
Shared implementation of ModuleCache::read().
@ Success
Annotation was successful.
Definition Parser.h:65
void maybePruneImpl(StringRef Path, time_t PruneInterval, time_t PruneAfter)
Shared implementation of ModuleCache::maybePrune().
std::error_code writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer)
Shared implementation of ModuleCache::write().