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/ArrayRef.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "mlir/Dialect/StandardOps/IR/Ops.h" // from @llvm-project
19 #include "mlir/IR/Attributes.h" // from @llvm-project
20 #include "mlir/IR/Builders.h" // from @llvm-project
21 #include "mlir/IR/BuiltinTypes.h" // from @llvm-project
22 #include "mlir/IR/MLIRContext.h" // from @llvm-project
23 #include "mlir/Pass/Pass.h" // from @llvm-project
24 #include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h"
25 #include "tensorflow/compiler/mlir/lite/transforms/passes.h"
26 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
27
28 namespace mlir {
29 namespace TFL {
30 namespace {
31 // This transformation pass takes an operation with unknown op properties and
32 // wrap it by a TFL::CustomTfOp.
33 struct RaiseCustomOpsPass
34 : public PassWrapper<RaiseCustomOpsPass, FunctionPass> {
35 void runOnFunction() override;
36 };
37
runOnFunction()38 void RaiseCustomOpsPass::runOnFunction() {
39 auto fn = getFunction();
40 OpBuilder builder(fn.getContext());
41
42 llvm::SmallVector<Operation *, 4> custom_ops;
43 for (Operation &op : fn.getOps()) {
44 // Skips the ops with known op property.
45 if (op.getAbstractOperation()) continue;
46 // Skips already imported ops that are imported as CustomTfOp.
47 if (op.getParentOfType<CustomTfOp>()) continue;
48 if (llvm::isa<TFL::CustomTfOp>(op) || llvm::isa<TFL::CustomOp>(op))
49 continue;
50 custom_ops.push_back(&op);
51 }
52
53 for (auto *op : custom_ops) {
54 builder.setInsertionPoint(op);
55 auto custom_op = builder.create<CustomTfOp>(
56 op->getLoc(), op->getResultTypes(), op->getOperands());
57 Region region;
58 region.push_back(new Block);
59
60 builder.setInsertionPointToEnd(®ion.front());
61 Operation *inner_op = builder.clone(*op);
62 builder.create<YieldOp>(op->getLoc(), inner_op->getResults());
63 custom_op.body().takeBody(region);
64
65 op->replaceAllUsesWith(custom_op);
66 op->erase();
67 }
68 }
69 } // namespace
70
71 // Creates an instance of the TensorFlow Lite dialect raise custom op pass.
CreateRaiseCustomOpsPass()72 std::unique_ptr<OperationPass<FuncOp>> CreateRaiseCustomOpsPass() {
73 return std::make_unique<RaiseCustomOpsPass>();
74 }
75
76 static PassRegistration<RaiseCustomOpsPass> pass(
77 "tfl-raise-custom-ops", "Raise custom ops into tflite dialect.");
78
79 } // namespace TFL
80 } // namespace mlir
81