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 "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/SetVector.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/Twine.h" 20 #include "llvm/Support/Debug.h" 21 #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project 22 #include "mlir/IR/Attributes.h" // from @llvm-project 23 #include "mlir/IR/Builders.h" // from @llvm-project 24 #include "mlir/IR/Visitors.h" // from @llvm-project 25 #include "mlir/Pass/Pass.h" // from @llvm-project 26 #include "mlir/Pass/PassManager.h" // from @llvm-project 27 #include "mlir/Support/LLVM.h" // from @llvm-project 28 #include "mlir/Transforms/InliningUtils.h" // from @llvm-project 29 #include "mlir/Transforms/Passes.h" // from @llvm-project 30 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_executor.h" 31 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" 32 #include "tensorflow/compiler/mlir/tensorflow/transforms/bridge.h" 33 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h" 34 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h" 35 #include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h" 36 37 #define DEBUG_TYPE "tf-executor-tpu-v1-island-inlining" 38 39 namespace mlir { 40 namespace tf_executor { 41 42 namespace { 43 constexpr llvm::StringRef kNestedModule = "_tpu_v1_compat_outlined"; 44 45 struct ExecutorTPUV1IslandInliningPass 46 : public TF::ExecutorTPUV1IslandInliningPassBase< 47 ExecutorTPUV1IslandInliningPass> { 48 void runOnOperation() override; 49 }; 50 runOnOperation()51void ExecutorTPUV1IslandInliningPass::runOnOperation() { 52 SymbolTable symbol_table(getOperation()); 53 Operation *nested_module = symbol_table.lookup(kNestedModule); 54 if (!nested_module) return; 55 56 InlinerInterface inliner(&getContext()); 57 auto walk_result = getOperation().walk([&](TF::PartitionedCallOp call_op) { 58 if (!call_op.f().getRootReference().getValue().startswith(kNestedModule)) 59 return WalkResult::advance(); 60 // This is a call we need to inline! 61 LLVM_DEBUG(llvm::dbgs() 62 << "Found call to inline: " << *call_op.getOperation() << "\n"); 63 64 auto call_interface = cast<CallOpInterface>(call_op.getOperation()); 65 auto called_func = 66 dyn_cast_or_null<func::FuncOp>(call_interface.resolveCallable()); 67 68 if (failed(inlineCall(inliner, call_interface, 69 cast<CallableOpInterface>(called_func.getOperation()), 70 called_func.getCallableRegion(), 71 /* shouldCloneInlinedRegion = */ false))) { 72 call_op.emitOpError() << "Failed to inline\n"; 73 return WalkResult::interrupt(); 74 } 75 called_func.erase(); 76 call_op.erase(); 77 return WalkResult::advance(); 78 }); 79 if (walk_result.wasInterrupted()) return signalPassFailure(); 80 // Move all remaining nested functions back into the parent module. 81 Block &nested_block = nested_module->getRegion(0).front(); 82 for (func::FuncOp func_op : 83 llvm::make_early_inc_range(nested_block.getOps<func::FuncOp>())) { 84 if (!symbol_table.lookupSymbolIn(getOperation(), func_op.getName())) { 85 nested_block.getOperations().remove(func_op.getOperation()); 86 symbol_table.insert(func_op.getOperation()); 87 } 88 } 89 nested_module->erase(); 90 } 91 92 } // namespace 93 94 std::unique_ptr<OperationPass<ModuleOp>> CreateTFExecutorTPUV1IslandInliningPass()95CreateTFExecutorTPUV1IslandInliningPass() { 96 return std::make_unique<ExecutorTPUV1IslandInliningPass>(); 97 } 98 99 } // namespace tf_executor 100 } // namespace mlir 101