xref: /aosp_15_r20/external/tensorflow/tensorflow/core/transforms/utils/op_cat_helper.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_CORE_TRANSFORMS_UTILS_OP_CAT_HELPER_H_
16 #define TENSORFLOW_CORE_TRANSFORMS_UTILS_OP_CAT_HELPER_H_
17 
18 #include "mlir/IR/MLIRContext.h"  // from @llvm-project
19 #include "mlir/IR/Operation.h"  // from @llvm-project
20 #include "tensorflow/core/ir/dialect.h"
21 #include "tensorflow/core/ir/tf_op_wrapper.h"
22 
23 namespace mlir {
24 namespace tfg {
25 // A Helper class to identify if an op belongs to certain op category.
26 class OpCatHelper {
27  public:
28   OpCatHelper() = default;
OpCatHelper(TFGraphDialect * dialect)29   explicit OpCatHelper(TFGraphDialect *dialect) : dialect_(dialect) {}
30 
31   bool IsAggregate(TFOp op);
32   bool IsCommutative(TFOp op);
33 
34   // Returns true if it's a splat tensor type and has the splat value 1.
35   bool IsOnes(TFOp op);
36   // Returns true if it's a splat tensor type and has the splat value 0.
37   bool IsZeros(TFOp op);
38 
39   // Returns true if the op is known to use persistent memory to store its
40   // value.
41   bool IsPersistent(TFOp op);
42 
43   // Returns true if the op belongs to the NC_DATASET class (see graph/graph.h).
44   bool IsDataset(TFOp op);
45 
getDialect()46   TFGraphDialect *getDialect() const { return dialect_; }
47 
48  protected:
49   TFGraphDialect *dialect_;
50 };
51 }  // namespace tfg
52 }  // namespace mlir
53 
54 #endif  // TENSORFLOW_CORE_TRANSFORMS_UTILS_OP_CAT_HELPER_H_
55