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