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 // This pass adds the device attribute to every tf.Const op based on the device
17 // attribute of the operations that read its result. If the result of a tf.Const
18 // op is read by operations placed on multiple devices, then the pass will
19 // replicate the tf.Const op once for each device.
20 
21 #include "mlir/IR/Builders.h"
22 #include "mlir/Pass/Pass.h"
23 #include "mlir/IR/UseDefLists.h"  // from @llvm-project
24 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
25 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops_a_m.h"
26 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h"
27 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h"
28 
29 namespace mlir {
30 namespace TF {
31 namespace {
32 
33 constexpr const char *kDeviceAttr = "device";
34 
35 struct ConstantOpDeviceAssignmentPass
36     : public ConstantOpDeviceAssignmentPassBase<
37           ConstantOpDeviceAssignmentPass> {
38   void runOnOperation() override;
39 };
40 
runOnOperation()41 void ConstantOpDeviceAssignmentPass::runOnOperation() {
42   ModuleOp module = getOperation();
43 
44   module.walk([&](TF::ConstOp op) {
45     // Keep the ConstOp if the op already have the device attribute.
46     if (StringAttr device_attr = op->getAttrOfType<StringAttr>(kDeviceAttr)) {
47       return WalkResult::advance();
48     }
49     OpBuilder builder(op);
50     llvm::StringMap<mlir::Operation *> cloned_op_by_device;
51     bool all_uses_replaced = true;
52 
53     for (mlir::OpOperand &use :
54          llvm::make_early_inc_range(op.getResult().getUses())) {
55       mlir::Operation *user_op = use.getOwner();
56       StringAttr device_attr = user_op->getAttrOfType<StringAttr>(kDeviceAttr);
57       if (!device_attr) {
58         all_uses_replaced = false;
59         continue;
60       }
61       // Cloned the ConstOp and set its device attribute to be the same as the
62       // device of the user operation.
63       if (cloned_op_by_device.find(device_attr.getValue()) ==
64           cloned_op_by_device.end()) {
65         mlir::Operation *new_op = builder.clone(*op.getOperation());
66         new_op->setAttr(kDeviceAttr, device_attr);
67         cloned_op_by_device[device_attr.getValue()] = new_op;
68       }
69       // Update the user operation to use the result of the cloned ConstOp.
70       mlir::Operation *new_op = cloned_op_by_device[device_attr.getValue()];
71       user_op->setOperand(use.getOperandNumber(), new_op->getResult(0));
72     }
73     // Erase the original ConstOp if all its uses have been replaced.
74     if (all_uses_replaced) {
75       op.erase();
76     }
77     return WalkResult::advance();
78   });
79 }
80 
81 }  // namespace
82 
83 std::unique_ptr<OperationPass<ModuleOp>>
CreateConstantOpDeviceAssignmentPass()84 CreateConstantOpDeviceAssignmentPass() {
85   return std::make_unique<ConstantOpDeviceAssignmentPass>();
86 }
87 
88 }  // namespace TF
89 }  // namespace mlir
90