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 16 #ifndef TENSORFLOW_DTENSOR_CC_DTENSOR_GRAPH_TO_MLIR_PASS_H_ 17 #define TENSORFLOW_DTENSOR_CC_DTENSOR_GRAPH_TO_MLIR_PASS_H_ 18 19 #include "absl/container/flat_hash_set.h" 20 #include "mlir/IR/MLIRContext.h" // from @llvm-project 21 #include "mlir/Pass/PassManager.h" // from @llvm-project 22 #include "tensorflow/core/common_runtime/device_set.h" 23 #include "tensorflow/core/framework/function.h" 24 #include "tensorflow/core/graph/graph.h" 25 #include "tensorflow/core/platform/fingerprint.h" 26 #include "tensorflow/dtensor/mlir/ir/tf_dtensor.h" 27 28 namespace tensorflow { 29 30 class DTensorMlirPassRunner { 31 public: 32 DTensorMlirPassRunner(); 33 // Translates `graph` and replaces it with the resulting rewritten graph. 34 Status RunOnGraph(const DeviceSet& device_set, bool is_func, 35 FunctionLibraryDefinition* flib_def, 36 std::unique_ptr<Graph>* graph, 37 absl::flat_hash_set<Node*>& control_ret_nodes, 38 Fprint128 cache_key); 39 40 private: 41 // N.B. op_registration_ must be initialized before context/pass-manager to 42 // ensure DTensor operations are available during optimization passes. 43 bool op_registration_ = mlir::TF::RegisterDTensorTFOps(); 44 mlir::MLIRContext context_; 45 mlir::PassManager pass_manager_; 46 47 bool logging_enabled_; 48 }; 49 50 } // namespace tensorflow 51 52 #endif // TENSORFLOW_DTENSOR_CC_DTENSOR_GRAPH_TO_MLIR_PASS_H_ 53