1 /* Copyright 2021 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/ADT/STLExtras.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "mlir/Pass/Pass.h"  // from @llvm-project
20 #include "mlir/Pass/PassRegistry.h"  // from @llvm-project
21 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h"
22 #include "tensorflow/compiler/mlir/tensorflow/transforms/tf_device_passes_detail.h"
23 #include "tensorflow/compiler/mlir/tensorflow/utils/device_util.h"
24 #include "tensorflow/compiler/mlir/tensorflow/utils/tpu_rewrite_device_util.h"
25 
26 namespace mlir {
27 namespace TFDevice {
28 
29 namespace {
30 
31 constexpr char kDeviceAttr[] = "device";
32 
33 struct DeviceAttributeToLaunch
34     : public DeviceAttributeToLaunchPassBase<DeviceAttributeToLaunch> {
35   void runOnOperation() override;
36 };
37 
WrapOpInLaunch(Operation * op,llvm::StringRef device)38 void WrapOpInLaunch(Operation* op, llvm::StringRef device) {
39   OpBuilder builder(op);
40 
41   auto launch_op = builder.create<tf_device::LaunchOp>(
42       op->getLoc(), builder.getStringAttr(device),
43       /*result_types=*/op->getResultTypes());
44   op->replaceAllUsesWith(launch_op);
45 
46   launch_op.body().push_back(new Block);
47   builder.setInsertionPointToEnd(&launch_op.GetBody());
48   auto* return_op =
49       builder.create<tf_device::ReturnOp>(op->getLoc(), op->getResults())
50           .getOperation();
51   MLIRContext* context = launch_op.getContext();
52   op->removeAttr(StringAttr::get(context, kDeviceAttr));
53   op->moveBefore(return_op);
54 }
55 
runOnOperation()56 void DeviceAttributeToLaunch::runOnOperation() {
57   const Dialect* tf_dialect = getContext().getLoadedDialect("tf");
58 
59   getOperation().walk([&](Operation* op) {
60     if (op->getDialect() != tf_dialect) return WalkResult::advance();
61     if (auto device = op->getAttrOfType<StringAttr>(kDeviceAttr)) {
62       if (!device.getValue().empty()) WrapOpInLaunch(op, device.getValue());
63     }
64     return WalkResult::advance();
65   });
66 }
67 
68 }  // anonymous namespace
69 
70 std::unique_ptr<OperationPass<func::FuncOp>>
CreateDeviceAttributeToLaunchPass()71 CreateDeviceAttributeToLaunchPass() {
72   return std::make_unique<DeviceAttributeToLaunch>();
73 }
74 
75 }  // namespace TFDevice
76 }  // namespace mlir
77