-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[CAS] Add ActionCache to LLVMCAS Library #114097
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
cachemeifyoucan
merged 10 commits into
main
from
users/cachemeifyoucan/spr/cas-add-actioncache-to-llvmcas-library
Aug 20, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0feea2f
[𝘀𝗽𝗿] changes to main this commit is based on
cachemeifyoucan 802cce3
[𝘀𝗽𝗿] initial version
cachemeifyoucan 26fa10e
[𝘀𝗽𝗿] changes introduced through rebase
cachemeifyoucan f4c81ce
Update to main
cachemeifyoucan f59b53b
clang-format
cachemeifyoucan 7d2e7d8
address review feedback
cachemeifyoucan 95c8053
clang-format
cachemeifyoucan 7d015ab
rebase to main
cachemeifyoucan 9f161b3
Merge branch 'main' into users/cachemeifyoucan/spr/cas-add-actioncach…
cachemeifyoucan fc14676
address review feedback
cachemeifyoucan 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
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,102 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| /// | ||
| /// \file | ||
| /// This file contains the declaration of the ActionCache class, which is the | ||
| /// base class for ActionCache implementations. | ||
| /// | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_CAS_ACTIONCACHE_H | ||
| #define LLVM_CAS_ACTIONCACHE_H | ||
|
|
||
| #include "llvm/ADT/StringRef.h" | ||
| #include "llvm/CAS/CASID.h" | ||
| #include "llvm/CAS/CASReference.h" | ||
| #include "llvm/Support/Error.h" | ||
|
|
||
| namespace llvm::cas { | ||
|
|
||
| class ObjectStore; | ||
| class CASID; | ||
| class ObjectProxy; | ||
|
|
||
| /// A key for caching an operation. | ||
| /// It is implemented as a bag of bytes and provides a convenient constructor | ||
| /// for CAS types. | ||
| class CacheKey { | ||
| public: | ||
| StringRef getKey() const { return Key; } | ||
|
|
||
| CacheKey(const CASID &ID); | ||
| CacheKey(const ObjectProxy &Proxy); | ||
| CacheKey(const ObjectStore &CAS, const ObjectRef &Ref); | ||
|
|
||
| private: | ||
| std::string Key; | ||
| }; | ||
|
|
||
| /// A cache from a key (that describes an action) to the result of performing | ||
| /// that action. | ||
| /// | ||
| /// Actions are expected to be pure. Storing mappings from one action to | ||
| /// multiple results will result in error (cache poisoning). | ||
| class ActionCache { | ||
| virtual void anchor(); | ||
|
|
||
| public: | ||
| /// Get a previously computed result for \p ActionKey. | ||
| /// | ||
| /// \param CanBeDistributed is a hint to the underlying implementation that if | ||
| /// it is true, the lookup is profitable to be done on a distributed caching | ||
| /// level, not just locally. The implementation is free to ignore this flag. | ||
| Expected<std::optional<CASID>> get(const CacheKey &ActionKey, | ||
| bool CanBeDistributed = false) const { | ||
| return getImpl(arrayRefFromStringRef(ActionKey.getKey()), CanBeDistributed); | ||
| } | ||
|
|
||
| /// Cache \p Result for the \p ActionKey computation. | ||
| /// | ||
| /// \param CanBeDistributed is a hint to the underlying implementation that if | ||
| /// it is true, the association is profitable to be done on a distributed | ||
| /// caching level, not just locally. The implementation is free to ignore this | ||
| /// flag. | ||
| Error put(const CacheKey &ActionKey, const CASID &Result, | ||
| bool CanBeDistributed = false) { | ||
| assert(Result.getContext().getHashSchemaIdentifier() == | ||
| getContext().getHashSchemaIdentifier() && | ||
| "Hash schema mismatch"); | ||
| return putImpl(arrayRefFromStringRef(ActionKey.getKey()), Result, | ||
| CanBeDistributed); | ||
| } | ||
|
|
||
| virtual ~ActionCache() = default; | ||
|
|
||
| protected: | ||
| // Implementation detail for \p get method. | ||
| virtual Expected<std::optional<CASID>> | ||
| getImpl(ArrayRef<uint8_t> ResolvedKey, bool CanBeDistributed) const = 0; | ||
|
|
||
| // Implementation details for \p put method. | ||
| virtual Error putImpl(ArrayRef<uint8_t> ResolvedKey, const CASID &Result, | ||
| bool CanBeDistributed) = 0; | ||
|
|
||
| ActionCache(const CASContext &Context) : Context(Context) {} | ||
|
|
||
| const CASContext &getContext() const { return Context; } | ||
|
|
||
| private: | ||
| const CASContext &Context; | ||
| }; | ||
|
|
||
| /// Create an action cache in memory. | ||
| std::unique_ptr<ActionCache> createInMemoryActionCache(); | ||
|
|
||
| } // end namespace llvm::cas | ||
|
|
||
| #endif // LLVM_CAS_ACTIONCACHE_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,22 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 "llvm/CAS/ActionCache.h" | ||
| #include "llvm/CAS/CASID.h" | ||
| #include "llvm/CAS/ObjectStore.h" | ||
|
|
||
| using namespace llvm; | ||
| using namespace llvm::cas; | ||
|
|
||
| void ActionCache::anchor() {} | ||
|
|
||
| CacheKey::CacheKey(const CASID &ID) : Key(toStringRef(ID.getHash()).str()) {} | ||
| CacheKey::CacheKey(const ObjectProxy &Proxy) | ||
| : CacheKey(Proxy.getCAS(), Proxy.getRef()) {} | ||
| CacheKey::CacheKey(const ObjectStore &CAS, const ObjectRef &Ref) | ||
| : Key(toStringRef(CAS.getID(Ref).getHash())) {} |
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,101 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| /// | ||
| /// \file This file implements the underlying ActionCache implementations. | ||
| /// | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "BuiltinCAS.h" | ||
| #include "llvm/ADT/TrieRawHashMap.h" | ||
| #include "llvm/CAS/ActionCache.h" | ||
| #include "llvm/Support/BLAKE3.h" | ||
|
|
||
| #define DEBUG_TYPE "cas-action-caches" | ||
|
|
||
| using namespace llvm; | ||
| using namespace llvm::cas; | ||
|
|
||
| namespace { | ||
|
|
||
| using HasherT = BLAKE3; | ||
| using HashType = decltype(HasherT::hash(std::declval<ArrayRef<uint8_t> &>())); | ||
ilovepi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| template <size_t Size> class CacheEntry { | ||
| public: | ||
| CacheEntry() = default; | ||
| CacheEntry(ArrayRef<uint8_t> Hash) { llvm::copy(Hash, Value.data()); } | ||
| CacheEntry(const CacheEntry &Entry) { llvm::copy(Entry.Value, Value.data()); } | ||
| ArrayRef<uint8_t> getValue() const { return Value; } | ||
|
|
||
| private: | ||
| std::array<uint8_t, Size> Value; | ||
| }; | ||
|
|
||
| /// Builtin InMemory ActionCache that stores the mapping in memory. | ||
| class InMemoryActionCache final : public ActionCache { | ||
cachemeifyoucan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public: | ||
| InMemoryActionCache() | ||
| : ActionCache(builtin::BuiltinCASContext::getDefaultContext()) {} | ||
|
|
||
| Error putImpl(ArrayRef<uint8_t> ActionKey, const CASID &Result, | ||
| bool CanBeDistributed) final; | ||
| Expected<std::optional<CASID>> getImpl(ArrayRef<uint8_t> ActionKey, | ||
| bool CanBeDistributed) const final; | ||
|
|
||
| private: | ||
| using DataT = CacheEntry<sizeof(HashType)>; | ||
| using InMemoryCacheT = ThreadSafeTrieRawHashMap<DataT, sizeof(HashType)>; | ||
|
|
||
| InMemoryCacheT Cache; | ||
| }; | ||
| } // end namespace | ||
|
|
||
| static Error createResultCachePoisonedError(ArrayRef<uint8_t> KeyHash, | ||
| const CASContext &Context, | ||
| CASID Output, | ||
| ArrayRef<uint8_t> ExistingOutput) { | ||
| std::string Existing = | ||
| CASID::create(&Context, toStringRef(ExistingOutput)).toString(); | ||
| SmallString<64> Key; | ||
| toHex(KeyHash, /*LowerCase=*/true, Key); | ||
| return createStringError(std::make_error_code(std::errc::invalid_argument), | ||
| "cache poisoned for '" + Key + "' (new='" + | ||
| Output.toString() + "' vs. existing '" + | ||
| Existing + "')"); | ||
| } | ||
|
|
||
| Expected<std::optional<CASID>> | ||
| InMemoryActionCache::getImpl(ArrayRef<uint8_t> Key, | ||
| bool /*CanBeDistributed*/) const { | ||
| auto Result = Cache.find(Key); | ||
| if (!Result) | ||
| return std::nullopt; | ||
| return CASID::create(&getContext(), toStringRef(Result->Data.getValue())); | ||
| } | ||
|
|
||
| Error InMemoryActionCache::putImpl(ArrayRef<uint8_t> Key, const CASID &Result, | ||
| bool /*CanBeDistributed*/) { | ||
| DataT Expected(Result.getHash()); | ||
| const InMemoryCacheT::value_type &Cached = *Cache.insertLazy( | ||
| Key, [&](auto ValueConstructor) { ValueConstructor.emplace(Expected); }); | ||
|
|
||
| const DataT &Observed = Cached.Data; | ||
| if (Expected.getValue() == Observed.getValue()) | ||
| return Error::success(); | ||
|
|
||
| return createResultCachePoisonedError(Key, getContext(), Result, | ||
| Observed.getValue()); | ||
| } | ||
|
|
||
| namespace llvm::cas { | ||
|
|
||
| std::unique_ptr<ActionCache> createInMemoryActionCache() { | ||
| return std::make_unique<InMemoryActionCache>(); | ||
| } | ||
|
|
||
| } // namespace llvm::cas | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| /// | ||
| /// \file | ||
| /// This file implements the tests for ActionCaches. | ||
| /// | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "llvm/CAS/ActionCache.h" | ||
| #include "CASTestConfig.h" | ||
| #include "llvm/CAS/ObjectStore.h" | ||
| #include "llvm/Testing/Support/Error.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| using namespace llvm; | ||
| using namespace llvm::cas; | ||
|
|
||
| TEST_P(CASTest, ActionCacheHit) { | ||
| std::shared_ptr<ObjectStore> CAS = createObjectStore(); | ||
| std::unique_ptr<ActionCache> Cache = createActionCache(); | ||
|
|
||
| std::optional<ObjectProxy> ID; | ||
| ASSERT_THAT_ERROR(CAS->createProxy({}, "1").moveInto(ID), Succeeded()); | ||
| std::optional<CASID> ResultID; | ||
| ASSERT_THAT_ERROR(Cache->put(*ID, *ID), Succeeded()); | ||
| ASSERT_THAT_ERROR(Cache->get(*ID).moveInto(ResultID), Succeeded()); | ||
| ASSERT_TRUE(ResultID); | ||
| std::optional<ObjectRef> Result = CAS->getReference(*ResultID); | ||
| ASSERT_TRUE(Result); | ||
| ASSERT_EQ(*ID, *Result); | ||
| } | ||
|
|
||
| TEST_P(CASTest, ActionCacheMiss) { | ||
| std::shared_ptr<ObjectStore> CAS = createObjectStore(); | ||
| std::unique_ptr<ActionCache> Cache = createActionCache(); | ||
|
|
||
| std::optional<ObjectProxy> ID1, ID2; | ||
| ASSERT_THAT_ERROR(CAS->createProxy({}, "1").moveInto(ID1), Succeeded()); | ||
| ASSERT_THAT_ERROR(CAS->createProxy({}, "2").moveInto(ID2), Succeeded()); | ||
| ASSERT_THAT_ERROR(Cache->put(*ID1, *ID2), Succeeded()); | ||
| // This is a cache miss for looking up a key doesn't exist. | ||
| std::optional<CASID> Result1; | ||
| ASSERT_THAT_ERROR(Cache->get(*ID2).moveInto(Result1), Succeeded()); | ||
| ASSERT_FALSE(Result1); | ||
|
|
||
| ASSERT_THAT_ERROR(Cache->put(*ID2, *ID1), Succeeded()); | ||
| // Cache hit after adding the value. | ||
| std::optional<CASID> Result2; | ||
| ASSERT_THAT_ERROR(Cache->get(*ID2).moveInto(Result2), Succeeded()); | ||
| ASSERT_TRUE(Result2); | ||
| std::optional<ObjectRef> Ref = CAS->getReference(*Result2); | ||
| ASSERT_TRUE(Ref); | ||
| ASSERT_EQ(*ID1, *Ref); | ||
| } | ||
|
|
||
| TEST_P(CASTest, ActionCacheRewrite) { | ||
| std::shared_ptr<ObjectStore> CAS = createObjectStore(); | ||
| std::unique_ptr<ActionCache> Cache = createActionCache(); | ||
|
|
||
| std::optional<ObjectProxy> ID1, ID2; | ||
| ASSERT_THAT_ERROR(CAS->createProxy({}, "1").moveInto(ID1), Succeeded()); | ||
| ASSERT_THAT_ERROR(CAS->createProxy({}, "2").moveInto(ID2), Succeeded()); | ||
| ASSERT_THAT_ERROR(Cache->put(*ID1, *ID1), Succeeded()); | ||
| // Writing to the same key with different value is error. | ||
| ASSERT_THAT_ERROR(Cache->put(*ID1, *ID2), Failed()); | ||
| // Writing the same value multiple times to the same key is fine. | ||
| ASSERT_THAT_ERROR(Cache->put(*ID1, *ID1), Succeeded()); | ||
| } |
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
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
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.