[PATCH] D129825: Fix assertion in SmallDenseMap constructor with reserve from non-power-of-2 buckets count
Afanasyev Ivan via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 14 19:38:17 PDT 2022
ivafanas created this revision.
ivafanas added reviewers: dblaikie, atrick, chandlerc, nikic.
Herald added a project: All.
ivafanas requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
`SmallDenseMap` constructor with reserve gets an arbitrary `NumInitBuckets` value and passes it below to `init` method.
If `NumInitBuckets` is greater then `InlineBuckets`, then `SmallDenseMap` initializes to large representation passing `NumInitBuckets` below to `DenseMap` initialization. `DenseMap::initEmpty` method asserts that initial buckets count must be a power of 2.
Proposed solution is to update `NumInitBuckets` value in `SmallDenseMap` constructor till the next power of 2. It should satisfy both `DenseMap` preconditions and required minimum buckets count for reservation.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D129825
Files:
llvm/include/llvm/ADT/DenseMap.h
llvm/unittests/ADT/DenseMapTest.cpp
Index: llvm/unittests/ADT/DenseMapTest.cpp
===================================================================
--- llvm/unittests/ADT/DenseMapTest.cpp
+++ llvm/unittests/ADT/DenseMapTest.cpp
@@ -607,6 +607,15 @@
EXPECT_TRUE(map.find(0) == map.end());
}
+TEST(DenseMapCustomTest, SmallDenseMapWithNumBucketsNonPowerOf2) {
+ // Is not power of 2.
+ const unsigned NumInitBuckets = 33;
+ // Power of 2 less then NumInitBuckets.
+ constexpr unsigned InlineBuckets = 4;
+ // Constructor should not trigger assert.
+ SmallDenseMap<int, int, InlineBuckets> map(NumInitBuckets);
+}
+
TEST(DenseMapCustomTest, TryEmplaceTest) {
DenseMap<int, std::unique_ptr<int>> Map;
std::unique_ptr<int> P(new int(2));
Index: llvm/include/llvm/ADT/DenseMap.h
===================================================================
--- llvm/include/llvm/ADT/DenseMap.h
+++ llvm/include/llvm/ADT/DenseMap.h
@@ -907,6 +907,8 @@
public:
explicit SmallDenseMap(unsigned NumInitBuckets = 0) {
+ if (NumInitBuckets > InlineBuckets)
+ NumInitBuckets = NextPowerOf2(NumInitBuckets - 1);
init(NumInitBuckets);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129825.444859.patch
Type: text/x-patch
Size: 1118 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220715/28038d12/attachment.bin>
More information about the llvm-commits
mailing list