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 // This pass removes the device attribute from every corert.executeop.
17
18 #include "llvm/ADT/SmallVector.h"
19 #include "mlir/IR/Attributes.h" // from @llvm-project
20 #include "mlir/IR/Builders.h" // from @llvm-project
21 #include "mlir/IR/BuiltinOps.h" // from @llvm-project
22 #include "mlir/IR/Types.h" // from @llvm-project
23 #include "mlir/Pass/PassManager.h" // from @llvm-project
24 #include "mlir/Transforms/Passes.h" // from @llvm-project
25 #include "tfrt/core_runtime/opdefs/core_runtime.h" // from @tf_runtime
26
27 namespace tensorflow {
28
29 namespace {
30
31 constexpr const char* kDevice = "device";
32
33 struct RemoveDeviceAttributePass
34 : public PassWrapper<RemoveDeviceAttributePass, OperationPass<ModuleOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_IDtensorflow::__anon179dbd780111::RemoveDeviceAttributePass35 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(RemoveDeviceAttributePass)
36
37 llvm::StringRef getArgument() const final {
38 return "tfrt-remove-device-attribute";
39 }
getDescriptiontensorflow::__anon179dbd780111::RemoveDeviceAttributePass40 llvm::StringRef getDescription() const final {
41 return "This pass removes the device attribute from every corert.executeop";
42 }
43
44 void runOnOperation() override;
45 };
46
runOnOperation()47 void RemoveDeviceAttributePass::runOnOperation() {
48 ModuleOp module = getOperation();
49
50 module.walk([&](tfrt::corert::ExecuteOp execute_op) {
51 SmallVector<std::pair<StringRef, Attribute>, 4> op_func_attrs;
52 SmallVector<std::pair<StringRef, Attribute>, 4> op_attrs;
53 SmallVector<std::pair<StringRef, Attribute>, 4> new_op_attrs;
54 execute_op.getOpFuncAttrs(&op_func_attrs);
55 execute_op.getOpAttrs(&op_attrs);
56 for (std::pair<StringRef, Attribute> attr : op_attrs) {
57 if (attr.first != kDevice) {
58 new_op_attrs.push_back(attr);
59 }
60 }
61 if (op_attrs.size() == new_op_attrs.size()) return WalkResult::advance();
62
63 OpBuilder builder(execute_op);
64 auto new_execute_op = builder.create<tfrt::corert::ExecuteOp>(
65 execute_op.getLoc(), execute_op.getResultTypes(),
66 execute_op.op_handler(), execute_op.operands(), new_op_attrs,
67 op_func_attrs, execute_op.op_name());
68 execute_op.replaceAllUsesWith(new_execute_op.results());
69 execute_op.erase();
70 return WalkResult::advance();
71 });
72 }
73
74 } // namespace
75
CreateRemoveDeviceAttributePass()76 std::unique_ptr<OperationPass<ModuleOp>> CreateRemoveDeviceAttributePass() {
77 return std::make_unique<RemoveDeviceAttributePass>();
78 }
79
80 static PassRegistration<RemoveDeviceAttributePass> pass;
81
82 } // namespace tensorflow
83