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 // TODO(denis0x0D): Handle other types. 58 if (auto memRefType = type.dyn_cast_or_null<MemRefType>()) 59 return memRefType.hasRank() && 60 (memRefType.getRank() >= 1 && memRefType.getRank() <= 3); 61 return false; 62 } 63 64 /// Declares the vulkan launch function. Returns an error if the any type of 65 /// operand is unsupported by Vulkan runtime. 66 LogicalResult declareVulkanLaunchFunc(Location loc, 67 gpu::LaunchFuncOp launchOp); 68 69 private: 70 /// The number of vulkan launch configuration operands, placed at the leading 71 /// positions of the operand list. 72 static constexpr unsigned kVulkanLaunchNumConfigOperands = 3; 73 }; 74 75 } // anonymous namespace 76 77 void ConvertGpuLaunchFuncToVulkanLaunchFunc::runOnOperation() { 78 bool done = false; 79 getOperation().walk([this, &done](gpu::LaunchFuncOp op) { 80 if (done) { 81 op.emitError("should only contain one 'gpu::LaunchFuncOp' op"); 82 return signalPassFailure(); 83 } 84 done = true; 85 convertGpuLaunchFunc(op); 86 }); 87 88 // Erase `gpu::GPUModuleOp` and `spirv::Module` operations. 89 for (auto gpuModule : 90 llvm::make_early_inc_range(getOperation().getOps<gpu::GPUModuleOp>())) 91 gpuModule.erase(); 92 93 for (auto spirvModule : 94 llvm::make_early_inc_range(getOperation().getOps<spirv::ModuleOp>())) 95 spirvModule.erase(); 96 } 97 98 LogicalResult ConvertGpuLaunchFuncToVulkanLaunchFunc::declareVulkanLaunchFunc( 99 Location loc, gpu::LaunchFuncOp launchOp) { 100 OpBuilder builder(getOperation().getBody()->getTerminator()); 101 102 // Workgroup size is written into the kernel. So to properly modelling 103 // vulkan launch, we have to skip local workgroup size configuration here. 104 SmallVector<Type, 8> gpuLaunchTypes(launchOp.getOperandTypes()); 105 // The first kVulkanLaunchNumConfigOperands of the gpu.launch_func op are the 106 // same as the config operands for the vulkan launch call op. 107 SmallVector<Type, 8> vulkanLaunchTypes(gpuLaunchTypes.begin(), 108 gpuLaunchTypes.begin() + 109 kVulkanLaunchNumConfigOperands); 110 vulkanLaunchTypes.append(gpuLaunchTypes.begin() + 111 gpu::LaunchOp::kNumConfigOperands, 112 gpuLaunchTypes.end()); 113 114 // Check that all operands have supported types except those for the 115 // launch configuration. 116 for (auto type : 117 llvm::drop_begin(vulkanLaunchTypes, kVulkanLaunchNumConfigOperands)) { 118 if (!isSupportedType(type)) 119 return launchOp.emitError() << type << " is unsupported to run on Vulkan"; 120 } 121 122 // Declare vulkan launch function. 123 builder.create<FuncOp>( 124 loc, kVulkanLaunch, 125 FunctionType::get(vulkanLaunchTypes, ArrayRef<Type>{}, loc->getContext()), 126 ArrayRef<NamedAttribute>{}); 127 128 return success(); 129 } 130 131 LogicalResult ConvertGpuLaunchFuncToVulkanLaunchFunc::createBinaryShader( 132 ModuleOp module, std::vector<char> &binaryShader) { 133 bool done = false; 134 SmallVector<uint32_t, 0> binary; 135 for (auto spirvModule : module.getOps<spirv::ModuleOp>()) { 136 if (done) 137 return spirvModule.emitError("should only contain one 'spv.module' op"); 138 done = true; 139 140 if (failed(spirv::serialize(spirvModule, binary))) 141 return failure(); 142 } 143 binaryShader.resize(binary.size() * sizeof(uint32_t)); 144 std::memcpy(binaryShader.data(), reinterpret_cast<char *>(binary.data()), 145 binaryShader.size()); 146 return success(); 147 } 148 149 void ConvertGpuLaunchFuncToVulkanLaunchFunc::convertGpuLaunchFunc( 150 gpu::LaunchFuncOp launchOp) { 151 ModuleOp module = getOperation(); 152 OpBuilder builder(launchOp); 153 Location loc = launchOp.getLoc(); 154 155 // Serialize `spirv::Module` into binary form. 156 std::vector<char> binary; 157 if (failed(createBinaryShader(module, binary))) 158 return signalPassFailure(); 159 160 // Declare vulkan launch function. 161 if (failed(declareVulkanLaunchFunc(loc, launchOp))) 162 return signalPassFailure(); 163 164 SmallVector<Value, 8> gpuLaunchOperands(launchOp.getOperands()); 165 SmallVector<Value, 8> vulkanLaunchOperands( 166 gpuLaunchOperands.begin(), 167 gpuLaunchOperands.begin() + kVulkanLaunchNumConfigOperands); 168 vulkanLaunchOperands.append(gpuLaunchOperands.begin() + 169 gpu::LaunchOp::kNumConfigOperands, 170 gpuLaunchOperands.end()); 171 172 // Create vulkan launch call op. 173 auto vulkanLaunchCallOp = builder.create<CallOp>( 174 loc, ArrayRef<Type>{}, builder.getSymbolRefAttr(kVulkanLaunch), 175 vulkanLaunchOperands); 176 177 // Set SPIR-V binary shader data as an attribute. 178 vulkanLaunchCallOp.setAttr( 179 kSPIRVBlobAttrName, 180 StringAttr::get({binary.data(), binary.size()}, loc->getContext())); 181 182 // Set entry point name as an attribute. 183 vulkanLaunchCallOp.setAttr( 184 kSPIRVEntryPointAttrName, 185 StringAttr::get(launchOp.kernel(), loc->getContext())); 186 187 launchOp.erase(); 188 } 189 190 std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>> 191 mlir::createConvertGpuLaunchFuncToVulkanLaunchFuncPass() { 192 return std::make_unique<ConvertGpuLaunchFuncToVulkanLaunchFunc>(); 193 } 194