xref: /aosp_15_r20/external/tensorflow/tensorflow/core/ir/importexport/mangling.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 #ifndef TENSORFLOW_CORE_IR_IMPORTEXPORT_MANGLING_H_
17 #define TENSORFLOW_CORE_IR_IMPORTEXPORT_MANGLING_H_
18 
19 #include "absl/strings/string_view.h"
20 #include "tensorflow/core/framework/tensor.pb.h"
21 #include "tensorflow/core/framework/tensor_shape.pb.h"
22 #include "tensorflow/core/framework/types.h"
23 #include "tensorflow/core/lib/core/status.h"
24 
25 namespace mlir {
26 namespace tfg {
27 namespace mangling_util {
28 // The type of a mangled string.
29 enum class MangledKind { kUnknown, kDataType, kTensorShape, kTensor };
30 
31 // Mangles an attribute name, marking the attribute as a TensorFlow attribute.
32 std::string MangleAttributeName(absl::string_view str);
33 
34 // Returns true if 'str' was mangled with MangleAttributeName.
35 bool IsMangledAttributeName(absl::string_view str);
36 
37 // Demangles an attribute name that was manged with MangleAttributeName.
38 // REQUIRES: IsMangledAttributeName returns true.
39 absl::string_view DemangleAttributeName(absl::string_view str);
40 
41 // Returns the type of a mangled string, or kUnknown.
42 MangledKind GetMangledKind(absl::string_view str);
43 
44 // Return a TensorShapeProto mangled as a string.
45 std::string MangleShape(const tensorflow::TensorShapeProto& shape);
46 // Demangle a string mangled with MangleShape.
47 tensorflow::Status DemangleShape(absl::string_view str,
48                                  tensorflow::TensorShapeProto* proto);
49 
50 // Return a TensorProto mangled as a string.
51 std::string MangleTensor(const tensorflow::TensorProto& tensor);
52 // Demangle a string mangled with MangleTensor.
53 tensorflow::Status DemangleTensor(absl::string_view str,
54                                   tensorflow::TensorProto* proto);
55 
56 // Return a DataType mangled as a string.
57 std::string MangleDataType(const tensorflow::DataType& dtype);
58 // Demangle a string mangled with MangleDataType.
59 tensorflow::Status DemangleDataType(absl::string_view str,
60                                     tensorflow::DataType* proto);
61 
62 }  // namespace mangling_util
63 }  // namespace tfg
64 }  // namespace mlir
65 
66 #endif  // TENSORFLOW_CORE_IR_IMPORTEXPORT_MANGLING_H_
67