[mlir] Use foldDynamicIndexList instead of canonicalizeSubViewPart.
authorAlexander Belyaev <pifon@google.com>
Tue, 21 Feb 2023 08:16:14 +0000 (09:16 +0100)
committerAlexander Belyaev <pifon@google.com>
Tue, 21 Feb 2023 18:28:00 +0000 (19:28 +0100)
Differential Revision: https://reviews.llvm.org/D144452

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

index 4bed6e5..bfb4702 100644 (file)
@@ -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 @@ public:
 
   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;
index 4d8b5ad..45a4bf7 100644 (file)
@@ -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.
index d567949..b6359a9 100644 (file)
@@ -2227,21 +2227,15 @@ public:
 
   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(