Index: llvm/include/llvm/ADT/SmallVector.h =================================================================== --- llvm/include/llvm/ADT/SmallVector.h +++ llvm/include/llvm/ADT/SmallVector.h @@ -136,11 +136,20 @@ this->Size = this->Capacity = 0; // FIXME: Setting Capacity to 0 is suspect. } - void assertSafeToPush(const void *Elt) { - assert( - (Elt < begin() || Elt >= end() || this->size() < this->capacity()) && - "Attempting to push_back to the vector an element of the vector without" - " enough space reserved"); + /// Check whether Elt will be invalidated by resizing the vector to NewSize. + void assertSafeToReferenceAfterResize(const void *Elt, size_t NewSize) { + assert((Elt >= this->end() || + (NewSize <= this->size() + ? Elt < this->begin() + NewSize + : (Elt < this->begin() || NewSize <= this->capacity()))) && + "Attempting to reference an element of the vector in an operation " + "that invalidates it"); + } + + /// Check whether Elt will be invalidated by increasing the size of the + /// vector by N. + void assertSafeToAdd(const void *Elt, size_t N = 1) { + return this->assertSafeToReferenceAfterResize(Elt, this->size() + N); } public: @@ -258,7 +267,7 @@ public: void push_back(const T &Elt) { - this->assertSafeToPush(&Elt); + this->assertSafeToAdd(&Elt); if (LLVM_UNLIKELY(this->size() >= this->capacity())) this->grow(); ::new ((void*) this->end()) T(Elt); @@ -266,7 +275,7 @@ } void push_back(T &&Elt) { - this->assertSafeToPush(&Elt); + this->assertSafeToAdd(&Elt); if (LLVM_UNLIKELY(this->size() >= this->capacity())) this->grow(); ::new ((void*) this->end()) T(::std::move(Elt)); @@ -362,7 +371,7 @@ public: void push_back(const T &Elt) { - this->assertSafeToPush(&Elt); + this->assertSafeToAdd(&Elt); if (LLVM_UNLIKELY(this->size() >= this->capacity())) this->grow(); memcpy(reinterpret_cast(this->end()), &Elt, sizeof(T)); @@ -418,6 +427,7 @@ } 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); @@ -464,6 +474,7 @@ /// Append \p NumInputs copies of \p Elt to the end. void append(size_type NumInputs, const T &Elt) { + this->assertSafeToAdd(&Elt, NumInputs); if (NumInputs > this->capacity() - this->size()) this->grow(this->size()+NumInputs); @@ -479,6 +490,7 @@ // re-initializing them - for all assign(...) variants. void assign(size_type NumElts, const T &Elt) { + this->assertSafeToReferenceAfterResize(&Elt, 0); clear(); if (this->capacity() < NumElts) this->grow(NumElts); @@ -543,6 +555,9 @@ assert(I >= this->begin() && "Insertion iterator is out of bounds."); assert(I <= this->end() && "Inserting past the end of the vector."); + // Check that adding an element won't invalidate Elt. + this->assertSafeToAdd(&Elt); + if (this->size() >= this->capacity()) { size_t EltNo = I-this->begin(); this->grow(); @@ -583,6 +598,9 @@ assert(I >= this->begin() && "Insertion iterator is out of bounds."); assert(I <= this->end() && "Inserting past the end of the vector."); + // Check that adding NumToInsert elements won't invalidate Elt. + this->assertSafeToAdd(&Elt, NumToInsert); + // Ensure there is enough space. reserve(this->size() + NumToInsert); @@ -686,7 +704,9 @@ insert(I, IL.begin(), IL.end()); } - template reference emplace_back(ArgTypes &&... Args) { +private: + template + reference emplace_back_impl(ArgTypes &&... Args) { if (LLVM_UNLIKELY(this->size() >= this->capacity())) this->grow(); ::new ((void *)this->end()) T(std::forward(Args)...); @@ -694,6 +714,40 @@ return this->back(); } +public: + reference emplace_back() { return emplace_back_impl(); } + + template < + class ArgType, + std::enable_if_t< + !std::is_same>, + T>::value, + bool> = true> + reference emplace_back(ArgType &&Arg) { + return emplace_back_impl(std::forward(Arg)); + } + + template < + class ArgType, + std::enable_if_t< + std::is_same>, + T>::value, + bool> = true> + reference emplace_back(ArgType &&Arg) { + // ArgType is a T and may be a reference into this vector. Check whether + // it's safe before forwarding to emplace_back_impl. + this->assertSafeToAdd(&Arg); + return emplace_back_impl(std::forward(Arg)); + } + + template + reference emplace_back(ArgType1 &&Arg1, ArgType2 &&Arg2, + ArgTypes &&... Args) { + return emplace_back_impl(std::forward(Arg1), + std::forward(Arg2), + std::forward(Args)...); + } + SmallVectorImpl &operator=(const SmallVectorImpl &RHS); SmallVectorImpl &operator=(SmallVectorImpl &&RHS); Index: llvm/unittests/ADT/SmallVectorTest.cpp =================================================================== --- llvm/unittests/ADT/SmallVectorTest.cpp +++ llvm/unittests/ADT/SmallVectorTest.cpp @@ -252,7 +252,9 @@ this->theVector.push_back(Constructable(2)); this->assertValuesInOrder(this->theVector, 2u, 1, 2); - // Insert at beginning + // Insert at beginning. Reserve space to avoid reference invalidation from + // this->theVector[1]. + this->theVector.reserve(this->theVector.size() + 1); this->theVector.insert(this->theVector.begin(), this->theVector[1]); this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2); @@ -999,4 +1001,119 @@ EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2})); } +template +class SmallVectorReferenceInvalidationTest : public SmallVectorTestBase { +protected: + const char *AssertionMessage = + "Attempting to reference an element of the vector in an operation \" " + "\"that invalidates it"; + + VectorT V; + + template + static unsigned NumBuiltinElts(const SmallVector &) { + return N; + } + + void SetUp() override { + SmallVectorTestBase::SetUp(); + + // Fill up the small size so that insertions move the elements. + V.push_back(0); + } +}; + +// Test one type that's trivially copyable (int) and one that isn't +// (Constructable) since reference invalidation may be fixed differently for +// each. +using SmallVectorReferenceInvalidationTestTypes = + ::testing::Types, SmallVector>; + +TYPED_TEST_CASE(SmallVectorReferenceInvalidationTest, + SmallVectorReferenceInvalidationTestTypes); + +TYPED_TEST(SmallVectorReferenceInvalidationTest, PushBack) { + auto &V = this->V; + (void)V; +#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST + EXPECT_DEATH(V.push_back(V.back()), this->AssertionMessage); +#endif +} + +TYPED_TEST(SmallVectorReferenceInvalidationTest, PushBackMoved) { + auto &V = this->V; + (void)V; +#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST + EXPECT_DEATH(V.push_back(std::move(V.back())), this->AssertionMessage); +#endif +} + +TYPED_TEST(SmallVectorReferenceInvalidationTest, Resize) { + auto &V = this->V; + (void)V; +#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST + EXPECT_DEATH(V.resize(2, V.back()), this->AssertionMessage); +#endif +} + +TYPED_TEST(SmallVectorReferenceInvalidationTest, Append) { + auto &V = this->V; + (void)V; +#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST + EXPECT_DEATH(V.append(1, V.back()), this->AssertionMessage); +#endif +} + +TYPED_TEST(SmallVectorReferenceInvalidationTest, Assign) { + auto &V = this->V; + (void)V; +#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST + // Regardless of capacity, assign should never reference an internal element. + EXPECT_DEATH(V.assign(1, V.back()), this->AssertionMessage); + EXPECT_DEATH(V.assign(2, V.back()), this->AssertionMessage); + EXPECT_DEATH(V.assign(3, V.back()), this->AssertionMessage); +#endif +} + +TYPED_TEST(SmallVectorReferenceInvalidationTest, Insert) { + auto &V = this->V; + (void)V; +#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST + EXPECT_DEATH(V.insert(V.begin(), V.back()), this->AssertionMessage); +#endif +} + +TYPED_TEST(SmallVectorReferenceInvalidationTest, InsertMoved) { + auto &V = this->V; + (void)V; +#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST + EXPECT_DEATH(V.insert(V.begin(), std::move(V.back())), + this->AssertionMessage); +#endif +} + +TYPED_TEST(SmallVectorReferenceInvalidationTest, InsertN) { + auto &V = this->V; + (void)V; +#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST + EXPECT_DEATH(V.insert(V.begin(), 2, V.back()), this->AssertionMessage); +#endif +} + +TYPED_TEST(SmallVectorReferenceInvalidationTest, EmplaceBack) { + auto &V = this->V; + (void)V; +#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST + EXPECT_DEATH(V.emplace_back(V.back()), this->AssertionMessage); +#endif +} + +TYPED_TEST(SmallVectorReferenceInvalidationTest, EmplaceBackMoved) { + auto &V = this->V; + (void)V; +#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST + EXPECT_DEATH(V.emplace_back(std::move(V.back())), this->AssertionMessage); +#endif +} + } // end namespace Index: llvm/utils/TableGen/CodeGenSchedule.cpp =================================================================== --- llvm/utils/TableGen/CodeGenSchedule.cpp +++ llvm/utils/TableGen/CodeGenSchedule.cpp @@ -1540,6 +1540,7 @@ if (SchedRW.IsVariadic) { unsigned OperIdx = RWSequences.size()-1; // Make N-1 copies of this transition's last sequence. + RWSequences.reserve(RWSequences.size() + SelectedRWs.size() - 1); RWSequences.insert(RWSequences.end(), SelectedRWs.size() - 1, RWSequences[OperIdx]); // Push each of the N elements of the SelectedRWs onto a copy of the last @@ -1625,6 +1626,7 @@ // any transition with compatible CPU ID. // In such case we create new empty transition with zero (AnyCPU) // index. + TransVec.reserve(TransVec.size() + 1); TransVec.emplace_back(TransVec[StartIdx].PredTerm); TransVec.back().ReadSequences.emplace_back(); CollectAndAddVariants(TransVec.size() - 1, SchedRW);