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