llvm.org GIT mirror llvm / fbaf206
Fixed bug in SmallDenseMap where it wouldn't leave enough space for an empty bucket if the number of values was exactly equal to the small capacity. This led to an infinite loop when finding a non-existent element git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@166492 91177308-0d34-0410-b5e6-96231b3b80d8 Pete Cooper 6 years ago
2 changed file(s) with 36 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
825825 }
826826
827827 void grow(unsigned AtLeast) {
828 if (AtLeast > InlineBuckets)
828 if (AtLeast >= InlineBuckets)
829829 AtLeast = std::max(64, NextPowerOf2(AtLeast));
830830
831831 if (Small) {
832 if (AtLeast <= InlineBuckets)
832 if (AtLeast < InlineBuckets)
833833 return; // Nothing to do.
834834
835835 // First move the inline buckets into a temporary storage.
329329 EXPECT_TRUE(map.find_as("d") == map.end());
330330 }
331331
332 }
332 struct ContiguousDenseMapInfo {
333 static inline unsigned getEmptyKey() { return ~0; }
334 static inline unsigned getTombstoneKey() { return ~0U - 1; }
335 static unsigned getHashValue(const unsigned& Val) { return Val; }
336 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
337 return LHS == RHS;
338 }
339 };
340
341 // Test that filling a small dense map with exactly the number of elements in
342 // the map grows to have enough space for an empty bucket.
343 TEST(DenseMapCustomTest, SmallDenseMapGrowTest) {
344 SmallDenseMap map;
345 // Add some number of elements, then delete a few to leave us some tombstones.
346 // If we just filled the map with 32 elements we'd grow because of not enough
347 // tombstones which masks the issue here.
348 for (unsigned i = 0; i < 20; ++i)
349 map[i] = i + 1;
350 for (unsigned i = 0; i < 10; ++i)
351 map.erase(i);
352 for (unsigned i = 20; i < 32; ++i)
353 map[i] = i + 1;
354
355 // Size tests
356 EXPECT_EQ(22u, map.size());
357
358 // Try to find an element which doesn't exist. There was a bug in
359 // SmallDenseMap which led to a map with num elements == small capacity not
360 // having an empty bucket any more. Finding an element not in the map would
361 // therefore never terminate.
362 EXPECT_TRUE(map.find(32) == map.end());
363 }
364
365 }