[llvm] TableGen: Emit perfect hash function for runtime libcalls (PR #150192)
Eli Friedman via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 13 13:46:56 PDT 2025
================
@@ -315,8 +325,183 @@ void RuntimeLibcallEmitter::emitGetRuntimeLibcallEnum(raw_ostream &OS) const {
"#endif\n\n";
}
+// StringMap uses xxh3_64bits, truncated to uint32_t.
+static uint64_t hash(StringRef Str) {
+ return static_cast<uint32_t>(xxh3_64bits(Str));
+}
+
+static void emitHashFunction(raw_ostream &OS) {
+ OS << "static inline uint64_t hash(StringRef Str) {\n"
+ " return static_cast<uint32_t>(xxh3_64bits(Str));\n"
+ "}\n\n";
+}
+
+/// Return the table size, maximum number of collisions for the set of hashes
+static std::pair<int, int>
+computePerfectHashParameters(ArrayRef<uint64_t> Hashes) {
+ const int SizeOverhead = 10;
+ const int NumHashes = Hashes.size();
+
+ // Index derived from hash -> number of collisions.
+ DenseMap<uint64_t, int> Table;
+
+ for (int MaxCollisions = 1;; ++MaxCollisions) {
+ for (int N = NumHashes; N < SizeOverhead * NumHashes; ++N) {
+ Table.clear();
+
+ bool NeedResize = false;
+ for (uint64_t H : Hashes) {
+ uint64_t Idx = H % static_cast<uint64_t>(N);
+ if (++Table[Idx] > MaxCollisions) {
+ // Need to resize the final table if we increased the collision count.
+ NeedResize = true;
+ break;
+ }
+ }
+
+ if (!NeedResize)
+ return {N, MaxCollisions};
+ }
+ }
+
+ llvm_unreachable("loop should terminate");
----------------
efriedma-quic wrote:
No, I mean, the point of llvm_unreachable is to mark codepaths where the compiler can't prove some aspect of the control flow, like a switch statement without a default. It's useless to write `if (x) return 1; else return 2; llvm_unreachable("else wasn't an else");`.
https://github.com/llvm/llvm-project/pull/150192
More information about the llvm-commits
mailing list