aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/ADT/DenseMapTest.cpp
diff options
context:
space:
mode:
authorVladislav Dzhidzhoev <vdzhidzhoev@accesssoftek.com>2022-07-25 16:39:46 +0000
committerDavid Blaikie <dblaikie@gmail.com>2022-07-25 17:09:44 +0000
commit2c84b92346bc45a88f92d781e30daa8490049c4d (patch)
treea3257b12ac1d8683a2c34c85942b5abde6f98eb8 /llvm/unittests/ADT/DenseMapTest.cpp
parentba49d39b20cc5358da28af2ac82bd336028780bc (diff)
downloadllvm-2c84b92346bc45a88f92d781e30daa8490049c4d.zip
llvm-2c84b92346bc45a88f92d781e30daa8490049c4d.tar.gz
llvm-2c84b92346bc45a88f92d781e30daa8490049c4d.tar.bz2
Fix assertion in SmallDenseMap constructor with reserve from non-power-of-2 buckets count
`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. Reviewed By: atrick Differential Revision: https://reviews.llvm.org/D129825
Diffstat (limited to 'llvm/unittests/ADT/DenseMapTest.cpp')
-rw-r--r--llvm/unittests/ADT/DenseMapTest.cpp9
1 files changed, 9 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp
index 4dd314c..619ddb0 100644
--- a/llvm/unittests/ADT/DenseMapTest.cpp
+++ b/llvm/unittests/ADT/DenseMapTest.cpp
@@ -607,6 +607,15 @@ TEST(DenseMapCustomTest, LargeSmallDenseMapCompaction) {
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));