xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/tensorflow/transforms/sink_constant.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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> {
43  public:
ClusterConstantSinkingPass(llvm::function_ref<bool (tf_device::ClusterOp,ElementsAttr)> filter)44   explicit ClusterConstantSinkingPass(
45       llvm::function_ref<bool(tf_device::ClusterOp, ElementsAttr)> filter)
46       : filter_(filter) {}
47 
runOnOperation()48   void runOnOperation() override {
49     getOperation().walk([filter = filter_](tf_device::ClusterOp cluster) {
50       LLVM_DEBUG(llvm::dbgs() << "Visit " << *cluster.getOperation() << "\n");
51       // For each launch op, we find the values used that come from a constant
52       // defined above and sink these constants in the region body.
53       // The sunk_constant map keeps a mapping from a ConstOp defined above to
54       // a sunk clone of it. This allows for reusing a sunk constant with
55       // multiple uses in the region.
56       llvm::DenseMap<Value, TF::ConstOp> sunk_constant;
57       Region &body = cluster.body();
58       visitUsedValuesDefinedAbove(body, [&](OpOperand *use) {
59         Value constant = use->get();
60         auto const_op = dyn_cast_or_null<TF::ConstOp>(constant.getDefiningOp());
61         if (!const_op) return;
62 
63         // Filter constants using user provided predicate function.
64         if (filter && !filter(cluster, const_op.value())) return;
65 
66         // We found a constant, try to insert it in the map and re-use its
67         // cloned value if any.
68         auto map_entry = sunk_constant.try_emplace(constant, nullptr);
69         if (!map_entry.second) {
70           // This constant has already been cloned into the region, reuse it.
71           use->set(map_entry.first->getSecond().getResult());
72           LLVM_DEBUG(llvm::dbgs() << "Re-use sunk constant " << use->get()
73                                   << "\n     in " << use->get() << "\n");
74           if (constant.use_empty()) const_op.erase();
75           return;
76         }
77         if (constant.hasOneUse()) {
78           LLVM_DEBUG(llvm::dbgs() << "Moved constant " << constant << "\n");
79           const_op.getOperation()->moveBefore(&body.begin()->front());
80           return;
81         }
82         map_entry.first->getSecond() = const_op.clone();
83         body.begin()->getOperations().insert(body.begin()->begin(),
84                                              map_entry.first->getSecond());
85         use->set(map_entry.first->getSecond().getResult());
86         LLVM_DEBUG(llvm::dbgs() << "Sunk cloned constant " << use->get()
87                                 << "\n     in " << use->get() << "\n");
88       });
89     });
90   }
91 
92  private:
93   llvm::function_ref<bool(tf_device::ClusterOp, ElementsAttr)> filter_;
94 };
95 
96 }  // anonymous namespace
97 
CreateClusterConstantSinkingPass(llvm::function_ref<bool (tf_device::ClusterOp,ElementsAttr)> filter)98 std::unique_ptr<OperationPass<func::FuncOp>> CreateClusterConstantSinkingPass(
99     llvm::function_ref<bool(tf_device::ClusterOp, ElementsAttr)> filter) {
100   return std::make_unique<ClusterConstantSinkingPass>(filter);
101 }
102 
103 }  // namespace TFDevice
104 }  // namespace mlir
105