Context: https://discourse.llvm.org/t/psa-retire-linalg-filter-based-patterns/63785
Differential Revision: https://reviews.llvm.org/D135454
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();
];
}
-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.";
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
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) {
/// 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.
}
private:
- /// LinalgTransformMarker handles special attribute manipulations.
- LinalgTransformationFilter filter;
/// Options to control padding and hoisting.
LinalgPaddingOptions options;
};
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<
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() {
/// 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;
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;
}