-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang][deps] Implement efficient in-process ModuleCache
#129751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
clang/include/clang/Tooling/DependencyScanning/InProcessModuleCache.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_INPROCESSMODULECACHE_H | ||
| #define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_INPROCESSMODULECACHE_H | ||
|
|
||
| #include "clang/Serialization/ModuleCache.h" | ||
| #include "llvm/ADT/StringMap.h" | ||
|
|
||
| #include <shared_mutex> | ||
|
|
||
| namespace clang { | ||
| namespace tooling { | ||
| namespace dependencies { | ||
| struct ModuleCacheMutexes { | ||
| std::mutex Mutex; | ||
| llvm::StringMap<std::unique_ptr<std::shared_mutex>> Map; | ||
| }; | ||
|
|
||
| IntrusiveRefCntPtr<ModuleCache> | ||
| makeInProcessModuleCache(ModuleCacheMutexes &Mutexes); | ||
| } // namespace dependencies | ||
| } // namespace tooling | ||
| } // namespace clang | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
clang/lib/Tooling/DependencyScanning/InProcessModuleCache.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "clang/Tooling/DependencyScanning/InProcessModuleCache.h" | ||
|
|
||
| #include "clang/Serialization/InMemoryModuleCache.h" | ||
| #include "llvm/Support/AdvisoryLock.h" | ||
|
|
||
| #include <mutex> | ||
|
|
||
| using namespace clang; | ||
| using namespace tooling; | ||
| using namespace dependencies; | ||
|
|
||
| namespace { | ||
| class ReaderWriterLock : public llvm::AdvisoryLock { | ||
| // TODO: Consider using std::atomic::{wait,notify_all} when we move to C++20. | ||
| std::unique_lock<std::shared_mutex> OwningLock; | ||
|
|
||
| public: | ||
| ReaderWriterLock(std::shared_mutex &Mutex) | ||
| : OwningLock(Mutex, std::defer_lock) {} | ||
|
|
||
| Expected<bool> tryLock() override { return OwningLock.try_lock(); } | ||
|
|
||
| llvm::WaitForUnlockResult | ||
| waitForUnlockFor(std::chrono::seconds MaxSeconds) override { | ||
| assert(!OwningLock); | ||
| // We do not respect the timeout here. It's very generous for implicit | ||
| // modules, so we'd typically only reach it if the owner crashed (but so did | ||
| // we, since we run in the same process), or encountered deadlock. | ||
| (void)MaxSeconds; | ||
| std::shared_lock Lock(*OwningLock.mutex()); | ||
| return llvm::WaitForUnlockResult::Success; | ||
| } | ||
|
|
||
| std::error_code unsafeMaybeUnlock() override { | ||
| // Unlocking the mutex here would trigger UB and we don't expect this to be | ||
| // actually called when compiling scanning modules due to the no-timeout | ||
| // guarantee above. | ||
| return {}; | ||
| } | ||
|
|
||
| ~ReaderWriterLock() override = default; | ||
| }; | ||
|
|
||
| class InProcessModuleCache : public ModuleCache { | ||
| ModuleCacheMutexes &Mutexes; | ||
|
|
||
| // TODO: If we changed the InMemoryModuleCache API and relied on strict | ||
| // context hash, we could probably create more efficient thread-safe | ||
| // implementation of the InMemoryModuleCache such that it does need to be | ||
| // recreated for each translation unit. | ||
| InMemoryModuleCache InMemory; | ||
|
|
||
| public: | ||
| InProcessModuleCache(ModuleCacheMutexes &Mutexes) : Mutexes(Mutexes) {} | ||
|
|
||
| void prepareForGetLock(StringRef Filename) override {} | ||
|
|
||
| std::unique_ptr<llvm::AdvisoryLock> getLock(StringRef Filename) override { | ||
| auto &Mtx = [&]() -> std::shared_mutex & { | ||
| std::lock_guard Lock(Mutexes.Mutex); | ||
| auto &Mutex = Mutexes.Map[Filename]; | ||
| if (!Mutex) | ||
| Mutex = std::make_unique<std::shared_mutex>(); | ||
| return *Mutex; | ||
| }(); | ||
| return std::make_unique<ReaderWriterLock>(Mtx); | ||
| } | ||
|
|
||
| InMemoryModuleCache &getInMemoryModuleCache() override { return InMemory; } | ||
| const InMemoryModuleCache &getInMemoryModuleCache() const override { | ||
| return InMemory; | ||
| } | ||
| }; | ||
| } // namespace | ||
|
|
||
| IntrusiveRefCntPtr<ModuleCache> | ||
| dependencies::makeInProcessModuleCache(ModuleCacheMutexes &Mutexes) { | ||
| return llvm::makeIntrusiveRefCnt<InProcessModuleCache>(Mutexes); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.