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_XLA_TRANSFORMS_PASSES_H_ 17 #define TENSORFLOW_COMPILER_MLIR_XLA_TRANSFORMS_PASSES_H_ 18 19 #include <memory> 20 21 #include "llvm/ADT/StringRef.h" 22 #include "mlir/IR/MLIRContext.h" // from @llvm-project 23 #include "mlir/IR/PatternMatch.h" // from @llvm-project 24 #include "mlir/Support/LogicalResult.h" // from @llvm-project 25 26 namespace mlir { 27 28 class FuncOp; 29 class ModuleOp; 30 class Operation; 31 template <typename T> 32 class OperationPass; 33 class Pass; 34 35 namespace mhlo { 36 37 /// Lowers from TF dialect to HLO dialect. When allow_partial_conversion is 38 /// false, emits an error if there is any operation that can't be legalized. 39 /// When `tf2xla_fallback_device_type` is not `None`, also uses legalization 40 /// patterns from TF2XLA fallback for provided device type (see 41 /// legalize_tf_with_tf2xla.cc for details). By default, TF2XLA fallback is not 42 /// used. 43 std::unique_ptr<OperationPass<FuncOp>> createLegalizeTFPass( 44 bool allow_partial_conversion = false, bool legalize_chlo = true, 45 llvm::Optional<StringRef> tf2xla_fallback_device_type = llvm::None); 46 47 /// Lowers from TF dialect to HLO dialect using tf2xla op kernels for the 48 /// specified device type. 49 std::unique_ptr<OperationPass<FuncOp>> createLegalizeTfWithTf2XlaPass( 50 llvm::StringRef device_type); 51 52 /// Replaces types that do not exist in MHLO with equivalent types that do 53 /// exist. 54 std::unique_ptr<OperationPass<ModuleOp>> CreateLegalizeTfTypesPass(); 55 56 /// Adds the TF to XLA via TF2XLA rewrite patterns to the pattern list. 57 void PopulateLegalizeTfWithTf2XlaPatterns(llvm::StringRef device_type, 58 OwningRewritePatternList& patterns); 59 60 /// Adds the TF to TF lowerings and TF to XLA rewrite patterns to the pattern 61 /// list. 62 void PopulateLegalizeTfPatterns(MLIRContext* context, 63 OwningRewritePatternList* patterns); 64 65 /// Checks whether the op is supported by the Tf2Xla fallback for legalization. 66 bool IsOpAllowedTf2XlaFallback(Operation* op); 67 68 /// Lowers from TF dialect's control flow to HLO dialect's control flow. 69 std::unique_ptr<OperationPass<ModuleOp>> createLegalizeTFControlFlowPass(); 70 71 /// Converts the provided Operation as well as all nested operations into HLO 72 /// dialect using the conversion patterns registered by the HLO dialect. When 73 /// allow_partial_conversion is false, emits an error if there is any operation 74 /// that can't be legalized. 75 /// When `tf2xla_fallback_device_type` is not `None`, also uses legalization 76 /// patterns from TF2XLA fallback for provided device type (see 77 /// legalize_tf_with_tf2xla.cc for details). By default, TF2XLA fallback is not 78 /// used. 79 LogicalResult legalizeTF( 80 Operation* op, bool allow_partial_conversion = false, 81 bool legalize_chlo = true, 82 llvm::Optional<StringRef> tf2xla_fallback_device_type = llvm::None); 83 84 // Legalizes TF/XLA communication ops (TF dialect) to HLO dialect communication 85 // ops. 86 std::unique_ptr<OperationPass<ModuleOp>> CreateLegalizeTFCommunicationPass(); 87 88 // Prepare module for export to XLA HLO protos/instruction. 89 std::unique_ptr<OperationPass<FuncOp>> CreatePrepareForExport(); 90 91 } // namespace mhlo 92 } // namespace mlir 93 94 #endif // TENSORFLOW_COMPILER_MLIR_XLA_TRANSFORMS_PASSES_H_ 95