xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/tensorflow/translate/export_graphdef.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2019 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 #ifndef TENSORFLOW_COMPILER_MLIR_TENSORFLOW_TRANSLATE_EXPORT_GRAPHDEF_H_
17 #define TENSORFLOW_COMPILER_MLIR_TENSORFLOW_TRANSLATE_EXPORT_GRAPHDEF_H_
18 
19 #include "absl/container/flat_hash_set.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
22 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
23 #include "mlir/IR/MLIRContext.h"  // from @llvm-project
24 #include "mlir/IR/Operation.h"  // from @llvm-project
25 #include "tensorflow/compiler/mlir/tensorflow/translate/mlir_roundtrip_flags.h"
26 #include "tensorflow/core/framework/function.h"
27 #include "tensorflow/core/framework/graph.pb.h"
28 #include "tensorflow/core/framework/node_def.pb.h"
29 #include "tensorflow/core/graph/graph.h"
30 #include "tensorflow/stream_executor/lib/statusor.h"
31 
32 namespace tensorflow {
33 // Given an MLIR module, returns a GraphDef.
34 stream_executor::port::StatusOr<std::unique_ptr<GraphDef>>
35 ConvertMlirToGraphdef(mlir::ModuleOp module, const GraphExportConfig& configs);
36 
37 // Converts an MLIR module to TensorFlow graph and FunctionLibraryDefinition.
38 // The "main" function of the module is stored in the graph and the rest of
39 // functions are stored in the library. Control ret nodes are stored separately
40 // in `control_ret_nodes`.
41 Status ConvertMlirToGraph(mlir::ModuleOp module,
42                           const GraphExportConfig& configs,
43                           std::unique_ptr<Graph>* graph,
44                           FunctionLibraryDefinition* flib_def,
45                           absl::flat_hash_set<Node*>* control_ret_nodes);
46 
47 // Converts an MLIR module to TensorFlow graph and FunctionLibraryDefinition.
48 // The "main" function of the module is stored in the graph and the rest of
49 // functions are stored in the library.
50 Status ConvertMlirToGraph(mlir::ModuleOp module,
51                           const GraphExportConfig& configs,
52                           std::unique_ptr<Graph>* graph,
53                           FunctionLibraryDefinition* flib_def);
54 
55 // Converts an MLIR function and adds it to a FunctionLibraryDefinition.
56 Status ConvertMlirFunctionToFunctionLibraryDef(mlir::func::FuncOp func,
57                                                const GraphExportConfig& configs,
58                                                FunctionDef* function_def);
59 }  // namespace tensorflow
60 
61 #endif  // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_TRANSLATE_EXPORT_GRAPHDEF_H_
62