1 //===- ConvertLaunchFuncToLLVMCalls.cpp - MLIR GPU launch to LLVM 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 passes to convert `gpu.launch_func` op into a sequence 10 // of LLVM calls that emulate the host and device sides. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "../PassDetail.h" 15 #include "mlir/Conversion/SPIRVToLLVM/SPIRVToLLVM.h" 16 #include "mlir/Conversion/SPIRVToLLVM/SPIRVToLLVMPass.h" 17 #include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h" 18 #include "mlir/Dialect/GPU/GPUDialect.h" 19 #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 20 #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h" 21 #include "mlir/Dialect/StandardOps/IR/Ops.h" 22 #include "mlir/IR/BuiltinOps.h" 23 #include "mlir/IR/SymbolTable.h" 24 #include "mlir/Transforms/DialectConversion.h" 25 26 #include "llvm/ADT/DenseMap.h" 27 #include "llvm/Support/FormatVariadic.h" 28 29 using namespace mlir; 30 31 static constexpr const char kSPIRVModule[] = "__spv__"; 32 33 //===----------------------------------------------------------------------===// 34 // Utility functions 35 //===----------------------------------------------------------------------===// 36 37 /// Returns the string name of the `DescriptorSet` decoration. 38 static std::string descriptorSetName() { 39 return llvm::convertToSnakeFromCamelCase( 40 stringifyDecoration(spirv::Decoration::DescriptorSet)); 41 } 42 43 /// Returns the string name of the `Binding` decoration. 44 static std::string bindingName() { 45 return llvm::convertToSnakeFromCamelCase( 46 stringifyDecoration(spirv::Decoration::Binding)); 47 } 48 49 /// Calculates the index of the kernel's operand that is represented by the 50 /// given global variable with the `bind` attribute. We assume that the index of 51 /// each kernel's operand is mapped to (descriptorSet, binding) by the map: 52 /// i -> (0, i) 53 /// which is implemented under `LowerABIAttributesPass`. 54 static unsigned calculateGlobalIndex(spirv::GlobalVariableOp op) { 55 IntegerAttr binding = op->getAttrOfType<IntegerAttr>(bindingName()); 56 return binding.getInt(); 57 } 58 59 /// Copies the given number of bytes from src to dst pointers. 60 static void copy(Location loc, Value dst, Value src, Value size, 61 OpBuilder &builder) { 62 MLIRContext *context = builder.getContext(); 63 auto llvmI1Type = LLVM::LLVMIntegerType::get(context, 1); 64 Value isVolatile = builder.create<LLVM::ConstantOp>( 65 loc, llvmI1Type, builder.getBoolAttr(false)); 66 builder.create<LLVM::MemcpyOp>(loc, dst, src, size, isVolatile); 67 } 68 69 /// Encodes the binding and descriptor set numbers into a new symbolic name. 70 /// The name is specified by 71 /// {kernel_module_name}_{variable_name}_descriptor_set{ds}_binding{b} 72 /// to avoid symbolic conflicts, where 'ds' and 'b' are descriptor set and 73 /// binding numbers. 74 static std::string 75 createGlobalVariableWithBindName(spirv::GlobalVariableOp op, 76 StringRef kernelModuleName) { 77 IntegerAttr descriptorSet = 78 op->getAttrOfType<IntegerAttr>(descriptorSetName()); 79 IntegerAttr binding = op->getAttrOfType<IntegerAttr>(bindingName()); 80 return llvm::formatv("{0}_{1}_descriptor_set{2}_binding{3}", 81 kernelModuleName.str(), op.sym_name().str(), 82 std::to_string(descriptorSet.getInt()), 83 std::to_string(binding.getInt())); 84 } 85 86 /// Returns true if the given global variable has both a descriptor set number 87 /// and a binding number. 88 static bool hasDescriptorSetAndBinding(spirv::GlobalVariableOp op) { 89 IntegerAttr descriptorSet = 90 op->getAttrOfType<IntegerAttr>(descriptorSetName()); 91 IntegerAttr binding = op->getAttrOfType<IntegerAttr>(bindingName()); 92 return descriptorSet && binding; 93 } 94 95 /// Fills `globalVariableMap` with SPIR-V global variables that represent kernel 96 /// arguments from the given SPIR-V module. We assume that the module contains a 97 /// single entry point function. Hence, all `spv.globalVariable`s with a bind 98 /// attribute are kernel arguments. 99 static LogicalResult getKernelGlobalVariables( 100 spirv::ModuleOp module, 101 DenseMap<uint32_t, spirv::GlobalVariableOp> &globalVariableMap) { 102 auto entryPoints = module.getOps<spirv::EntryPointOp>(); 103 if (!llvm::hasSingleElement(entryPoints)) { 104 return module.emitError( 105 "The module must contain exactly one entry point function"); 106 } 107 auto globalVariables = module.getOps<spirv::GlobalVariableOp>(); 108 for (auto globalOp : globalVariables) { 109 if (hasDescriptorSetAndBinding(globalOp)) 110 globalVariableMap[calculateGlobalIndex(globalOp)] = globalOp; 111 } 112 return success(); 113 } 114 115 /// Encodes the SPIR-V module's symbolic name into the name of the entry point 116 /// function. 117 static LogicalResult encodeKernelName(spirv::ModuleOp module) { 118 StringRef spvModuleName = module.sym_name().getValue(); 119 // We already know that the module contains exactly one entry point function 120 // based on `getKernelGlobalVariables()` call. Update this function's name 121 // to: 122 // {spv_module_name}_{function_name} 123 auto entryPoint = *module.getOps<spirv::EntryPointOp>().begin(); 124 StringRef funcName = entryPoint.fn(); 125 auto funcOp = module.lookupSymbol<spirv::FuncOp>(funcName); 126 std::string newFuncName = spvModuleName.str() + "_" + funcName.str(); 127 if (failed(SymbolTable::replaceAllSymbolUses(funcOp, newFuncName, module))) 128 return failure(); 129 SymbolTable::setSymbolName(funcOp, newFuncName); 130 return success(); 131 } 132 133 //===----------------------------------------------------------------------===// 134 // Conversion patterns 135 //===----------------------------------------------------------------------===// 136 137 namespace { 138 139 /// Structure to group information about the variables being copied. 140 struct CopyInfo { 141 Value dst; 142 Value src; 143 Value size; 144 }; 145 146 /// This pattern emulates a call to the kernel in LLVM dialect. For that, we 147 /// copy the data to the global variable (emulating device side), call the 148 /// kernel as a normal void LLVM function, and copy the data back (emulating the 149 /// host side). 150 class GPULaunchLowering : public ConvertOpToLLVMPattern<gpu::LaunchFuncOp> { 151 using ConvertOpToLLVMPattern<gpu::LaunchFuncOp>::ConvertOpToLLVMPattern; 152 153 LogicalResult 154 matchAndRewrite(gpu::LaunchFuncOp launchOp, ArrayRef<Value> operands, 155 ConversionPatternRewriter &rewriter) const override { 156 auto *op = launchOp.getOperation(); 157 MLIRContext *context = rewriter.getContext(); 158 auto module = launchOp->getParentOfType<ModuleOp>(); 159 160 // Get the SPIR-V module that represents the gpu kernel module. The module 161 // is named: 162 // __spv__{kernel_module_name} 163 // based on GPU to SPIR-V conversion. 164 StringRef kernelModuleName = launchOp.getKernelModuleName(); 165 std::string spvModuleName = kSPIRVModule + kernelModuleName.str(); 166 auto spvModule = module.lookupSymbol<spirv::ModuleOp>(spvModuleName); 167 if (!spvModule) { 168 return launchOp.emitOpError("SPIR-V kernel module '") 169 << spvModuleName << "' is not found"; 170 } 171 172 // Declare kernel function in the main module so that it later can be linked 173 // with its definition from the kernel module. We know that the kernel 174 // function would have no arguments and the data is passed via global 175 // variables. The name of the kernel will be 176 // {spv_module_name}_{kernel_function_name} 177 // to avoid symbolic name conflicts. 178 StringRef kernelFuncName = launchOp.getKernelName(); 179 std::string newKernelFuncName = spvModuleName + "_" + kernelFuncName.str(); 180 auto kernelFunc = module.lookupSymbol<LLVM::LLVMFuncOp>(newKernelFuncName); 181 if (!kernelFunc) { 182 OpBuilder::InsertionGuard guard(rewriter); 183 rewriter.setInsertionPointToStart(module.getBody()); 184 kernelFunc = rewriter.create<LLVM::LLVMFuncOp>( 185 rewriter.getUnknownLoc(), newKernelFuncName, 186 LLVM::LLVMFunctionType::get(LLVM::LLVMVoidType::get(context), 187 ArrayRef<LLVM::LLVMType>())); 188 rewriter.setInsertionPoint(launchOp); 189 } 190 191 // Get all global variables associated with the kernel operands. 192 DenseMap<uint32_t, spirv::GlobalVariableOp> globalVariableMap; 193 if (failed(getKernelGlobalVariables(spvModule, globalVariableMap))) 194 return failure(); 195 196 // Traverse kernel operands that were converted to MemRefDescriptors. For 197 // each operand, create a global variable and copy data from operand to it. 198 Location loc = launchOp.getLoc(); 199 SmallVector<CopyInfo, 4> copyInfo; 200 auto numKernelOperands = launchOp.getNumKernelOperands(); 201 auto kernelOperands = operands.take_back(numKernelOperands); 202 for (auto operand : llvm::enumerate(kernelOperands)) { 203 // Check if the kernel's opernad is a ranked memref. 204 auto memRefType = launchOp.getKernelOperand(operand.index()) 205 .getType() 206 .dyn_cast<MemRefType>(); 207 if (!memRefType) 208 return failure(); 209 210 // Calculate the size of the memref and get the pointer to the allocated 211 // buffer. 212 SmallVector<Value, 4> sizes; 213 SmallVector<Value, 4> strides; 214 Value sizeBytes; 215 getMemRefDescriptorSizes(loc, memRefType, operand.value(), rewriter, 216 sizes, strides, sizeBytes); 217 MemRefDescriptor descriptor(operand.value()); 218 Value src = descriptor.allocatedPtr(rewriter, loc); 219 220 // Get the global variable in the SPIR-V module that is associated with 221 // the kernel operand. Construct its new name and create a corresponding 222 // LLVM dialect global variable. 223 spirv::GlobalVariableOp spirvGlobal = globalVariableMap[operand.index()]; 224 auto pointeeType = 225 spirvGlobal.type().cast<spirv::PointerType>().getPointeeType(); 226 auto dstGlobalType = typeConverter->convertType(pointeeType); 227 if (!dstGlobalType) 228 return failure(); 229 std::string name = 230 createGlobalVariableWithBindName(spirvGlobal, spvModuleName); 231 // Check if this variable has already been created. 232 auto dstGlobal = module.lookupSymbol<LLVM::GlobalOp>(name); 233 if (!dstGlobal) { 234 OpBuilder::InsertionGuard guard(rewriter); 235 rewriter.setInsertionPointToStart(module.getBody()); 236 dstGlobal = rewriter.create<LLVM::GlobalOp>( 237 loc, dstGlobalType.cast<LLVM::LLVMType>(), 238 /*isConstant=*/false, LLVM::Linkage::Linkonce, name, Attribute()); 239 rewriter.setInsertionPoint(launchOp); 240 } 241 242 // Copy the data from src operand pointer to dst global variable. Save 243 // src, dst and size so that we can copy data back after emulating the 244 // kernel call. 245 Value dst = rewriter.create<LLVM::AddressOfOp>(loc, dstGlobal); 246 copy(loc, dst, src, sizeBytes, rewriter); 247 248 CopyInfo info; 249 info.dst = dst; 250 info.src = src; 251 info.size = sizeBytes; 252 copyInfo.push_back(info); 253 } 254 // Create a call to the kernel and copy the data back. 255 rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, kernelFunc, 256 ArrayRef<Value>()); 257 for (CopyInfo info : copyInfo) 258 copy(loc, info.src, info.dst, info.size, rewriter); 259 return success(); 260 } 261 }; 262 263 class LowerHostCodeToLLVM 264 : public LowerHostCodeToLLVMBase<LowerHostCodeToLLVM> { 265 public: 266 void runOnOperation() override { 267 ModuleOp module = getOperation(); 268 269 // Erase the GPU module. 270 for (auto gpuModule : 271 llvm::make_early_inc_range(module.getOps<gpu::GPUModuleOp>())) 272 gpuModule.erase(); 273 274 // Specify options to lower Standard to LLVM and pull in the conversion 275 // patterns. 276 LowerToLLVMOptions options = { 277 /*useBarePtrCallConv=*/false, 278 /*emitCWrappers=*/true, 279 /*indexBitwidth=*/kDeriveIndexBitwidthFromDataLayout}; 280 auto *context = module.getContext(); 281 OwningRewritePatternList patterns; 282 LLVMTypeConverter typeConverter(context, options); 283 populateStdToLLVMConversionPatterns(typeConverter, patterns); 284 patterns.insert<GPULaunchLowering>(typeConverter); 285 286 // Pull in SPIR-V type conversion patterns to convert SPIR-V global 287 // variable's type to LLVM dialect type. 288 populateSPIRVToLLVMTypeConversion(typeConverter); 289 290 ConversionTarget target(*context); 291 target.addLegalDialect<LLVM::LLVMDialect>(); 292 if (failed(applyPartialConversion(module, target, std::move(patterns)))) 293 signalPassFailure(); 294 295 // Finally, modify the kernel function in SPIR-V modules to avoid symbolic 296 // conflicts. 297 for (auto spvModule : module.getOps<spirv::ModuleOp>()) 298 encodeKernelName(spvModule); 299 } 300 }; 301 } // namespace 302 303 std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>> 304 mlir::createLowerHostCodeToLLVMPass() { 305 return std::make_unique<LowerHostCodeToLLVM>(); 306 } 307