xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/lite/experimental/tac/common/utils.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_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_UTILS_H_
17 #define TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_UTILS_H_
18 
19 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"  // from @llvm-project
20 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
21 #include "mlir/IR/BuiltinTypes.h"  // from @llvm-project
22 #include "mlir/Interfaces/CallInterfaces.h"  // from @llvm-project
23 #include "tensorflow/compiler/mlir/lite/experimental/tac/common/targets.h"
24 #include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h"
25 
26 namespace mlir {
27 namespace TFL {
28 namespace tac {
29 
30 // Returns true if 'op' is non const op. Returns false otherwise or if
31 // 'op' is null.
IsNonConstOp(Operation * op)32 inline bool IsNonConstOp(Operation* op) {
33   if (!op) return false;
34   if (llvm::isa<arith::ConstantOp, mlir::func::ConstantOp>(op)) return false;
35   if (op->hasTrait<OpTrait::ConstantLike>()) return false;
36   if (llvm::isa<TFL::ConstOp, TFL::QConstOp>(op)) return false;
37   return true;
38 }
39 
40 // Returns true if 'op' is a terminator op, otherwise false.
41 bool IsTerminatorOp(Operation* op);
42 
43 // Returns true if 'op' is not TFL Quant / Dequant op. Returns False otherwise
44 // or if 'op' is null.
45 bool NotTFLQuantDequantizeOp(Operation* op);
46 
47 // Returns true if it is a shaped type of f32 elements.
IsF32ShapedType(Type t)48 inline bool IsF32ShapedType(Type t) {
49   if (auto shaped_type = t.dyn_cast_or_null<ShapedType>()) {
50     return shaped_type.getElementType().isF32();
51   }
52   return false;
53 }
54 
55 // Return true when the given element_type is QI8.
IsQI8Type(Type t)56 inline bool IsQI8Type(Type t) {
57   auto quantized_type = quant::QuantizedType::getQuantizedElementType(t);
58   return quantized_type != nullptr &&
59          quantized_type.getStorageTypeIntegralWidth() == 8 &&
60          quantized_type.isSigned();
61 }
62 
63 // Return true when the given element_type is QUI8.
IsQUI8Type(Type t)64 inline bool IsQUI8Type(Type t) {
65   auto quantized_type = quant::QuantizedType::getQuantizedElementType(t);
66   return quantized_type != nullptr &&
67          quantized_type.getStorageTypeIntegralWidth() == 8 &&
68          !quantized_type.isSigned();
69 }
70 
71 // Return true when the given element_type is QI32.
IsQI32Type(Type t)72 inline bool IsQI32Type(Type t) {
73   auto quantized_type = quant::QuantizedType::getQuantizedElementType(t);
74   return quantized_type != nullptr &&
75          quantized_type.getStorageTypeIntegralWidth() == 32 &&
76          quantized_type.isSigned();
77 }
78 
79 // Try to guess the inference type of the op.
80 InferenceType GetInferenceType(Operation* op);
81 
82 }  // namespace tac
83 }  // namespace TFL
84 }  // namespace mlir
85 
86 #endif  // TENSORFLOW_COMPILER_MLIR_LITE_EXPERIMENTAL_TAC_COMMON_UTILS_H_
87