1 //===- ConvertGPULaunchFuncToVulkanLaunchFunc.cpp - MLIR conversion pass --===// 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 gpu launch function into a vulkan 10 // launch function. Creates a SPIR-V binary shader from the `spirv::ModuleOp` 11 // using `spirv::serialize` function, attaches binary data and entry point name 12 // as an attributes to vulkan launch call op. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "../PassDetail.h" 17 #include "mlir/Conversion/GPUToVulkan/ConvertGPUToVulkanPass.h" 18 #include "mlir/Dialect/GPU/GPUDialect.h" 19 #include "mlir/Dialect/SPIRV/SPIRVOps.h" 20 #include "mlir/Dialect/SPIRV/Serialization.h" 21 #include "mlir/Dialect/StandardOps/IR/Ops.h" 22 #include "mlir/IR/Attributes.h" 23 #include "mlir/IR/Builders.h" 24 #include "mlir/IR/Function.h" 25 #include "mlir/IR/Module.h" 26 #include "mlir/IR/StandardTypes.h" 27 28 using namespace mlir; 29 30 static constexpr const char *kSPIRVBlobAttrName = "spirv_blob"; 31 static constexpr const char *kSPIRVEntryPointAttrName = "spirv_entry_point"; 32 static constexpr const char *kVulkanLaunch = "vulkanLaunch"; 33 34 namespace { 35 36 /// A pass to convert gpu launch op to vulkan launch call op, by creating a 37 /// SPIR-V binary shader from `spirv::ModuleOp` using `spirv::serialize` 38 /// function and attaching binary data and entry point name as an attributes to 39 /// created vulkan launch call op. 40 class ConvertGpuLaunchFuncToVulkanLaunchFunc 41 : public ConvertGpuLaunchFuncToVulkanLaunchFuncBase< 42 ConvertGpuLaunchFuncToVulkanLaunchFunc> { 43 public: 44 void runOnOperation() override; 45 46 private: 47 /// Creates a SPIR-V binary shader from the given `module` using 48 /// `spirv::serialize` function. 49 LogicalResult createBinaryShader(ModuleOp module, 50 std::vector<char> &binaryShader); 51 52 /// Converts the given `launchOp` to vulkan launch call. 53 void convertGpuLaunchFunc(gpu::LaunchFuncOp launchOp); 54 55 /// Checks where the given type is supported by Vulkan runtime. 56 bool isSupportedType(Type type) { 57 if (auto memRefType = type.dyn_cast_or_null<MemRefType>()) { 58 auto elementType = memRefType.getElementType(); 59 return memRefType.hasRank() && 60 (memRefType.getRank() >= 1 && memRefType.getRank() <= 3) && 61 (elementType.isIntOrFloat()); 62 } 63 return false; 64 } 65 66 /// Declares the vulkan launch function. Returns an error if the any type of 67 /// operand is unsupported by Vulkan runtime. 68 LogicalResult declareVulkanLaunchFunc(Location loc, 69 gpu::LaunchFuncOp launchOp); 70 71 private: 72 /// The number of vulkan launch configuration operands, placed at the leading 73 /// positions of the operand list. 74 static constexpr unsigned kVulkanLaunchNumConfigOperands = 3; 75 }; 76 77 } // anonymous namespace 78 79 void ConvertGpuLaunchFuncToVulkanLaunchFunc::runOnOperation() { 80 bool done = false; 81 getOperation().walk([this, &done](gpu::LaunchFuncOp op) { 82 if (done) { 83 op.emitError("should only contain one 'gpu::LaunchFuncOp' op"); 84 return signalPassFailure(); 85 } 86 done = true; 87 convertGpuLaunchFunc(op); 88 }); 89 90 // Erase `gpu::GPUModuleOp` and `spirv::Module` operations. 91 for (auto gpuModule : 92 llvm::make_early_inc_range(getOperation().getOps<gpu::GPUModuleOp>())) 93 gpuModule.erase(); 94 95 for (auto spirvModule : 96 llvm::make_early_inc_range(getOperation().getOps<spirv::ModuleOp>())) 97 spirvModule.erase(); 98 } 99 100 LogicalResult ConvertGpuLaunchFuncToVulkanLaunchFunc::declareVulkanLaunchFunc( 101 Location loc, gpu::LaunchFuncOp launchOp) { 102 OpBuilder builder(getOperation().getBody()->getTerminator()); 103 104 // Workgroup size is written into the kernel. So to properly modelling 105 // vulkan launch, we have to skip local workgroup size configuration here. 106 SmallVector<Type, 8> gpuLaunchTypes(launchOp.getOperandTypes()); 107 // The first kVulkanLaunchNumConfigOperands of the gpu.launch_func op are the 108 // same as the config operands for the vulkan launch call op. 109 SmallVector<Type, 8> vulkanLaunchTypes(gpuLaunchTypes.begin(), 110 gpuLaunchTypes.begin() + 111 kVulkanLaunchNumConfigOperands); 112 vulkanLaunchTypes.append(gpuLaunchTypes.begin() + 113 gpu::LaunchOp::kNumConfigOperands, 114 gpuLaunchTypes.end()); 115 116 // Check that all operands have supported types except those for the 117 // launch configuration. 118 for (auto type : 119 llvm::drop_begin(vulkanLaunchTypes, kVulkanLaunchNumConfigOperands)) { 120 if (!isSupportedType(type)) 121 return launchOp.emitError() << type << " is unsupported to run on Vulkan"; 122 } 123 124 // Declare vulkan launch function. 125 builder.create<FuncOp>( 126 loc, kVulkanLaunch, 127 FunctionType::get(vulkanLaunchTypes, ArrayRef<Type>{}, loc->getContext()), 128 ArrayRef<NamedAttribute>{}); 129 130 return success(); 131 } 132 133 LogicalResult ConvertGpuLaunchFuncToVulkanLaunchFunc::createBinaryShader( 134 ModuleOp module, std::vector<char> &binaryShader) { 135 bool done = false; 136 SmallVector<uint32_t, 0> binary; 137 for (auto spirvModule : module.getOps<spirv::ModuleOp>()) { 138 if (done) 139 return spirvModule.emitError("should only contain one 'spv.module' op"); 140 done = true; 141 142 if (failed(spirv::serialize(spirvModule, binary))) 143 return failure(); 144 } 145 binaryShader.resize(binary.size() * sizeof(uint32_t)); 146 std::memcpy(binaryShader.data(), reinterpret_cast<char *>(binary.data()), 147 binaryShader.size()); 148 return success(); 149 } 150 151 void ConvertGpuLaunchFuncToVulkanLaunchFunc::convertGpuLaunchFunc( 152 gpu::LaunchFuncOp launchOp) { 153 ModuleOp module = getOperation(); 154 OpBuilder builder(launchOp); 155 Location loc = launchOp.getLoc(); 156 157 // Serialize `spirv::Module` into binary form. 158 std::vector<char> binary; 159 if (failed(createBinaryShader(module, binary))) 160 return signalPassFailure(); 161 162 // Declare vulkan launch function. 163 if (failed(declareVulkanLaunchFunc(loc, launchOp))) 164 return signalPassFailure(); 165 166 SmallVector<Value, 8> gpuLaunchOperands(launchOp.getOperands()); 167 SmallVector<Value, 8> vulkanLaunchOperands( 168 gpuLaunchOperands.begin(), 169 gpuLaunchOperands.begin() + kVulkanLaunchNumConfigOperands); 170 vulkanLaunchOperands.append(gpuLaunchOperands.begin() + 171 gpu::LaunchOp::kNumConfigOperands, 172 gpuLaunchOperands.end()); 173 174 // Create vulkan launch call op. 175 auto vulkanLaunchCallOp = builder.create<CallOp>( 176 loc, ArrayRef<Type>{}, builder.getSymbolRefAttr(kVulkanLaunch), 177 vulkanLaunchOperands); 178 179 // Set SPIR-V binary shader data as an attribute. 180 vulkanLaunchCallOp.setAttr( 181 kSPIRVBlobAttrName, 182 StringAttr::get({binary.data(), binary.size()}, loc->getContext())); 183 184 // Set entry point name as an attribute. 185 vulkanLaunchCallOp.setAttr( 186 kSPIRVEntryPointAttrName, 187 StringAttr::get(launchOp.getKernelName(), loc->getContext())); 188 189 launchOp.erase(); 190 } 191 192 std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>> 193 mlir::createConvertGpuLaunchFuncToVulkanLaunchFuncPass() { 194 return std::make_unique<ConvertGpuLaunchFuncToVulkanLaunchFunc>(); 195 } 196