1930c74f1SLei Zhang //===- GPUToSPIRVPass.cpp - GPU to SPIR-V Passes --------------------------===//
2930c74f1SLei Zhang //
3930c74f1SLei Zhang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4930c74f1SLei Zhang // See https://llvm.org/LICENSE.txt for license information.
5930c74f1SLei Zhang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6930c74f1SLei Zhang //
7930c74f1SLei Zhang //===----------------------------------------------------------------------===//
8930c74f1SLei Zhang //
9930c74f1SLei Zhang // This file implements a pass to convert a kernel function in the GPU Dialect
10930c74f1SLei Zhang // into a spv.module operation.
11930c74f1SLei Zhang //
12930c74f1SLei Zhang //===----------------------------------------------------------------------===//
13930c74f1SLei Zhang 
14930c74f1SLei Zhang #include "mlir/Conversion/GPUToSPIRV/GPUToSPIRVPass.h"
15930c74f1SLei Zhang 
16930c74f1SLei Zhang #include "../PassDetail.h"
17a54f4eaeSMogball #include "mlir/Conversion/ArithmeticToSPIRV/ArithmeticToSPIRV.h"
183ba66435SRiver Riddle #include "mlir/Conversion/FuncToSPIRV/FuncToSPIRV.h"
19930c74f1SLei Zhang #include "mlir/Conversion/GPUToSPIRV/GPUToSPIRV.h"
2026be7fe2SLei Zhang #include "mlir/Conversion/MemRefToSPIRV/MemRefToSPIRV.h"
21*d7ef488bSMogball #include "mlir/Dialect/GPU/IR/GPUDialect.h"
22930c74f1SLei Zhang #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
23930c74f1SLei Zhang #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
24930c74f1SLei Zhang #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
25930c74f1SLei Zhang 
26930c74f1SLei Zhang using namespace mlir;
27930c74f1SLei Zhang 
28930c74f1SLei Zhang namespace {
29930c74f1SLei Zhang /// Pass to lower GPU Dialect to SPIR-V. The pass only converts the gpu.func ops
30930c74f1SLei Zhang /// inside gpu.module ops. i.e., the function that are referenced in
31930c74f1SLei Zhang /// gpu.launch_func ops. For each such function
32930c74f1SLei Zhang ///
33930c74f1SLei Zhang /// 1) Create a spirv::ModuleOp, and clone the function into spirv::ModuleOp
34930c74f1SLei Zhang /// (the original function is still needed by the gpu::LaunchKernelOp, so cannot
35930c74f1SLei Zhang /// replace it).
36930c74f1SLei Zhang ///
37930c74f1SLei Zhang /// 2) Lower the body of the spirv::ModuleOp.
38930c74f1SLei Zhang struct GPUToSPIRVPass : public ConvertGPUToSPIRVBase<GPUToSPIRVPass> {
39930c74f1SLei Zhang   void runOnOperation() override;
40930c74f1SLei Zhang };
41930c74f1SLei Zhang } // namespace
42930c74f1SLei Zhang 
runOnOperation()43930c74f1SLei Zhang void GPUToSPIRVPass::runOnOperation() {
44930c74f1SLei Zhang   MLIRContext *context = &getContext();
45930c74f1SLei Zhang   ModuleOp module = getOperation();
46930c74f1SLei Zhang 
47930c74f1SLei Zhang   SmallVector<Operation *, 1> kernelModules;
48930c74f1SLei Zhang   OpBuilder builder(context);
49930c74f1SLei Zhang   module.walk([&builder, &kernelModules](gpu::GPUModuleOp moduleOp) {
50930c74f1SLei Zhang     // For each kernel module (should be only 1 for now, but that is not a
51930c74f1SLei Zhang     // requirement here), clone the module for conversion because the
52930c74f1SLei Zhang     // gpu.launch function still needs the kernel module.
53930c74f1SLei Zhang     builder.setInsertionPoint(moduleOp.getOperation());
54930c74f1SLei Zhang     kernelModules.push_back(builder.clone(*moduleOp.getOperation()));
55930c74f1SLei Zhang   });
56930c74f1SLei Zhang 
57930c74f1SLei Zhang   auto targetAttr = spirv::lookupTargetEnvOrDefault(module);
58930c74f1SLei Zhang   std::unique_ptr<ConversionTarget> target =
596dd07fa5SLei Zhang       SPIRVConversionTarget::get(targetAttr);
60930c74f1SLei Zhang 
61930c74f1SLei Zhang   SPIRVTypeConverter typeConverter(targetAttr);
62dc4e913bSChris Lattner   RewritePatternSet patterns(context);
633a506b31SChris Lattner   populateGPUToSPIRVPatterns(typeConverter, patterns);
6426be7fe2SLei Zhang 
6526be7fe2SLei Zhang   // TODO: Change SPIR-V conversion to be progressive and remove the following
6626be7fe2SLei Zhang   // patterns.
67a54f4eaeSMogball   mlir::arith::populateArithmeticToSPIRVPatterns(typeConverter, patterns);
6826be7fe2SLei Zhang   populateMemRefToSPIRVPatterns(typeConverter, patterns);
693ba66435SRiver Riddle   populateFuncToSPIRVPatterns(typeConverter, patterns);
70930c74f1SLei Zhang 
71930c74f1SLei Zhang   if (failed(applyFullConversion(kernelModules, *target, std::move(patterns))))
72930c74f1SLei Zhang     return signalPassFailure();
73930c74f1SLei Zhang }
74930c74f1SLei Zhang 
createConvertGPUToSPIRVPass()75930c74f1SLei Zhang std::unique_ptr<OperationPass<ModuleOp>> mlir::createConvertGPUToSPIRVPass() {
76930c74f1SLei Zhang   return std::make_unique<GPUToSPIRVPass>();
77930c74f1SLei Zhang }
78