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 <cstddef>
17 #include <utility>
18
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Support/Debug.h"
22 #include "mlir/Pass/Pass.h" // from @llvm-project
23 #include "mlir/Pass/PassManager.h" // from @llvm-project
24 #include "mlir/Support/LLVM.h" // from @llvm-project
25 #include "mlir/Transforms/Passes.h" // from @llvm-project
26 #include "mlir/Transforms/RegionUtils.h" // from @llvm-project
27 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h"
28 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
29 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h"
30 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h"
31 #include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h"
32
33 #define DEBUG_TYPE "tf-executor-sink-constant"
34
35 namespace mlir {
36 namespace TFDevice {
37
38 namespace {
39 using ::mlir::TF::ConstOp;
40
41 class ClusterConstantSinkingPass
42 : public TF::ClusterConstantSinkingPassBase<ClusterConstantSinkingPass> {
runOnFunction()43 void runOnFunction() override {
44 getFunction().walk([](tf_device::ClusterOp cluster) {
45 LLVM_DEBUG(llvm::dbgs() << "Visit " << *cluster.getOperation() << "\n");
46 // For each launch op, we find the values used that come from a constant
47 // defined above and sink these constants in the region body.
48 // The sunk_constant map keeps a mapping from a ConstOp defined above to
49 // a sunk clone of it. This allows for reusing a sunk constant with
50 // multiple uses in the region.
51 llvm::DenseMap<Value, TF::ConstOp> sunk_constant;
52 Region &body = cluster.body();
53 visitUsedValuesDefinedAbove(body, [&](OpOperand *use) {
54 Value constant = use->get();
55 auto const_op = dyn_cast_or_null<TF::ConstOp>(constant.getDefiningOp());
56 if (!const_op) return;
57
58 // We found a constant, try to insert it in the map and re-use its
59 // cloned value if any.
60 auto map_entry = sunk_constant.try_emplace(constant, nullptr);
61 if (!map_entry.second) {
62 // This constant has already been cloned into the region, reuse it.
63 use->set(map_entry.first->getSecond().getResult());
64 LLVM_DEBUG(llvm::dbgs() << "Re-use sunk constant " << use->get()
65 << "\n in " << use->get() << "\n");
66 if (constant.use_empty()) const_op.erase();
67 return;
68 }
69 if (constant.hasOneUse()) {
70 LLVM_DEBUG(llvm::dbgs() << "Moved constant " << constant << "\n");
71 const_op.getOperation()->moveBefore(&body.begin()->front());
72 return;
73 }
74 map_entry.first->getSecond() = const_op.clone();
75 body.begin()->getOperations().insert(body.begin()->begin(),
76 map_entry.first->getSecond());
77 use->set(map_entry.first->getSecond().getResult());
78 LLVM_DEBUG(llvm::dbgs() << "Sunk cloned constant " << use->get()
79 << "\n in " << use->get() << "\n");
80 });
81 });
82 }
83 };
84
85 } // anonymous namespace
86
CreateClusterConstantSinkingPass()87 std::unique_ptr<OperationPass<FuncOp>> CreateClusterConstantSinkingPass() {
88 return std::make_unique<ClusterConstantSinkingPass>();
89 }
90
91 } // namespace TFDevice
92 } // namespace mlir
93