1 /* Copyright 2022 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #include <iterator> 17 #include <memory> 18 #include <utility> 19 20 #include "mlir-hlo/Dialect/gml_st/IR/gml_st_ops.h" 21 #include "mlir-hlo/Dialect/gml_st/transforms/compose_set_interface.h" 22 #include "mlir-hlo/Dialect/gml_st/transforms/pass_detail.h" 23 #include "mlir-hlo/Dialect/gml_st/transforms/passes.h" 24 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" 25 #include "mlir/Dialect/Func/IR/FuncOps.h" 26 #include "mlir/IR/PatternMatch.h" 27 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" 28 29 namespace mlir { 30 namespace gml_st { 31 namespace { 32 33 struct ComposeSetPattern 34 : public OpInterfaceRewritePattern<ComposeSetInterface> { 35 using OpInterfaceRewritePattern< 36 ComposeSetInterface>::OpInterfaceRewritePattern; 37 matchAndRewritemlir::gml_st::__anon9ddc0ab20111::ComposeSetPattern38 LogicalResult matchAndRewrite(ComposeSetInterface iface, 39 PatternRewriter& rewriter) const override { 40 Value composed = iface.compose(rewriter); 41 if (!composed) return failure(); 42 43 rewriter.replaceOp(iface.getOperation(), composed); 44 return success(); 45 } 46 }; 47 48 class ComposeSetOpsPass : public ComposeSetOpsPassBase<ComposeSetOpsPass> { getDependentDialects(DialectRegistry & registry) const49 void getDependentDialects(DialectRegistry& registry) const final { 50 registry.insert<arith::ArithmeticDialect, GmlStDialect>(); 51 } 52 runOnOperation()53 void runOnOperation() final { 54 MLIRContext* ctx = &getContext(); 55 56 RewritePatternSet patterns(ctx); 57 patterns.insert<ComposeSetPattern>(ctx); 58 59 // Apply patterns from the top down. This makes sure that we have already 60 // composed the operand of a tiling op. 61 mlir::GreedyRewriteConfig config; 62 config.useTopDownTraversal = true; 63 64 if (failed(applyPatternsAndFoldGreedily(getOperation(), std::move(patterns), 65 config))) { 66 return signalPassFailure(); 67 } 68 } 69 }; 70 71 } // namespace 72 createComposeSetOpsPass()73std::unique_ptr<OperationPass<func::FuncOp>> createComposeSetOpsPass() { 74 return std::make_unique<ComposeSetOpsPass>(); 75 } 76 77 } // namespace gml_st 78 } // namespace mlir 79