[mlir][Linalg] Retire LinalgStrategyPadPass and filter-based pattern.
authorNicolas Vasilache <nicolas.vasilache@gmail.com>
Fri, 7 Oct 2022 16:00:48 +0000 (09:00 -0700)
committerNicolas Vasilache <nicolas.vasilache@gmail.com>
Fri, 7 Oct 2022 16:41:26 +0000 (09:41 -0700)
Context: https://discourse.llvm.org/t/psa-retire-linalg-filter-based-patterns/63785

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

mlir/include/mlir/Dialect/Linalg/Passes.h
mlir/include/mlir/Dialect/Linalg/Passes.td
mlir/include/mlir/Dialect/Linalg/Transforms/CodegenStrategy.h
mlir/include/mlir/Dialect/Linalg/Transforms/Transforms.h
mlir/lib/Dialect/Linalg/Transforms/LinalgStrategyPasses.cpp
mlir/lib/Dialect/Linalg/Transforms/Transforms.cpp

index 2738838..719a792 100644 (file)
@@ -90,13 +90,6 @@ std::unique_ptr<OperationPass<func::FuncOp>> createLinalgStrategyTilePass(
     const linalg::LinalgTransformationFilter &filter =
         linalg::LinalgTransformationFilter());
 
-/// Create a LinalgStrategyPadPass.
-std::unique_ptr<OperationPass<func::FuncOp>> createLinalgStrategyPadPass(
-    StringRef opName = "",
-    const linalg::LinalgPaddingOptions &opt = linalg::LinalgPaddingOptions(),
-    const linalg::LinalgTransformationFilter &filter =
-        linalg::LinalgTransformationFilter());
-
 /// Create a LinalgStrategyRemoveMarkersPass.
 std::unique_ptr<OperationPass<func::FuncOp>>
 createLinalgStrategyRemoveMarkersPass();
index 9c0b414..1889d1e 100644 (file)
@@ -187,19 +187,6 @@ def LinalgStrategyTilePass
   ];
 }
 
-def LinalgStrategyPadPass
-    : Pass<"linalg-strategy-pad-pass", "func::FuncOp"> {
-  let summary = "Configurable pass to apply padding and hoisting.";
-  let constructor = "mlir::createLinalgStrategyPadPass()";
-  let dependentDialects = ["linalg::LinalgDialect"];
-  let options = [
-    Option<"anchorFuncName", "anchor-func", "std::string", /*default=*/"",
-      "Which func op is the anchor to latch on.">,
-    Option<"anchorOpName", "anchor-op", "std::string", /*default=*/"",
-      "Which linalg op within the func is the anchor to latch on.">,
-  ];
-}
-
 def LinalgStrategyRemoveMarkersPass
     : Pass<"linalg-strategy-remove-markers-pass", "func::FuncOp"> {
   let summary = "Cleanup pass that drops markers.";
index b9b26bf..6f56702 100644 (file)
@@ -64,23 +64,6 @@ private:
   linalg::LinalgTilingOptions options;
 };
 
-/// Represent one application of LinalgStrategyPadPass.
-struct Pad : public Transformation {
-  Pad(StringRef name, linalg::LinalgPaddingOptions options,
-      LinalgTransformationFilter::FilterFunction f = nullptr)
-      : Transformation(std::move(f)), opName(name),
-        options(std::move(options)) {}
-
-  void addToPassPipeline(OpPassManager &pm,
-                         LinalgTransformationFilter m) const override {
-    pm.addPass(createLinalgStrategyPadPass(opName, options, m));
-  }
-
-private:
-  std::string opName;
-  linalg::LinalgPaddingOptions options;
-};
-
 /// Codegen strategy controls how a Linalg op is progressively lowered.
 struct CodegenStrategy {
   /// Append a pattern to tile the Op `opName` and fuse its producers with
@@ -115,22 +98,6 @@ struct CodegenStrategy {
          LinalgTransformationFilter::FilterFunction f = nullptr) {
     return b ? tile(opName, std::move(options), std::move(f)) : *this;
   }
-  /// Append a pattern to pad and hoist the operands of Op `opName` with padding
-  /// `options`.
-  CodegenStrategy &
-  pad(StringRef opName, const linalg::LinalgPaddingOptions &options,
-      const LinalgTransformationFilter::FilterFunction &f = nullptr) {
-    transformationSequence.emplace_back(
-        std::make_unique<Pad>(opName, options, f));
-    return *this;
-  }
-  /// Conditionally append a pattern to pad and hoist the operands of Op
-  /// `opName` with padding `options`.
-  CodegenStrategy &
-  padIf(bool b, StringRef opName, linalg::LinalgPaddingOptions options,
-        LinalgTransformationFilter::FilterFunction f = nullptr) {
-    return b ? pad(opName, std::move(options), std::move(f)) : *this;
-  }
   /// Configure the post staged-patterns global enabling passes options.
   CodegenStrategy &
   setVectorTransferToSCFOptions(LinalgEnablingOptions options) {
index 735ce0a..b7f99ab 100644 (file)
@@ -729,18 +729,9 @@ private:
 /// See `padding` for more details.
 struct LinalgPaddingPattern : public OpInterfaceRewritePattern<LinalgOp> {
   /// Construct a generic pattern applied to all LinalgOp that verify `filter`.
-  LinalgPaddingPattern(
-      MLIRContext *context,
-      LinalgPaddingOptions options = LinalgPaddingOptions(),
-      LinalgTransformationFilter f = LinalgTransformationFilter(),
-      PatternBenefit benefit = 1);
-
-  /// Construct a pattern specifically applied to `opName`.
-  LinalgPaddingPattern(
-      StringRef opName, MLIRContext *context,
-      LinalgPaddingOptions options = LinalgPaddingOptions(),
-      LinalgTransformationFilter f = LinalgTransformationFilter(),
-      PatternBenefit benefit = 1);
+  LinalgPaddingPattern(MLIRContext *context,
+                       LinalgPaddingOptions options = LinalgPaddingOptions(),
+                       PatternBenefit benefit = 1);
 
   /// `matchAndRewrite` implementation that returns the significant transformed
   /// pieces of IR.
@@ -753,8 +744,6 @@ struct LinalgPaddingPattern : public OpInterfaceRewritePattern<LinalgOp> {
   }
 
 private:
-  /// LinalgTransformMarker handles special attribute manipulations.
-  LinalgTransformationFilter filter;
   /// Options to control padding and hoisting.
   LinalgPaddingOptions options;
 };
index ddf5d55..3faf45e 100644 (file)
@@ -123,38 +123,6 @@ struct LinalgStrategyTilePass
   LinalgTransformationFilter filter;
 };
 
-/// Configurable pass to apply hoisting and padding.
-struct LinalgStrategyPadPass
-    : public impl::LinalgStrategyPadPassBase<LinalgStrategyPadPass> {
-
-  LinalgStrategyPadPass() = default;
-
-  LinalgStrategyPadPass(StringRef opName, LinalgPaddingOptions opt,
-                        LinalgTransformationFilter filt)
-      : options(std::move(opt)), filter(std::move(filt)) {
-    this->anchorOpName.setValue(opName.str());
-  }
-
-  void runOnOperation() override {
-    auto funcOp = getOperation();
-    if (!anchorFuncName.empty() && funcOp.getName() != anchorFuncName)
-      return;
-
-    RewritePatternSet paddingPattern(funcOp.getContext());
-    if (!anchorOpName.empty()) {
-      paddingPattern.add<LinalgPaddingPattern>(
-          anchorOpName, funcOp.getContext(), options, filter);
-    } else {
-      paddingPattern.add<LinalgPaddingPattern>(funcOp.getContext(), options,
-                                               filter);
-    }
-    (void)applyPatternsAndFoldGreedily(funcOp, std::move(paddingPattern));
-  }
-
-  LinalgPaddingOptions options;
-  LinalgTransformationFilter filter;
-};
-
 /// Configurable pass to lower vector operations.
 struct LinalgStrategyRemoveMarkersPass
     : public impl::LinalgStrategyRemoveMarkersPassBase<
@@ -188,14 +156,6 @@ mlir::createLinalgStrategyTilePass(StringRef opName,
   return std::make_unique<LinalgStrategyTilePass>(opName, opt, filter);
 }
 
-/// Create a LinalgStrategyPadPass.
-std::unique_ptr<OperationPass<func::FuncOp>>
-mlir::createLinalgStrategyPadPass(StringRef opName,
-                                  const LinalgPaddingOptions &opt,
-                                  const LinalgTransformationFilter &filter) {
-  return std::make_unique<LinalgStrategyPadPass>(opName, opt, filter);
-}
-
 /// Create a LinalgStrategyRemoveMarkersPass.
 std::unique_ptr<OperationPass<func::FuncOp>>
 mlir::createLinalgStrategyRemoveMarkersPass() {
index 9a4f9a8..b3062f5 100644 (file)
@@ -396,24 +396,15 @@ mlir::linalg::LinalgTilingPattern::returningMatchAndRewrite(
 
 /// Linalg padding pattern.
 mlir::linalg::LinalgPaddingPattern::LinalgPaddingPattern(
-    MLIRContext *context, LinalgPaddingOptions options,
-    LinalgTransformationFilter f, PatternBenefit benefit)
-    : OpInterfaceRewritePattern<LinalgOp>(context, benefit),
-      filter(std::move(f)), options(std::move(options)) {}
-
-mlir::linalg::LinalgPaddingPattern::LinalgPaddingPattern(
-    StringRef opName, MLIRContext *context, LinalgPaddingOptions options,
-    LinalgTransformationFilter f, PatternBenefit benefit)
+    MLIRContext *context, LinalgPaddingOptions options, PatternBenefit benefit)
     : OpInterfaceRewritePattern<LinalgOp>(context, benefit),
-      filter(f.addOpNameFilter(opName)), options(std::move(options)) {}
+      options(std::move(options)) {}
 
 FailureOr<LinalgOp>
 mlir::linalg::LinalgPaddingPattern::returningMatchAndRewrite(
     LinalgOp linalgOp, PatternRewriter &rewriter) const {
   if (!linalgOp.hasTensorSemantics())
     return failure();
-  if (failed(filter.checkAndNotify(rewriter, linalgOp)))
-    return failure();
 
   // Pad the operation.
   LinalgOp paddedOp;
@@ -448,15 +439,10 @@ mlir::linalg::LinalgPaddingPattern::returningMatchAndRewrite(
     if (failed(newResult))
       continue;
     rewriter.replaceOp(padOp, *newResult);
-
-    // Do not apply hoist padding to the newly introduced transpose operations.
-    for (GenericOp transposeOp : transposeOps)
-      filter.replaceLinalgTransformationFilter(rewriter, transposeOp);
   }
 
   // Replace the original operation to pad.
   rewriter.replaceOp(linalgOp, *newResults);
-  filter.replaceLinalgTransformationFilter(rewriter, paddedOp);
 
   return paddedOp;
 }