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 <memory>
17 #include <tuple>
18
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project
22 #include "mlir/IR/BuiltinOps.h" // from @llvm-project
23 #include "mlir/IR/Operation.h" // from @llvm-project
24 #include "mlir/IR/Region.h" // from @llvm-project
25 #include "mlir/Interfaces/CallInterfaces.h" // from @llvm-project
26 #include "mlir/Pass/Pass.h" // from @llvm-project
27 #include "mlir/Pass/PassRegistry.h" // from @llvm-project
28 #include "mlir/Support/LLVM.h" // from @llvm-project
29 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h"
30 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
31 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h"
32
33 namespace mlir {
34 namespace TFTPU {
35
36 namespace {
37
38 // This pass removes Identity/IdentityN ops from the TPU computation and
39 // reachable functions.
40 // TODO(lyandy): Remove this pass once resource op lifting is migrated to use
41 // resource alias analysis and support region based control flow. Removing
42 // Identity ops may remove `_XlaSharding` annotation attribute if Identity ops
43 // are used to propagate such information.
44
45 struct TPUIdentityPruning
46 : public TF::TPUIdentityPruningPassBase<TPUIdentityPruning> {
47 void runOnOperation() override;
48 };
49
50 // Collects all reachable functions (via call ops) from a given region.
CollectReachableFunctions(Region & region)51 SmallVector<func::FuncOp, 4> CollectReachableFunctions(Region& region) {
52 llvm::SmallPtrSet<func::FuncOp, 4> reachable_funcs;
53
54 auto collect_reachable_funcs =
55 [&reachable_funcs](Region& src,
56 SmallVectorImpl<func::FuncOp>& funcs_to_visit) {
57 src.walk([&reachable_funcs, &funcs_to_visit](CallOpInterface call_op) {
58 auto func = dyn_cast_or_null<func::FuncOp>(call_op.resolveCallable());
59 if (func && reachable_funcs.insert(func).second)
60 funcs_to_visit.push_back(func);
61 });
62 };
63
64 SmallVector<func::FuncOp, 4> funcs_to_visit;
65 collect_reachable_funcs(region, funcs_to_visit);
66
67 while (!funcs_to_visit.empty()) {
68 SmallVector<func::FuncOp, 4> new_funcs_to_visit;
69 for (func::FuncOp func_to_visit : funcs_to_visit) {
70 if (!func_to_visit.getCallableRegion()) continue;
71 collect_reachable_funcs(*func_to_visit.getCallableRegion(),
72 new_funcs_to_visit);
73 }
74 funcs_to_visit.swap(new_funcs_to_visit);
75 }
76
77 return llvm::to_vector<4>(reachable_funcs);
78 }
79
80 // Removes Identity/IdentityN ops from a region and forwards its operands to its
81 // results.
RemoveIdentityFromRegion(Region & region)82 void RemoveIdentityFromRegion(Region& region) {
83 region.walk([](Operation* op) {
84 if (isa<TF::IdentityOp, TF::IdentityNOp>(op)) {
85 op->replaceAllUsesWith(op->getOperands());
86 op->erase();
87 }
88 });
89 }
90
runOnOperation()91 void TPUIdentityPruning::runOnOperation() {
92 SmallVector<tf_device::ClusterOp, 4> clusters;
93 getOperation().walk(
94 [&](tf_device::ClusterOp cluster) { clusters.push_back(cluster); });
95
96 for (tf_device::ClusterOp cluster : clusters) {
97 RemoveIdentityFromRegion(cluster.body());
98 auto reachable_funcs = CollectReachableFunctions(cluster.body());
99 for (func::FuncOp reachable_func : reachable_funcs)
100 RemoveIdentityFromRegion(*reachable_func.getCallableRegion());
101 }
102 }
103
104 } // anonymous namespace
105
CreateTPUIdentityPruningPass()106 std::unique_ptr<OperationPass<ModuleOp>> CreateTPUIdentityPruningPass() {
107 return std::make_unique<TPUIdentityPruning>();
108 }
109
110 } // namespace TFTPU
111 } // namespace mlir
112