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.layoutKind = "lane";
66 pm.addNestedPass<ModuleOp>(createCSEPass());
67 if (options.xegpuOpLevel == "workgroup") {
68 xegpu::XeGPUPropagateLayoutOptions sgLayoutOptions;
69 sgLayoutOptions.layoutKind = "subgroup";
70 pm.addNestedPass<gpu::GPUModuleOp>(
71 xegpu::createXeGPUPropagateLayout(sgLayoutOptions));
72 pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUWgToSgDistribute());
73 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
74 pm.addNestedPass<gpu::GPUModuleOp>(createLowerAffinePass());
75 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
76 xegpu::XeGPUPropagateLayoutOptions instDataOptions;
77 instDataOptions.layoutKind = "inst";
78 pm.addNestedPass<gpu::GPUModuleOp>(
79 xegpu::createXeGPUPropagateLayout(instDataOptions));
80 pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUBlocking());
81 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
82 }
83 if (options.xegpuOpLevel == "subgroup" ||
84 options.xegpuOpLevel == "workgroup") {
85 pm.addNestedPass<gpu::GPUModuleOp>(
86 xegpu::createXeGPUPropagateLayout(laneLayoutOptions));
87 pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUPeepHoleOptimizer());
88 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
89 pm.addNestedPass<gpu::GPUModuleOp>(
90 xegpu::createXeGPUPropagateLayout(laneLayoutOptions));
91 pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUSubgroupDistribute());
92 pm.addNestedPass<gpu::GPUModuleOp>(createCanonicalizerPass());
93 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
94 pm.addNestedPass<gpu::GPUModuleOp>(createLoopInvariantCodeMotionPass());
95 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
96 pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUVectorLinearize());
97 }
98 pm.addNestedPass<gpu::GPUModuleOp>(createConvertMathToXeVM());
99 pm.addNestedPass<gpu::GPUModuleOp>(createConvertXeGPUToXeVMPass());
100 {
101 ConvertGpuOpsToLLVMSPVOpsOptions gpuToLLVMSPVOptions;
102 gpuToLLVMSPVOptions.use64bitIndex = options.use64bitIndex;
103 pm.addNestedPass<gpu::GPUModuleOp>(
104 createConvertGpuOpsToLLVMSPVOps(gpuToLLVMSPVOptions));
105 }
106 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
107 pm.addNestedPass<gpu::GPUModuleOp>(createReconcileUnrealizedCastsPass());
108}
109
110//===----------------------------------------------------------------------===//
111// Post-GPU pipeline for both Host and GPU.
112//===----------------------------------------------------------------------===//
113void buildPostGPUCommonPassPipeline(
115 // builtin.module scope passes.
116 pm.addPass(createSCFToControlFlowPass());
117 pm.addPass(memref::createExpandStridedMetadataPass());
118 {
119 GpuToLLVMConversionPassOptions gpuToLLVMOptions;
120 gpuToLLVMOptions.hostBarePtrCallConv = options.hostBarePtrCallConv;
121 gpuToLLVMOptions.kernelBarePtrCallConv = options.kernelBarePtrCallConv;
122 pm.addPass(createGpuToLLVMConversionPass(gpuToLLVMOptions));
123 }
124 pm.addPass(createLowerAffinePass());
125 pm.addPass(createConvertVectorToLLVMPass());
126 pm.addPass(createConvertToLLVMPass());
127 pm.addPass(createReconcileUnrealizedCastsPass());
128 pm.addNestedPass<gpu::GPUModuleOp>(createCanonicalizerPass());
129 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
130 // XeVM-to-LLVM must be the last pass before gpu-module-to-binary.
131 pm.addNestedPass<gpu::GPUModuleOp>(createConvertXeVMToLLVMPass());
132 // gpu-module-to-binary
133 {
134 GpuModuleToBinaryPassOptions gpuToModuleBinOptions;
135 gpuToModuleBinOptions.compilationTarget = options.binaryFormat;
136 gpuToModuleBinOptions.cmdOptions = options.cmdOptions;
137 pm.addPass(createGpuModuleToBinaryPass(gpuToModuleBinOptions));
138 }
139}
140} // namespace
141
144 // Pre-GPU common pipelines.
145 buildPreGPUCommonPassPipeline(pm, options);
146
147 // GPUModule-specific stuff.
148 buildGPUPassPipeline(pm, options);
149
150 // Post-GPU pipeline for both Host and GPU.
151 buildPostGPUCommonPassPipeline(pm, options);
152}
153
156 "gpu-lower-to-xevm-pipeline",
157 "The default GPU to XeVM lowering pipeline. It starts by lowering GPU "
158 "code to the "
159 "specified compilation target (default is fatbin) then lowers the host "
160 "code.",
162}
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...