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