Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
Hashing.hpp
Go to the documentation of this file.
1#ifndef PROTEUS_HASHING_HPP
2#define PROTEUS_HASHING_HPP
3
7
8#include <llvm/ADT/ArrayRef.h>
9#include <string>
10#if LLVM_VERSION_MAJOR >= 18
11#include <llvm/ADT/StableHashing.h>
12#else
13#include <llvm/CodeGen/StableHashing.h>
14#endif
15
16namespace proteus {
17
18using namespace llvm;
19
20class HashT {
21private:
22 stable_hash Value;
23
24public:
25 inline HashT(const stable_hash HashValue) { Value = HashValue; }
26 inline HashT(StringRef S) { S.getAsInteger(0, Value); }
27 inline stable_hash getValue() const { return Value; }
28 inline std::string toString() const { return std::to_string(Value); }
29 inline bool operator==(const HashT &Other) const {
30 return Value == Other.Value;
31 }
32
33 inline bool operator<(const HashT &Other) const {
34 return Value < Other.Value;
35 }
36};
37
38inline HashT hashValue(HashT &H) { return H; }
39
40inline HashT hashValue(StringRef &S) { return stable_hash_combine_string(S); }
41
42inline HashT hashValue(const std::string &S) {
43 return stable_hash_combine_string(S);
44}
45
46template <typename T>
47inline std::enable_if_t<std::is_scalar<T>::value, HashT> hashValue(const T &V) {
48 return stable_hash_combine_string(
49 StringRef{reinterpret_cast<const char *>(&V), sizeof(T)});
50}
51
52template <typename T>
54 if (RC.ArrInfo.NumElts <= 0)
55 PROTEUS_FATAL_ERROR("Invalid number of elements in array: " +
56 std::to_string(RC.ArrInfo.NumElts));
57
58 if (!RC.ArrInfo.Blob)
59 PROTEUS_FATAL_ERROR("Expected non-null Blob");
60
61 return stable_hash_combine_string(
62 StringRef{reinterpret_cast<const char *>(RC.ArrInfo.Blob.get()),
63 sizeof(T) * RC.ArrInfo.NumElts});
64}
65
67 if (RC.ObjInfo.Size <= 0)
68 PROTEUS_FATAL_ERROR("Invalid object size <= 0");
69
70 if (!RC.ObjInfo.Blob)
71 PROTEUS_FATAL_ERROR("Expected non-null Blob");
72
73 return stable_hash_combine_string(
74 StringRef{reinterpret_cast<const char *>(RC.ObjInfo.Blob.get()),
75 static_cast<size_t>(RC.ObjInfo.Size)});
76}
77
82 switch (RC.ArrInfo.EltType) {
84 return hashRuntimeConstantArray<bool>(RC);
86 return hashRuntimeConstantArray<int8_t>(RC);
88 return hashRuntimeConstantArray<int32_t>(RC);
90 return hashRuntimeConstantArray<int64_t>(RC);
92 return hashRuntimeConstantArray<float>(RC);
94 return hashRuntimeConstantArray<double>(RC);
95 default:
96 PROTEUS_FATAL_ERROR("Unsupported array element type: " +
98 }
99 } else if (RC.Type == RuntimeConstantType::OBJECT) {
100 return hashRuntimeConstantObject(RC);
101 } else if (isScalarRuntimeConstantType(RC.Type)) {
102 return stable_hash_combine_string(
103 StringRef{reinterpret_cast<const char *>(&RC.Value), sizeof(RC.Value)});
104 }
105
106 PROTEUS_FATAL_ERROR("Unsupported type " + toString(RC.Type));
107}
108
109inline HashT hashValue(ArrayRef<RuntimeConstant> Arr) {
110 if (Arr.empty())
111 return 0;
112
113 HashT HashValue = hashArrayRefElement(Arr[0]);
114 for (int I = 1, E = Arr.size(); I < E; ++I)
115 HashValue = stable_hash_combine(HashValue.getValue(),
116 hashArrayRefElement(Arr[I]).getValue());
117
118 return HashValue;
119}
120
122 return stable_hash_combine(A.getValue(), B.getValue());
123}
124
125template <typename FirstT, typename... RestTs>
126inline HashT hash(FirstT &&First, RestTs &&...Rest) {
127 TIMESCOPE(__FUNCTION__);
128 HashT HashValue = hashValue(First);
129
130 (
131 [&HashValue, &Rest]() {
132 HashValue = stable_hash_combine(HashValue.getValue(),
133 hashValue(Rest).getValue());
134 }(),
135 ...);
136
137 return HashValue;
138}
139
140template <typename T> inline HashT hash(T &&Data) {
141 HashT HashValue = hashValue(Data);
142 return HashValue;
143}
144
145} // namespace proteus
146
147namespace std {
148template <> struct hash<proteus::HashT> {
149 std::size_t operator()(const proteus::HashT &Key) const {
150 return Key.getValue();
151 }
152};
153
154} // namespace std
155#endif
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:7
#define TIMESCOPE(x)
Definition TimeTracing.hpp:64
Definition Hashing.hpp:20
std::string toString() const
Definition Hashing.hpp:28
bool operator==(const HashT &Other) const
Definition Hashing.hpp:29
stable_hash getValue() const
Definition Hashing.hpp:27
HashT(StringRef S)
Definition Hashing.hpp:26
bool operator<(const HashT &Other) const
Definition Hashing.hpp:33
HashT(const stable_hash HashValue)
Definition Hashing.hpp:25
Definition Helpers.h:76
Definition BuiltinsCUDA.cpp:4
@ ARRAY
Definition CompilerInterfaceTypes.h:33
@ VECTOR
Definition CompilerInterfaceTypes.h:32
@ INT32
Definition CompilerInterfaceTypes.h:25
@ INT64
Definition CompilerInterfaceTypes.h:26
@ FLOAT
Definition CompilerInterfaceTypes.h:27
@ STATIC_ARRAY
Definition CompilerInterfaceTypes.h:31
@ INT8
Definition CompilerInterfaceTypes.h:24
@ BOOL
Definition CompilerInterfaceTypes.h:23
@ OBJECT
Definition CompilerInterfaceTypes.h:34
@ DOUBLE
Definition CompilerInterfaceTypes.h:28
HashT hash(FirstT &&First, RestTs &&...Rest)
Definition Hashing.hpp:126
T getValue(const RuntimeConstant &RC)
Definition RuntimeConstantTypeHelpers.h:95
HashT hashValue(HashT &H)
Definition Hashing.hpp:38
HashT hashArrayRefElement(const RuntimeConstant &RC)
Definition Hashing.hpp:78
HashT hashCombine(HashT A, HashT B)
Definition Hashing.hpp:121
std::string toString(CodegenOption Option)
Definition Config.hpp:23
HashT hashRuntimeConstantObject(const RuntimeConstant &RC)
Definition Hashing.hpp:66
bool isScalarRuntimeConstantType(RuntimeConstantType RCType)
Definition RuntimeConstantTypeHelpers.h:148
HashT hashRuntimeConstantArray(const RuntimeConstant &RC)
Definition Hashing.hpp:53
Definition Hashing.hpp:147
int32_t NumElts
Definition CompilerInterfaceTypes.h:42
RuntimeConstantType EltType
Definition CompilerInterfaceTypes.h:43
std::shared_ptr< unsigned char[]> Blob
Definition CompilerInterfaceTypes.h:44
std::shared_ptr< unsigned char[]> Blob
Definition CompilerInterfaceTypes.h:53
int32_t Size
Definition CompilerInterfaceTypes.h:51
Definition CompilerInterfaceTypes.h:72
ArrayInfo ArrInfo
Definition CompilerInterfaceTypes.h:78
RuntimeConstantValue Value
Definition CompilerInterfaceTypes.h:73
RuntimeConstantType Type
Definition CompilerInterfaceTypes.h:74
ObjectInfo ObjInfo
Definition CompilerInterfaceTypes.h:79
std::size_t operator()(const proteus::HashT &Key) const
Definition Hashing.hpp:149