Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
Hashing.h
Go to the documentation of this file.
1#ifndef PROTEUS_HASHING_H
2#define PROTEUS_HASHING_H
3
7
8#include <llvm/ADT/ArrayRef.h>
9#if LLVM_VERSION_MAJOR >= 18
10#include <llvm/ADT/StableHashing.h>
11#else
12#include <llvm/CodeGen/StableHashing.h>
13#endif
14
15#include <string>
16
17namespace proteus {
18
19using namespace llvm;
20
21class HashT {
22private:
23 stable_hash Value;
24
25public:
26 inline HashT(const stable_hash HashValue) { Value = HashValue; }
27 inline HashT(const StringRef &S) { S.getAsInteger(0, Value); }
28 inline stable_hash getValue() const { return Value; }
29 inline std::string toString() const { return std::to_string(Value); }
30 // Returns a suffix for mangled JIT function names. CUDA uses "$" delimiters
31 // because "." generates invalid PTX. HIP/Host use "." for demangle-ability.
32 inline std::string toMangledSuffix() const {
33#if PROTEUS_ENABLE_CUDA
34 return "$jit$" + toString() + "$";
35#else
36 return ".jit." + toString();
37#endif
38 }
39 inline bool operator==(const HashT &Other) const {
40 return Value == Other.Value;
41 }
42
43 inline bool operator<(const HashT &Other) const {
44 return Value < Other.Value;
45 }
46};
47
48inline HashT hashValue(const HashT &H) { return H; }
49
50// Function that abstracts interface differences in stable hashing across LLVM.
51inline HashT hashValue(const StringRef &S) {
52#if LLVM_VERSION_MAJOR >= 20
53 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(S.data()),
54 S.size());
55 return xxh3_64bits(Bytes);
56#else
57 return stable_hash_combine_string(S);
58#endif
59}
60
61inline HashT hashValue(const std::string &S) { return hashValue(StringRef{S}); }
62
63template <typename T>
64inline std::enable_if_t<std::is_scalar<T>::value, HashT> hashValue(const T &V) {
65 return hashValue(StringRef{reinterpret_cast<const char *>(&V), sizeof(T)});
66}
67
68template <typename T>
70 if (RC.ArrInfo.NumElts <= 0)
71 reportFatalError("Invalid number of elements in array: " +
72 std::to_string(RC.ArrInfo.NumElts));
73
74 if (!RC.ArrInfo.Blob)
75 reportFatalError("Expected non-null Blob");
76
77 return hashValue(
78 StringRef{reinterpret_cast<const char *>(RC.ArrInfo.Blob.get()),
79 sizeof(T) * RC.ArrInfo.NumElts});
80}
81
83 if (RC.ObjInfo.Size <= 0)
84 reportFatalError("Invalid object size <= 0");
85
86 if (!RC.ObjInfo.Blob)
87 reportFatalError("Expected non-null Blob");
88
89 return hashValue(
90 StringRef{reinterpret_cast<const char *>(RC.ObjInfo.Blob.get()),
91 static_cast<size_t>(RC.ObjInfo.Size)});
92}
93
98 switch (RC.ArrInfo.EltType) {
100 return hashRuntimeConstantArray<bool>(RC);
102 return hashRuntimeConstantArray<int8_t>(RC);
104 return hashRuntimeConstantArray<int32_t>(RC);
106 return hashRuntimeConstantArray<int64_t>(RC);
108 return hashRuntimeConstantArray<float>(RC);
110 return hashRuntimeConstantArray<double>(RC);
111 default:
112 reportFatalError("Unsupported array element type: " +
114 }
115 } else if (RC.Type == RuntimeConstantType::OBJECT) {
116 return hashRuntimeConstantObject(RC);
117 } else if (isScalarRuntimeConstantType(RC.Type)) {
118 return hashValue(
119 StringRef{reinterpret_cast<const char *>(&RC.Value), sizeof(RC.Value)});
120 }
121
122 reportFatalError("Unsupported type " + toString(RC.Type));
123}
124
125inline HashT hashValue(ArrayRef<RuntimeConstant> Arr) {
126 if (Arr.empty())
127 return 0;
128
129 HashT HashValue = hashArrayRefElement(Arr[0]);
130 for (int I = 1, E = Arr.size(); I < E; ++I)
131 HashValue = stable_hash_combine(HashValue.getValue(),
132 hashArrayRefElement(Arr[I]).getValue());
133
134 return HashValue;
135}
136
138 return stable_hash_combine(A.getValue(), B.getValue());
139}
140
141template <typename FirstT, typename... RestTs>
142inline HashT hash(FirstT &&First, RestTs &&...Rest) {
143 TIMESCOPE(__FUNCTION__);
144 HashT HashValue = hashValue(First);
145
146 ((HashValue = hashCombine(HashValue, hashValue(Rest))), ...);
147
148 return HashValue;
149}
150
151template <typename T> inline HashT hash(T &&Data) {
152 HashT HashValue = hashValue(Data);
153 return HashValue;
154}
155
156} // namespace proteus
157
158namespace std {
159template <> struct hash<proteus::HashT> {
160 std::size_t operator()(const proteus::HashT &Key) const {
161 return Key.getValue();
162 }
163};
164
165} // namespace std
166#endif
#define TIMESCOPE(x)
Definition TimeTracing.h:59
Definition Hashing.h:21
std::string toString() const
Definition Hashing.h:29
bool operator==(const HashT &Other) const
Definition Hashing.h:39
stable_hash getValue() const
Definition Hashing.h:28
bool operator<(const HashT &Other) const
Definition Hashing.h:43
HashT(const stable_hash HashValue)
Definition Hashing.h:26
HashT(const StringRef &S)
Definition Hashing.h:27
std::string toMangledSuffix() const
Definition Hashing.h:32
Definition CompiledLibrary.h:7
Definition MemoryCache.h:26
HashT hashValue(const HashT &H)
Definition Hashing.h:48
@ 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.h:142
void reportFatalError(const llvm::Twine &Reason, const char *FILE, unsigned Line)
Definition Error.cpp:14
HashT hashArrayRefElement(const RuntimeConstant &RC)
Definition Hashing.h:94
HashT hashCombine(HashT A, HashT B)
Definition Hashing.h:137
std::string toString(CodegenOption Option)
Definition Config.h:28
HashT hashRuntimeConstantObject(const RuntimeConstant &RC)
Definition Hashing.h:82
bool isScalarRuntimeConstantType(RuntimeConstantType RCType)
Definition RuntimeConstantTypeHelpers.h:148
HashT hashRuntimeConstantArray(const RuntimeConstant &RC)
Definition Hashing.h:69
Definition Hashing.h:158
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.h:160