MLIR  22.0.0git
GPUToXeVMPipeline.cpp
Go to the documentation of this file.
1 //===- GPUToXeVMPipeline.cpp - Lowering pipeline to XeVM/LLVM -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a pass for testing the lowering to XeVM as a generally
10 // usable sink pass. If XeGPU ops are used, it expects the MLIR code to have
11 // XeGPU ops already embedded in gpu code.
12 //
13 //===----------------------------------------------------------------------===//
14 
17 #include "mlir/Conversion/Passes.h"
29 #include "mlir/Pass/PassManager.h"
30 #include "mlir/Pass/PassOptions.h"
32 #include "mlir/Transforms/Passes.h"
33 
34 using namespace mlir;
35 
36 namespace {
37 //===----------------------------------------------------------------------===//
38 // Pre-GPU common pipeline for both Host and GPU.
39 //===----------------------------------------------------------------------===//
40 void buildPreGPUCommonPassPipeline(
42  // builtin.module scope passes.
43  pm.addPass(createCSEPass());
45  {
46  GpuXeVMAttachTargetOptions xevmTargetOptions;
47  xevmTargetOptions.moduleMatcher = options.xevmModuleMatcher;
48  xevmTargetOptions.triple = options.zebinTriple;
49  xevmTargetOptions.chip = options.zebinChip;
50  xevmTargetOptions.optLevel = options.optLevel;
51  xevmTargetOptions.cmdOptions = options.cmdOptions;
52  pm.addPass(createGpuXeVMAttachTarget(xevmTargetOptions));
53  }
54  pm.addPass(createLowerAffinePass());
55  pm.addNestedPass<func::FuncOp>(createGpuAsyncRegionPass());
56 }
57 
58 //===----------------------------------------------------------------------===//
59 // GPUModule-specific stuff.
60 //===----------------------------------------------------------------------===//
61 void buildGPUPassPipeline(OpPassManager &pm,
63  if (options.xegpuOpLevel == "workgroup") {
64  pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUWgToSgDistribute());
65  pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
66  pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUBlocking());
67  pm.addNestedPass<gpu::GPUModuleOp>(createCanonicalizerPass());
68  pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
69  }
70  if (options.xegpuOpLevel == "subgroup" ||
71  options.xegpuOpLevel == "workgroup") {
72  pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUPropagateLayout());
73  pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUSubgroupDistribute());
74  pm.addNestedPass<gpu::GPUModuleOp>(createCanonicalizerPass());
75  pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
76  pm.addNestedPass<gpu::GPUModuleOp>(createLoopInvariantCodeMotionPass());
77  pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
78  pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUVectorLinearize());
79  }
80  pm.addNestedPass<gpu::GPUModuleOp>(createConvertMathToXeVM());
81  pm.addNestedPass<gpu::GPUModuleOp>(createConvertXeGPUToXeVMPass());
82  {
83  ConvertGpuOpsToLLVMSPVOpsOptions gpuToLLVMSPVOptions;
84  gpuToLLVMSPVOptions.use64bitIndex = options.use64bitIndex;
85  pm.addNestedPass<gpu::GPUModuleOp>(
86  createConvertGpuOpsToLLVMSPVOps(gpuToLLVMSPVOptions));
87  }
88  pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
89  pm.addNestedPass<gpu::GPUModuleOp>(createReconcileUnrealizedCastsPass());
90 }
91 
92 //===----------------------------------------------------------------------===//
93 // Post-GPU pipeline for both Host and GPU.
94 //===----------------------------------------------------------------------===//
95 void buildPostGPUCommonPassPipeline(
97  // builtin.module scope passes.
98  pm.addPass(createSCFToControlFlowPass());
99  pm.addPass(memref::createExpandStridedMetadataPass());
100  {
101  GpuToLLVMConversionPassOptions gpuToLLVMOptions;
102  gpuToLLVMOptions.hostBarePtrCallConv = options.hostBarePtrCallConv;
103  gpuToLLVMOptions.kernelBarePtrCallConv = options.kernelBarePtrCallConv;
104  pm.addPass(createGpuToLLVMConversionPass(gpuToLLVMOptions));
105  }
106  pm.addPass(createLowerAffinePass());
107  pm.addPass(createConvertToLLVMPass());
108  pm.addPass(createReconcileUnrealizedCastsPass());
109  // gpu-module-to-binary
110  {
111  GpuModuleToBinaryPassOptions gpuToModuleBinOptions;
112  gpuToModuleBinOptions.compilationTarget = options.binaryFormat;
113  gpuToModuleBinOptions.cmdOptions = options.cmdOptions;
114  pm.addPass(createGpuModuleToBinaryPass(gpuToModuleBinOptions));
115  }
116 }
117 } // namespace
118 
121  // Pre-GPU common pipelines.
122  buildPreGPUCommonPassPipeline(pm, options);
123 
124  // GPUModule-specific stuff.
125  buildGPUPassPipeline(pm, options);
126 
127  // Post-GPU pipeline for both Host and GPU.
128  buildPostGPUCommonPassPipeline(pm, options);
129 }
130 
133  "gpu-lower-to-xevm-pipeline",
134  "The default GPU to XeVM lowering pipeline. It starts by lowering GPU "
135  "code to the "
136  "specified compilation target (default is fatbin) then lowers the host "
137  "code.",
139 }
static llvm::ManagedStatic< PassManagerOptions > options
This class represents a pass manager that runs passes on either a specific operation type,...
Definition: PassManager.h:46
void addPass(std::unique_ptr< Pass > pass)
Add the given pass to this pass manager.
Definition: Pass.cpp:392
void addNestedPass(std::unique_ptr< Pass > pass)
Add the given pass to a nested pass manager for the given operation kind OpT.
Definition: PassManager.h:115
void registerGPUToXeVMPipeline()
void buildLowerToXeVMPassPipeline(OpPassManager &pm, const GPUToXeVMPipelineOptions &options)
Adds the GPU to XeVM pipeline to the given pass manager.
Include the generated interface declarations.
std::unique_ptr< Pass > createCSEPass()
Creates a pass to perform common sub expression elimination.
Definition: CSE.cpp:412
std::unique_ptr< Pass > createLoopInvariantCodeMotionPass()
Creates a loop invariant code motion pass that hoists loop invariant instructions out of the loop.
std::unique_ptr< Pass > createCanonicalizerPass()
Creates an instance of the Canonicalizer pass, configured with default settings (which can be overrid...
std::unique_ptr< Pass > createConvertVectorToSCFPass(const VectorTransferToSCFOptions &options=VectorTransferToSCFOptions())
Create a pass to convert a subset of vector ops to SCF.
PassPipelineRegistration provides a global initializer that registers a Pass pipeline builder routine...
Definition: PassRegistry.h:177