[llvm] ba9dc5f - [ADT] Add is_splat overload accepting initializer_list
Jakub Kuderski via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 8 07:24:13 PDT 2022
Author: Jakub Kuderski
Date: 2022-08-08T10:20:02-04:00
New Revision: ba9dc5f577389db2cc706c0c66615f430a55d153
URL: https://github.com/llvm/llvm-project/commit/ba9dc5f577389db2cc706c0c66615f430a55d153
DIFF: https://github.com/llvm/llvm-project/commit/ba9dc5f577389db2cc706c0c66615f430a55d153.diff
LOG: [ADT] Add is_splat overload accepting initializer_list
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`.
Reviewed By: dblaikie
Differential Revision: https://reviews.llvm.org/D131289
Added:
Modified:
llvm/include/llvm/ADT/STLExtras.h
llvm/unittests/ADT/STLExtrasTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 0efa96e69a8ce..0aaefb5e0ee29 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1796,6 +1796,11 @@ bool is_splat(R &&Range) {
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<std::initializer_list<T>>(std::move(Values));
+}
+
/// Provide a container algorithm similar to C++ Library Fundamentals v2's
/// `erase_if` which is equivalent to:
///
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index a32c823e73b66..f7281f8af0704 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -606,7 +606,7 @@ TEST(STLExtrasTest, EarlyIncrementTestCustomPointerIterator) {
EXPECT_EQ(EIR.end(), I);
}
-TEST(STLExtrasTest, splat) {
+TEST(STLExtrasTest, IsSplat) {
std::vector<int> V;
EXPECT_FALSE(is_splat(V));
@@ -621,6 +621,13 @@ TEST(STLExtrasTest, splat) {
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));
More information about the llvm-commits
mailing list