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 spv.module operation.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "mlir/Conversion/GPUToSPIRV/GPUToSPIRVPass.h"
15
16 #include "../PassDetail.h"
17 #include "mlir/Conversion/ArithmeticToSPIRV/ArithmeticToSPIRV.h"
18 #include "mlir/Conversion/FuncToSPIRV/FuncToSPIRV.h"
19 #include "mlir/Conversion/GPUToSPIRV/GPUToSPIRV.h"
20 #include "mlir/Conversion/MemRefToSPIRV/MemRefToSPIRV.h"
21 #include "mlir/Dialect/GPU/IR/GPUDialect.h"
22 #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
23 #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
24 #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
25
26 using namespace mlir;
27
28 namespace {
29 /// Pass to lower GPU Dialect to SPIR-V. The pass only converts the gpu.func ops
30 /// inside gpu.module ops. i.e., the function that are referenced in
31 /// gpu.launch_func ops. For each such function
32 ///
33 /// 1) Create a spirv::ModuleOp, and clone the function into spirv::ModuleOp
34 /// (the original function is still needed by the gpu::LaunchKernelOp, so cannot
35 /// replace it).
36 ///
37 /// 2) Lower the body of the spirv::ModuleOp.
38 struct GPUToSPIRVPass : public ConvertGPUToSPIRVBase<GPUToSPIRVPass> {
39 void runOnOperation() override;
40 };
41 } // namespace
42
runOnOperation()43 void GPUToSPIRVPass::runOnOperation() {
44 MLIRContext *context = &getContext();
45 ModuleOp module = getOperation();
46
47 SmallVector<Operation *, 1> kernelModules;
48 OpBuilder builder(context);
49 module.walk([&builder, &kernelModules](gpu::GPUModuleOp moduleOp) {
50 // For each kernel module (should be only 1 for now, but that is not a
51 // requirement here), clone the module for conversion because the
52 // gpu.launch function still needs the kernel module.
53 builder.setInsertionPoint(moduleOp.getOperation());
54 kernelModules.push_back(builder.clone(*moduleOp.getOperation()));
55 });
56
57 auto targetAttr = spirv::lookupTargetEnvOrDefault(module);
58 std::unique_ptr<ConversionTarget> target =
59 SPIRVConversionTarget::get(targetAttr);
60
61 SPIRVTypeConverter typeConverter(targetAttr);
62 RewritePatternSet patterns(context);
63 populateGPUToSPIRVPatterns(typeConverter, patterns);
64
65 // TODO: Change SPIR-V conversion to be progressive and remove the following
66 // patterns.
67 mlir::arith::populateArithmeticToSPIRVPatterns(typeConverter, patterns);
68 populateMemRefToSPIRVPatterns(typeConverter, patterns);
69 populateFuncToSPIRVPatterns(typeConverter, patterns);
70
71 if (failed(applyFullConversion(kernelModules, *target, std::move(patterns))))
72 return signalPassFailure();
73 }
74
createConvertGPUToSPIRVPass()75 std::unique_ptr<OperationPass<ModuleOp>> mlir::createConvertGPUToSPIRVPass() {
76 return std::make_unique<GPUToSPIRVPass>();
77 }
78