[llvm] [ORC] Add automatic shared library resolver for unresolved symbols. (PR #148410)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 7 21:58:02 PDT 2025
================
@@ -0,0 +1,173 @@
+//===--- SymbolFilter.h - Utils for Symbol Filter ---*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_SYMBOLFILTER_H
+#define LLVM_EXECUTIONENGINE_ORC_SHARED_SYMBOLFILTER_H
+
+#include "llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h"
+
+#include <math.h>
+#include <type_traits>
+#include <vector>
+
+namespace llvm {
+namespace orc {
+
+namespace shared {
+using SPSBloomFilter =
+ SPSTuple<bool, uint32_t, uint32_t, uint32_t, SPSSequence<uint64_t>>;
+}
+
+class BloomFilter {
+public:
+ using HashFunc = std::function<uint32_t(StringRef)>;
+
+ BloomFilter() = default;
+ BloomFilter(BloomFilter &&) noexcept = default;
+ BloomFilter &operator=(BloomFilter &&) noexcept = default;
+ BloomFilter(const BloomFilter &) = delete;
+ BloomFilter &operator=(const BloomFilter &) = delete;
+
+ BloomFilter(uint32_t symbolCount, float falsePositiveRate, HashFunc hashFn)
+ : hashFunc(std::move(hashFn)) {
+ initialize(symbolCount, falsePositiveRate);
+ }
+ bool IsInitialized() const { return initialized; }
+
+ void add(StringRef symbol) {
+ assert(initialized);
+ addHash(hashFunc(symbol));
+ }
+
+ bool mayContain(StringRef symbol) const {
+ return !isEmpty() && testHash(hashFunc(symbol));
+ }
+
+ bool isEmpty() const { return symbolCount_ == 0; }
+
+private:
+ friend class shared::SPSSerializationTraits<shared::SPSBloomFilter,
+ BloomFilter>;
+ static constexpr uint32_t bitsPerEntry = 64;
+
+ bool initialized = false;
+ uint32_t symbolCount_ = 0;
+ uint32_t bloomSize = 0;
+ uint32_t bloomShift = 0;
+ std::vector<uint64_t> bloomTable;
+ HashFunc hashFunc;
+
+ void initialize(uint32_t symbolCount, float falsePositiveRate) {
+ assert(symbolCount > 0);
+ symbolCount_ = symbolCount;
+ initialized = true;
+
+ float ln2 = std::log(2.0f);
+ float m = -1.0f * symbolCount * std::log(falsePositiveRate) / (ln2 * ln2);
+ bloomSize = static_cast<uint32_t>(std::ceil(m / bitsPerEntry));
+ bloomShift = std::min(6u, log2ceil(symbolCount));
+ bloomTable.resize(bloomSize, 0);
+ }
+
+ void addHash(uint32_t hash) {
+ uint32_t hash2 = hash >> bloomShift;
+ uint32_t n = (hash / bitsPerEntry) % bloomSize;
+ uint64_t mask =
+ (1ULL << (hash % bitsPerEntry)) | (1ULL << (hash2 % bitsPerEntry));
+ bloomTable[n] |= mask;
+ }
+
+ bool testHash(uint32_t hash) const {
+ uint32_t hash2 = hash >> bloomShift;
+ uint32_t n = (hash / bitsPerEntry) % bloomSize;
+ uint64_t mask =
+ (1ULL << (hash % bitsPerEntry)) | (1ULL << (hash2 % bitsPerEntry));
+ return (bloomTable[n] & mask) == mask;
+ }
+
+ static constexpr uint32_t log2ceil(uint32_t v) {
+ return v <= 1 ? 0 : 32 - countl_zero(v - 1);
+ }
+};
+
+class BloomFilterBuilder {
+public:
+ using HashFunc = BloomFilter::HashFunc;
+
+ BloomFilterBuilder() = default;
+
+ BloomFilterBuilder &setFalsePositiveRate(float rate) {
+ assert(rate > 0.0f && rate < 1.0f);
+ falsePositiveRate = rate;
+ return *this;
+ }
+
+ BloomFilterBuilder &setHashFunction(HashFunc func) {
+ hashFunc = std::move(func);
+ return *this;
+ }
+
+ BloomFilter build(const std::vector<std::string> &symbols) const {
----------------
SahilPatidar wrote:
can try `StringRef` or Range?
https://github.com/llvm/llvm-project/pull/148410
More information about the llvm-commits
mailing list