[PATCH] D131289: [ADT] Add is_splat overload accepting initializer_list
Jakub Kuderski via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 5 13:32:45 PDT 2022
kuhar created this revision.
kuhar added reviewers: antiagainst, ThomasRaoux, asbirlea, beanz, dblaikie.
Herald added a reviewer: grosser.
Herald added a project: All.
kuhar requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Allow for `is_splat` to be used inline, similar to `is_contained`, e.g.,
if (is_splat({type1, type2, type3, type4}))
...
which is much more concise and less typo-prone than an equivalent chain of equality comparisons.
My immediate motivation is to clean up some code in the SPIR-V dialect that currently needs to either construct a temporary container or use `makeArrayRef` before calling `is_splat`.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D131289
Files:
llvm/include/llvm/ADT/STLExtras.h
llvm/unittests/ADT/STLExtrasTest.cpp
Index: llvm/unittests/ADT/STLExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/STLExtrasTest.cpp
+++ llvm/unittests/ADT/STLExtrasTest.cpp
@@ -606,7 +606,7 @@
EXPECT_EQ(EIR.end(), I);
}
-TEST(STLExtrasTest, splat) {
+TEST(STLExtrasTest, IsSplat) {
std::vector<int> V;
EXPECT_FALSE(is_splat(V));
@@ -621,6 +621,13 @@
EXPECT_FALSE(is_splat(V));
}
+TEST(STLExtrasTest, IsSplatInitializerList) {
+ EXPECT_TRUE(is_splat({1}));
+ EXPECT_TRUE(is_splat({1, 1}));
+ EXPECT_FALSE(is_splat({1, 2}));
+ EXPECT_TRUE(is_splat({1, 1, 1}));
+}
+
TEST(STLExtrasTest, to_address) {
int *V1 = new int;
EXPECT_EQ(V1, to_address(V1));
Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -1796,6 +1796,11 @@
std::equal(adl_begin(Range) + 1, adl_end(Range), adl_begin(Range)));
}
+/// Returns true iff all Values in the initializer lists are same.
+template <typename T> bool is_splat(std::initializer_list<T> Values) {
+ return is_splat(make_range(Values.begin(), Values.end()));
+}
+
/// Provide a container algorithm similar to C++ Library Fundamentals v2's
/// `erase_if` which is equivalent to:
///
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131289.450373.patch
Type: text/x-patch
Size: 1320 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220805/b38f77f8/attachment.bin>
More information about the llvm-commits
mailing list