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 "absl/container/flat_hash_set.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "mlir/Dialect/StandardOps/IR/Ops.h" // from @llvm-project
21 #include "mlir/IR/Attributes.h" // from @llvm-project
22 #include "mlir/IR/Block.h" // from @llvm-project
23 #include "mlir/IR/Builders.h" // from @llvm-project
24 #include "mlir/IR/BuiltinTypes.h" // from @llvm-project
25 #include "mlir/IR/MLIRContext.h" // from @llvm-project
26 #include "mlir/Pass/Pass.h" // from @llvm-project
27 #include "mlir/Support/LLVM.h" // from @llvm-project
28 #include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h"
29 #include "tensorflow/compiler/mlir/lite/transforms/passes.h"
30 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
31
32 // NOLINTNEXTLINE
33 static llvm::cl::list<std::string> target_ops(
34 "tfl-test-raise-tf-targets", llvm::cl::value_desc("list"),
35 llvm::cl::desc("comma separated list of target op names to be wrapped. Only"
36 " used in tests"),
37 llvm::cl::CommaSeparated);
38
39 namespace mlir {
40 namespace TFL {
41 namespace {
42 // This transformation pass takes an operation with unknown op properties and
43 // wrap it by a TFL::CustomTfOp.
44 struct RaiseCustomOpsPass
45 : public PassWrapper<RaiseCustomOpsPass, FunctionPass> {
46 public:
RaiseCustomOpsPassmlir::TFL::__anone96a51bc0111::RaiseCustomOpsPass47 explicit RaiseCustomOpsPass()
48 : target_op_names(target_ops.begin(), target_ops.end()) {}
RaiseCustomOpsPassmlir::TFL::__anone96a51bc0111::RaiseCustomOpsPass49 explicit RaiseCustomOpsPass(const std::vector<std::string> &target_ops)
50 : target_op_names(target_ops.begin(), target_ops.end()) {}
51
getArgumentmlir::TFL::__anone96a51bc0111::RaiseCustomOpsPass52 StringRef getArgument() const final {
53 // This is the argument used to refer to the pass in
54 // the textual format (on the commandline for example).
55 return "tfl-raise-custom-ops";
56 }
getDescriptionmlir::TFL::__anone96a51bc0111::RaiseCustomOpsPass57 StringRef getDescription() const final {
58 // This is a brief description of the pass.
59 return "Raise custom ops into tflite dialect.";
60 }
61
62 void runOnFunction() override;
63
64 private:
65 // If this set is empty, then all the qualified ops will be wrapped.
66 const absl::flat_hash_set<std::string> target_op_names;
67 };
68
runOnFunction()69 void RaiseCustomOpsPass::runOnFunction() {
70 auto fn = getFunction();
71 OpBuilder builder(fn.getContext());
72
73 llvm::SmallVector<Operation *, 4> custom_ops;
74 fn.walk([&](Operation *op) {
75 // Skips already imported ops that are imported as CustomTfOp.
76 if (op->getParentOfType<CustomTfOp>()) return;
77 if (llvm::isa<TFL::CustomTfOp>(op) || llvm::isa<TFL::CustomOp>(op)) return;
78
79 std::string op_name = op->getName().getIdentifier().str();
80 // Wrap the operation, if
81 // - the op is targeted explicitly, or
82 // - the op isn't registered when there are no target list.
83 if (target_op_names.contains(op_name) ||
84 (target_op_names.empty() && !op->getAbstractOperation())) {
85 custom_ops.push_back(op);
86 }
87 });
88
89 for (auto *op : custom_ops) {
90 builder.setInsertionPoint(op);
91 auto custom_op = builder.create<CustomTfOp>(
92 op->getLoc(), op->getResultTypes(), op->getOperands());
93 Region region;
94 Block *new_block = new Block;
95 region.push_back(new_block);
96
97 builder.setInsertionPointToEnd(®ion.front());
98 Operation *inner_op = builder.clone(*op);
99
100 new_block->addArguments(op->getOperandTypes());
101 for (auto idx_args : llvm::enumerate(new_block->getArguments())) {
102 inner_op->setOperand(idx_args.index(), idx_args.value());
103 }
104 custom_op->setAttrs(inner_op->getAttrs());
105 builder.create<YieldOp>(op->getLoc(), inner_op->getResults());
106 custom_op.body().takeBody(region);
107
108 op->replaceAllUsesWith(custom_op);
109 op->erase();
110 }
111 }
112 } // namespace
113
114 // Creates an instance of the TensorFlow Lite dialect raise custom op pass.
CreateRaiseCustomOpsPass(const std::vector<std::string> & target_ops)115 std::unique_ptr<OperationPass<FuncOp>> CreateRaiseCustomOpsPass(
116 const std::vector<std::string> &target_ops) {
117 return std::make_unique<RaiseCustomOpsPass>(target_ops);
118 }
119
120 static PassRegistration<RaiseCustomOpsPass> pass;
121
122 } // namespace TFL
123 } // namespace mlir
124