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
6
7#include <llvm/ADT/ArrayRef.h>
8#include <string>
9#if LLVM_VERSION_MAJOR == 18
10#include <llvm/ADT/StableHashing.h>
11#else
12#include <llvm/CodeGen/StableHashing.h>
13#endif
14
15namespace proteus {
16
17using namespace llvm;
18
19class HashT {
20private:
21 stable_hash Value;
22
23public:
24 inline HashT(const stable_hash HashValue) { Value = HashValue; }
25 inline HashT(StringRef S) { S.getAsInteger(0, Value); }
26 inline stable_hash getValue() const { return Value; }
27 inline std::string toString() const { return std::to_string(Value); }
28 inline bool operator==(const HashT &Other) const {
29 return Value == Other.Value;
30 }
31
32 inline bool operator<(const HashT &Other) const {
33 return Value < Other.Value;
34 }
35};
36
37inline HashT hashValue(HashT &H) { return H; }
38
39inline HashT hashValue(StringRef &S) { return stable_hash_combine_string(S); }
40
41inline HashT hashValue(const std::string &S) {
42 return stable_hash_combine_string(S);
43}
44
45template <typename T>
46inline std::enable_if_t<std::is_scalar<T>::value, HashT> hashValue(const T &V) {
47 return stable_hash_combine_string(
48 StringRef{reinterpret_cast<const char *>(&V), sizeof(T)});
49}
50
52 return stable_hash_combine_string(
53 StringRef{reinterpret_cast<const char *>(&RC.Value), sizeof(RC.Value)});
54}
55
56inline HashT hashValue(const ArrayRef<RuntimeConstant> &Arr) {
57 if (Arr.empty())
58 return 0;
59
60 HashT HashValue = hashArrayRefElement(Arr[0]);
61 for (int I = 1, E = Arr.size(); I < E; ++I)
62 HashValue = stable_hash_combine(HashValue.getValue(),
64
65 return HashValue;
66}
67
69 return stable_hash_combine(A.getValue(), B.getValue());
70}
71
72template <typename FirstT, typename... RestTs>
73inline HashT hash(FirstT &&First, RestTs &&...Rest) {
74 TIMESCOPE(__FUNCTION__);
75 HashT HashValue = hashValue(First);
76
77 (
78 [&HashValue, &Rest]() {
79 HashValue = stable_hash_combine(HashValue.getValue(),
80 hashValue(Rest).getValue());
81 }(),
82 ...);
83
84 return HashValue;
85}
86
87template <typename T> inline HashT hash(T &&Data) {
88 HashT HashValue = hashValue(Data);
89 return HashValue;
90}
91
92} // namespace proteus
93
94namespace std {
95template <> struct hash<proteus::HashT> {
96 std::size_t operator()(const proteus::HashT &Key) const {
97 return Key.getValue();
98 }
99};
100
101} // namespace std
102#endif
#define TIMESCOPE(x)
Definition TimeTracing.hpp:35
Definition Hashing.hpp:19
std::string toString() const
Definition Hashing.hpp:27
bool operator==(const HashT &Other) const
Definition Hashing.hpp:28
stable_hash getValue() const
Definition Hashing.hpp:26
HashT(StringRef S)
Definition Hashing.hpp:25
bool operator<(const HashT &Other) const
Definition Hashing.hpp:32
HashT(const stable_hash HashValue)
Definition Hashing.hpp:24
Definition JitEngine.cpp:20
HashT hash(FirstT &&First, RestTs &&...Rest)
Definition Hashing.hpp:73
HashT hashValue(HashT &H)
Definition Hashing.hpp:37
HashT hashArrayRefElement(const RuntimeConstant &RC)
Definition Hashing.hpp:51
HashT hashCombine(HashT A, HashT B)
Definition Hashing.hpp:68
Definition Hashing.hpp:94
Definition CompilerInterfaceTypes.h:30
RuntimeConstantType Value
Definition CompilerInterfaceTypes.h:43
std::size_t operator()(const proteus::HashT &Key) const
Definition Hashing.hpp:96