1//===------------------ TestVulkanRunnerPipeline.cpp --------------------===//
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// Implements a pipeline for use by Vulkan runner tests.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Conversion/GPUCommon/GPUCommonPass.h"
14#include "mlir/Conversion/GPUToSPIRV/GPUToSPIRVPass.h"
15#include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
16#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
17#include "mlir/Dialect/Func/IR/FuncOps.h"
18#include "mlir/Dialect/GPU/IR/GPUDialect.h"
19#include "mlir/Dialect/GPU/Transforms/Passes.h"
20#include "mlir/Dialect/GPU/Transforms/SPIRV/Passes.h"
21#include "mlir/Dialect/LLVMIR/Transforms/RequestCWrappers.h"
22#include "mlir/Dialect/MemRef/Transforms/Passes.h"
23#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
24#include "mlir/Dialect/SPIRV/Transforms/Passes.h"
25#include "mlir/Pass/PassManager.h"
26#include "mlir/Pass/PassOptions.h"
27
28using namespace mlir;
29
30// Defined in the test directory, no public header.
31namespace mlir::test {
32std::unique_ptr<Pass> createTestConvertToSPIRVPass(bool convertGPUModules,
33 bool nestInGPUModule);
34} // namespace mlir::test
35
36namespace {
37
38struct VulkanRunnerPipelineOptions
39 : PassPipelineOptions<VulkanRunnerPipelineOptions> {
40 Option<bool> spirvWebGPUPrepare{
41 *this, "spirv-webgpu-prepare",
42 llvm::cl::desc("Run MLIR transforms used when targetting WebGPU")};
43};
44
45void buildTestVulkanRunnerPipeline(OpPassManager &passManager,
46 const VulkanRunnerPipelineOptions &options) {
47 passManager.addPass(pass: createGpuKernelOutliningPass());
48 passManager.addPass(pass: memref::createFoldMemRefAliasOpsPass());
49
50 GpuSPIRVAttachTargetOptions attachTargetOptions{};
51 attachTargetOptions.spirvVersion = "v1.0";
52 attachTargetOptions.spirvCapabilities.push_back(Elt: "Shader");
53 attachTargetOptions.spirvExtensions.push_back(
54 Elt: "SPV_KHR_storage_buffer_storage_class");
55 passManager.addPass(pass: createGpuSPIRVAttachTarget(options: attachTargetOptions));
56
57 passManager.addPass(pass: test::createTestConvertToSPIRVPass(
58 /*convertGPUModules=*/true, /*nestInGPUModule=*/true));
59
60 OpPassManager &spirvModulePM =
61 passManager.nest<gpu::GPUModuleOp>().nest<spirv::ModuleOp>();
62 spirvModulePM.addPass(pass: spirv::createSPIRVLowerABIAttributesPass());
63 spirvModulePM.addPass(pass: spirv::createSPIRVUpdateVCEPass());
64 if (options.spirvWebGPUPrepare)
65 spirvModulePM.addPass(pass: spirv::createSPIRVWebGPUPreparePass());
66
67 passManager.addPass(pass: createGpuModuleToBinaryPass());
68
69 passManager.addPass(pass: createFinalizeMemRefToLLVMConversionPass());
70 passManager.nest<func::FuncOp>().addPass(
71 pass: LLVM::createLLVMRequestCWrappersPass());
72 // VulkanRuntimeWrappers.cpp requires these calling convention options.
73 GpuToLLVMConversionPassOptions opt;
74 opt.hostBarePtrCallConv = false;
75 opt.kernelBarePtrCallConv = true;
76 opt.kernelIntersperseSizeCallConv = true;
77 passManager.addPass(pass: createGpuToLLVMConversionPass(options: opt));
78 passManager.addPass(pass: createReconcileUnrealizedCastsPass());
79}
80
81} // namespace
82
83namespace mlir::test {
84void registerTestVulkanRunnerPipeline() {
85 PassPipelineRegistration<VulkanRunnerPipelineOptions>(
86 "test-vulkan-runner-pipeline",
87 "Runs a series of passes intended for Vulkan runner tests. Lowers GPU "
88 "dialect to LLVM dialect for the host and to serialized Vulkan SPIR-V "
89 "for the device.",
90 buildTestVulkanRunnerPipeline);
91}
92} // namespace mlir::test
93