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 #ifndef TENSORFLOW_COMPILER_MLIR_TFRT_ANALYSIS_TENSOR_ARRAY_SIDE_EFFECT_ANALYSIS_H_
16 #define TENSORFLOW_COMPILER_MLIR_TFRT_ANALYSIS_TENSOR_ARRAY_SIDE_EFFECT_ANALYSIS_H_
17 
18 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
19 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
20 
21 namespace tensorflow {
22 namespace tfrt_compiler {
23 
24 // Return true if it is a TensorArrayOp, eg. TensorArrayV3Op.
25 bool IsTensorArrayOp(mlir::Operation* op);
26 
27 // This class provides utilities for analyzing side effects for TensorArray ops
28 // in the graph. mlir::TF::SideEffectAnalysis currently produces suboptimal
29 // side-effect analysis for TensorArray ops. On the other hand, control
30 // dependencies are already sorted out for TensorArray ops in the original TF
31 // graph. Each TensorArray op will take or produce a `flow` value and they are
32 // already properly chained in the origninal TF graph.
33 class TensorArraySideEffectAnalysis {
34  public:
35   explicit TensorArraySideEffectAnalysis(mlir::ModuleOp module);
36 
37   // Return if the function contains only non-side-effecting ops or TensorArray
38   // ops.
HasAtMostTensorArrayEffect(mlir::func::FuncOp func_op)39   bool HasAtMostTensorArrayEffect(mlir::func::FuncOp func_op) const {
40     return set_.count(func_op) > 0;
41   }
42 
43  private:
44   llvm::DenseSet<mlir::func::FuncOp> set_;
45 };
46 
47 }  // namespace tfrt_compiler
48 }  // namespace tensorflow
49 
50 #endif  // TENSORFLOW_COMPILER_MLIR_TFRT_ANALYSIS_TENSOR_ARRAY_SIDE_EFFECT_ANALYSIS_H_
51