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 #include "llvm/Support/Casting.h"
17 #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project
18 #include "mlir/IR/Attributes.h" // from @llvm-project
19 #include "mlir/IR/BuiltinOps.h" // from @llvm-project
20 #include "mlir/IR/Dialect.h" // from @llvm-project
21 #include "mlir/IR/Operation.h" // from @llvm-project
22 #include "mlir/IR/Visitors.h" // from @llvm-project
23 #include "mlir/Pass/Pass.h" // from @llvm-project
24 #include "mlir/Support/LogicalResult.h" // from @llvm-project
25 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h"
26 #include "tensorflow/compiler/mlir/tensorflow/transforms/tf_device_passes_detail.h"
27
28 namespace mlir {
29 namespace TFDevice {
30 namespace {
31 constexpr char kDeviceAttr[] = "device";
32
33 struct LaunchToDeviceAttributePass
34 : public LaunchToDeviceAttributePassBase<LaunchToDeviceAttributePass> {
35 void runOnOperation() override;
36 };
37
38 // Assign all ops in region with specified device from launch.
AssignDevicesInRegion(const Dialect * tf_dialect,tf_device::LaunchOp launch,Region & region)39 LogicalResult AssignDevicesInRegion(const Dialect* tf_dialect,
40 tf_device::LaunchOp launch,
41 Region& region) {
42 auto result = region.walk([&](Operation* op) -> WalkResult {
43 if (op->getDialect() != tf_dialect) return WalkResult::advance();
44
45 auto device_attr = op->getAttr(kDeviceAttr);
46 if (!device_attr) {
47 op->setAttr(kDeviceAttr, launch.deviceAttr());
48 return WalkResult::advance();
49 }
50
51 if (auto device_str_attr = device_attr.dyn_cast<StringAttr>()) {
52 if (device_str_attr.getValue().empty()) {
53 op->setAttr(kDeviceAttr, launch.deviceAttr());
54 return WalkResult::advance();
55 } else if (device_str_attr.getValue() != launch.device()) {
56 return launch.emitOpError()
57 << "inner op has conflicting 'device' attribute, "
58 "got '"
59 << device_str_attr.getValue() << "' but expected '"
60 << launch.device() << "'";
61 }
62 } else {
63 return launch.emitOpError()
64 << "inner op has bad 'device' attribute, got " << device_attr;
65 }
66
67 return WalkResult::advance();
68 });
69
70 return failure(result.wasInterrupted());
71 }
72
HoistOpsAndAnnotateWithDevice(const Dialect * tf_dialect,tf_device::LaunchOp launch)73 LogicalResult HoistOpsAndAnnotateWithDevice(const Dialect* tf_dialect,
74 tf_device::LaunchOp launch) {
75 // Forward launch inner op results to launch op results.
76 launch.replaceAllUsesWith(launch.GetBody().getTerminator()->getOperands());
77
78 // For all inner ops, assign the launch device as a `device` attribute.
79 if (failed(AssignDevicesInRegion(tf_dialect, launch, launch.body())))
80 return failure();
81
82 // Move all inner ops of the launch to the block containing the launch.
83 auto body = launch.GetBody().without_terminator();
84 Operation* launch_op = launch.getOperation();
85 launch_op->getBlock()->getOperations().splice(
86 launch_op->getIterator(), launch.GetBody().getOperations(), body.begin(),
87 body.end());
88
89 launch.erase();
90
91 return success();
92 }
93
runOnOperation()94 void LaunchToDeviceAttributePass::runOnOperation() {
95 const Dialect* tf_dialect = getContext().getLoadedDialect("tf");
96 if (!tf_dialect) {
97 getOperation().emitError() << "'tf' dialect is not registered";
98 return signalPassFailure();
99 }
100
101 auto result = getOperation().walk([&tf_dialect](tf_device::LaunchOp launch) {
102 if (failed(HoistOpsAndAnnotateWithDevice(tf_dialect, launch)))
103 return WalkResult::interrupt();
104
105 return WalkResult::advance();
106 });
107
108 if (result.wasInterrupted()) return signalPassFailure();
109 }
110
111 } // anonymous namespace
112
113 std::unique_ptr<OperationPass<func::FuncOp>>
CreateLaunchToDeviceAttributePass()114 CreateLaunchToDeviceAttributePass() {
115 return std::make_unique<LaunchToDeviceAttributePass>();
116 }
117
118 } // namespace TFDevice
119 } // namespace mlir
120