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 <iterator>
17 #include <memory>
18 #include <tuple>
19
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Support/Casting.h"
22 #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project
23 #include "mlir/IR/BuiltinOps.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/Support/LogicalResult.h" // from @llvm-project
27 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_executor.h"
28 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h"
29
30 namespace mlir {
31
32 namespace {
33
34 struct ExecutorDialectToFunctionalConversion
35 : public TF::ExecutorDialectToFunctionalPassBase<
36 ExecutorDialectToFunctionalConversion> {
37 void runOnOperation() override;
38 };
39
40 // Extracts inner ops of tf_executor.island ops in a tf_executor.graph, in the
41 // order of ops in tf_executor.graph.
LiftIslandOpInnerOpsFromGraph(tf_executor::GraphOp graph)42 LogicalResult LiftIslandOpInnerOpsFromGraph(tf_executor::GraphOp graph) {
43 auto graph_position = graph.getOperation()->getIterator();
44 Block* parent_block = graph.getOperation()->getBlock();
45 for (Operation& op : graph.GetBody().without_terminator()) {
46 auto island_op = llvm::dyn_cast<tf_executor::IslandOp>(op);
47 if (!island_op)
48 return op.emitOpError()
49 << "is not supported for lifting out of tf_executor.graph, "
50 "expected tf_executor.island";
51
52 // Move inner ops in island to before the outer graph.
53 auto& island_body = island_op.GetBody().getOperations();
54 parent_block->getOperations().splice(graph_position, island_body,
55 island_body.begin(),
56 std::prev(island_body.end()));
57 // Forward island fetches (tf_executor.yield operands) to island op result
58 // uses.
59 for (auto result :
60 llvm::zip(island_op.outputs(), island_op.GetYield().fetches()))
61 std::get<0>(result).replaceAllUsesWith(std::get<1>(result));
62 }
63
64 // Forward graph fetches (tf_executor.fetch operands) to graph op result uses.
65 for (auto result : llvm::zip(graph.results(), graph.GetFetch().fetches()))
66 std::get<0>(result).replaceAllUsesWith(std::get<1>(result));
67
68 graph.erase();
69 return success();
70 }
71
runOnOperation()72 void ExecutorDialectToFunctionalConversion::runOnOperation() {
73 auto result = getOperation().walk([](tf_executor::GraphOp graph) {
74 if (failed(LiftIslandOpInnerOpsFromGraph(graph)))
75 return WalkResult::interrupt();
76
77 return WalkResult::advance();
78 });
79 if (result.wasInterrupted()) signalPassFailure();
80 }
81 } // end anonymous namespace
82
83 std::unique_ptr<OperationPass<func::FuncOp>>
CreateExecutorDialectToFunctionalConversionPass()84 CreateExecutorDialectToFunctionalConversionPass() {
85 return std::make_unique<ExecutorDialectToFunctionalConversion>();
86 }
87
88 } // namespace mlir
89
90