[PATCH] D72112: [mlir][Linalg] NFC - Post-commit cleanups

Nicolas Vasilache via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 2 14:39:11 PST 2020


nicolasvasilache created this revision.
nicolasvasilache added a reviewer: yangjunpro.
Herald added subscribers: llvm-commits, lucyrfox, mgester, arpith-jacob, antiagainst, shauheen, burmako, jpienaar, rriddle, mehdi_amini.
Herald added a project: LLVM.

This diff addresses leftovers from https://reviews.llvm.org/D72022.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D72112

Files:
  mlir/include/mlir/Dialect/Linalg/IR/LinalgStructuredOps.td
  mlir/include/mlir/Dialect/Linalg/IR/LinalgTraits.h
  mlir/include/mlir/Support/Functional.h


Index: mlir/include/mlir/Support/Functional.h
===================================================================
--- mlir/include/mlir/Support/Functional.h
+++ mlir/include/mlir/Support/Functional.h
@@ -44,6 +44,29 @@
   return map(fun, std::begin(input), std::end(input));
 }
 
+/// Map with iterators. `fun` also acts like a filter: only mapped elements that
+/// evaluate to true are copied.
+template <typename Fn, typename IterType>
+auto map_filter(Fn fun, IterType begin, IterType end)
+    -> SmallVector<typename std::result_of<Fn(decltype(*begin))>::type, 8> {
+  using R = typename std::result_of<Fn(decltype(*begin))>::type;
+  SmallVector<R, 8> res;
+  // auto i works with both pointer types and value types with an operator*.
+  // auto *i only works for pointer types.
+  for (auto i = begin; i != end; ++i)
+    if (auto v = fun(*i))
+      res.push_back(v);
+  return res;
+}
+
+/// Map with templated container. `fun` also acts like a filter: only mapped
+/// elements that evaluate to true are copied.
+template <typename Fn, typename ContainerType>
+auto map_filter(Fn fun, ContainerType input)
+    -> decltype(map(fun, std::begin(input), std::end(input))) {
+  return map_filter(fun, std::begin(input), std::end(input));
+}
+
 /// Zip map with 2 templated container, iterates to the min of the sizes of
 /// the 2 containers.
 /// TODO(ntv): make variadic when needed.
Index: mlir/include/mlir/Dialect/Linalg/IR/LinalgTraits.h
===================================================================
--- mlir/include/mlir/Dialect/Linalg/IR/LinalgTraits.h
+++ mlir/include/mlir/Dialect/Linalg/IR/LinalgTraits.h
@@ -13,6 +13,7 @@
 #include "mlir/Dialect/Utils/StructuredOpsUtils.h"
 #include "mlir/IR/OpDefinition.h"
 #include "mlir/IR/StandardTypes.h"
+#include "mlir/Support/Functional.h"
 #include "mlir/Support/LLVM.h"
 
 namespace mlir {
@@ -114,19 +115,15 @@
   }
   /// Query the subset of input operands that are of ranked tensor type.
   SmallVector<RankedTensorType, 4> getInputTensorTypes() {
-    SmallVector<RankedTensorType, 4> res;
-    for (Type type : getInputs().getTypes())
-      if (auto t = type.template dyn_cast<RankedTensorType>())
-        res.push_back(t);
-    return res;
+    return functional::map_filter(
+        [](Type t) { return t.dyn_cast<RankedTensorType>(); },
+        getInputs().getTypes());
   }
   /// Query the subset of output operands that are of ranked tensor type.
   SmallVector<RankedTensorType, 4> getOutputTensorTypes() {
-    SmallVector<RankedTensorType, 4> res;
-    for (Type type : getOutputs().getTypes())
-      if (auto t = type.template dyn_cast<RankedTensorType>())
-        res.push_back(t);
-    return res;
+    return functional::map_filter(
+        [](Type t) { return t.dyn_cast<RankedTensorType>(); },
+        getOutputs().getTypes());
   }
   /// Return the range over outputs.
   Operation::operand_range getOutputs() {
Index: mlir/include/mlir/Dialect/Linalg/IR/LinalgStructuredOps.td
===================================================================
--- mlir/include/mlir/Dialect/Linalg/IR/LinalgStructuredOps.td
+++ mlir/include/mlir/Dialect/Linalg/IR/LinalgStructuredOps.td
@@ -547,7 +547,7 @@
     tensor arguments. The semantics is that the `linalg.generic` op
     produces (i.e. allocates and fills) its return values.
     Tensor values must be legalized by a buffer allocation pass before most
-    transformations can be applied. In particular, transformations that create
+    transformations can be applied. In particular, transformations which create
     control flow around linalg.generic operations are not expected to mix with
     tensors because SSA values do not escape naturally. Still, transformations
     and rewrites that take advantage of tensor SSA values are expected to be


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72112.235951.patch
Type: text/x-patch
Size: 3797 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200102/584dca67/attachment.bin>


More information about the llvm-commits mailing list