MLIR 24.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
36
37using namespace mlir;
38
39namespace {
40//===----------------------------------------------------------------------===//
41// Pre-GPU common pipeline for both Host and GPU.
42//===----------------------------------------------------------------------===//
43void buildPreGPUCommonPassPipeline(
45 // builtin.module scope passes.
47 {
48 GpuXeVMAttachTargetOptions xevmTargetOptions;
49 xevmTargetOptions.moduleMatcher = options.xevmModuleMatcher;
50 xevmTargetOptions.triple = options.zebinTriple;
51 xevmTargetOptions.chip = options.zebinChip;
52 xevmTargetOptions.optLevel = options.optLevel;
53 xevmTargetOptions.cmdOptions = options.cmdOptions;
54 pm.addPass(createGpuXeVMAttachTarget(xevmTargetOptions));
55 }
57 pm.addNestedPass<func::FuncOp>(createGpuAsyncRegionPass());
58}
59
60//===----------------------------------------------------------------------===//
61// GPUModule-specific stuff.
62//===----------------------------------------------------------------------===//
63void buildGPUPassPipeline(OpPassManager &pm,
65 xegpu::XeGPUPropagateLayoutOptions laneLayoutOptions;
66 laneLayoutOptions.indexBitWidth = options.use64bitIndex ? 64 : 32;
67 laneLayoutOptions.layoutKind = "lane";
68 pm.addNestedPass<ModuleOp>(createCSEPass());
69 if (options.enableVectorToXeGPU)
70 pm.addNestedPass<gpu::GPUModuleOp>(createConvertVectorToXeGPU());
71 if (options.xegpuOpLevel == "workgroup") {
73 sgLayoutOptions.layoutKind = "subgroup";
74 pm.addNestedPass<gpu::GPUModuleOp>(
75 xegpu::createXeGPUPropagateLayout(sgLayoutOptions));
77 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
78 pm.addNestedPass<gpu::GPUModuleOp>(createLowerAffinePass());
79 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
81 instDataOptions.layoutKind = "inst";
82 pm.addNestedPass<gpu::GPUModuleOp>(
83 xegpu::createXeGPUPropagateLayout(instDataOptions));
84 pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUBlocking());
85 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
86 }
87 if (options.xegpuOpLevel == "subgroup" ||
88 options.xegpuOpLevel == "workgroup") {
89 pm.addNestedPass<gpu::GPUModuleOp>(
90 xegpu::createXeGPUPropagateLayout(laneLayoutOptions));
92 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
93 pm.addNestedPass<gpu::GPUModuleOp>(
94 xegpu::createXeGPUPropagateLayout(laneLayoutOptions));
96 pm.addNestedPass<gpu::GPUModuleOp>(createCanonicalizerPass());
97 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
99 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
100 pm.addNestedPass<gpu::GPUModuleOp>(xegpu::createXeGPUVectorLinearize());
101 pm.addNestedPass<gpu::GPUModuleOp>(createCanonicalizerPass());
102 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
103 }
104 // Break down high-level micro-scaling (MX) ops (arith.scaling_extf and
105 // arith.scaling_truncf) into standard arith ops (extf/truncf + mulf), and
106 // expand extf/truncf on f8E8M0FNU into integer bit manipulation. This runs
107 // before the XeVM/LLVM conversions. The f4E2M1FN expansion patterns are
108 // intentionally left disabled: f4E2M1FN extf/truncf are lowered by the XeVM
109 // conversions (xevm.extf), whereas f8E8M0FNU is not supported there and so
110 // must be expanded here.
111 {
112 arith::ArithExpandOpsPassOptions arithExpandOptions;
113 arithExpandOptions.includeF8E8M0 = true;
114 pm.addNestedPass<gpu::GPUModuleOp>(
115 arith::createArithExpandOpsPass(arithExpandOptions));
116 }
117 pm.addNestedPass<gpu::GPUModuleOp>(createConvertMathToXeVM());
118 ConvertXeGPUToXeVMPassOptions xegpuToXeVMOptions;
119 xegpuToXeVMOptions.use64bitIndex = options.use64bitIndex;
120 pm.addNestedPass<gpu::GPUModuleOp>(
121 createConvertXeGPUToXeVMPass(xegpuToXeVMOptions));
122 {
123 ConvertGpuOpsToLLVMSPVOpsOptions gpuToLLVMSPVOptions;
124 gpuToLLVMSPVOptions.use64bitIndex = options.use64bitIndex;
125 pm.addNestedPass<gpu::GPUModuleOp>(
126 createConvertGpuOpsToLLVMSPVOps(gpuToLLVMSPVOptions));
127 }
128 // Legalize math/arith ops on floating-point types that the XeVM target
129 // cannot handle natively (e.g. bf16) by wrapping them with extf/truncf
130 // around a supported type (defaulting to f32).
131 {
133 mathExtendOptions.extraTypeStrs.assign(options.mathExtendExtraTypes.begin(),
134 options.mathExtendExtraTypes.end());
135 mathExtendOptions.targetTypeStr = options.supportedTargetTypes;
136 pm.addNestedPass<gpu::GPUModuleOp>(
137 math::createMathExtendToSupportedTypes(mathExtendOptions));
138 }
139 {
141 arithEmulateOptions.sourceTypeStrs.assign(
142 options.unsupportedSourceTypes.begin(),
143 options.unsupportedSourceTypes.end());
144 arithEmulateOptions.targetTypeStr = options.supportedTargetTypes;
145 pm.addNestedPass<gpu::GPUModuleOp>(
146 arith::createArithEmulateUnsupportedFloats(arithEmulateOptions));
147 }
148 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
150}
151
152//===----------------------------------------------------------------------===//
153// Post-GPU pipeline for both Host and GPU.
154//===----------------------------------------------------------------------===//
155void buildPostGPUCommonPassPipeline(
157 // builtin.module scope passes.
161 {
162 GpuToLLVMConversionPassOptions gpuToLLVMOptions;
163 gpuToLLVMOptions.hostBarePtrCallConv = options.hostBarePtrCallConv;
164 gpuToLLVMOptions.kernelBarePtrCallConv = options.kernelBarePtrCallConv;
165 pm.addPass(createGpuToLLVMConversionPass(gpuToLLVMOptions));
166 }
171 pm.addNestedPass<gpu::GPUModuleOp>(createCanonicalizerPass());
172 pm.addNestedPass<gpu::GPUModuleOp>(createCSEPass());
173 // XeVM-to-LLVM must be the last pass before gpu-module-to-binary.
174 pm.addNestedPass<gpu::GPUModuleOp>(createConvertXeVMToLLVMPass());
175 // gpu-module-to-binary
176 {
177 GpuModuleToBinaryPassOptions gpuToModuleBinOptions;
178 gpuToModuleBinOptions.compilationTarget = options.binaryFormat;
179 gpuToModuleBinOptions.cmdOptions = options.cmdOptions;
180 pm.addPass(createGpuModuleToBinaryPass(gpuToModuleBinOptions));
181 }
182}
183} // namespace
184
187 // Pre-GPU common pipelines.
188 buildPreGPUCommonPassPipeline(pm, options);
189
190 // GPUModule-specific stuff.
191 buildGPUPassPipeline(pm, options);
192
193 // Post-GPU pipeline for both Host and GPU.
194 buildPostGPUCommonPassPipeline(pm, options);
195}
196
199 "gpu-lower-to-xevm-pipeline",
200 "The default GPU to XeVM lowering pipeline. It starts by lowering GPU "
201 "code to the "
202 "specified compilation target (default is fatbin) then lowers the host "
203 "code.",
205}
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.
std::unique_ptr<::mlir::Pass > createArithEmulateUnsupportedFloats()
std::unique_ptr<::mlir::Pass > createArithExpandOpsPass()
void registerGPUToXeVMPipeline()
void buildLowerToXeVMPassPipeline(OpPassManager &pm, const GPUToXeVMPipelineOptions &options)
Adds the GPU to XeVM pipeline to the given pass manager.
std::unique_ptr<::mlir::Pass > createMathExtendToSupportedTypes()
std::unique_ptr<::mlir::Pass > createExpandStridedMetadataPass()
std::unique_ptr<::mlir::Pass > createXeGPUWgToSgDistribute()
std::unique_ptr<::mlir::Pass > createXeGPUVectorLinearize()
std::unique_ptr<::mlir::Pass > createXeGPUSgToLaneDistribute()
std::unique_ptr<::mlir::Pass > createXeGPUPeepHoleOptimizer()
std::unique_ptr<::mlir::Pass > createXeGPUBlocking()
std::unique_ptr<::mlir::Pass > createXeGPUPropagateLayout()
Include the generated interface declarations.
std::unique_ptr<::mlir::Pass > createConvertToLLVMPass()
std::unique_ptr<::mlir::Pass > createSCFToControlFlowPass()
std::unique_ptr<::mlir::Pass > createConvertGpuOpsToLLVMSPVOps()
std::unique_ptr<::mlir::Pass > createReconcileUnrealizedCastsPass()
std::unique_ptr<::mlir::Pass > createConvertVectorToLLVMPass()
std::unique_ptr<::mlir::Pass > createGpuAsyncRegionPass()
std::unique_ptr<::mlir::Pass > createCanonicalizerPass()
std::unique_ptr<::mlir::Pass > createConvertXeGPUToXeVMPass()
std::unique_ptr<::mlir::Pass > createLowerAffinePass()
std::unique_ptr<::mlir::Pass > createGpuXeVMAttachTarget()
std::unique_ptr<::mlir::Pass > createConvertVectorToXeGPU()
std::unique_ptr<::mlir::Pass > createConvertMathToXeVM()
std::unique_ptr<::mlir::Pass > createLoopInvariantCodeMotionPass()
std::unique_ptr<::mlir::Pass > createGpuToLLVMConversionPass()
std::unique_ptr<::mlir::Pass > createGpuModuleToBinaryPass()
std::unique_ptr< Pass > createConvertVectorToSCFPass(const VectorTransferToSCFOptions &options=VectorTransferToSCFOptions())
Create a pass to convert a subset of vector ops to SCF.
std::unique_ptr<::mlir::Pass > createConvertXeVMToLLVMPass()
std::unique_ptr<::mlir::Pass > createCSEPass()
Definition CSE.cpp:177
PassPipelineRegistration provides a global initializer that registers a Pass pipeline builder routine...
::llvm::SmallVector< std::string > sourceTypeStrs
Definition Passes.h:20
::llvm::SmallVector< std::string > extraTypeStrs
Definition Passes.h:114