1 /* Copyright 2022 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 #ifndef TENSORFLOW_COMPILER_MLIR_QUANTIZATION_TENSORFLOW_UTILS_OPS_LIFTING_UTILS_H_
16 #define TENSORFLOW_COMPILER_MLIR_QUANTIZATION_TENSORFLOW_UTILS_OPS_LIFTING_UTILS_H_
17 
18 #include "absl/strings/string_view.h"
19 #include "llvm/ADT/SmallVector.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 "tensorflow/compiler/mlir/tensorflow/ir/tf_dialect.h"
24 
25 // This header file defines common utils used by TF-Quant transformation
26 // passes to lift op compositions to a function.
27 namespace mlir {
28 namespace quant {
29 
30 inline constexpr absl::string_view kAttrMapAttribute = "attr_map";
31 // This attribute will be set for functions created by this pass.
32 inline constexpr absl::string_view kFusedFunctionAttr =
33     "tf_quant.composite_function";
34 // The keyword to detect if this is a `NullAttribute`.
35 inline constexpr absl::string_view kNullAttributeValue = "N/A";
36 
37 // Checks if the op is inside a lifted function.
38 bool IsInLiftedFunc(Operation *op);
39 
40 // Creates a function to wrap the section between arguments and results.
41 llvm::SmallVector<Value, 4> LiftAsFunctionCall(
42     OpBuilder builder, Location location, StringRef func_name,
43     const llvm::SmallVector<Value> &arguments,
44     const llvm::SmallVector<Value> &results,
45     const llvm::SmallVector<NamedAttribute> &attributes);
46 
47 // Same as above but with empty attributes.
48 llvm::SmallVector<Value, 4> LiftAsFunctionCall(
49     OpBuilder builder, Location location, StringRef func_name,
50     const llvm::SmallVector<Value> &arguments,
51     const llvm::SmallVector<Value> &results);
52 
53 }  // namespace quant
54 }  // namespace mlir
55 #endif  // TENSORFLOW_COMPILER_MLIR_QUANTIZATION_TENSORFLOW_UTILS_OPS_LIFTING_UTILS_H_
56