1//===- GPUToSPIRVPass.cpp - GPU to SPIR-V Passes --------------------------===//
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 to convert a kernel function in the GPU Dialect
10// into a spirv.module operation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Conversion/GPUToSPIRV/GPUToSPIRVPass.h"
15
16#include "mlir/Conversion/ArithToSPIRV/ArithToSPIRV.h"
17#include "mlir/Conversion/FuncToSPIRV/FuncToSPIRV.h"
18#include "mlir/Conversion/GPUToSPIRV/GPUToSPIRV.h"
19#include "mlir/Conversion/MemRefToSPIRV/MemRefToSPIRV.h"
20#include "mlir/Conversion/SCFToSPIRV/SCFToSPIRV.h"
21#include "mlir/Conversion/VectorToSPIRV/VectorToSPIRV.h"
22#include "mlir/Dialect/Func/IR/FuncOps.h"
23#include "mlir/Dialect/GPU/IR/GPUDialect.h"
24#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
25#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
26#include "mlir/IR/PatternMatch.h"
27
28namespace mlir {
29#define GEN_PASS_DEF_CONVERTGPUTOSPIRV
30#include "mlir/Conversion/Passes.h.inc"
31} // namespace mlir
32
33using namespace mlir;
34
35namespace {
36/// Pass to lower GPU Dialect to SPIR-V. The pass only converts the gpu.func ops
37/// inside gpu.module ops. i.e., the function that are referenced in
38/// gpu.launch_func ops. For each such function
39///
40/// 1) Create a spirv::ModuleOp, and clone the function into spirv::ModuleOp
41/// (the original function is still needed by the gpu::LaunchKernelOp, so cannot
42/// replace it).
43///
44/// 2) Lower the body of the spirv::ModuleOp.
45struct GPUToSPIRVPass final : impl::ConvertGPUToSPIRVBase<GPUToSPIRVPass> {
46 explicit GPUToSPIRVPass(bool mapMemorySpace)
47 : mapMemorySpace(mapMemorySpace) {}
48 void runOnOperation() override;
49
50private:
51 bool mapMemorySpace;
52};
53
54void GPUToSPIRVPass::runOnOperation() {
55 MLIRContext *context = &getContext();
56 ModuleOp module = getOperation();
57
58 SmallVector<Operation *, 1> gpuModules;
59 OpBuilder builder(context);
60
61 auto targetEnvSupportsKernelCapability = [](gpu::GPUModuleOp moduleOp) {
62 Operation *gpuModule = moduleOp.getOperation();
63 auto targetAttr = spirv::lookupTargetEnvOrDefault(op: gpuModule);
64 spirv::TargetEnv targetEnv(targetAttr);
65 return targetEnv.allows(spirv::Capability::Kernel);
66 };
67
68 module.walk(callback: [&](gpu::GPUModuleOp moduleOp) {
69 // Clone each GPU kernel module for conversion, given that the GPU
70 // launch op still needs the original GPU kernel module.
71 // For Vulkan Shader capabilities, we insert the newly converted SPIR-V
72 // module right after the original GPU module, as that's the expectation of
73 // the in-tree SPIR-V CPU runner (the Vulkan runner does not use this pass).
74 // For OpenCL Kernel capabilities, we insert the newly converted SPIR-V
75 // module inside the original GPU module, as that's the expectaion of the
76 // normal GPU compilation pipeline.
77 if (targetEnvSupportsKernelCapability(moduleOp)) {
78 builder.setInsertionPointToStart(moduleOp.getBody());
79 } else {
80 builder.setInsertionPoint(moduleOp.getOperation());
81 }
82 gpuModules.push_back(Elt: builder.clone(op&: *moduleOp.getOperation()));
83 });
84
85 // Run conversion for each module independently as they can have different
86 // TargetEnv attributes.
87 for (Operation *gpuModule : gpuModules) {
88 spirv::TargetEnvAttr targetAttr =
89 spirv::lookupTargetEnvOrDefault(op: gpuModule);
90
91 // Map MemRef memory space to SPIR-V storage class first if requested.
92 if (mapMemorySpace) {
93 spirv::MemorySpaceToStorageClassMap memorySpaceMap =
94 targetEnvSupportsKernelCapability(
95 dyn_cast<gpu::GPUModuleOp>(Val: gpuModule))
96 ? spirv::mapMemorySpaceToOpenCLStorageClass
97 : spirv::mapMemorySpaceToVulkanStorageClass;
98 spirv::MemorySpaceToStorageClassConverter converter(memorySpaceMap);
99 spirv::convertMemRefTypesAndAttrs(op: gpuModule, typeConverter&: converter);
100
101 // Check if there are any illegal ops remaining.
102 std::unique_ptr<ConversionTarget> target =
103 spirv::getMemorySpaceToStorageClassTarget(*context);
104 gpuModule->walk(callback: [&target, this](Operation *childOp) {
105 if (target->isIllegal(op: childOp)) {
106 childOp->emitOpError(message: "failed to legalize memory space");
107 signalPassFailure();
108 return WalkResult::interrupt();
109 }
110 return WalkResult::advance();
111 });
112 }
113
114 std::unique_ptr<ConversionTarget> target =
115 SPIRVConversionTarget::get(targetAttr);
116
117 SPIRVConversionOptions options;
118 options.use64bitIndex = this->use64bitIndex;
119 SPIRVTypeConverter typeConverter(targetAttr, options);
120 populateMMAToSPIRVCoopMatrixTypeConversion(typeConverter);
121
122 RewritePatternSet patterns(context);
123 populateGPUToSPIRVPatterns(typeConverter, patterns);
124 populateGpuWMMAToSPIRVCoopMatrixKHRConversionPatterns(typeConverter,
125 patterns);
126
127 // TODO: Change SPIR-V conversion to be progressive and remove the following
128 // patterns.
129 ScfToSPIRVContext scfContext;
130 populateSCFToSPIRVPatterns(typeConverter, scfToSPIRVContext&: scfContext, patterns);
131 mlir::arith::populateArithToSPIRVPatterns(typeConverter, patterns);
132 populateMemRefToSPIRVPatterns(typeConverter, patterns);
133 populateFuncToSPIRVPatterns(typeConverter, patterns);
134 populateVectorToSPIRVPatterns(typeConverter, patterns);
135
136 if (failed(Result: applyFullConversion(op: gpuModule, target: *target, patterns: std::move(patterns))))
137 return signalPassFailure();
138 }
139
140 // For OpenCL, the gpu.func op in the original gpu.module op needs to be
141 // replaced with an empty func.func op with the same arguments as the gpu.func
142 // op. The func.func op needs gpu.kernel attribute set.
143 module.walk(callback: [&](gpu::GPUModuleOp moduleOp) {
144 if (targetEnvSupportsKernelCapability(moduleOp)) {
145 moduleOp.walk(callback: [&](gpu::GPUFuncOp funcOp) {
146 builder.setInsertionPoint(funcOp);
147 auto newFuncOp = builder.create<func::FuncOp>(
148 location: funcOp.getLoc(), args: funcOp.getName(), args: funcOp.getFunctionType());
149 auto entryBlock = newFuncOp.addEntryBlock();
150 builder.setInsertionPointToEnd(entryBlock);
151 builder.create<func::ReturnOp>(location: funcOp.getLoc());
152 newFuncOp->setAttr(name: gpu::GPUDialect::getKernelFuncAttrName(),
153 value: builder.getUnitAttr());
154 funcOp.erase();
155 });
156 }
157 });
158}
159
160} // namespace
161
162std::unique_ptr<OperationPass<ModuleOp>>
163mlir::createConvertGPUToSPIRVPass(bool mapMemorySpace) {
164 return std::make_unique<GPUToSPIRVPass>(args&: mapMemorySpace);
165}
166

source code of mlir/lib/Conversion/GPUToSPIRV/GPUToSPIRVPass.cpp