Skip to content

[ADT] Make Zippy more iterator-like for lifetime safety #112441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions llvm/include/llvm/ADT/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,12 @@ struct zip_common : public zip_traits<ZipType, ReferenceTupleType, Iters...> {
using value_type = typename Base::value_type;

std::tuple<Iters...> iterators;
mutable std::optional<value_type> value;

protected:
template <size_t... Ns> value_type deref(std::index_sequence<Ns...>) const {
return value_type(*std::get<Ns>(iterators)...);
template <size_t... Ns> const value_type &deref(std::index_sequence<Ns...>) const {
value.emplace(*std::get<Ns>(iterators)...);
return *value;
Comment on lines +711 to +712
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between emplace and assignment here?

}

template <size_t... Ns> void tup_inc(std::index_sequence<Ns...>) {
Expand All @@ -728,7 +730,7 @@ struct zip_common : public zip_traits<ZipType, ReferenceTupleType, Iters...> {
public:
zip_common(Iters &&... ts) : iterators(std::forward<Iters>(ts)...) {}

value_type operator*() const { return deref(IndexSequence{}); }
const value_type &operator*() const { return deref(IndexSequence{}); }

ZipType &operator++() {
tup_inc(IndexSequence{});
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9991,7 +9991,7 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
if (!E->ReorderIndices.empty() && CommonVF == E->ReorderIndices.size() &&
CommonVF == CommonMask.size() &&
any_of(enumerate(CommonMask),
[](const auto &&P) {
[](const auto &P) {
return P.value() != PoisonMaskElem &&
static_cast<unsigned>(P.value()) != P.index();
}) &&
Expand Down
36 changes: 20 additions & 16 deletions llvm/unittests/ADT/IteratorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,39 +482,26 @@ TEST(ZipIteratorTest, ZipEqualConstCorrectness) {
EXPECT_THAT(first, ElementsAre(0, 0, 0));
EXPECT_THAT(second, ElementsAre(true, true, true));

std::vector<bool> nemesis = {true, false, true};
const std::vector<bool> c_nemesis = nemesis;

for (auto &&[a, b, c, d] : zip_equal(first, c_first, nemesis, c_nemesis)) {
for (auto &&[a, b] : zip_equal(first, c_first)) {
a = 2;
c = true;
static_assert(!IsConstRef<decltype(a)>);
static_assert(IsConstRef<decltype(b)>);
static_assert(!IsBoolConstRef<decltype(c)>);
static_assert(IsBoolConstRef<decltype(d)>);
Comment on lines -485 to -494
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed?

}

EXPECT_THAT(first, ElementsAre(2, 2, 2));
EXPECT_THAT(nemesis, ElementsAre(true, true, true));

unsigned iters = 0;
for (const auto &[a, b, c, d] :
zip_equal(first, c_first, nemesis, c_nemesis)) {
for (const auto &[a, b] : zip_equal(first, c_first)) {
static_assert(!IsConstRef<decltype(a)>);
static_assert(IsConstRef<decltype(b)>);
static_assert(!IsBoolConstRef<decltype(c)>);
static_assert(IsBoolConstRef<decltype(d)>);
++iters;
}
EXPECT_EQ(iters, 3u);
iters = 0;

for (const auto &[a, b, c, d] :
MakeConst(zip_equal(first, c_first, nemesis, c_nemesis))) {
for (const auto &[a, b] : MakeConst(zip_equal(first, c_first))) {
static_assert(!IsConstRef<decltype(a)>);
static_assert(IsConstRef<decltype(b)>);
static_assert(!IsBoolConstRef<decltype(c)>);
static_assert(IsBoolConstRef<decltype(d)>);
++iters;
}
EXPECT_EQ(iters, 3u);
Expand Down Expand Up @@ -643,6 +630,23 @@ TEST(ZipIteratorTest, Mutability) {
}
}

TEST(ZipIteratorTest, Lifetime) {
SmallVector<unsigned> v1 = {1, 2, 3, 4};
SmallVector<unsigned> v2 = {5, 6, 7, 8};

auto zipper = zip(v1, v2);

auto zip_iter = zipper.begin();
EXPECT_NE(zip_iter, zipper.end());

auto *elem = &*zip_iter;
EXPECT_EQ(std::get<0>(*elem), 1u);
EXPECT_EQ(std::get<1>(*elem), 5u);

std::get<0>(*elem) = 42;
EXPECT_EQ(v1[0], 42u);
}

TEST(ZipIteratorTest, ZipFirstMutability) {
using namespace std;
vector<unsigned> pi{3, 1, 4, 1, 5, 9};
Expand Down
Loading