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 #include "llvm/Support/Debug.h"
17 #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project
18 #include "mlir/IR/Builders.h" // from @llvm-project
19 #include "mlir/IR/Operation.h" // from @llvm-project
20 #include "mlir/Pass/Pass.h" // from @llvm-project
21 #include "mlir/Pass/PassRegistry.h" // from @llvm-project
22 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_executor.h"
23 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h"
24
25 #define DEBUG_TYPE "tf-functional-to-executor"
26
27 namespace mlir {
28
29 namespace {
30 // This pass converts mlir functions consisting of mlir ops into a tf_executor
31 // dialect as a single island.
32 // Result like so:
33 // func @my_fn(%argi...) -> (result_t) {
34 // %results:[[n_args]] = tf_executor.graph {
35 // %island_results:[[nargs + 1]] = tf_executor.island {
36 // ... original ops ...
37 // tf_executor.yield %results...
38 // }
39 // tf_executor.fetch %island_results#...
40 // }
41 // return %graph_results#...
42 // }
43 struct FunctionalToExecutorDialectConversion
44 : public TF::FunctionalToExecutorDialectConversionPassBase<
45 FunctionalToExecutorDialectConversion> {
46 void runOnOperation() override;
47 };
48 } // end anonymous namespace
49
runOnOperation()50 void FunctionalToExecutorDialectConversion::runOnOperation() {
51 auto func = getOperation();
52 if (func.isExternal()) return;
53 if (!llvm::hasSingleElement(func)) {
54 LLVM_DEBUG(llvm::dbgs() << "Expect single block function, skip conversion "
55 "to tf_executor dialect\n");
56 return;
57 }
58 auto loc = func.getLoc();
59 mlir::Block& body = func.front();
60 // Find region of interest and ReturnOp.
61 auto copy_range = body.without_terminator();
62 if (copy_range.begin() != copy_range.end() &&
63 std::next(copy_range.begin()) == copy_range.end() &&
64 isa<tf_executor::GraphOp>(*copy_range.begin())) {
65 // Already a graph.
66 return;
67 }
68
69 auto return_op = dyn_cast<func::ReturnOp>(body.getTerminator());
70 if (!return_op) {
71 LLVM_DEBUG(llvm::dbgs() << "Expect function to end with return\n");
72 return;
73 }
74 // Build GraphOp.
75 OpBuilder builder(&body, body.begin());
76 auto graph_op = builder.create<tf_executor::GraphOp>(
77 loc, func.getFunctionType().getResults());
78 graph_op.body().push_back(new Block);
79 builder.setInsertionPointToEnd(&graph_op.GetBody());
80 auto island = builder.create<tf_executor::IslandOp>(
81 loc, func.getFunctionType().getResults(),
82 tf_executor::ControlType::get(&getContext()), ArrayRef<Value>());
83 // Create Fetch.
84 ValueRange to_fetch = island.getResults();
85 if (to_fetch.size() != 1) {
86 // Drop control result for fetch.
87 to_fetch = to_fetch.drop_back();
88 }
89 builder.create<tf_executor::FetchOp>(loc, to_fetch);
90 // Build Island.
91 island.body().push_back(new Block);
92 island.body().front().getOperations().splice(
93 island.body().front().begin(), body.getOperations(), copy_range.begin(),
94 copy_range.end());
95 builder.setInsertionPointToEnd(&island.body().front());
96 builder.create<tf_executor::YieldOp>(loc, return_op.getOperands());
97 for (auto item : llvm::enumerate(graph_op.getResults())) {
98 return_op.setOperand(item.index(), item.value());
99 }
100 }
101
102 std::unique_ptr<OperationPass<func::FuncOp>>
CreateFunctionalToExecutorDialectConversionPass()103 CreateFunctionalToExecutorDialectConversionPass() {
104 return std::make_unique<FunctionalToExecutorDialectConversion>();
105 }
106
107 } // namespace mlir
108