1 | /* Copyright 2022 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 <algorithm> |
17 | #include <memory> |
18 | |
19 | #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project |
20 | #include "mlir/IR/Builders.h" // from @llvm-project |
21 | #include "mlir/IR/Operation.h" // from @llvm-project |
22 | #include "mlir/Pass/Pass.h" // from @llvm-project |
23 | #include "tensorflow/dtensor/cc/tensor_layout.h" |
24 | #include "tensorflow/dtensor/mlir/ir/tf_dtensor.h" |
25 | |
26 | namespace tensorflow { |
27 | namespace dtensor { |
28 | |
29 | namespace { |
30 | #define GEN_PASS_DEF_DTENSORUNDOMERGECONSTACROSSMESH |
31 | #include "tensorflow/dtensor/mlir/dtensor_passes.h.inc" |
32 | |
33 | // MLIR pass that undoes unintended const merging across different meshes within |
34 | // the same Block by canonicalization passes. |
35 | struct DTensorUndoMergeConstAcrossMesh |
36 | : public impl::DTensorUndoMergeConstAcrossMeshBase< |
37 | DTensorUndoMergeConstAcrossMesh> { |
38 | void runOnOperation() override { |
39 | mlir::MLIRContext& context = getContext(); |
40 | mlir::OpBuilder builder(&context); |
41 | getOperation().walk([&builder](mlir::TF::ConstOp const_op) { |
42 | llvm::SmallVector<Mesh> known_meshes; |
43 | llvm::SmallVector<mlir::TF::DTensorLayout> unique_layout_ops; |
44 | for (mlir::Operation* consumer : const_op->getUsers()) { |
45 | mlir::TF::DTensorLayout layout_op = |
46 | mlir::dyn_cast<mlir::TF::DTensorLayout>(consumer); |
47 | if (!layout_op) continue; |
48 | |
49 | const Layout layout = layout_op.layout(); // keep-alive for mesh. |
50 | const Mesh& mesh = layout.mesh(); |
51 | if (std::find(known_meshes.begin(), known_meshes.end(), mesh) == |
52 | known_meshes.end()) { |
53 | if (!known_meshes.empty()) { |
54 | // We skip the first layout_op to preserve its original ConstOp. |
55 | unique_layout_ops.push_back(layout_op); |
56 | } |
57 | known_meshes.emplace_back(mesh); |
58 | } |
59 | } |
60 | for (auto& layout_op : unique_layout_ops) { |
61 | builder.setInsertionPoint(layout_op); |
62 | layout_op->replaceUsesOfWith(const_op, |
63 | builder.cloneWithoutRegions(const_op)); |
64 | } |
65 | }); |
66 | } |
67 | }; |
68 | |
69 | } // namespace |
70 | |
71 | std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>> |
72 | CreateDTensorUndoMergeConstAcrossMesh() { |
73 | return std::make_unique<DTensorUndoMergeConstAcrossMesh>(); |
74 | } |
75 | } // namespace dtensor |
76 | } // namespace tensorflow |
77 |