Use isl C++ foreach implementation
authorTobias Grosser <tobias@grosser.es>
Fri, 14 Apr 2017 13:39:40 +0000 (13:39 +0000)
committerTobias Grosser <tobias@grosser.es>
Fri, 14 Apr 2017 13:39:40 +0000 (13:39 +0000)
This commit switches Polly over to the isl::obj::foreach_* implementation, which
is part of the new isl bindings and follows the foreach pattern established in
Polly by Michael Kruse.

The original isl C function:

  isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
      isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user);

which required the user to define a static callback function to which all
interesting parameters are passed via a 'void *' user-pointer, is on the
C++ side available as a function that takes a std::function<>, which can
carry any additional arguments without the need for a user pointer:

  stat UnionSet::foreach_set(const std::function<stat(set)> &fn) const;

The following code illustrates the use of the new C++ interface:

  auto Lambda = [=, &Result](isl::set Set) -> isl::stat {
    auto Shifted = shiftDimension(Set, Pos, Amount);
    Result = Result.add(Shifted);
    return isl::stat::ok;
  }

  UnionSet.foreach_set(Lambda);

Polly had some specialized foreach functions which did not require the lambdas
to return a status flag. We remove these functions in this commit to move Polly
completely over to the new isl interface. We may in the future discuss if
functors without return values can be supported easily.

Another extension proposed by Michael Kruse is the use of C++ iterators to allow
the use of normal for loops to iterate over these sets. Such an extension would
allow us to further simplify the code.

Reviewed-by: Michael Kruse <llvm@meinersbur.de>
Differential Revision: https://reviews.llvm.org/D30620

llvm-svn: 300323

polly/lib/Support/GICHelper.cpp
polly/lib/Support/ISLTools.cpp
polly/lib/Transform/FlattenAlgo.cpp
polly/lib/Transform/FlattenSchedule.cpp
polly/unittests/DeLICM/DeLICMTest.cpp
polly/unittests/Isl/IslTest.cpp

index 9e16793..e2e1011 100644 (file)
@@ -203,106 +203,3 @@ std::string polly::getIslCompatibleName(const std::string &Prefix,
   ValStr.erase(0, 1);
   return getIslCompatibleName(Prefix, ValStr, Suffix);
 }
-
-void polly::foreachElt(const isl::map &Map,
-                       const std::function<void(isl::basic_map)> &F) {
-  isl_map_foreach_basic_map(
-      Map.keep(),
-      [](__isl_take isl_basic_map *BMap, void *User) -> isl_stat {
-        auto &F =
-            *static_cast<const std::function<void(isl::basic_map)> *>(User);
-        F(give(BMap));
-        return isl_stat_ok;
-      },
-      const_cast<void *>(static_cast<const void *>(&F)));
-}
-
-void polly::foreachElt(const isl::set &Set,
-                       const std::function<void(isl::basic_set)> &F) {
-  isl_set_foreach_basic_set(
-      Set.keep(),
-      [](__isl_take isl_basic_set *BSet, void *User) -> isl_stat {
-        auto &F =
-            *static_cast<const std::function<void(isl::basic_set)> *>(User);
-        F(give(BSet));
-        return isl_stat_ok;
-      },
-      const_cast<void *>(static_cast<const void *>(&F)));
-}
-
-void polly::foreachElt(const isl::union_map &UMap,
-                       const std::function<void(isl::map Map)> &F) {
-  isl_union_map_foreach_map(
-      UMap.keep(),
-      [](__isl_take isl_map *Map, void *User) -> isl_stat {
-        auto &F = *static_cast<const std::function<void(isl::map)> *>(User);
-        F(give(Map));
-        return isl_stat_ok;
-      },
-      const_cast<void *>(static_cast<const void *>(&F)));
-}
-
-void polly::foreachElt(const isl::union_set &USet,
-                       const std::function<void(isl::set Set)> &F) {
-  isl_union_set_foreach_set(
-      USet.keep(),
-      [](__isl_take isl_set *Set, void *User) -> isl_stat {
-        auto &F = *static_cast<const std::function<void(isl::set)> *>(User);
-        F(give(Set));
-        return isl_stat_ok;
-      },
-      const_cast<void *>(static_cast<const void *>(&F)));
-}
-
-void polly::foreachElt(const isl::union_pw_aff &UPwAff,
-                       const std::function<void(isl::pw_aff)> &F) {
-  isl_union_pw_aff_foreach_pw_aff(
-      UPwAff.keep(),
-      [](__isl_take isl_pw_aff *PwAff, void *User) -> isl_stat {
-        auto &F = *static_cast<const std::function<void(isl::pw_aff)> *>(User);
-        F(give(PwAff));
-        return isl_stat_ok;
-      },
-      const_cast<void *>(static_cast<const void *>(&F)));
-}
-
-isl_stat
-polly::foreachEltWithBreak(const isl::map &Map,
-                           const std::function<isl_stat(isl::basic_map)> &F) {
-  return isl_map_foreach_basic_map(
-      Map.keep(),
-      [](__isl_take isl_basic_map *BMap, void *User) -> isl_stat {
-        auto &F =
-            *static_cast<const std::function<isl_stat(isl::basic_map)> *>(User);
-        return F(give(BMap));
-      },
-      const_cast<void *>(static_cast<const void *>(&F)));
-}
-
-isl_stat
-polly::foreachEltWithBreak(const isl::union_map &UMap,
-                           const std::function<isl_stat(isl::map Map)> &F) {
-  return isl_union_map_foreach_map(
-      UMap.keep(),
-      [](__isl_take isl_map *Map, void *User) -> isl_stat {
-        auto &F =
-            *static_cast<const std::function<isl_stat(isl::map Map)> *>(User);
-        return F(give(Map));
-      },
-      const_cast<void *>(static_cast<const void *>(&F)));
-}
-
-isl_stat polly::foreachPieceWithBreak(
-    const isl::pw_aff &PwAff,
-    const std::function<isl_stat(isl::set, isl::aff)> &F) {
-  return isl_pw_aff_foreach_piece(
-      PwAff.keep(),
-      [](__isl_take isl_set *Domain, __isl_take isl_aff *Aff,
-         void *User) -> isl_stat {
-        auto &F =
-            *static_cast<const std::function<isl_stat(isl::set, isl::aff)> *>(
-                User);
-        return F(give(Domain), give(Aff));
-      },
-      const_cast<void *>(static_cast<const void *>(&F)));
-}
index 7eea616..883ff91 100644 (file)
@@ -89,9 +89,10 @@ isl::map polly::beforeScatter(isl::map Map, bool Strict) {
 
 isl::union_map polly::beforeScatter(isl::union_map UMap, bool Strict) {
   auto Result = give(isl_union_map_empty(isl_union_map_get_space(UMap.keep())));
-  foreachElt(UMap, [=, &Result](isl::map Map) {
+  UMap.foreach_map([=, &Result](isl::map Map) -> isl::stat {
     auto After = beforeScatter(Map, Strict);
     Result = give(isl_union_map_add_map(Result.take(), After.take()));
+    return isl::stat::ok;
   });
   return Result;
 }
@@ -105,9 +106,10 @@ isl::map polly::afterScatter(isl::map Map, bool Strict) {
 
 isl::union_map polly::afterScatter(const isl::union_map &UMap, bool Strict) {
   auto Result = give(isl_union_map_empty(isl_union_map_get_space(UMap.keep())));
-  foreachElt(UMap, [=, &Result](isl::map Map) {
+  UMap.foreach_map([=, &Result](isl::map Map) -> isl::stat {
     auto After = afterScatter(Map, Strict);
     Result = give(isl_union_map_add_map(Result.take(), After.take()));
+    return isl::stat::ok;
   });
   return Result;
 }
@@ -158,8 +160,9 @@ isl::set polly::singleton(isl::union_set USet, isl::space ExpectedSpace) {
 
 unsigned polly::getNumScatterDims(const isl::union_map &Schedule) {
   unsigned Dims = 0;
-  foreachElt(Schedule, [&Dims](isl::map Map) {
+  Schedule.foreach_map([&Dims](isl::map Map) -> isl::stat {
     Dims = std::max(Dims, isl_map_dim(Map.keep(), isl_dim_out));
+    return isl::stat::ok;
   });
   return Dims;
 }
@@ -176,13 +179,14 @@ isl::space polly::getScatterSpace(const isl::union_map &Schedule) {
 isl::union_map polly::makeIdentityMap(const isl::union_set &USet,
                                       bool RestrictDomain) {
   auto Result = give(isl_union_map_empty(isl_union_set_get_space(USet.keep())));
-  foreachElt(USet, [=, &Result](isl::set Set) {
+  USet.foreach_set([=, &Result](isl::set Set) -> isl::stat {
     auto IdentityMap = give(isl_map_identity(
         isl_space_map_from_set(isl_set_get_space(Set.keep()))));
     if (RestrictDomain)
       IdentityMap =
           give(isl_map_intersect_domain(IdentityMap.take(), Set.take()));
     Result = give(isl_union_map_add_map(Result.take(), IdentityMap.take()));
+    return isl::stat::ok;
   });
   return Result;
 }
@@ -198,9 +202,10 @@ isl::map polly::reverseDomain(isl::map Map) {
 
 isl::union_map polly::reverseDomain(const isl::union_map &UMap) {
   auto Result = give(isl_union_map_empty(isl_union_map_get_space(UMap.keep())));
-  foreachElt(UMap, [=, &Result](isl::map Map) {
+  UMap.foreach_map([=, &Result](isl::map Map) -> isl::stat {
     auto Reversed = reverseDomain(std::move(Map));
     Result = give(isl_union_map_add_map(Result.take(), Reversed.take()));
+    return isl::stat::ok;
   });
   return Result;
 }
@@ -219,9 +224,10 @@ isl::set polly::shiftDim(isl::set Set, int Pos, int Amount) {
 
 isl::union_set polly::shiftDim(isl::union_set USet, int Pos, int Amount) {
   auto Result = give(isl_union_set_empty(isl_union_set_get_space(USet.keep())));
-  foreachElt(USet, [=, &Result](isl::set Set) {
+  USet.foreach_set([=, &Result](isl::set Set) -> isl::stat {
     auto Shifted = shiftDim(Set, Pos, Amount);
     Result = give(isl_union_set_add_set(Result.take(), Shifted.take()));
+    return isl::stat::ok;
   });
   return Result;
 }
@@ -259,9 +265,10 @@ isl::union_map polly::shiftDim(isl::union_map UMap, isl::dim Dim, int Pos,
                                int Amount) {
   auto Result = isl::union_map::empty(UMap.get_space());
 
-  foreachElt(UMap, [=, &Result](isl::map Map) {
+  UMap.foreach_map([=, &Result](isl::map Map) -> isl::stat {
     auto Shifted = shiftDim(Map, Dim, Pos, Amount);
     Result = std::move(Result).add_map(Shifted);
+    return isl::stat::ok;
   });
   return Result;
 }
@@ -475,9 +482,10 @@ isl::map polly::distributeDomain(isl::map Map) {
 
 isl::union_map polly::distributeDomain(isl::union_map UMap) {
   auto Result = give(isl_union_map_empty(isl_union_map_get_space(UMap.keep())));
-  foreachElt(UMap, [=, &Result](isl::map Map) {
+  UMap.foreach_map([=, &Result](isl::map Map) {
     auto Distributed = distributeDomain(Map);
     Result = give(isl_union_map_add_map(Result.take(), Distributed.copy()));
+    return isl::stat::ok;
   });
   return Result;
 }
index bd5d765..46c40a8 100644 (file)
@@ -52,20 +52,20 @@ bool isVariableDim(const isl::basic_map &BMap) {
 
 /// Whether Map's first out dimension is no constant nor piecewise constant.
 bool isVariableDim(const isl::map &Map) {
-  return foreachEltWithBreak(Map, [](isl::basic_map BMap) -> isl_stat {
+  return Map.foreach_basic_map([](isl::basic_map BMap) -> isl::stat {
     if (isVariableDim(BMap))
-      return isl_stat_error;
-    return isl_stat_ok;
-  });
+      return isl::stat::error;
+    return isl::stat::ok;
+  }) == isl::stat::ok;
 }
 
 /// Whether UMap's first out dimension is no (piecewise) constant.
 bool isVariableDim(const isl::union_map &UMap) {
-  return foreachEltWithBreak(UMap, [](isl::map Map) -> isl_stat {
+  return UMap.foreach_map([](isl::map Map) -> isl::stat {
     if (isVariableDim(Map))
-      return isl_stat_error;
-    return isl_stat_ok;
-  });
+      return isl::stat::error;
+    return isl::stat::ok;
+  }) == isl::stat::ok;
 }
 
 /// If @p PwAff maps to a constant, return said constant. If @p Max/@p Min, it
@@ -74,39 +74,39 @@ bool isVariableDim(const isl::union_map &UMap) {
 isl::val getConstant(isl::pw_aff PwAff, bool Max, bool Min) {
   assert(!Max || !Min);
   isl::val Result;
-  foreachPieceWithBreak(PwAff, [=, &Result](isl::set Set, isl::aff Aff) {
+  PwAff.foreach_piece([=, &Result](isl::set Set, isl::aff Aff) -> isl::stat {
     if (Result && Result.is_nan())
-      return isl_stat_ok;
+      return isl::stat::ok;
 
     // TODO: If Min/Max, we can also determine a minimum/maximum value if
     // Set is constant-bounded.
     if (!Aff.is_cst()) {
       Result = isl::val::nan(Aff.get_ctx());
-      return isl_stat_error;
+      return isl::stat::error;
     }
 
     auto ThisVal = Aff.get_constant();
     if (!Result) {
       Result = ThisVal;
-      return isl_stat_ok;
+      return isl::stat::ok;
     }
 
     if (Result.eq(ThisVal))
-      return isl_stat_ok;
+      return isl::stat::ok;
 
     if (Max && ThisVal.gt(Result)) {
       Result = ThisVal;
-      return isl_stat_ok;
+      return isl::stat::ok;
     }
 
     if (Min && ThisVal.lt(Result)) {
       Result = ThisVal;
-      return isl_stat_ok;
+      return isl::stat::ok;
     }
 
     // Not compatible
     Result = isl::val::nan(Aff.get_ctx());
-    return isl_stat_error;
+    return isl::stat::error;
   });
   return Result;
 }
@@ -117,11 +117,12 @@ isl::union_pw_aff subtract(isl::union_pw_aff UPwAff, isl::val Val) {
     return UPwAff;
 
   auto Result = isl::union_pw_aff::empty(UPwAff.get_space());
-  foreachElt(UPwAff, [=, &Result](isl::pw_aff PwAff) {
+  UPwAff.foreach_pw_aff([=, &Result](isl::pw_aff PwAff) -> isl::stat {
     auto ValAff =
         isl::pw_aff(isl::set::universe(PwAff.get_space().domain()), Val);
     auto Subtracted = PwAff.sub(ValAff);
     Result = Result.union_add(isl::union_pw_aff(Subtracted));
+    return isl::stat::ok;
   });
   return Result;
 }
@@ -132,11 +133,12 @@ isl::union_pw_aff multiply(isl::union_pw_aff UPwAff, isl::val Val) {
     return UPwAff;
 
   auto Result = isl::union_pw_aff::empty(UPwAff.get_space());
-  foreachElt(UPwAff, [=, &Result](isl::pw_aff PwAff) {
+  UPwAff.foreach_pw_aff([=, &Result](isl::pw_aff PwAff) -> isl::stat {
     auto ValAff =
         isl::pw_aff(isl::set::universe(PwAff.get_space().domain()), Val);
     auto Multiplied = PwAff.mul(ValAff);
     Result = Result.union_add(Multiplied);
+    return isl::stat::ok;
   });
   return Result;
 }
@@ -152,9 +154,10 @@ isl::union_map scheduleProjectOut(const isl::union_map &UMap, unsigned first,
                     have no effect on schedule ranges */
 
   auto Result = isl::union_map::empty(UMap.get_space());
-  foreachElt(UMap, [=, &Result](isl::map Map) {
+  UMap.foreach_map([=, &Result](isl::map Map) -> isl::stat {
     auto Outprojected = Map.project_out(isl::dim::out, first, n);
     Result = Result.add_map(Outprojected);
+    return isl::stat::ok;
   });
   return Result;
 }
@@ -166,8 +169,9 @@ isl::union_map scheduleProjectOut(const isl::union_map &UMap, unsigned first,
 /// number of dimensions is not supported by the other code in this file.
 size_t scheduleScatterDims(const isl::union_map &Schedule) {
   unsigned Dims = 0;
-  foreachElt(Schedule, [&Dims](isl::map Map) {
+  Schedule.foreach_map([&Dims](isl::map Map) -> isl::stat {
     Dims = std::max(Dims, Map.dim(isl::dim::out));
+    return isl::stat::ok;
   });
   return Dims;
 }
@@ -175,11 +179,12 @@ size_t scheduleScatterDims(const isl::union_map &Schedule) {
 /// Return the @p pos' range dimension, converted to an isl_union_pw_aff.
 isl::union_pw_aff scheduleExtractDimAff(isl::union_map UMap, unsigned pos) {
   auto SingleUMap = isl::union_map::empty(UMap.get_space());
-  foreachElt(UMap, [=, &SingleUMap](isl::map Map) {
+  UMap.foreach_map([=, &SingleUMap](isl::map Map) -> isl::stat {
     auto MapDims = Map.dim(isl::dim::out);
     auto SingleMap = Map.project_out(isl::dim::out, 0, pos);
     SingleMap = SingleMap.project_out(isl::dim::out, 1, MapDims - pos - 1);
     SingleUMap = SingleUMap.add_map(SingleMap);
+    return isl::stat::ok;
   });
 
   auto UAff = isl::union_pw_multi_aff(SingleUMap);
index aac4acd..cd5db6e 100644 (file)
@@ -29,8 +29,10 @@ namespace {
 /// Prints the schedule for each statements on a new line.
 void printSchedule(raw_ostream &OS, const isl::union_map &Schedule,
                    int indent) {
-  foreachElt(Schedule,
-             [&OS, indent](isl::map Map) { OS.indent(indent) << Map << "\n"; });
+  Schedule.foreach_map([&OS, indent](isl::map Map) -> isl::stat {
+    OS.indent(indent) << Map << "\n";
+    return isl::stat::ok;
+  });
 }
 
 /// Flatten the schedule stored in an polly::Scop.
index a1154fc..b22070d 100644 (file)
@@ -24,10 +24,11 @@ namespace {
 /// Get the universes of all spaces in @p USet.
 isl::union_set unionSpace(const isl::union_set &USet) {
   auto Result = give(isl_union_set_empty(isl_union_set_get_space(USet.keep())));
-  foreachElt(USet, [=, &Result](isl::set Set) {
+  USet.foreach_set([=, &Result](isl::set Set) -> isl::stat {
     auto Space = give(isl_set_get_space(Set.keep()));
     auto Universe = give(isl_set_universe(Space.take()));
     Result = give(isl_union_set_add_set(Result.take(), Universe.take()));
+    return isl::stat::ok;
   });
   return Result;
 }
@@ -43,10 +44,11 @@ void completeLifetime(isl::union_set Universe, isl::union_map OccupiedAndKnown,
 
     Known = isl::union_map::empty(ParamSpace);
     Occupied = OccupiedAndKnown.domain();
-    foreachElt(OccupiedAndKnown, [&Known](isl::map Map) {
+    OccupiedAndKnown.foreach_map([&Known](isl::map Map) -> isl::stat {
       if (isl_map_has_tuple_name(Map.keep(), isl_dim_out) != isl_bool_true)
-        return;
+        return isl::stat::ok;
       Known = give(isl_union_map_add_map(Known.take(), Map.take()));
+      return isl::stat::ok;
     });
   }
 
index eeb9152..6ac1169 100644 (file)
@@ -299,36 +299,40 @@ TEST(Isl, Foreach) {
 
   {
     auto NumBMaps = 0;
-    foreachElt(TestMap, [&](isl::basic_map BMap) {
+    TestMap.foreach_basic_map([&](isl::basic_map BMap) -> isl::stat {
       EXPECT_EQ(BMap, TestBMap);
       NumBMaps++;
+      return isl::stat::ok;
     });
     EXPECT_EQ(1, NumBMaps);
   }
 
   {
     auto NumBSets = 0;
-    foreachElt(TestSet, [&](isl::basic_set BSet) {
+    TestSet.foreach_basic_set([&](isl::basic_set BSet) -> isl::stat {
       EXPECT_EQ(BSet, TestBSet);
       NumBSets++;
+      return isl::stat::ok;
     });
     EXPECT_EQ(1, NumBSets);
   }
 
   {
     auto NumMaps = 0;
-    foreachElt(TestUMap, [&](isl::map Map) {
+    TestUMap.foreach_map([&](isl::map Map) -> isl::stat {
       EXPECT_EQ(Map, TestMap);
       NumMaps++;
+      return isl::stat::ok;
     });
     EXPECT_EQ(1, NumMaps);
   }
 
   {
     auto NumSets = 0;
-    foreachElt(TestUSet, [&](isl::set Set) {
+    TestUSet.foreach_set([&](isl::set Set) -> isl::stat {
       EXPECT_EQ(Set, TestSet);
       NumSets++;
+      return isl::stat::ok;
     });
     EXPECT_EQ(1, NumSets);
   }
@@ -336,32 +340,32 @@ TEST(Isl, Foreach) {
   {
     auto UPwAff = isl::union_pw_aff(TestUSet, isl::val::zero(Ctx.get()));
     auto NumPwAffs = 0;
-    foreachElt(UPwAff, [&](isl::pw_aff PwAff) {
+    UPwAff.foreach_pw_aff([&](isl::pw_aff PwAff) -> isl::stat {
       EXPECT_TRUE(PwAff.is_cst());
       NumPwAffs++;
+      return isl::stat::ok;
     });
     EXPECT_EQ(1, NumPwAffs);
   }
 
   {
     auto NumBMaps = 0;
-    EXPECT_EQ(
-        isl_stat_error,
-        foreachEltWithBreak(TestMap, [&](isl::basic_map BMap) -> isl_stat {
-          EXPECT_EQ(BMap, TestBMap);
-          NumBMaps++;
-          return isl_stat_error;
-        }));
+    EXPECT_EQ(isl::stat::error,
+              TestMap.foreach_basic_map([&](isl::basic_map BMap) -> isl::stat {
+                EXPECT_EQ(BMap, TestBMap);
+                NumBMaps++;
+                return isl::stat::error;
+              }));
     EXPECT_EQ(1, NumBMaps);
   }
 
   {
     auto NumMaps = 0;
-    EXPECT_EQ(isl_stat_error,
-              foreachEltWithBreak(TestUMap, [&](isl::map Map) -> isl_stat {
+    EXPECT_EQ(isl::stat::error,
+              TestUMap.foreach_map([&](isl::map Map) -> isl::stat {
                 EXPECT_EQ(Map, TestMap);
                 NumMaps++;
-                return isl_stat_error;
+                return isl::stat::error;
               }));
     EXPECT_EQ(1, NumMaps);
   }
@@ -369,13 +373,12 @@ TEST(Isl, Foreach) {
   {
     auto TestPwAff = isl::pw_aff(TestSet, isl::val::zero(Ctx.get()));
     auto NumPieces = 0;
-    foreachPieceWithBreak(TestPwAff,
-                          [&](isl::set Domain, isl::aff Aff) -> isl_stat {
-                            EXPECT_EQ(Domain, TestSet);
-                            EXPECT_TRUE(Aff.is_cst());
-                            NumPieces++;
-                            return isl_stat_error;
-                          });
+    TestPwAff.foreach_piece([&](isl::set Domain, isl::aff Aff) -> isl::stat {
+      EXPECT_EQ(Domain, TestSet);
+      EXPECT_TRUE(Aff.is_cst());
+      NumPieces++;
+      return isl::stat::error;
+    });
     EXPECT_EQ(1, NumPieces);
   }
 }