LLVM 22.0.0git
BuiltinCASContext.h
Go to the documentation of this file.
1//===- BuiltinCASContext.h --------------------------------------*- 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
9#ifndef LLVM_CAS_BUILTINCASCONTEXT_H
10#define LLVM_CAS_BUILTINCASCONTEXT_H
11
12#include "llvm/CAS/CASID.h"
13#include "llvm/Support/BLAKE3.h"
14#include "llvm/Support/Error.h"
15
17
18/// Current hash type for the builtin CAS.
19///
20/// FIXME: This should be configurable via an enum to allow configuring the hash
21/// function. The enum should be sent into \a createInMemoryCAS() and \a
22/// createOnDiskCAS().
23///
24/// This is important (at least) for future-proofing, when we want to make new
25/// CAS instances use BLAKE7, but still know how to read/write BLAKE3.
26///
27/// Even just for BLAKE3, it would be useful to have these values:
28///
29/// BLAKE3 => 32B hash from BLAKE3
30/// BLAKE3_16B => 16B hash from BLAKE3 (truncated)
31///
32/// ... where BLAKE3_16 uses \a TruncatedBLAKE3<16>.
33///
34/// Motivation for a truncated hash is that it's cheaper to store. It's not
35/// clear if we always (or ever) need the full 32B, and for an ephemeral
36/// in-memory CAS, we almost certainly don't need it.
37///
38/// Note that the cost is linear in the number of objects for the builtin CAS,
39/// since we're using internal offsets and/or pointers as an optimization.
40///
41/// However, it's possible we'll want to hook up a local builtin CAS to, e.g.,
42/// a distributed generic hash map to use as an ActionCache. In that scenario,
43/// the transitive closure of the structured objects that are the results of
44/// the cached actions would need to be serialized into the map, something
45/// like:
46///
47/// "action:<schema>:<key>" -> "0123"
48/// "object:<schema>:0123" -> "3,4567,89AB,CDEF,9,some data"
49/// "object:<schema>:4567" -> ...
50/// "object:<schema>:89AB" -> ...
51/// "object:<schema>:CDEF" -> ...
52///
53/// These references would be full cost.
55using HashType = decltype(HasherT::hash(std::declval<ArrayRef<uint8_t> &>()));
56
57/// CASContext for LLVM builtin CAS using BLAKE3 hash type.
59 void printIDImpl(raw_ostream &OS, const CASID &ID) const final;
60 void anchor() override;
61
62public:
63 /// Get the name of the hash for any table identifiers.
64 ///
65 /// FIXME: This should be configurable via an enum, with at the following
66 /// values:
67 ///
68 /// "BLAKE3" => 32B hash from BLAKE3
69 /// "BLAKE3.16" => 16B hash from BLAKE3 (truncated)
70 ///
71 /// Enum can be sent into \a createInMemoryCAS() and \a createOnDiskCAS().
72 static StringRef getHashName() { return "BLAKE3"; }
74 static const std::string ID =
75 ("llvm.cas.builtin.v2[" + getHashName() + "]").str();
76 return ID;
77 }
78
80
81 BuiltinCASContext() = default;
82
83 static Expected<HashType> parseID(StringRef PrintedDigest);
84 static void printID(ArrayRef<uint8_t> Digest, raw_ostream &OS);
85};
86
87} // namespace llvm::cas::builtin
88
89#endif // LLVM_CAS_BUILTINCASCONTEXT_H
aarch64 promote const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
A class that wraps the BLAKE3 algorithm.
Definition BLAKE3.h:38
static BLAKE3Result< NumBytes > hash(ArrayRef< uint8_t > Data)
Returns a BLAKE3 hash for the given data.
Definition BLAKE3.h:86
Tagged union holding either a T or a Error.
Definition Error.h:485
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Context for CAS identifiers.
Definition CASID.h:28
friend class CASID
Definition CASID.h:44
CASContext for LLVM builtin CAS using BLAKE3 hash type.
StringRef getHashSchemaIdentifier() const final
Get an identifer for the schema used by this CAS context.
static const BuiltinCASContext & getDefaultContext()
static Expected< HashType > parseID(StringRef PrintedDigest)
static void printID(ArrayRef< uint8_t > Digest, raw_ostream &OS)
static StringRef getHashName()
Get the name of the hash for any table identifiers.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
BLAKE3 HasherT
Current hash type for the builtin CAS.
decltype(HasherT::hash(std::declval< ArrayRef< uint8_t > & >())) HashType