[Mlir-commits] [mlir] 25dbf8f - [mlir] Use foldDynamicIndexList instead of canonicalizeSubViewPart.

Alexander Belyaev llvmlistbot at llvm.org
Tue Feb 21 10:28:17 PST 2023


Author: Alexander Belyaev
Date: 2023-02-21T19:28:00+01:00
New Revision: 25dbf8f3c66b625973f12ba81b99f8e797ab4e76

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

LOG: [mlir] Use foldDynamicIndexList instead of canonicalizeSubViewPart.

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/Arith/Utils/Utils.h
    mlir/lib/Dialect/Arith/Utils/Utils.cpp
    mlir/lib/Dialect/Tensor/IR/TensorOps.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Arith/Utils/Utils.h b/mlir/include/mlir/Dialect/Arith/Utils/Utils.h
index 4bed6e5016f80..bfb470231e986 100644
--- a/mlir/include/mlir/Dialect/Arith/Utils/Utils.h
+++ b/mlir/include/mlir/Dialect/Arith/Utils/Utils.h
@@ -26,12 +26,6 @@ namespace mlir {
 /// Matches a ConstantIndexOp.
 detail::op_matcher<arith::ConstantIndexOp> matchConstantIndex();
 
-/// Detects the `values` produced by a ConstantIndexOp and places the new
-/// constant in place of the corresponding sentinel value.
-/// TODO(pifon2a): Remove this function and use foldDynamicIndexList.
-void canonicalizeSubViewPart(SmallVectorImpl<OpFoldResult> &values,
-                             function_ref<bool(int64_t)> isDynamic);
-
 /// Returns `success` when any of the elements in `ofrs` was produced by
 /// arith::ConstantIndexOp. In that case the constant attribute replaces the
 /// Value. Returns `failure` when no folding happened.
@@ -50,20 +44,15 @@ class OpWithOffsetSizesAndStridesConstantArgumentFolder final
 
   LogicalResult matchAndRewrite(OpType op,
                                 PatternRewriter &rewriter) const override {
-    // No constant operand, just return;
-    if (llvm::none_of(op.getOperands(), [](Value operand) {
-          return matchPattern(operand, matchConstantIndex());
-        }))
-      return failure();
-
-    // At least one of offsets/sizes/strides is a new constant.
-    // Form the new list of operands and constant attributes from the existing.
     SmallVector<OpFoldResult> mixedOffsets(op.getMixedOffsets());
     SmallVector<OpFoldResult> mixedSizes(op.getMixedSizes());
     SmallVector<OpFoldResult> mixedStrides(op.getMixedStrides());
-    canonicalizeSubViewPart(mixedOffsets, ShapedType::isDynamic);
-    canonicalizeSubViewPart(mixedSizes, ShapedType::isDynamic);
-    canonicalizeSubViewPart(mixedStrides, ShapedType::isDynamic);
+
+    // No constant operands were folded, just return;
+    if (failed(foldDynamicIndexList(rewriter, mixedOffsets)) &&
+        failed(foldDynamicIndexList(rewriter, mixedSizes)) &&
+        failed(foldDynamicIndexList(rewriter, mixedStrides)))
+      return failure();
 
     // Create the new op in canonical form.
     ResultTypeFunc resultTypeFunc;

diff  --git a/mlir/lib/Dialect/Arith/Utils/Utils.cpp b/mlir/lib/Dialect/Arith/Utils/Utils.cpp
index 4d8b5adad7b7c..45a4bf74b9154 100644
--- a/mlir/lib/Dialect/Arith/Utils/Utils.cpp
+++ b/mlir/lib/Dialect/Arith/Utils/Utils.cpp
@@ -23,21 +23,6 @@ detail::op_matcher<arith::ConstantIndexOp> mlir::matchConstantIndex() {
   return detail::op_matcher<arith::ConstantIndexOp>();
 }
 
-// Detects the `values` produced by a ConstantIndexOp and places the new
-// constant in place of the corresponding sentinel value.
-void mlir::canonicalizeSubViewPart(
-    SmallVectorImpl<OpFoldResult> &values,
-    llvm::function_ref<bool(int64_t)> isDynamic) {
-  for (OpFoldResult &ofr : values) {
-    if (ofr.is<Attribute>())
-      continue;
-    // Newly static, move from Value to constant.
-    if (auto cstOp =
-            ofr.dyn_cast<Value>().getDefiningOp<arith::ConstantIndexOp>())
-      ofr = OpBuilder(cstOp).getIndexAttr(cstOp.value());
-  }
-}
-
 // Returns `success` when any of the elements in `ofrs` was produced by
 // arith::ConstantIndexOp. In that case the constant attribute replaces the
 // Value. Returns `failure` when no folding happened.

diff  --git a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
index d5679495a3bce..b6359a9357823 100644
--- a/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
+++ b/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
@@ -2227,21 +2227,15 @@ class InsertSliceOpConstantArgumentFolder final
 
   LogicalResult matchAndRewrite(InsertOpTy insertSliceOp,
                                 PatternRewriter &rewriter) const override {
-    // No constant operand, just return.
-    if (llvm::none_of(insertSliceOp.getOperands(), [](Value operand) {
-          return matchPattern(operand, matchConstantIndex());
-        }))
-      return failure();
-
-    // At least one of offsets/sizes/strides is a new constant.
-    // Form the new list of operands and constant attributes from the
-    // existing.
     SmallVector<OpFoldResult> mixedOffsets(insertSliceOp.getMixedOffsets());
     SmallVector<OpFoldResult> mixedSizes(insertSliceOp.getMixedSizes());
     SmallVector<OpFoldResult> mixedStrides(insertSliceOp.getMixedStrides());
-    canonicalizeSubViewPart(mixedOffsets, ShapedType::isDynamic);
-    canonicalizeSubViewPart(mixedSizes, ShapedType::isDynamic);
-    canonicalizeSubViewPart(mixedStrides, ShapedType::isDynamic);
+
+    // No constant operands were folded, just return;
+    if (failed(foldDynamicIndexList(rewriter, mixedOffsets)) &&
+        failed(foldDynamicIndexList(rewriter, mixedSizes)) &&
+        failed(foldDynamicIndexList(rewriter, mixedStrides)))
+      return failure();
 
     // Create the new op in canonical form.
     auto sourceType = ExtractSliceOp::inferCanonicalRankReducedResultType(


        


More information about the Mlir-commits mailing list