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 #include "tensorflow/compiler/mlir/tfrt/analysis/tensor_array_side_effect_analysis.h" 16 17 #include "mlir/Interfaces/SideEffectInterfaces.h" // from @llvm-project 18 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" 19 20 namespace tensorflow { 21 namespace tfrt_compiler { 22 IsTensorArrayOp(mlir::Operation * op)23bool IsTensorArrayOp(mlir::Operation* op) { 24 return llvm::isa<mlir::TF::TensorArrayV3Op, mlir::TF::TensorArrayScatterV3Op, 25 mlir::TF::TensorArrayGatherV3Op, 26 mlir::TF::TensorArrayReadV3Op, 27 mlir::TF::TensorArrayWriteV3Op>(op); 28 } 29 FunctionContainsOnlyNoSideEffectOpOrTensorArrayOp(mlir::FuncOp func_op)30static bool FunctionContainsOnlyNoSideEffectOpOrTensorArrayOp( 31 mlir::FuncOp func_op) { 32 for (mlir::Operation& op : func_op.front()) { 33 if (!mlir::MemoryEffectOpInterface::hasNoEffect(&op) && 34 !IsTensorArrayOp(&op)) 35 return false; 36 } 37 38 return true; 39 } 40 TensorArraySideEffectAnalysis(mlir::ModuleOp module)41TensorArraySideEffectAnalysis::TensorArraySideEffectAnalysis( 42 mlir::ModuleOp module) { 43 for (auto func_op : module.getOps<mlir::FuncOp>()) { 44 if (FunctionContainsOnlyNoSideEffectOpOrTensorArrayOp(func_op)) { 45 set_.insert(func_op); 46 } 47 } 48 } 49 50 } // namespace tfrt_compiler 51 } // namespace tensorflow 52