xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/pjrt/mlir_to_hlo.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 
16 #include "tensorflow/compiler/xla/pjrt/mlir_to_hlo.h"
17 
18 #include <utility>
19 
20 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
21 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
22 #include "mlir/Parser/Parser.h"  // from @llvm-project
23 #include "mlir/Pass/Pass.h"  // from @llvm-project
24 #include "mlir/Pass/PassManager.h"  // from @llvm-project
25 #include "mlir/Transforms/Passes.h"  // from @llvm-project
26 #include "tensorflow/compiler/mlir/tensorflow/utils/error_util.h"
27 #include "tensorflow/compiler/mlir/xla/mlir_hlo_to_hlo.h"
28 #include "tensorflow/compiler/xla/mlir_hlo/include/mlir-hlo/Dialect/mhlo/IR/chlo_ops.h"
29 #include "tensorflow/compiler/xla/mlir_hlo/include/mlir-hlo/Dialect/mhlo/IR/hlo_ops.h"
30 #include "tensorflow/compiler/xla/mlir_hlo/include/mlir-hlo/Dialect/mhlo/transforms/passes.h"
31 
32 namespace xla {
33 
MlirToXlaComputation(mlir::ModuleOp module,XlaComputation & xla_computation,bool use_tuple_args,bool return_tuple)34 Status MlirToXlaComputation(mlir::ModuleOp module,
35                             XlaComputation& xla_computation,
36                             bool use_tuple_args, bool return_tuple) {
37   mlir::StatusScopedDiagnosticHandler diagnostic_handler(module->getContext());
38   {
39     mlir::PassManager pm(module->getContext());
40     pm.addNestedPass<mlir::func::FuncOp>(
41         mlir::mhlo::createChloLegalizeToHloPass(
42             /*legalize_broadcasts=*/true, /*expand_compositions=*/true));
43     pm.addNestedPass<mlir::func::FuncOp>(mlir::createCanonicalizerPass());
44     // In order to export to XLA, we must sink constants to control flow
45     // regions, since XLA uses functional control flow.
46     pm.addNestedPass<mlir::func::FuncOp>(
47         mlir::mhlo::createSinkConstantsToControlFlowPass());
48     if (failed(pm.run(module))) {
49       VLOG(1) << "MHLO->HLO lowering passes failed.";
50       module->dump();
51       return diagnostic_handler.ConsumeStatus();
52     }
53 
54     VLOG(5) << "MHLO module after lowering, before HLO import ";
55     if (VLOG_IS_ON(5)) {
56       module->dump();
57     }
58   }
59 
60   HloProto proto;
61   mlir::MlirToHloConversionOptions options;
62   // We don't want the conversion to muck with our operator names.
63   options.legalize_node_names = false;
64   TF_RETURN_IF_ERROR(ConvertMlirHloToHlo(module, &proto, use_tuple_args,
65                                          return_tuple, options));
66 
67   xla_computation = XlaComputation(std::move(*proto.mutable_hlo_module()));
68   return OkStatus();
69 }
70 
ParseMlirModuleString(absl::string_view mlir_module_str,mlir::MLIRContext & context)71 StatusOr<mlir::OwningOpRef<mlir::ModuleOp>> ParseMlirModuleString(
72     absl::string_view mlir_module_str, mlir::MLIRContext& context) {
73   mlir::OwningOpRef<mlir::ModuleOp> module;
74   context.loadDialect<mlir::func::FuncDialect>();
75   context.loadDialect<mlir::mhlo::MhloDialect>();
76   context.loadDialect<mlir::chlo::ChloDialect>();
77   mlir::StatusScopedDiagnosticHandler diagnostic_handler(&context);
78   module = mlir::parseSourceString<mlir::ModuleOp>(
79       llvm::StringRef(mlir_module_str.data(), mlir_module_str.size()),
80       &context);
81   if (!module) {
82     return diagnostic_handler.ConsumeStatus();
83   }
84   if (failed(module->verifyInvariants())) {
85     VLOG(1) << "MLIR verification failed.";
86     module->dump();
87     return diagnostic_handler.ConsumeStatus();
88   }
89   return std::move(module);
90 }
91 
ParseMlirModuleStringAndConvertToXlaComputation(absl::string_view mlir_module_str,XlaComputation & xla_computation,bool use_tuple_args,bool return_tuple)92 Status ParseMlirModuleStringAndConvertToXlaComputation(
93     absl::string_view mlir_module_str, XlaComputation& xla_computation,
94     bool use_tuple_args, bool return_tuple) {
95   mlir::MLIRContext context;
96   TF_ASSIGN_OR_RETURN(mlir::OwningOpRef<mlir::ModuleOp> module,
97                       xla::ParseMlirModuleString(mlir_module_str, context));
98   return xla::MlirToXlaComputation(*module, xla_computation, use_tuple_args,
99                                    return_tuple);
100 }
101 
102 }  // namespace xla
103