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 <type_traits>
16#include <vector>
17
18namespace llvm {
19namespace orc {
20
21namespace shared {
24}
25
27public:
28 using HashFunc = std::function<uint32_t(StringRef)>;
29
30 BloomFilter() = default;
31 BloomFilter(BloomFilter &&) noexcept = default;
32 BloomFilter &operator=(BloomFilter &&) noexcept = default;
34 BloomFilter &operator=(const BloomFilter &) = delete;
35
36 BloomFilter(uint32_t SymbolCount, float FalsePositiveRate, HashFunc hashFn)
37 : HashFn(std::move(hashFn)) {
38 initialize(SymbolCount, FalsePositiveRate);
39 }
40 bool isInitialized() const { return Initialized; }
41
42 void add(StringRef Sym) {
43 assert(Initialized);
44 addHash(HashFn(Sym));
45 }
46
47 bool mayContain(StringRef Sym) const {
48 return !isEmpty() && testHash(HashFn(Sym));
49 }
50
51 bool isEmpty() const { return SymbolCount == 0; }
52
53private:
54 friend class shared::SPSSerializationTraits<shared::SPSBloomFilter,
56 static constexpr uint32_t BitsPerEntry = 64;
57
58 bool Initialized = false;
59 uint32_t SymbolCount = 0;
60 uint32_t BloomSize = 0;
61 uint32_t BloomShift = 0;
62 std::vector<uint64_t> BloomTable;
63 HashFunc HashFn;
64
65 void initialize(uint32_t SymCount, float FalsePositiveRate) {
66 assert(SymCount > 0);
67 SymbolCount = SymCount;
68 Initialized = true;
69
70 float ln2 = std::log(2.0f);
71 float M = -1.0f * SymbolCount * std::log(FalsePositiveRate) / (ln2 * ln2);
72 BloomSize = static_cast<uint32_t>(std::ceil(M / BitsPerEntry));
73 BloomShift = std::min(6u, log2ceil(SymbolCount));
74 BloomTable.resize(BloomSize, 0);
75 }
76
77 void addHash(uint32_t Hash) {
78 uint32_t Hash2 = Hash >> BloomShift;
79 uint32_t N = (Hash / BitsPerEntry) % BloomSize;
80 uint64_t Mask =
81 (1ULL << (Hash % BitsPerEntry)) | (1ULL << (Hash2 % BitsPerEntry));
82 BloomTable[N] |= Mask;
83 }
84
85 bool testHash(uint32_t Hash) const {
86 uint32_t Hash2 = Hash >> BloomShift;
87 uint32_t N = (Hash / BitsPerEntry) % BloomSize;
88 uint64_t Mask =
89 (1ULL << (Hash % BitsPerEntry)) | (1ULL << (Hash2 % BitsPerEntry));
90 return (BloomTable[N] & Mask) == Mask;
91 }
92
93 static constexpr uint32_t log2ceil(uint32_t V) {
94 return V <= 1 ? 0 : 32 - countl_zero(V - 1);
95 }
96};
97
99public:
101
103
105 assert(Rate > 0.0f && Rate < 1.0f);
106 FalsePositiveRate = Rate;
107 return *this;
108 }
109
111 HashFn = std::move(Fn);
112 return *this;
113 }
114
116 assert(!Symbols.empty() && "Cannot build filter from empty symbol list.");
117 BloomFilter F(static_cast<uint32_t>(Symbols.size()), FalsePositiveRate,
118 HashFn);
119 for (const auto &Sym : Symbols)
120 F.add(Sym);
121
122 return F;
123 }
124
125private:
126 float FalsePositiveRate = 0.02f;
127 HashFunc HashFn = [](StringRef S) -> uint32_t {
128 uint32_t H = 5381;
129 for (char C : S)
130 H = ((H << 5) + H) + static_cast<uint8_t>(C); // H * 33 + C
131 return H;
132 };
133};
134
135namespace shared {
136
138public:
139 static size_t size(const BloomFilter &Filter) {
140 return SPSBloomFilter::AsArgList::size(
141 Filter.Initialized, Filter.SymbolCount, Filter.BloomSize,
142 Filter.BloomShift, Filter.BloomTable);
143 }
144
145 static bool serialize(SPSOutputBuffer &OB, const BloomFilter &Filter) {
146 return SPSBloomFilter::AsArgList::serialize(
147 OB, Filter.Initialized, Filter.SymbolCount, Filter.BloomSize,
148 Filter.BloomShift, Filter.BloomTable);
149 }
150
152 bool IsInitialized;
153 uint32_t SymbolCount = 0, BloomSize = 0, BloomShift = 0;
154 std::vector<uint64_t> BloomTable;
155
156 if (!SPSBloomFilter::AsArgList::deserialize(
157 IB, IsInitialized, SymbolCount, BloomSize, BloomShift, BloomTable))
158 return false;
159
160 Filter.Initialized = IsInitialized;
161 Filter.SymbolCount = SymbolCount;
162 Filter.BloomSize = BloomSize;
163 Filter.BloomShift = BloomShift;
164 Filter.BloomTable = std::move(BloomTable);
165
166 return true;
167 }
168};
169
170} // end namespace shared
171} // end namespace orc
172} // end namespace llvm
173#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:55
#define H(x, y, z)
Definition MD5.cpp:57
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:41
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