1/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project
17#include "mlir/IR/Builders.h" // from @llvm-project
18#include "mlir/IR/Operation.h" // from @llvm-project
19#include "mlir/Transforms/FoldUtils.h" // from @llvm-project
20#include "mlir/Transforms/Passes.h" // from @llvm-project
21#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
22#include "tensorflow/dtensor/mlir/dtensor_mlir_passes.h"
23
24namespace tensorflow {
25namespace dtensor {
26
27namespace {
28#define GEN_PASS_DEF_DTENSORCONSTANTFOLDING
29#include "tensorflow/dtensor/mlir/dtensor_passes.h.inc"
30
31constexpr int kMaxIteration = 10;
32
33mlir::LogicalResult FoldConstantOp(mlir::OperationFolder& folder,
34 mlir::TF::ConstOp op) {
35 bool changed = false;
36 int i = 0;
37 // Iterate until convergence or until maxIterations. Deletion of the op as
38 // a result of being dead or folded is convergence.
39 do {
40 changed = false;
41
42 // If the operation is trivially dead - remove it.
43 if (isOpTriviallyDead(op)) {
44 op->erase();
45 return mlir::success();
46 }
47
48 // Try to fold this op.
49 bool inPlaceUpdate;
50 if (succeeded(folder.tryToFold(op,
51 /*processGeneratedConstants=*/nullptr,
52 /*preReplaceAction=*/nullptr,
53 &inPlaceUpdate))) {
54 changed = true;
55 if (!inPlaceUpdate) {
56 return mlir::success();
57 }
58 }
59 } while (changed && ++i < kMaxIteration);
60 return mlir::success();
61}
62
63// MLIR pass that folds constants that can be removed or deduplicated away.
64struct DTensorConstantFolding
65 : public impl::DTensorConstantFoldingBase<DTensorConstantFolding> {
66 void runOnOperation() override {
67 mlir::MLIRContext& context = getContext();
68 mlir::OperationFolder helper(&context);
69
70 // Collect and fold the operations within the function.
71 llvm::SmallVector<mlir::TF::ConstOp, 8> const_ops;
72 getOperation().walk([&](mlir::TF::ConstOp op) { const_ops.push_back(op); });
73
74 // Attempt to fold the specified operation, including handling unused or
75 // duplicated constants.
76 for (mlir::TF::ConstOp op : llvm::reverse(const_ops))
77 if (mlir::failed(FoldConstantOp(helper, op))) return signalPassFailure();
78 }
79};
80
81} // namespace
82
83std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>>
84CreateDTensorConstantFolding() {
85 return std::make_unique<DTensorConstantFolding>();
86}
87
88} // namespace dtensor
89} // namespace tensorflow
90