MLIR 23.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
34
35using namespace mlir;
36
37namespace {
38//===----------------------------------------------------------------------===//
39// Pre-GPU common pipeline for both Host and GPU.
40//===----------------------------------------------------------------------===//
41void buildPreGPUCommonPassPipeline(
43 // builtin.module scope passes.
44 pm.addPass(createCSEPass());
46 {
47 GpuXeVMAttachTargetOptions xevmTargetOptions;
48 xevmTargetOptions.moduleMatcher = options.xevmModuleMatcher;
49 xevmTargetOptions.triple = options.zebinTriple;
50 xevmTargetOptions.chip = options.zebinChip;
51 xevmTargetOptions.optLevel = options.optLevel;
52 xevmTargetOptions.cmdOptions = options.cmdOptions;
53 pm.addPass(createGpuXeVMAttachTarget(xevmTargetOptions));
54 }
55 pm.addPass(createLowerAffinePass());
56 pm.addNestedPass<func::FuncOp>(createGpuAsyncRegionPass());
57}
58
59//===----------------------------------------------------------------------===//
60// GPUModule-specific stuff.
61//===----------------------------------------------------------------------===//
62void buildGPUPassPipeline(OpPassManager &pm,
64 xegpu::XeGPUPropagateLayoutOptions laneLayoutOptions;
65 laneLayoutOptions.indexBitWidth = options.use64bitIndex ? 64 : 32;
66 laneLayoutOptions.layoutKind = "lane";
67 pm.addNestedPass<ModuleOp>(createCSEPass());
68 if (options.xegpuOpLevel == "workgroup") {
69 xegpu::XeGPUPropagateLayoutOptions sgLayoutOptions;
70 sgLayoutOptions.layoutKind = "subgroup";
71 pm.addNestedPass<gpu::GPUModuleOp>(
72 xegpu::createXeGPUPropagateLayout(sgLayoutOptions));
73 pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUWgToSgDistribute());
74 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
75 pm.addNestedPass<gpu::GPUModuleOp>(createLowerAffinePass());
76 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
77 xegpu::XeGPUPropagateLayoutOptions instDataOptions;
78 instDataOptions.layoutKind = "inst";
79 pm.addNestedPass<gpu::GPUModuleOp>(
80 xegpu::createXeGPUPropagateLayout(instDataOptions));
81 pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUBlocking());
82 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
83 }
84 if (options.xegpuOpLevel == "subgroup" ||
85 options.xegpuOpLevel == "workgroup") {
86 pm.addNestedPass<gpu::GPUModuleOp>(
87 xegpu::createXeGPUPropagateLayout(laneLayoutOptions));
88 pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUPeepHoleOptimizer());
89 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
90 pm.addNestedPass<gpu::GPUModuleOp>(
91 xegpu::createXeGPUPropagateLayout(laneLayoutOptions));
92 pm.addNestedPass<gpu::GPUModuleOp>(
93 xegpu::createXeGPUSgToWiDistributeExperimental());
94 pm.addNestedPass<gpu::GPUModuleOp>(createCanonicalizerPass());
95 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
96 pm.addNestedPass<gpu::GPUModuleOp>(createLoopInvariantCodeMotionPass());
97 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
98 pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUVectorLinearize());
99 }
100 pm.addNestedPass<gpu::GPUModuleOp>(createConvertMathToXeVM());
101 ConvertXeGPUToXeVMPassOptions xegpuToXeVMOptions;
102 xegpuToXeVMOptions.use64bitIndex = options.use64bitIndex;
103 pm.addNestedPass<gpu::GPUModuleOp>(
104 createConvertXeGPUToXeVMPass(xegpuToXeVMOptions));
105 {
106 ConvertGpuOpsToLLVMSPVOpsOptions gpuToLLVMSPVOptions;
107 gpuToLLVMSPVOptions.use64bitIndex = options.use64bitIndex;
108 pm.addNestedPass<gpu::GPUModuleOp>(
109 createConvertGpuOpsToLLVMSPVOps(gpuToLLVMSPVOptions));
110 }
111 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
112 pm.addNestedPass<gpu::GPUModuleOp>(createReconcileUnrealizedCastsPass());
113}
114
115//===----------------------------------------------------------------------===//
116// Post-GPU pipeline for both Host and GPU.
117//===----------------------------------------------------------------------===//
118void buildPostGPUCommonPassPipeline(
120 // builtin.module scope passes.
121 pm.addPass(createSCFToControlFlowPass());
122 pm.addPass(memref::createExpandStridedMetadataPass());
123 {
124 GpuToLLVMConversionPassOptions gpuToLLVMOptions;
125 gpuToLLVMOptions.hostBarePtrCallConv = options.hostBarePtrCallConv;
126 gpuToLLVMOptions.kernelBarePtrCallConv = options.kernelBarePtrCallConv;
127 pm.addPass(createGpuToLLVMConversionPass(gpuToLLVMOptions));
128 }
129 pm.addPass(createLowerAffinePass());
130 pm.addPass(createConvertVectorToLLVMPass());
131 pm.addPass(createConvertToLLVMPass());
132 pm.addPass(createReconcileUnrealizedCastsPass());
133 pm.addNestedPass<gpu::GPUModuleOp>(createCanonicalizerPass());
134 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
135 // XeVM-to-LLVM must be the last pass before gpu-module-to-binary.
136 pm.addNestedPass<gpu::GPUModuleOp>(createConvertXeVMToLLVMPass());
137 // gpu-module-to-binary
138 {
139 GpuModuleToBinaryPassOptions gpuToModuleBinOptions;
140 gpuToModuleBinOptions.compilationTarget = options.binaryFormat;
141 gpuToModuleBinOptions.cmdOptions = options.cmdOptions;
142 pm.addPass(createGpuModuleToBinaryPass(gpuToModuleBinOptions));
143 }
144}
145} // namespace
146
149 // Pre-GPU common pipelines.
150 buildPreGPUCommonPassPipeline(pm, options);
151
152 // GPUModule-specific stuff.
153 buildGPUPassPipeline(pm, options);
154
155 // Post-GPU pipeline for both Host and GPU.
156 buildPostGPUCommonPassPipeline(pm, options);
157}
158
161 "gpu-lower-to-xevm-pipeline",
162 "The default GPU to XeVM lowering pipeline. It starts by lowering GPU "
163 "code to the "
164 "specified compilation target (default is fatbin) then lowers the host "
165 "code.",
167}
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.
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 > createCanonicalizerPass(const GreedyRewriteConfig &config, ArrayRef< std::string > disabledPatterns={}, ArrayRef< std::string > enabledPatterns={})
Creates an instance of the Canonicalizer pass with the specified config.
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...