From f3b6d9bac2ff64a0792d3c15f88cdd26534b62e2 Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Mon, 9 May 2022 18:38:52 -0700 Subject: [PATCH 1/2] [CASDB] OnDiskCAS should not create LeafNode when node has refs Fix a bug that OnDiskCAS drops all the references when the data is large and a leaf node is created. Leaf node should only be used when there are no refs. --- llvm/lib/CAS/OnDiskCAS.cpp | 2 +- llvm/unittests/CAS/CASDBTest.cpp | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/llvm/lib/CAS/OnDiskCAS.cpp b/llvm/lib/CAS/OnDiskCAS.cpp index cb06677eab9d6..6c30b24c3e187 100644 --- a/llvm/lib/CAS/OnDiskCAS.cpp +++ b/llvm/lib/CAS/OnDiskCAS.cpp @@ -1903,7 +1903,7 @@ Expected OnDiskCAS::storeNodeImpl(ArrayRef ComputedHash, } // Big leaf nodes. - if (Data.size() > TrieRecord::MaxEmbeddedSize) + if (Refs.empty() && Data.size() > TrieRecord::MaxEmbeddedSize) return createStandaloneLeaf(I, Data); // TODO: Check whether it's worth checking the index for an already existing diff --git a/llvm/unittests/CAS/CASDBTest.cpp b/llvm/unittests/CAS/CASDBTest.cpp index 0901569ba0d49..f6abdc4860ad9 100644 --- a/llvm/unittests/CAS/CASDBTest.cpp +++ b/llvm/unittests/CAS/CASDBTest.cpp @@ -400,15 +400,21 @@ TEST_P(CASDBTest, NodesBig) { if (Storage.size() < SizeE) Storage.resize(SizeE, '\01'); + SmallVector CreatedNodes; // Avoid checking every size because this is an expensive test. Just check - // for data that is 8B-word-aligned, and one less. + // for data that is 8B-word-aligned, and one less. Also appending the created + // nodes as the references in the next block to check references are created + // correctly. for (size_t Size = SizeB; Size < SizeE; Size += WordSize) { for (bool IsAligned : {false, true}) { StringRef Data(Storage.data(), Size - (IsAligned ? 0 : 1)); Optional Node; - ASSERT_THAT_ERROR(CAS->createNode(None, Data).moveInto(Node), Succeeded()); + ASSERT_THAT_ERROR(CAS->createNode(CreatedNodes, Data).moveInto(Node), + Succeeded()); ASSERT_EQ(Data, Node->getData()); ASSERT_EQ(0, Node->getData().end()[0]); + ASSERT_EQ(Node->getNumReferences(), CreatedNodes.size()); + CreatedNodes.emplace_back(Node->getID()); } } } From 0df5d9bca0e2632ae50bdd85337641f20f4c7b18 Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Tue, 10 May 2022 08:03:52 -0700 Subject: [PATCH 2/2] Bump the OnDiskCAS to v4 --- llvm/lib/CAS/OnDiskCAS.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/CAS/OnDiskCAS.cpp b/llvm/lib/CAS/OnDiskCAS.cpp index 6c30b24c3e187..dab6e6d8c98bd 100644 --- a/llvm/lib/CAS/OnDiskCAS.cpp +++ b/llvm/lib/CAS/OnDiskCAS.cpp @@ -765,7 +765,7 @@ class OnDiskCAS : public BuiltinCAS { static constexpr StringLiteral DataPoolFile = "data"; static constexpr StringLiteral ActionCacheFile = "actions"; - static constexpr StringLiteral FilePrefix = "v3."; + static constexpr StringLiteral FilePrefix = "v4."; static constexpr StringLiteral FileSuffixData = ".data"; static constexpr StringLiteral FileSuffixLeaf = ".leaf"; static constexpr StringLiteral FileSuffixLeaf0 = ".leaf+0";