xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/mlir_hlo/lib/Transforms/hlo_to_gpu_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 /// This files contains a pipeline which converts HLO operations to GPU kernels
17 /// written in a combination of LLVM and NVVM dialects.
18 
19 #include "mlir-hlo/Dialect/mhlo/transforms/passes.h"
20 #include "mlir-hlo/Transforms/gpu_passes.h"
21 #include "mlir-hlo/Transforms/passes.h"
22 #include "mlir/Conversion/AffineToStandard/AffineToStandard.h"
23 #include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
24 #include "mlir/Conversion/SCFToGPU/SCFToGPUPass.h"
25 #include "mlir/Conversion/ShapeToStandard/ShapeToStandard.h"
26 #include "mlir/Dialect/Bufferization/Transforms/Passes.h"
27 #include "mlir/Dialect/Func/IR/FuncOps.h"
28 #include "mlir/Dialect/GPU/Transforms/Passes.h"
29 #include "mlir/Dialect/Linalg/Passes.h"
30 #include "mlir/Dialect/SCF/Transforms/Passes.h"
31 #include "mlir/Pass/Pass.h"
32 #include "mlir/Pass/PassManager.h"
33 #include "mlir/Transforms/Passes.h"
34 
35 using namespace mlir;
36 using ::mlir::func::FuncOp;
37 using ::mlir::gpu::GPUModuleOp;
38 
39 // TODO(b/233761238): We only want to have this pipeline temporarily, as it is
40 // not yet clear how exactly it will look like. The goal is to merge this with
41 // the unified kernel generator + autofusion + XLA Next pipeline once we have
42 // it, and once this code stabilizes.
createHloToGpuPipeline(OpPassManager & pm,ArrayRef<int64_t> tileSizes,ArrayRef<int64_t> unrollFactors)43 void mlir::createHloToGpuPipeline(OpPassManager &pm,
44                                   ArrayRef<int64_t> tileSizes,
45                                   ArrayRef<int64_t> unrollFactors) {
46   // HLO -> Loops
47   pm.addNestedPass<FuncOp>(mhlo::createLegalizeHloToLinalgPass());
48   pm.addNestedPass<FuncOp>(createLinalgElementwiseOpFusionPass());
49   pm.addNestedPass<FuncOp>(createLinalgInitTensorToAllocTensorPass());
50   pm.addPass(createComputeOpAndFuncBufferizePass());
51   pm.addNestedPass<FuncOp>(createCanonicalizerPass());
52   pm.addNestedPass<FuncOp>(createConvertLinalgToParallelLoopsPass());
53   pm.addNestedPass<FuncOp>(bufferization::createBufferDeallocationPass());
54   // Loops -> GPU
55   pm.addNestedPass<FuncOp>(createCollapseParallelLoopsTo1DPass());
56   pm.addNestedPass<FuncOp>(createTileLoopsPass(tileSizes, unrollFactors));
57   pm.addNestedPass<FuncOp>(createGpuMapParallelLoopsPass());
58   pm.addNestedPass<FuncOp>(createLoopInvariantCodeMotionPass());
59   pm.addNestedPass<FuncOp>(createParallelLoopToGpuPass());
60   pm.addNestedPass<FuncOp>(createCanonicalizerPass());
61   pm.addPass(createGpuLauchSinkIndexComputationsPass());
62   constexpr llvm::StringRef kGpuDataLayoutSpec =
63       "#dlti.dl_spec<#dlti.dl_entry<index,32:i32>>";
64   pm.addPass(createGpuKernelOutliningPass(kGpuDataLayoutSpec));
65   pm.addNestedPass<GPUModuleOp>(createForLoopSpecializationPass());
66   pm.addNestedPass<GPUModuleOp>(createLowerAffinePass());
67   pm.addNestedPass<GPUModuleOp>(createCanonicalizerPass());
68   pm.addNestedPass<GPUModuleOp>(createConvertSCFToCFPass());
69   // GPU -> low-level IR
70 #if TENSORFLOW_USE_ROCM
71   pm.addNestedPass<GPUModuleOp>(createGpuKernelToRocdlPass());
72 #else
73   pm.addNestedPass<GPUModuleOp>(createGpuKernelToNvvmPass());
74 #endif
75   pm.addPass(createPropagateStaticShapesToKernelPass());
76   // Some instructions crash ptxas down the line if they have debug info
77   // attached.
78   pm.addNestedPass<GPUModuleOp>(createStripDebugInfoPass());
79 }
80