1 /* Copyright 2019 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 // This file implements LhloFusionInlinerPass, which inline the body 17 // contents of lmhlo.fusion_op after its body is fully lowered. 18 // 19 #include "mlir-hlo/Dialect/mhlo/IR/lhlo_ops.h" 20 #include "mlir-hlo/Dialect/mhlo/transforms/PassDetail.h" 21 #include "mlir/Pass/Pass.h" 22 23 namespace mlir { 24 namespace lmhlo { 25 26 struct LhloFusionInlinerPass 27 : public LhloFusionInlinerPassBase<LhloFusionInlinerPass> { 28 public: runOnFunctionmlir::lmhlo::LhloFusionInlinerPass29 void runOnFunction() override { 30 auto func = getFunction(); 31 SmallVector<FusionOp> worklist; 32 func.walk([&](FusionOp fusion) { worklist.push_back(fusion); }); 33 for (FusionOp fusion : worklist) { 34 InlineFusion(fusion); 35 fusion.erase(); 36 } 37 } 38 39 private: InlineFusionmlir::lmhlo::LhloFusionInlinerPass40 void InlineFusion(FusionOp fusion) { 41 Block& block = fusion.region().front(); 42 assert(block.getNumArguments() == 0); 43 for (Operation& op : llvm::make_early_inc_range(block.getOperations())) { 44 if (!isa<TerminatorOp>(&op)) { 45 op.moveBefore(fusion.getOperation()); 46 } 47 } 48 } 49 }; 50 createLhloFusionInlinerPass()51std::unique_ptr<OperationPass<FuncOp>> createLhloFusionInlinerPass() { 52 return std::make_unique<LhloFusionInlinerPass>(); 53 } 54 55 } // namespace lmhlo 56 } // namespace mlir 57