LLVM 23.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
24 BuiltinObjectHasher H;
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
34 BuiltinObjectHasher H;
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
42 static Expected<HashT> hashFile(StringRef FilePath);
43
44private:
45 HashT finish() { return Hasher.final(); }
46
47 void updateRef(const ObjectStore &CAS, ObjectRef Ref) {
48 updateID(CAS.getID(Ref));
49 }
50
51 void updateID(const CASID &ID) { updateID(ID.getHash()); }
52
53 void updateID(ArrayRef<uint8_t> Hash) {
54 // NOTE: Does not hash the size of the hash. That's a CAS implementation
55 // detail that shouldn't leak into the UUID for an object.
56 assert(Hash.size() == sizeof(HashT) &&
57 "Expected object ref to match the hash size");
58 Hasher.update(Hash);
59 }
60
61 void updateArray(ArrayRef<uint8_t> Bytes) {
62 updateSize(Bytes.size());
63 Hasher.update(Bytes);
64 }
65
66 void updateArray(ArrayRef<char> Bytes) {
67 updateArray(ArrayRef(reinterpret_cast<const uint8_t *>(Bytes.data()),
68 Bytes.size()));
69 }
70
71 void updateSize(uint64_t Size) {
73 Hasher.update(
74 ArrayRef(reinterpret_cast<const uint8_t *>(&Size), sizeof(Size)));
75 }
76
77 BuiltinObjectHasher() = default;
78 ~BuiltinObjectHasher() = default;
79 HasherT Hasher;
80};
81
82} // namespace llvm::cas
83
84#endif // LLVM_CAS_BUILTINOBJECTHASHER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define H(x, y, z)
Definition MD5.cpp:56
This file contains the declaration of the ObjectStore class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
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
static HashT hashObject(const ObjectStore &CAS, ArrayRef< ObjectRef > Refs, ArrayRef< char > Data)
static Expected< HashT > hashFile(StringRef FilePath)
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.
Content-addressable storage for objects.
Definition ObjectStore.h:90
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.
Definition ModRef.h:32
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
ArrayRef(const T &OneElt) -> ArrayRef< T >