1 /* Copyright 2019 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_TENSORFLOW_UTILS_EXPORT_UTILS_H_ 17 #define TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_EXPORT_UTILS_H_ 18 19 #include <functional> 20 #include <memory> 21 #include <string> 22 23 #include "absl/container/flat_hash_set.h" 24 #include "llvm/ADT/ArrayRef.h" 25 #include "llvm/ADT/StringRef.h" 26 #include "mlir/IR/Attributes.h" // from @llvm-project 27 #include "mlir/IR/Location.h" // from @llvm-project 28 #include "mlir/IR/Operation.h" // from @llvm-project 29 #include "mlir/IR/Types.h" // from @llvm-project 30 #include "tensorflow/core/framework/attr_value.pb.h" 31 #include "tensorflow/core/framework/function.h" 32 #include "tensorflow/core/framework/node_def.pb.h" 33 #include "tensorflow/core/framework/node_def_util.h" 34 #include "tensorflow/core/lib/core/status.h" 35 #include "tensorflow/stream_executor/lib/statusor.h" 36 37 namespace mlir { 38 class ShapedType; 39 } // namespace mlir 40 41 namespace tensorflow { 42 43 using stream_executor::port::StatusOr; 44 45 // Add custom op prefix for TensorFlow dialects. 46 Status AddTensorFlowOpPrefix(std::string); 47 48 // Maps an MLIR op name in the TensorFlow dialect or the TensorFlow control 49 // dialect back into a TensorFlow valid op name. 50 StatusOr<llvm::StringRef> GetTensorFlowOpName(llvm::StringRef); 51 52 // Converts an MLIR operation to TensorFlow NodeDef with given node name. This 53 // name should be unique to the graph it is being inserted into. 54 StatusOr<std::unique_ptr<NodeDef>> GetOperationNodeDef( 55 mlir::Operation* inst, llvm::StringRef name); 56 57 // Converts MLIR attributes with values to their tensorflow equivalent. 58 // "name" and "device" attributes are ignored by default. Use attrs_to_ignore to 59 // specify any other attributes that should be ignored. 60 Status ConvertAttributes( 61 const llvm::ArrayRef<mlir::NamedAttribute> attrs, 62 const absl::flat_hash_set<absl::string_view>& attrs_to_ignore, 63 bool remove_ref_type, AttrValueMap* values); 64 65 // Sets shape attribute with the given name. If the attribute already exists 66 // with a different value, returns an error. 67 Status SetShapeAttribute(absl::string_view name, mlir::ShapedType shape, 68 AttrValueMap* values); 69 70 // Returns true if the given instruction is an mlir::TF::LegacyCallOp or the 71 // result of such an operation transformed by the 72 // ExecutorToControlDialectConversion pass. 73 // 74 // TODO(b/145706023): When the ExecutorToControlDialectConversion pass runs 75 // before the exporter, it mutates an mlir::TF::LegacyCallOp instruction to 76 // an instruction with a different operation name. As such, this routine checks 77 // both forms of a LegacyCall instruction. We only need to check for 78 // mlir::TF::LegacyCallOp when the ticket is resolved. 79 bool IsLegacyCallInstruction(mlir::Operation* inst); 80 } // namespace tensorflow 81 #endif // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_EXPORTER_UTILS_H_ 82