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/ADT/SetVector.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Twine.h"
19 #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project
20 #include "mlir/IR/Attributes.h" // from @llvm-project
21 #include "mlir/IR/Builders.h" // from @llvm-project
22 #include "mlir/IR/SymbolTable.h" // from @llvm-project
23 #include "mlir/Pass/Pass.h" // from @llvm-project
24 #include "mlir/Pass/PassManager.h" // from @llvm-project
25 #include "mlir/Support/LLVM.h" // from @llvm-project
26 #include "mlir/Transforms/Passes.h" // from @llvm-project
27 #include "mlir/Transforms/RegionUtils.h" // from @llvm-project
28 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h"
29 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_executor.h"
30 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
31 #include "tensorflow/compiler/mlir/tensorflow/transforms/bridge.h"
32 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h"
33 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h"
34 #include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h"
35
36 namespace mlir {
37 namespace TFDevice {
38
39 namespace {
40
41 // Rewrites tf_device::LaunchFuncOp into TF::PartitionedCallOp.
42 struct ConvertLaunchFuncToTFCallPass
43 : public TF::ConvertLaunchFuncToTFCallPassBase<
44 ConvertLaunchFuncToTFCallPass> {
45 void runOnOperation() override;
46 };
47
runOnOperation()48 void ConvertLaunchFuncToTFCallPass::runOnOperation() {
49 ModuleOp module = getOperation();
50 module.walk([&](tf_device::LaunchFuncOp launch) {
51 OpBuilder builder(launch);
52 auto call_op = builder.create<TF::PartitionedCallOp>(
53 module.getLoc(), launch.getResultTypes(), launch.operands(),
54 SymbolRefAttr::get(builder.getContext(), launch.func()),
55 /*config=*/builder.getStringAttr(""),
56 /*config_proto=*/builder.getStringAttr(""),
57 /*executor_type=*/builder.getStringAttr(""));
58 call_op->setAttr("device", launch->getAttrOfType<StringAttr>("device"));
59 launch.replaceAllUsesWith(call_op);
60 launch.erase();
61 });
62 }
63
64 } // namespace
65
CreateConvertLaunchFuncToTFCallPass()66 std::unique_ptr<OperationPass<ModuleOp>> CreateConvertLaunchFuncToTFCallPass() {
67 return std::make_unique<ConvertLaunchFuncToTFCallPass>();
68 }
69
70 } // namespace TFDevice
71 } // namespace mlir
72