[polly] r335951 - Implement an iterator for isl maps, basic_maps, sets, basic_sets

Philip Pfaffe via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 29 01:17:04 PDT 2018


Author: pfaffe
Date: Fri Jun 29 01:17:03 2018
New Revision: 335951

URL: http://llvm.org/viewvc/llvm-project?rev=335951&view=rev
Log:
Implement an iterator for isl maps, basic_maps, sets, basic_sets

Summary:
Provide an iterator to simplify iteration over some isl collections.
Since these types do not natively support iteration, they have to be converted
to an list first by the caller, but can then be used in a ranged for loop:
```
isl::set S;
for (auto SubSet : S.get_basic_set_list ()) {
  // ...
}
```

Reviewers: bollu, Meinersbur, grosser, dexonsmith

Reviewed By: bollu

Subscribers: hfinkel, mgorny, Meinersbur, mehdi_amini, bollu, steven_wu, llvm-commits

Differential Revision: https://reviews.llvm.org/D48136

Added:
    polly/trunk/unittests/Support/
    polly/trunk/unittests/Support/CMakeLists.txt
    polly/trunk/unittests/Support/ISLTools.cpp
Modified:
    polly/trunk/include/polly/Support/ISLTools.h
    polly/trunk/unittests/CMakeLists.txt

Modified: polly/trunk/include/polly/Support/ISLTools.h
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/include/polly/Support/ISLTools.h?rev=335951&r1=335950&r2=335951&view=diff
==============================================================================
--- polly/trunk/include/polly/Support/ISLTools.h (original)
+++ polly/trunk/include/polly/Support/ISLTools.h Fri Jun 29 01:17:03 2018
@@ -16,6 +16,96 @@
 #define POLLY_ISLTOOLS_H
 
 #include "polly/Support/GICHelper.h"
+#include "llvm/ADT/iterator_range.h"
+
+namespace isl {
+inline namespace noexceptions {
+
+template <typename DerivedT, typename ElementT, typename ListT>
+struct iterator_base : public llvm::iterator_facade_base<
+                           iterator_base<DerivedT, ElementT, ListT>,
+                           std::forward_iterator_tag, ElementT> {
+  explicit iterator_base(const ListT &List) : List(&List) {
+    Position = static_cast<DerivedT *>(this)->list_size();
+  }
+  iterator_base(const ListT &List, int Position) : List(&List), Position(Position) {}
+  iterator_base &operator=(const iterator_base &R) = default;
+
+  bool operator==(const iterator_base &O) const {
+    return List == O.List && Position == O.Position;
+  }
+
+  DerivedT &operator++() {
+    ++Position;
+    return *static_cast<DerivedT *>(this);
+  }
+
+  DerivedT operator++(int) {
+    DerivedT Copy{static_cast<DerivedT *>(this)};
+    ++Position;
+    return Copy;
+  }
+
+protected:
+  const ListT *List;
+  int Position = 0;
+};
+
+struct map_iterator : iterator_base<map_iterator, isl::map, isl::map_list> {
+  using BaseT = iterator_base<map_iterator, isl::map, isl::map_list>;
+  using BaseT::BaseT;
+  isl::map operator*() const { return List->get_map(this->Position); }
+  int list_size() const { return this->List->n_map(); }
+};
+
+struct basic_map_iterator
+    : iterator_base<basic_map_iterator, isl::basic_map, isl::basic_map_list> {
+  using BaseT =
+      iterator_base<basic_map_iterator, isl::basic_map, isl::basic_map_list>;
+  using BaseT::BaseT;
+  isl::basic_map operator*() const {
+    return List->get_basic_map(this->Position);
+  }
+  int list_size() const { return this->List->n_basic_map(); }
+};
+
+struct set_iterator : iterator_base<set_iterator, isl::set, isl::set_list> {
+  using BaseT = iterator_base<set_iterator, isl::set, isl::set_list>;
+  using BaseT::BaseT;
+  isl::set operator*() const { return List->get_set(this->Position); }
+  int list_size() const { return this->List->n_set(); }
+};
+
+struct basic_set_iterator
+    : iterator_base<basic_set_iterator, isl::basic_set, isl::basic_set_list> {
+  using BaseT =
+      iterator_base<basic_set_iterator, isl::basic_set, isl::basic_set_list>;
+  using BaseT::BaseT;
+  isl::basic_set operator*() const {
+    return List->get_basic_set(this->Position);
+  }
+  int list_size() const { return this->List->n_basic_set(); }
+};
+
+inline map_iterator begin(const isl::map_list &M) { return map_iterator(M, 0); }
+inline map_iterator end(const isl::map_list &M) { return map_iterator(M); }
+inline set_iterator begin(const isl::set_list &M) { return set_iterator(M, 0); }
+inline set_iterator end(const isl::set_list &M) { return set_iterator(M); }
+inline basic_map_iterator begin(const isl::basic_map_list &M) {
+  return basic_map_iterator(M, 0);
+}
+inline basic_map_iterator end(const isl::basic_map_list &M) {
+  return basic_map_iterator(M);
+}
+inline basic_set_iterator begin(const isl::basic_set_list &M) {
+  return basic_set_iterator(M, 0);
+}
+inline basic_set_iterator end(const isl::basic_set_list &M) {
+  return basic_set_iterator(M);
+}
+
+} // namespace noexceptions
+} // namespace isl
 
 namespace polly {
 

Modified: polly/trunk/unittests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/unittests/CMakeLists.txt?rev=335951&r1=335950&r2=335951&view=diff
==============================================================================
--- polly/trunk/unittests/CMakeLists.txt (original)
+++ polly/trunk/unittests/CMakeLists.txt Fri Jun 29 01:17:03 2018
@@ -24,3 +24,4 @@ add_subdirectory(Flatten)
 add_subdirectory(DeLICM)
 add_subdirectory(ScopPassManager)
 add_subdirectory(ScheduleOptimizer)
+add_subdirectory(Support)

Added: polly/trunk/unittests/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/unittests/Support/CMakeLists.txt?rev=335951&view=auto
==============================================================================
--- polly/trunk/unittests/Support/CMakeLists.txt (added)
+++ polly/trunk/unittests/Support/CMakeLists.txt Fri Jun 29 01:17:03 2018
@@ -0,0 +1,3 @@
+add_polly_unittest(ISLToolsTests
+  ISLTools.cpp
+  )

Added: polly/trunk/unittests/Support/ISLTools.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/unittests/Support/ISLTools.cpp?rev=335951&view=auto
==============================================================================
--- polly/trunk/unittests/Support/ISLTools.cpp (added)
+++ polly/trunk/unittests/Support/ISLTools.cpp Fri Jun 29 01:17:03 2018
@@ -0,0 +1,27 @@
+#include "polly/Support/ISLTools.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace isl {
+static bool operator==(const isl::basic_set &A, const isl::basic_set &B) {
+  return A.is_equal(B);
+}
+}
+
+TEST(Support, isl_iterator) {
+  std::unique_ptr<isl_ctx, decltype(&isl_ctx_free)> RawCtx(isl_ctx_alloc(),
+                                                           &isl_ctx_free);
+  isl::ctx Ctx(RawCtx.get());
+
+  isl::basic_set A(
+      Ctx, "{ [x, y] : 0 <= x <= 5 and y >= 0 and x > 0 and 0 < y <= 5 }");
+  isl::basic_set B(
+      Ctx, "{ [x, y] : 0 <= x <= 5 and y >= 0 and x <= 4 and y <= 3 + x }");
+  isl::set S = A.unite(B);
+
+  ASSERT_EQ(S.n_basic_set(), 2);
+  std::vector<isl::basic_set> Sets;
+  for (auto BS : S.get_basic_set_list())
+    Sets.push_back(BS);
+  EXPECT_THAT(Sets, testing::UnorderedElementsAre(A, B));
+}




More information about the llvm-commits mailing list