xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/lite/utils/attribute_utils.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 // This header file defines common utils used by TFLite transformation
17 // passes to work with op attributes.
18 
19 #ifndef TENSORFLOW_COMPILER_MLIR_LITE_UTILS_ATTRIBUTE_UTILS_H_
20 #define TENSORFLOW_COMPILER_MLIR_LITE_UTILS_ATTRIBUTE_UTILS_H_
21 
22 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
23 
24 namespace mlir {
25 namespace TFL {
26 
27 // Returns true if none of the three attributes are empty.
HasAll3Attrs(Attribute a,Attribute b,Attribute c)28 inline bool HasAll3Attrs(Attribute a, Attribute b, Attribute c) {
29   return a != Attribute() && b != Attribute() && c != Attribute();
30 }
31 
32 // Returns the single float element from an ElementsAttr. Returns empty
33 // attribute if the number of elements in the attribute is not 1 or the
34 // element isn't a float attribute.
35 FloatAttr ExtractSingleElementAsFloat(ElementsAttr attr);
36 
37 // Returns the single float element if the input is an ElementsAttr, or return
38 // itself as a float element. Returns empty attribute if the number of elements
39 // in the attribute is not 1, the element or itself isn't a float attribute.
40 FloatAttr GetSingleElementAsFloatOrSelf(Attribute attr);
41 
42 // Returns the single integer element from an ElementsAttr. Returns empty
43 // attribute if the number of elements in the attribute is not 1 or the
44 // element isn't a integer attribute.
45 IntegerAttr ExtractSingleElementAsInteger(ElementsAttr attr);
46 
47 }  // end namespace TFL
48 }  // end namespace mlir
49 
50 #endif  // TENSORFLOW_COMPILER_MLIR_LITE_UTILS_ATTRIBUTE_UTILS_H_
51