[Mlir-commits] [mlir] b65f21a - [mlir] Provide getMixedOffsets/sizes/strides as free functions

Ivan Butygin llvmlistbot at llvm.org
Thu Jan 13 02:46:39 PST 2022


Author: Ivan Butygin
Date: 2022-01-13T13:46:31+03:00
New Revision: b65f21a4988dc4666d228f5f00891c25aa0e921b

URL: https://github.com/llvm/llvm-project/commit/b65f21a4988dc4666d228f5f00891c25aa0e921b
DIFF: https://github.com/llvm/llvm-project/commit/b65f21a4988dc4666d228f5f00891c25aa0e921b.diff

LOG: [mlir] Provide getMixedOffsets/sizes/strides as free functions

* This is useful when you need to build mixed array from external static/dynamic arrays (e.g. from adaptor during dialect conversion)
* Also, to reduce C++ code in td and generated files

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

Added: 
    

Modified: 
    mlir/include/mlir/Interfaces/ViewLikeInterface.h
    mlir/include/mlir/Interfaces/ViewLikeInterface.td
    mlir/lib/Interfaces/ViewLikeInterface.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Interfaces/ViewLikeInterface.h b/mlir/include/mlir/Interfaces/ViewLikeInterface.h
index 7df3d1e95bab4..8b07972af3890 100644
--- a/mlir/include/mlir/Interfaces/ViewLikeInterface.h
+++ b/mlir/include/mlir/Interfaces/ViewLikeInterface.h
@@ -31,6 +31,24 @@ struct Range {
 
 class OffsetSizeAndStrideOpInterface;
 
+/// Return a vector of all the static or dynamic offsets of the op from provided
+/// external static and dynamic offsets.
+SmallVector<OpFoldResult, 4> getMixedOffsets(OffsetSizeAndStrideOpInterface op,
+                                             ArrayAttr staticOffsets,
+                                             ValueRange offsets);
+
+/// Return a vector of all the static or dynamic sizes of the op from provided
+/// external static and dynamic sizes.
+SmallVector<OpFoldResult, 4> getMixedSizes(OffsetSizeAndStrideOpInterface op,
+                                           ArrayAttr staticSizes,
+                                           ValueRange sizes);
+
+/// Return a vector of all the static or dynamic strides of the op from provided
+/// external static and dynamic strides.
+SmallVector<OpFoldResult, 4> getMixedStrides(OffsetSizeAndStrideOpInterface op,
+                                             ArrayAttr staticStrides,
+                                             ValueRange strides);
+
 namespace detail {
 LogicalResult verifyOffsetSizeAndStrideOp(OffsetSizeAndStrideOpInterface op);
 

diff  --git a/mlir/include/mlir/Interfaces/ViewLikeInterface.td b/mlir/include/mlir/Interfaces/ViewLikeInterface.td
index b829760ba4591..e3cbd2c667b71 100644
--- a/mlir/include/mlir/Interfaces/ViewLikeInterface.td
+++ b/mlir/include/mlir/Interfaces/ViewLikeInterface.td
@@ -165,16 +165,8 @@ def OffsetSizeAndStrideOpInterface : OpInterface<"OffsetSizeAndStrideOpInterface
       /*args=*/(ins),
       /*methodBody=*/"",
       /*defaultImplementation=*/[{
-        ::mlir::SmallVector<::mlir::OpFoldResult, 4> res;
-        unsigned numDynamic = 0;
-        unsigned count = $_op.static_offsets().size();
-        for (unsigned idx = 0; idx < count; ++idx) {
-          if (isDynamicOffset(idx))
-            res.push_back($_op.offsets()[numDynamic++]);
-          else
-            res.push_back($_op.static_offsets()[idx]);
-        }
-        return res;
+        return ::mlir::getMixedOffsets($_op, $_op.static_offsets(),
+                                       $_op.offsets());
       }]
     >,
     InterfaceMethod<
@@ -186,16 +178,7 @@ def OffsetSizeAndStrideOpInterface : OpInterface<"OffsetSizeAndStrideOpInterface
       /*args=*/(ins),
       /*methodBody=*/"",
       /*defaultImplementation=*/[{
-        ::mlir::SmallVector<::mlir::OpFoldResult, 4> res;
-        unsigned numDynamic = 0;
-        unsigned count = $_op.static_sizes().size();
-        for (unsigned idx = 0; idx < count; ++idx) {
-          if (isDynamicSize(idx))
-            res.push_back($_op.sizes()[numDynamic++]);
-          else
-            res.push_back($_op.static_sizes()[idx]);
-        }
-        return res;
+        return ::mlir::getMixedSizes($_op, $_op.static_sizes(), $_op.sizes());
       }]
     >,
     InterfaceMethod<
@@ -207,16 +190,8 @@ def OffsetSizeAndStrideOpInterface : OpInterface<"OffsetSizeAndStrideOpInterface
       /*args=*/(ins),
       /*methodBody=*/"",
       /*defaultImplementation=*/[{
-        ::mlir::SmallVector<::mlir::OpFoldResult, 4> res;
-        unsigned numDynamic = 0;
-        unsigned count = $_op.static_strides().size();
-        for (unsigned idx = 0; idx < count; ++idx) {
-          if (isDynamicStride(idx))
-            res.push_back($_op.strides()[numDynamic++]);
-          else
-            res.push_back($_op.static_strides()[idx]);
-        }
-        return res;
+        return ::mlir::getMixedStrides($_op, $_op.static_strides(),
+                                       $_op.strides());
       }]
     >,
 

diff  --git a/mlir/lib/Interfaces/ViewLikeInterface.cpp b/mlir/lib/Interfaces/ViewLikeInterface.cpp
index cccc8339a6446..0e772efd17ce9 100644
--- a/mlir/lib/Interfaces/ViewLikeInterface.cpp
+++ b/mlir/lib/Interfaces/ViewLikeInterface.cpp
@@ -195,3 +195,48 @@ void OffsetSizeAndStrideOpInterface::expandToRank(
     strides.push_back(one);
   }
 }
+
+SmallVector<OpFoldResult, 4>
+mlir::getMixedOffsets(OffsetSizeAndStrideOpInterface op,
+                      ArrayAttr staticOffsets, ValueRange offsets) {
+  SmallVector<OpFoldResult, 4> res;
+  unsigned numDynamic = 0;
+  unsigned count = static_cast<unsigned>(staticOffsets.size());
+  for (unsigned idx = 0; idx < count; ++idx) {
+    if (op.isDynamicOffset(idx))
+      res.push_back(offsets[numDynamic++]);
+    else
+      res.push_back(staticOffsets[idx]);
+  }
+  return res;
+}
+
+SmallVector<OpFoldResult, 4>
+mlir::getMixedSizes(OffsetSizeAndStrideOpInterface op, ArrayAttr staticSizes,
+                    ValueRange sizes) {
+  SmallVector<OpFoldResult, 4> res;
+  unsigned numDynamic = 0;
+  unsigned count = static_cast<unsigned>(staticSizes.size());
+  for (unsigned idx = 0; idx < count; ++idx) {
+    if (op.isDynamicSize(idx))
+      res.push_back(sizes[numDynamic++]);
+    else
+      res.push_back(staticSizes[idx]);
+  }
+  return res;
+}
+
+SmallVector<OpFoldResult, 4>
+mlir::getMixedStrides(OffsetSizeAndStrideOpInterface op,
+                      ArrayAttr staticStrides, ValueRange strides) {
+  SmallVector<OpFoldResult, 4> res;
+  unsigned numDynamic = 0;
+  unsigned count = static_cast<unsigned>(staticStrides.size());
+  for (unsigned idx = 0; idx < count; ++idx) {
+    if (op.isDynamicStride(idx))
+      res.push_back(strides[numDynamic++]);
+    else
+      res.push_back(staticStrides[idx]);
+  }
+  return res;
+}


        


More information about the Mlir-commits mailing list