• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "mlir/IR/OperationSupport.h"  // from @llvm-project
17 #include "mlir/Pass/Pass.h"  // from @llvm-project
18 #include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h"
19 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_saved_model.h"
20 
21 namespace mlir {
22 namespace TFL {
23 namespace {
24 
25 // This pass inserts a TFL::CallOnce op when tf_saved_model's session
26 // initializer is given.
27 class InsertCallOnceOpFromSessionInitializerPass
28     : public mlir::PassWrapper<InsertCallOnceOpFromSessionInitializerPass,
29                                OperationPass<ModuleOp>> {
30  private:
31   void runOnOperation() override;
32 };
33 
runOnOperation()34 void InsertCallOnceOpFromSessionInitializerPass::runOnOperation() {
35   ModuleOp module = getOperation();
36   tf_saved_model::SessionInitializerOp session_init_op =
37       tf_saved_model::GetSessionInitializerOp(module);
38 
39   if (!session_init_op) return;
40 
41   SymbolTable symbol_table(module);
42 
43   for (auto sym_ref : session_init_op.initializers()) {
44     FuncOp init_func_op = symbol_table.lookup<mlir::FuncOp>(
45         sym_ref.cast<FlatSymbolRefAttr>().getValue());
46 
47     if (!init_func_op) {
48       module.emitError("no session initializer function found");
49       return signalPassFailure();
50     }
51 
52     for (auto func : module.getOps<FuncOp>()) {
53       auto dict_attr =
54           func->getAttrOfType<mlir::DictionaryAttr>("tf.entry_function");
55       if (!dict_attr) continue;
56 
57       OpBuilder builder(func.getContext());
58       builder.setInsertionPointToStart(&func.getBlocks().front());
59       builder.create<TFL::CallOnceOp>(func.getLoc(), init_func_op.getName());
60     }
61   }
62 }
63 
64 }  // namespace
65 
66 // Inserts a TFL::CallOnce op when tf_saved_model's session initializer is
67 // given.
68 std::unique_ptr<OperationPass<ModuleOp>>
CreateInsertCallOnceOpFromSessionInitializerPass()69 CreateInsertCallOnceOpFromSessionInitializerPass() {
70   return std::make_unique<InsertCallOnceOpFromSessionInitializerPass>();
71 }
72 
73 static PassRegistration<InsertCallOnceOpFromSessionInitializerPass> pass(
74     "tfl-insert-call-once-op",
75     "Insert CallOnce op when tf_saved_model's session initializer is given");
76 
77 }  // namespace TFL
78 }  // namespace mlir
79