xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/python/mlir.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 <string>
17 
18 #include "llvm/Support/raw_ostream.h"
19 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
20 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
21 #include "mlir/IR/MLIRContext.h"  // from @llvm-project
22 #include "mlir/Parser/Parser.h"  // from @llvm-project
23 #include "pybind11/pybind11.h"
24 #include "tensorflow/compiler/mlir/xla/hlo_to_mlir_hlo.h"
25 #include "tensorflow/compiler/xla/client/xla_computation.h"
26 #include "tensorflow/compiler/xla/mlir_hlo/include/mlir-hlo/Dialect/mhlo/IR/chlo_ops.h"
27 #include "tensorflow/compiler/xla/mlir_hlo/include/mlir-hlo/Dialect/mhlo/IR/hlo_ops.h"
28 #include "tensorflow/compiler/xla/pjrt/mlir_to_hlo.h"
29 #include "tensorflow/compiler/xla/python/types.h"
30 #include "tensorflow/compiler/xla/status.h"
31 
32 namespace py = pybind11;
33 
34 namespace xla {
35 namespace {
36 
37 // Converts an XlaComputation to an MHLO mlir::Module string. Exists for
38 // backwards compatibility.
39 // TODO(phawkins): port remaining users of XlaComputations to use mlir::Modules
40 // instead and delete this function.
PyXlaComputationToMlirModule(const XlaComputation & computation)41 StatusOr<std::string> PyXlaComputationToMlirModule(
42     const XlaComputation& computation) {
43   mlir::MLIRContext context;
44   mlir::OwningOpRef<mlir::ModuleOp> module =
45       mlir::ModuleOp::create(mlir::UnknownLoc::get(&context));
46   context.loadDialect<mlir::func::FuncDialect>();
47   context.loadDialect<mlir::mhlo::MhloDialect>();
48   TF_RETURN_IF_ERROR(ConvertHloToMlirHlo(*module, &computation.proto(),
49                                          /*import_all_computations=*/true));
50   std::string s;
51   llvm::raw_string_ostream os(s);
52   mlir::OpPrintingFlags flags;
53   flags.enableDebugInfo();
54   module->print(os, flags);
55   return s;
56 }
57 
PyMlirModuleToXlaComputation(std::string mlir_module,bool use_tuple_args,bool return_tuple)58 StatusOr<XlaComputation> PyMlirModuleToXlaComputation(std::string mlir_module,
59                                                       bool use_tuple_args,
60                                                       bool return_tuple) {
61   mlir::MLIRContext context;
62   mlir::OwningOpRef<mlir::ModuleOp> module;
63   context.loadDialect<mlir::func::FuncDialect>();
64   context.loadDialect<mlir::mhlo::MhloDialect>();
65   context.loadDialect<mlir::chlo::ChloDialect>();
66   mlir::StatusScopedDiagnosticHandler diagnostic_handler(&context);
67   module = mlir::parseSourceString<mlir::ModuleOp>(
68       llvm::StringRef(mlir_module.data(), mlir_module.size()), &context);
69   if (!module) {
70     return diagnostic_handler.ConsumeStatus();
71   }
72   if (failed(module->verifyInvariants())) {
73     VLOG(1) << "MLIR verification failed.";
74     module->dump();
75     return diagnostic_handler.ConsumeStatus();
76   }
77 
78   XlaComputation computation;
79   TF_RETURN_IF_ERROR(
80       MlirToXlaComputation(*module, computation, use_tuple_args, return_tuple));
81   return computation;
82 }
83 
84 }  // namespace
85 
BuildMlirSubmodule(py::module & m)86 void BuildMlirSubmodule(py::module& m) {
87   py::module mlir_module = m.def_submodule("mlir", "MLIR/XLA integration");
88 
89   mlir_module.def("xla_computation_to_mlir_module",
90                   &PyXlaComputationToMlirModule);
91   mlir_module.def("mlir_module_to_xla_computation",
92                   &PyMlirModuleToXlaComputation, py::arg("mlir_module"),
93                   py::arg("use_tuple_args") = false,
94                   py::arg("return_tuple") = false);
95 }
96 
97 }  // namespace xla
98