LLVM 22.0.0git
BuiltinObjectHasher.h
Go to the documentation of this file.
1//===- BuiltinObjectHasher.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_BUILTINOBJECTHASHER_H
10#define LLVM_CAS_BUILTINOBJECTHASHER_H
11
13#include "llvm/Support/Endian.h"
14
15namespace llvm::cas {
16
17/// Hasher for stored objects in builtin CAS.
18template <class HasherT> class BuiltinObjectHasher {
19public:
20 using HashT = decltype(HasherT::hash(std::declval<ArrayRef<uint8_t> &>()));
21
25 H.updateSize(Refs.size());
26 for (const ObjectRef &Ref : Refs)
27 H.updateRef(CAS, Ref);
28 H.updateArray(Data);
29 return H.finish();
30 }
31
35 H.updateSize(Refs.size());
36 for (const ArrayRef<uint8_t> &Ref : Refs)
37 H.updateID(Ref);
38 H.updateArray(Data);
39 return H.finish();
40 }
41
42private:
43 HashT finish() { return Hasher.final(); }
44
45 void updateRef(const ObjectStore &CAS, ObjectRef Ref) {
46 updateID(CAS.getID(Ref));
47 }
48
49 void updateID(const CASID &ID) { updateID(ID.getHash()); }
50
51 void updateID(ArrayRef<uint8_t> Hash) {
52 // NOTE: Does not hash the size of the hash. That's a CAS implementation
53 // detail that shouldn't leak into the UUID for an object.
54 assert(Hash.size() == sizeof(HashT) &&
55 "Expected object ref to match the hash size");
56 Hasher.update(Hash);
57 }
58
59 void updateArray(ArrayRef<uint8_t> Bytes) {
60 updateSize(Bytes.size());
61 Hasher.update(Bytes);
62 }
63
64 void updateArray(ArrayRef<char> Bytes) {
65 updateArray(ArrayRef(reinterpret_cast<const uint8_t *>(Bytes.data()),
66 Bytes.size()));
67 }
68
69 void updateSize(uint64_t Size) {
71 Hasher.update(
72 ArrayRef(reinterpret_cast<const uint8_t *>(&Size), sizeof(Size)));
73 }
74
75 BuiltinObjectHasher() = default;
76 ~BuiltinObjectHasher() = default;
77 HasherT Hasher;
78};
79
80} // namespace llvm::cas
81
82#endif // LLVM_CAS_BUILTINOBJECTHASHER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t Size
#define H(x, y, z)
Definition: MD5.cpp:57
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
Hasher for stored objects in builtin CAS.
static HashT hashObject(const ObjectStore &CAS, ArrayRef< ObjectRef > Refs, ArrayRef< char > Data)
static HashT hashObject(ArrayRef< ArrayRef< uint8_t > > Refs, ArrayRef< char > Data)
decltype(HasherT::hash(std::declval< ArrayRef< uint8_t > & >())) HashT
Reference to an object in an ObjectStore instance.
Definition: CASReference.h:108
Content-addressable storage for objects.
Definition: ObjectStore.h:85
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.
value_type byte_swap(value_type value, endianness endian)
Definition: Endian.h:44
@ Ref
The access may reference the value stored in memory.
ArrayRef(const T &OneElt) -> ArrayRef< T >