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 #include "tensorflow/compiler/mlir/xla/hlo_module_importer.h"
17
18 #include "mlir/Dialect/StandardOps/IR/Ops.h" // from @llvm-project
19 #include "mlir/IR/Attributes.h" // from @llvm-project
20 #include "mlir/IR/BuiltinTypes.h" // from @llvm-project
21 #include "mlir/IR/Location.h" // from @llvm-project
22 #include "mlir/IR/OperationSupport.h" // from @llvm-project
23 #include "mlir/IR/Types.h" // from @llvm-project
24 #include "tensorflow/compiler/mlir/hlo/include/mlir-hlo/Dialect/mhlo/IR/hlo_ops.h"
25 #include "tensorflow/compiler/mlir/xla/hlo_function_importer.h"
26 #include "tensorflow/compiler/xla/service/hlo_computation.h"
27 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
28 #include "tensorflow/compiler/xla/service/hlo_module.h"
29 #include "tensorflow/compiler/xla/xla.pb.h"
30
31 namespace xla {
32
HloModuleImporter(mlir::ModuleOp module,bool import_all_computation)33 HloModuleImporter::HloModuleImporter(mlir::ModuleOp module,
34 bool import_all_computation)
35 : import_all_computation_(import_all_computation),
36 module_(module),
37 builder_(module.getContext()) {
38 module.getContext()->loadDialect<mlir::StandardOpsDialect>();
39 module.getContext()->loadDialect<mlir::mhlo::MhloDialect>();
40 }
41
Import(const xla::HloModule & module)42 Status HloModuleImporter::Import(const xla::HloModule& module) {
43 if (!import_all_computation_)
44 // Only import the entry computation, any reachable one will be imported
45 // unless turned into a region operation.
46 return HloFunctionImporter::ImportAsFunc(
47 *module.entry_computation(), module_, &function_map_, &builder_);
48
49 for (const auto* computation : module.computations())
50 TF_RETURN_IF_ERROR(HloFunctionImporter::ImportAsFunc(
51 *computation, module_, &function_map_, &builder_));
52
53 return Status::OK();
54 }
55
Import(const xla::HloModuleProto & module_proto)56 Status HloModuleImporter::Import(const xla::HloModuleProto& module_proto) {
57 xla::DebugOptions debug_options;
58 TF_ASSIGN_OR_RETURN(
59 auto module_config,
60 xla::HloModule::CreateModuleConfigFromProto(module_proto, debug_options));
61 TF_ASSIGN_OR_RETURN(auto module, xla::HloModule::CreateFromProto(
62 module_proto, module_config));
63
64 return Import(*module);
65 }
66
67 } // namespace xla
68