[llvm] 1542114 - [ADT] Fix SmallVector append with input iterators (#191030)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 9 11:06:01 PDT 2026
Author: Alexis Engelke
Date: 2026-04-09T18:05:54Z
New Revision: 1542114a1aea30b6fd7167ddd51f54c99882b309
URL: https://github.com/llvm/llvm-project/commit/1542114a1aea30b6fd7167ddd51f54c99882b309
DIFF: https://github.com/llvm/llvm-project/commit/1542114a1aea30b6fd7167ddd51f54c99882b309.diff
LOG: [ADT] Fix SmallVector append with input iterators (#191030)
append()/assign()/insert() iterate over the iterator twice -- once to
get the length and once to actually append the content. This is only
permitted with forward iterators, not input iterators.
For append()/assign(), implement a version that uses emplace_back() in
a loop. insert() at the end is append(); for insert() in the middle,
append() elements first and then rotate them into their correct
position.
Originally introduced in https://reviews.llvm.org/D33919.
Added:
Modified:
llvm/include/llvm/ADT/SmallVector.h
llvm/include/llvm/IR/Metadata.h
llvm/include/llvm/SandboxIR/User.h
llvm/unittests/ADT/SmallVectorTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index 23d40c3e07675..7f63b569643df 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -38,10 +38,13 @@ template <typename T> class ArrayRef;
template <typename IteratorT> class iterator_range;
+template <class Iterator, class Tag>
+using HasIteratorTag = std::is_convertible<
+ typename std::iterator_traits<Iterator>::iterator_category, Tag>;
+
template <class Iterator>
-using EnableIfConvertibleToInputIterator = std::enable_if_t<std::is_convertible<
- typename std::iterator_traits<Iterator>::iterator_category,
- std::input_iterator_tag>::value>;
+using EnableIfConvertibleToInputIterator =
+ std::enable_if_t<HasIteratorTag<Iterator, std::input_iterator_tag>::value>;
/// This is all the stuff common to all SmallVectors.
///
@@ -684,11 +687,17 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
/// Add the specified range to the end of the SmallVector.
template <typename ItTy, typename = EnableIfConvertibleToInputIterator<ItTy>>
void append(ItTy in_start, ItTy in_end) {
- this->assertSafeToAddRange(in_start, in_end);
- size_type NumInputs = std::distance(in_start, in_end);
- this->reserve(this->size() + NumInputs);
- this->uninitialized_copy(in_start, in_end, this->end());
- this->set_size(this->size() + NumInputs);
+ if constexpr (HasIteratorTag<ItTy, std::forward_iterator_tag>::value) {
+ this->assertSafeToAddRange(in_start, in_end);
+ size_type NumInputs = std::distance(in_start, in_end);
+ this->reserve(this->size() + NumInputs);
+ this->uninitialized_copy(in_start, in_end, this->end());
+ this->set_size(this->size() + NumInputs);
+ } else {
+ // Input iterator, we can't know ahead how many elements we'll add.
+ for (; in_start != in_end; ++in_start)
+ this->emplace_back(*in_start);
+ }
}
/// Append \p NumInputs copies of \p Elt to the end.
@@ -890,6 +899,15 @@ class SmallVectorImpl : public SmallVectorTemplateBase<T> {
return this->begin()+InsertElt;
}
+ if constexpr (!HasIteratorTag<ItTy, std::forward_iterator_tag>::value) {
+ // For input iterators, we don't know the number of elements to insert.
+ size_t OldSize = this->size();
+ append(From, To);
+ I = this->begin() + InsertElt; // Uninvalidate the iterator.
+ std::rotate(I, this->begin() + OldSize, this->end());
+ return I;
+ }
+
assert(this->isReferenceToStorage(I) && "Insertion iterator is out of bounds.");
// Check that the reserve that follows doesn't invalidate the iterators.
diff --git a/llvm/include/llvm/IR/Metadata.h b/llvm/include/llvm/IR/Metadata.h
index f6c393c1d8c67..09bdb79a4dfda 100644
--- a/llvm/include/llvm/IR/Metadata.h
+++ b/llvm/include/llvm/IR/Metadata.h
@@ -1625,7 +1625,7 @@ template <class T> class TypedMDOperandIterator {
MDNode::op_iterator I = nullptr;
public:
- using iterator_category = std::input_iterator_tag;
+ using iterator_category = std::forward_iterator_tag;
using value_type = T *;
using
diff erence_type = std::ptr
diff _t;
using pointer = void;
diff --git a/llvm/include/llvm/SandboxIR/User.h b/llvm/include/llvm/SandboxIR/User.h
index c552e2e3378be..2404ca40a72ae 100644
--- a/llvm/include/llvm/SandboxIR/User.h
+++ b/llvm/include/llvm/SandboxIR/User.h
@@ -34,7 +34,7 @@ class OperandUseIterator {
using value_type = sandboxir::Use;
using pointer = value_type *;
using reference = value_type &;
- using iterator_category = std::input_iterator_tag;
+ using iterator_category = std::forward_iterator_tag;
OperandUseIterator() = default;
LLVM_ABI value_type operator*() const;
diff --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp
index dbc626db54482..c4934d53ff3ce 100644
--- a/llvm/unittests/ADT/SmallVectorTest.cpp
+++ b/llvm/unittests/ADT/SmallVectorTest.cpp
@@ -533,6 +533,50 @@ TYPED_TEST(SmallVectorTest, AppendNonIterTest) {
assertValuesInOrder(V, 3u, 1, 7, 7);
}
+struct input_iterator {
+ using iterator_category = std::input_iterator_tag;
+ using value_type = int;
+ using
diff erence_type = int;
+ using pointer = value_type *;
+ using reference = value_type &;
+
+ const int **State;
+ int operator*() const { return **State; }
+ input_iterator &operator++() {
+ (*State)++;
+ return *this;
+ }
+ bool operator!=(const input_iterator &Other) const {
+ return *State != *Other.State;
+ }
+};
+
+TYPED_TEST(SmallVectorTest, AppendInputIterator) {
+ auto &V = this->theVector;
+ V.push_back(1);
+ static constexpr int Src[] = {5, 6, 7, 8};
+ // Construct an input iterator that actually returns
diff erent results on the
+ // second iteration.
+ const int *BeginState = &Src[0];
+ const int *EndState = &Src[2];
+ V.append(input_iterator{&BeginState}, input_iterator{&EndState});
+ assertValuesInOrder(V, 3u, 1, 5, 6);
+}
+
+TYPED_TEST(SmallVectorTest, InsertInputIterator) {
+ auto &V = this->theVector;
+ V.push_back(1);
+ V.push_back(2);
+ static constexpr int Src[] = {5, 6, 7, 8};
+ // Construct an input iterator that actually returns
diff erent results on the
+ // second iteration.
+ const int *BeginState = &Src[0];
+ const int *EndState = &Src[2];
+ V.insert(V.begin() + 1, input_iterator{&BeginState},
+ input_iterator{&EndState});
+ assertValuesInOrder(V, 4u, 1, 5, 6, 2);
+}
+
struct output_iterator {
using iterator_category = std::output_iterator_tag;
using value_type = int;
More information about the llvm-commits
mailing list