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 "llvm/Support/FormatVariadic.h"
20 #include "mlir/Analysis/CallGraph.h"  // from @llvm-project
21 #include "mlir/Pass/Pass.h"  // from @llvm-project
22 #include "mlir/Pass/PassRegistry.h"  // from @llvm-project
23 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h"
24 #include "tensorflow/compiler/mlir/tensorflow/transforms/tf_device_passes_detail.h"
25 #include "tensorflow/compiler/mlir/tensorflow/utils/device_util.h"
26 #include "tensorflow/compiler/mlir/tensorflow/utils/tpu_cluster_util.h"
27 #include "tensorflow/compiler/mlir/tensorflow/utils/tpu_rewrite_device_util.h"
28 
29 namespace mlir {
30 namespace TFDevice {
31 
32 namespace {
33 
34 constexpr char kDeviceAttr[] = "device";
35 constexpr char kXlaOutsideCompilationAttr[] = "_xla_outside_compilation";
36 
37 struct HostLaunchToOutsideCompiledPass
38     : public HostLaunchToOutsideCompiledPassBase<
39           HostLaunchToOutsideCompiledPass> {
40   void runOnOperation() override;
41 };
42 
43 // Assign all ops in region with _xla_outside_compilation attribute.
MarkOutsideCompiledInRegion(Region & region)44 void MarkOutsideCompiledInRegion(Region& region) {
45   region.walk([&](Operation* op) {
46     op->setAttr(kXlaOutsideCompilationAttr,
47                 StringAttr::get(op->getContext(), "from_launch"));
48   });
49 }
50 
HoistOpsAndAnnotateWithOutsideCompilation(tf_device::LaunchOp launch)51 void HoistOpsAndAnnotateWithOutsideCompilation(tf_device::LaunchOp launch) {
52   // Forward launch inner op results to launch op results.
53   launch.replaceAllUsesWith(launch.GetBody().getTerminator()->getOperands());
54 
55   // For all inner ops, assign the launch device as a `device` attribute.
56   MarkOutsideCompiledInRegion(launch.body());
57 
58   // Move all inner ops of the launch to the block containing the launch.
59   auto body = launch.GetBody().without_terminator();
60   Operation* launch_op = launch.getOperation();
61   launch_op->getBlock()->getOperations().splice(
62       launch_op->getIterator(), launch.GetBody().getOperations(), body.begin(),
63       body.end());
64 
65   launch.erase();
66 }
67 
runOnOperation()68 void HostLaunchToOutsideCompiledPass::runOnOperation() {
69   auto traverse_op = [&](Operation* op, tf_device::ClusterOp tpu_cluster,
70                          std::optional<std::string> host_device) {
71     // Hoist launch.
72     if (tf_device::LaunchOp launch = dyn_cast<tf_device::LaunchOp>(op)) {
73       StringAttr device_attr = launch->getAttrOfType<StringAttr>(kDeviceAttr);
74       if (host_device && device_attr &&
75           device_attr.getValue().equals(*host_device))
76         HoistOpsAndAnnotateWithOutsideCompilation(launch);
77     }
78     return WalkResult::advance();
79   };
80 
81   ModuleOp module = getOperation();
82   if (failed(TFTPU::WalkReachableFromTpuCluster(module, traverse_op)))
83     return signalPassFailure();
84 }
85 
86 }  // anonymous namespace
87 
88 std::unique_ptr<OperationPass<ModuleOp>>
CreateHostLaunchToOutsideCompiledPass()89 CreateHostLaunchToOutsideCompiledPass() {
90   return std::make_unique<HostLaunchToOutsideCompiledPass>();
91 }
92 
93 }  // namespace TFDevice
94 }  // namespace mlir
95