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