-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[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 5 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,94 @@ | ||
| //===- llvm/CAS/ActionCache.h -----------------------------------*- C++ -*-===// | ||
| // | ||
| // 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_CAS_CASACTIONCACHE_H | ||
| #define LLVM_CAS_CASACTIONCACHE_H | ||
cachemeifyoucan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #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; } | ||
|
|
||
| // TODO: Support CacheKey other than a CASID but rather any array of bytes. | ||
ilovepi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // To do that, ActionCache need to be able to rehash the key into the index, | ||
| // which then `getOrCompute` method can be used to avoid multiple calls to | ||
| // has function. | ||
cachemeifyoucan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CacheKey(const CASID &ID); | ||
| CacheKey(const ObjectProxy &Proxy); | ||
| CacheKey(const ObjectStore &CAS, const ObjectRef &Ref); | ||
|
|
||
| private: | ||
| std::string Key; | ||
| }; | ||
|
|
||
| /// A cache from a key describing an action to the result of doing it. | ||
ilovepi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// Actions are expected to be pure (collision is an error). | ||
ilovepi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| class ActionCache { | ||
| virtual void anchor(); | ||
|
|
||
| public: | ||
| /// Get a previously computed result for \p ActionKey. | ||
| /// | ||
| /// \param Globally if true it is a hint to the underlying implementation that | ||
| /// the lookup is profitable to be done on a distributed caching level, not | ||
| /// just locally. The implementation is free to ignore this flag. | ||
cachemeifyoucan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Expected<std::optional<CASID>> get(const CacheKey &ActionKey, | ||
| bool Globally = false) const { | ||
ilovepi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return getImpl(arrayRefFromStringRef(ActionKey.getKey()), Globally); | ||
| } | ||
|
|
||
| /// Cache \p Result for the \p ActionKey computation. | ||
| /// | ||
| /// \param Globally if true it is a hint to the underlying implementation that | ||
| /// 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 Globally = false) { | ||
| assert(Result.getContext().getHashSchemaIdentifier() == | ||
| getContext().getHashSchemaIdentifier() && | ||
| "Hash schema mismatch"); | ||
| return putImpl(arrayRefFromStringRef(ActionKey.getKey()), Result, Globally); | ||
ilovepi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| virtual ~ActionCache() = default; | ||
|
|
||
| protected: | ||
| virtual Expected<std::optional<CASID>> getImpl(ArrayRef<uint8_t> ResolvedKey, | ||
| bool Globally) const = 0; | ||
|
|
||
| virtual Error putImpl(ArrayRef<uint8_t> ResolvedKey, const CASID &Result, | ||
| bool Globally) = 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_CASACTIONCACHE_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 @@ | ||
| //===- ActionCache.cpp ------------------------------------------*- C++ -*-===// | ||
| // | ||
| // 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,99 @@ | ||
| //===- ActionCaches.cpp -----------------------------------------*- C++ -*-===// | ||
| // | ||
| // 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 "BuiltinCAS.h" | ||
| #include "llvm/ADT/TrieRawHashMap.h" | ||
| #include "llvm/CAS/ActionCache.h" | ||
| #include "llvm/Support/BLAKE3.h" | ||
|
|
||
| #define DEBUG_TYPE "action-caches" | ||
cachemeifyoucan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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; | ||
| }; | ||
|
|
||
| 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 Globally) final; | ||
| Expected<std::optional<CASID>> getImpl(ArrayRef<uint8_t> ActionKey, | ||
| bool Globally) const final; | ||
|
|
||
| private: | ||
| using DataT = CacheEntry<sizeof(HashType)>; | ||
| using InMemoryCacheT = ThreadSafeTrieRawHashMap<DataT, sizeof(HashType)>; | ||
|
|
||
| InMemoryCacheT Cache; | ||
| }; | ||
| } // end namespace | ||
|
|
||
| static std::string hashToString(ArrayRef<uint8_t> Hash) { | ||
cachemeifyoucan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| SmallString<64> Str; | ||
| toHex(Hash, /*LowerCase=*/true, Str); | ||
| return Str.str().str(); | ||
| } | ||
|
|
||
| static Error createResultCachePoisonedError(StringRef Key, | ||
| const CASContext &Context, | ||
| CASID Output, | ||
| ArrayRef<uint8_t> ExistingOutput) { | ||
| std::string Existing = | ||
| CASID::create(&Context, toStringRef(ExistingOutput)).toString(); | ||
| 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 /*Globally*/) const { | ||
cachemeifyoucan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 /*Globally*/) { | ||
cachemeifyoucan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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(hashToString(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,68 @@ | ||
| //===- ActionCacheTest.cpp ------------------------------------------------===// | ||
| // | ||
| // 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 "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.