xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/mlir/transforms/runtime/compilation_pipeline.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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/mlir/transforms/runtime/compilation_pipeline.h"
17 
18 #include <utility>
19 
20 #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h"  // from @llvm-project
21 #include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"  // from @llvm-project
22 #include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"  // from @llvm-project
23 #include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"  // from @llvm-project
24 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
25 #include "mlir/Dialect/MemRef/IR/MemRef.h"  // from @llvm-project
26 #include "mlir/Dialect/SCF/IR/SCF.h"  // from @llvm-project
27 #include "mlir/Pass/Pass.h"  // from @llvm-project
28 #include "mlir/Pass/PassRegistry.h"  // from @llvm-project
29 #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"  // from @llvm-project
30 #include "tensorflow/compiler/xla/mlir/transforms/runtime/passes.h"
31 
32 namespace xla {
33 namespace runtime {
34 
RegisterDefaultXlaRuntimeDialects(mlir::DialectRegistry & registry)35 void RegisterDefaultXlaRuntimeDialects(mlir::DialectRegistry& registry) {
36   // Register MLIR dialects supported by the compiled kernels.
37   registry.insert<mlir::memref::MemRefDialect, mlir::scf::SCFDialect,
38                   mlir::func::FuncDialect, RuntimeDialect>();
39 
40   // Register MLIR dialects that can be translated to LLVM IR.
41   mlir::registerLLVMDialectTranslation(registry);
42 }
43 
CreateDefaultXlaRuntimeCompilationPipeline(mlir::OpPassManager & pm,const CompilationPipelineOptions & opts)44 void CreateDefaultXlaRuntimeCompilationPipeline(
45     mlir::OpPassManager& pm, const CompilationPipelineOptions& opts) {
46   pm.addPass(mlir::createConvertSCFToCFPass());
47 
48   // Convert entry function to the XLA entrypoint.
49   pm.addPass(CreateConvertToEntrypoint());
50 
51   // Convert runtime operations and custom calls to LLVM dialect.
52   ConvertRuntimeToLLvmOpts rt_to_llvm_opts = {
53       opts.populate_type_id_names, opts.populate_type_conversions,
54       opts.populate_arg_encodings, opts.populate_attr_encodings};
55   pm.addPass(CreateConvertRuntimeToLLVMPass(std::move(rt_to_llvm_opts)));
56 
57   // Convert everythinG else to LLVM dialect.
58   pm.addPass(mlir::createMemRefToLLVMPass());
59   pm.addPass(mlir::createConvertFuncToLLVMPass());
60   pm.addPass(mlir::createReconcileUnrealizedCastsPass());
61 }
62 
CreateDefaultPipeline(mlir::OpPassManager & pm)63 static void CreateDefaultPipeline(mlir::OpPassManager& pm) {
64   CompilationPipelineOptions copts;
65   CreateDefaultXlaRuntimeCompilationPipeline(pm, copts);
66 }
67 
68 static mlir::PassPipelineRegistration<> kXlaRuntimePipeline(
69     "xla-runtime-default-pipeline", "Default XLA runtime compilation pipeline",
70     CreateDefaultPipeline);
71 
72 }  // namespace runtime
73 }  // namespace xla
74