LLVM 22.0.0git
SymbolFilter.h
Go to the documentation of this file.
1//===- SymbolFilter.h - Utilities for Symbol Filtering ---------*- 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_EXECUTIONENGINE_ORC_SHARED_SYMBOLFILTER_H
10#define LLVM_EXECUTIONENGINE_ORC_SHARED_SYMBOLFILTER_H
11
13
14#include <cmath>
15#include <vector>
16
17namespace llvm {
18namespace orc {
19
20namespace shared {
23}
24
26public:
27 using HashFunc = std::function<uint32_t(StringRef)>;
28
29 BloomFilter() = default;
30 BloomFilter(BloomFilter &&) noexcept = default;
31 BloomFilter &operator=(BloomFilter &&) noexcept = default;
33 BloomFilter &operator=(const BloomFilter &) = delete;
34
35 BloomFilter(uint32_t SymbolCount, float FalsePositiveRate, HashFunc hashFn)
36 : HashFn(std::move(hashFn)) {
37 initialize(SymbolCount, FalsePositiveRate);
38 }
39 bool isInitialized() const { return Initialized; }
40
41 void add(StringRef Sym) {
42 assert(Initialized);
43 addHash(HashFn(Sym));
44 }
45
46 bool mayContain(StringRef Sym) const {
47 return !isEmpty() && testHash(HashFn(Sym));
48 }
49
50 bool isEmpty() const { return SymbolCount == 0; }
51
52private:
53 friend class shared::SPSSerializationTraits<shared::SPSBloomFilter,
55 static constexpr uint32_t BitsPerEntry = 64;
56
57 bool Initialized = false;
58 uint32_t SymbolCount = 0;
59 uint32_t BloomSize = 0;
60 uint32_t BloomShift = 0;
61 std::vector<uint64_t> BloomTable;
62 HashFunc HashFn;
63
64 void initialize(uint32_t SymCount, float FalsePositiveRate) {
65 assert(SymCount > 0);
66 SymbolCount = SymCount;
67 Initialized = true;
68
69 float ln2 = std::log(2.0f);
70 float M = -1.0f * SymbolCount * std::log(FalsePositiveRate) / (ln2 * ln2);
71 BloomSize = static_cast<uint32_t>(std::ceil(M / BitsPerEntry));
72 BloomShift = std::min(6u, log2ceil(SymbolCount));
73 BloomTable.resize(BloomSize, 0);
74 }
75
76 void addHash(uint32_t Hash) {
77 uint32_t Hash2 = Hash >> BloomShift;
78 uint32_t N = (Hash / BitsPerEntry) % BloomSize;
79 uint64_t Mask =
80 (1ULL << (Hash % BitsPerEntry)) | (1ULL << (Hash2 % BitsPerEntry));
81 BloomTable[N] |= Mask;
82 }
83
84 bool testHash(uint32_t Hash) const {
85 uint32_t Hash2 = Hash >> BloomShift;
86 uint32_t N = (Hash / BitsPerEntry) % BloomSize;
87 uint64_t Mask =
88 (1ULL << (Hash % BitsPerEntry)) | (1ULL << (Hash2 % BitsPerEntry));
89 return (BloomTable[N] & Mask) == Mask;
90 }
91
92 static constexpr uint32_t log2ceil(uint32_t V) {
93 return V <= 1 ? 0 : 32 - countl_zero(V - 1);
94 }
95};
96
98public:
100
102
104 assert(Rate > 0.0f && Rate < 1.0f);
105 FalsePositiveRate = Rate;
106 return *this;
107 }
108
110 HashFn = std::move(Fn);
111 return *this;
112 }
113
115 assert(!Symbols.empty() && "Cannot build filter from empty symbol list.");
116 BloomFilter F(static_cast<uint32_t>(Symbols.size()), FalsePositiveRate,
117 HashFn);
118 for (const auto &Sym : Symbols)
119 F.add(Sym);
120
121 return F;
122 }
123
124private:
125 float FalsePositiveRate = 0.02f;
126 HashFunc HashFn = [](StringRef S) -> uint32_t {
127 uint32_t H = 5381;
128 for (char C : S)
129 H = ((H << 5) + H) + static_cast<uint8_t>(C); // H * 33 + C
130 return H;
131 };
132};
133
134namespace shared {
135
137public:
138 static size_t size(const BloomFilter &Filter) {
139 return SPSBloomFilter::AsArgList::size(
140 Filter.Initialized, Filter.SymbolCount, Filter.BloomSize,
141 Filter.BloomShift, Filter.BloomTable);
142 }
143
144 static bool serialize(SPSOutputBuffer &OB, const BloomFilter &Filter) {
145 return SPSBloomFilter::AsArgList::serialize(
146 OB, Filter.Initialized, Filter.SymbolCount, Filter.BloomSize,
147 Filter.BloomShift, Filter.BloomTable);
148 }
149
151 bool IsInitialized;
152 uint32_t SymbolCount = 0, BloomSize = 0, BloomShift = 0;
153 std::vector<uint64_t> BloomTable;
154
155 if (!SPSBloomFilter::AsArgList::deserialize(
156 IB, IsInitialized, SymbolCount, BloomSize, BloomShift, BloomTable))
157 return false;
158
159 Filter.Initialized = IsInitialized;
160 Filter.SymbolCount = SymbolCount;
161 Filter.BloomSize = BloomSize;
162 Filter.BloomShift = BloomShift;
163 Filter.BloomTable = std::move(BloomTable);
164
165 return true;
166 }
167};
168
169} // end namespace shared
170} // end namespace orc
171} // end namespace llvm
172#endif // LLVM_EXECUTIONENGINE_ORC_SHARED_SYMBOLFILTER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
#define F(x, y, z)
Definition MD5.cpp:54
#define H(x, y, z)
Definition MD5.cpp:56
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
BloomFilter::HashFunc HashFunc
BloomFilterBuilder & setFalsePositiveRate(float Rate)
BloomFilterBuilder & setHashFunction(HashFunc Fn)
BloomFilter build(ArrayRef< StringRef > Symbols) const
bool isInitialized() const
std::function< uint32_t(StringRef)> HashFunc
void add(StringRef Sym)
BloomFilter(BloomFilter &&) noexcept=default
bool mayContain(StringRef Sym) const
Input char buffer with underflow check.
Output char buffer with overflow check.
static bool serialize(SPSOutputBuffer &OB, const BloomFilter &Filter)
static bool deserialize(SPSInputBuffer &IB, BloomFilter &Filter)
Specialize to describe how to serialize/deserialize to/from the given concrete type.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
SPSTuple< bool, uint32_t, uint32_t, uint32_t, SPSSequence< uint64_t > > SPSBloomFilter
This is an optimization pass for GlobalISel generic memory operations.
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition bit.h:236
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1867
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867
#define N