1 /* Copyright 2020 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 "mlir-hlo/Dialect/mhlo/IR/hlo_ops.h" 17 #include "mlir-hlo/Dialect/mhlo/transforms/PassDetail.h" 18 #include "mlir-hlo/Dialect/mhlo/transforms/passes.h" 19 #include "mlir-hlo/Dialect/mhlo/transforms/rewriters.h" 20 #include "mlir/Dialect/Func/IR/FuncOps.h" 21 #include "mlir/IR/BuiltinOps.h" 22 #include "mlir/IR/MLIRContext.h" 23 #include "mlir/IR/Operation.h" 24 #include "mlir/Pass/Pass.h" 25 #include "mlir/Transforms/DialectConversion.h" 26 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" 27 28 namespace mlir { 29 namespace mhlo { 30 namespace { 31 class OptimizeMhloPass : public OptimizeMhloPassBase<OptimizeMhloPass> { 32 public: 33 /// Performs the lowering to MHLO dialect. 34 void runOnOperation() override; 35 }; 36 // Lowers the complex operations that can be represented using other operations. runOnOperation()37void OptimizeMhloPass::runOnOperation() { 38 // Add lowering patterns to the list. 39 RewritePatternSet patterns(&getContext()); 40 populateOptimizeMhloPatterns(&getContext(), &patterns); 41 42 if (failed(applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)))) 43 return signalPassFailure(); 44 } 45 } // end anonymous namespace 46 } // namespace mhlo 47 } // namespace mlir 48 49 std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>> createOptimizeMhloPass()50mlir::mhlo::createOptimizeMhloPass() { 51 return std::make_unique<mlir::mhlo::OptimizeMhloPass>(); 52 } 53