[llvm] 0a1e832 - [ADT] Exit doFind on an empty map, not just an unallocated one (#220294)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 1 16:53:39 PDT 2026
Author: Matsu
Date: 2026-09-01T16:53:34-07:00
New Revision: 0a1e8326ea877739c921ef4434b469442de73066
URL: https://github.com/llvm/llvm-project/commit/0a1e8326ea877739c921ef4434b469442de73066
DIFF: https://github.com/llvm/llvm-project/commit/0a1e8326ea877739c921ef4434b469442de73066.diff
LOG: [ADT] Exit doFind on an empty map, not just an unallocated one (#220294)
`doFind` exits early only when no buckets were ever allocated. A map that
had entries and lost them keeps its bucket array. `NumBuckets` is then not
zero, so every lookup hashes the key and probes.
For a `SmallDenseMap` in small mode, `NumBuckets` is the template parameter
`InlineBuckets`, a nonzero constant. The existing check can never fire for
those maps. An empty one hashes and probes on every lookup.
`getNumEntries() == 0` covers both cases. It also subsumes the old check.
There are no entries without buckets, so `Mask = NumBuckets - 1` is still
safe. It is one test either way, so non-empty lookups are unchanged. The
check goes before `getRep()`. Keeping it after costs 0.158% on clang, so
those loads are not sunk past the branch.
| workload | instructions:u |
|---|---:|
| clang compiling 600 LLVM/Clang/MLIR translation units | **-0.028%** |
| `mlir-opt` canonicalize/cse over 1060 `mlir/test` files >4KB |
**-0.291%** |
Three interleaved runs each. Ranges do not overlap. Exit codes are
identical on all 1448 candidate `mlir/test` files.
Adding `LLVM_UNLIKELY` to the branch is a regression, not a win. It takes
clang from -0.133% to -0.007%. Once this is the first check, an empty map
is the common case, not the rare one.
Added:
Modified:
llvm/include/llvm/ADT/DenseMap.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 6d036d4a0729f..3073ab0ec9712 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -721,9 +721,9 @@ class DenseMapBase : public DebugEpochBase {
template <typename LookupKeyT>
const BucketT *doFind(const LookupKeyT &Val) const {
- auto [BucketsPtr, U, NumBuckets] = getRep();
- if (NumBuckets == 0)
+ if (empty())
return nullptr;
+ auto [BucketsPtr, U, NumBuckets] = getRep();
const unsigned Mask = NumBuckets - 1;
unsigned BucketNo = KeyInfoT::getHashValue(Val) & Mask;
More information about the llvm-commits
mailing list