[MLIR] Add mapping based on ValueRange to BlockAndValueMapper.
authorStephan Herhut <herhut@google.com>
Mon, 3 Feb 2020 15:33:36 +0000 (16:33 +0100)
committerStephan Herhut <herhut@google.com>
Wed, 5 Feb 2020 14:48:13 +0000 (15:48 +0100)
Summary:
It is often needed to map entire ranges rather than single values. To avoid
writing the same for loop every time, I have added an overload to the map
method.

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

mlir/include/mlir/IR/BlockAndValueMapping.h
mlir/lib/Dialect/Linalg/Transforms/LinalgToLoops.cpp

index ea91010..c15920f 100644 (file)
@@ -32,6 +32,15 @@ public:
     valueMap[from.getAsOpaquePointer()] = to.getAsOpaquePointer();
   }
 
+  template <
+      typename S, typename T,
+      std::enable_if_t<!std::is_assignable<Value, S>::value &&
+                       !std::is_assignable<Block *, S>::value> * = nullptr>
+  void map(S &&from, T &&to) {
+    for (auto pair : llvm::zip(from, to))
+      map(std::get<0>(pair), std::get<1>(pair));
+  }
+
   /// Erases a mapping for 'from'.
   void erase(Block *from) { valueMap.erase(from); }
   void erase(Value from) { valueMap.erase(from.getAsOpaquePointer()); }
index 286dd8a..bc325d6 100644 (file)
@@ -273,13 +273,11 @@ public:
     // 2. Inline region, currently only works for a single basic block.
     BlockAndValueMapping map;
     auto &block = genericOp.region().front();
-    for (auto it : llvm::zip(block.getArguments(), indexedValues))
-      map.map(std::get<0>(it), std::get<1>(it));
+    map.map(block.getArguments(), indexedValues);
     for (auto &op : block.without_terminator()) {
       assert(op.getNumRegions() == 0);
       auto *newOp = b.clone(op, map);
-      for (auto it : llvm::zip(op.getResults(), newOp->getResults()))
-        map.map(std::get<0>(it), std::get<1>(it));
+      map.map(op.getResults(), newOp->getResults());
     }
 
     // 3. Emit std_store.
@@ -377,13 +375,11 @@ public:
     // 2. Inline region, currently only works for a single basic block.
     BlockAndValueMapping map;
     auto &block = indexedGenericOp.region().front();
-    for (auto it : llvm::zip(block.getArguments(), indexedValues))
-      map.map(std::get<0>(it), std::get<1>(it));
+    map.map(block.getArguments(), indexedValues);
     for (auto &op : block.without_terminator()) {
       assert(op.getNumRegions() == 0);
       auto *newOp = b.clone(op, map);
-      for (auto it : llvm::zip(op.getResults(), newOp->getResults()))
-        map.map(std::get<0>(it), std::get<1>(it));
+      map.map(op.getResults(), newOp->getResults());
     }
 
     // 3. Emit std_store.