[llvm] 40e8772 - ADT: Weaken SmallVector::resize assertion from 5abf76fbe37380874a88cc9aa02164800e4e10f3

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 19 17:27:46 PST 2020


Author: Duncan P. N. Exon Smith
Date: 2020-11-19T17:25:36-08:00
New Revision: 40e877264dc5bd9a025bc1fc0f01fb69a3f3cdc2

URL: https://github.com/llvm/llvm-project/commit/40e877264dc5bd9a025bc1fc0f01fb69a3f3cdc2
DIFF: https://github.com/llvm/llvm-project/commit/40e877264dc5bd9a025bc1fc0f01fb69a3f3cdc2.diff

LOG: ADT: Weaken SmallVector::resize assertion from 5abf76fbe37380874a88cc9aa02164800e4e10f3

There's no need to check for reference invalidation when
`SmallVector::resize` is shrinking; the parameter isn't accessed.

Differential Revision: https://reviews.llvm.org/D91832

Added: 
    

Modified: 
    llvm/include/llvm/ADT/SmallVector.h
    llvm/unittests/ADT/SmallVectorTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index 08274ead873b..18463214df80 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -462,11 +462,11 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
   }
 
   void resize(size_type N, const T &NV) {
-    this->assertSafeToReferenceAfterResize(&NV, N);
     if (N < this->size()) {
       this->destroy_range(this->begin()+N, this->end());
       this->set_size(N);
     } else if (N > this->size()) {
+      this->assertSafeToReferenceAfterResize(&NV, N);
       if (this->capacity() < N)
         this->grow(N);
       std::uninitialized_fill(this->end(), this->begin()+N, NV);

diff  --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp
index 1a61d2304d51..74b6561db83d 100644
--- a/llvm/unittests/ADT/SmallVectorTest.cpp
+++ b/llvm/unittests/ADT/SmallVectorTest.cpp
@@ -1051,9 +1051,13 @@ TYPED_TEST(SmallVectorReferenceInvalidationTest, PushBackMoved) {
 TYPED_TEST(SmallVectorReferenceInvalidationTest, Resize) {
   auto &V = this->V;
   (void)V;
+  int N = this->NumBuiltinElts(V);
 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
-  EXPECT_DEATH(V.resize(2, V.back()), this->AssertionMessage);
+  EXPECT_DEATH(V.resize(N + 1, V.back()), this->AssertionMessage);
 #endif
+
+  // No assertion when shrinking, since the parameter isn't accessed.
+  V.resize(N - 1, V.back());
 }
 
 TYPED_TEST(SmallVectorReferenceInvalidationTest, Append) {


        


More information about the llvm-commits mailing list