|
| 1 | +//===--- CIRGenCUDARuntime.cpp - Interface to CUDA Runtimes ----*- C++ -*--===// |
| 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 provides an abstract class for CUDA CIR generation. Concrete |
| 10 | +// subclasses of this implement code generation for specific OpenCL |
| 11 | +// runtime libraries. |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#include "CIRGenCUDARuntime.h" |
| 16 | +#include "CIRGenFunction.h" |
| 17 | +#include "clang/Basic/Cuda.h" |
| 18 | +#include "clang/CIR/Dialect/IR/CIRTypes.h" |
| 19 | + |
| 20 | +using namespace clang; |
| 21 | +using namespace clang::CIRGen; |
| 22 | + |
| 23 | +CIRGenCUDARuntime::~CIRGenCUDARuntime() {} |
| 24 | + |
| 25 | +void CIRGenCUDARuntime::emitDeviceStubBodyLegacy(CIRGenFunction &cgf, |
| 26 | + cir::FuncOp fn, |
| 27 | + FunctionArgList &args) { |
| 28 | + llvm_unreachable("NYI"); |
| 29 | +} |
| 30 | + |
| 31 | +void CIRGenCUDARuntime::emitDeviceStubBodyNew(CIRGenFunction &cgf, |
| 32 | + cir::FuncOp fn, |
| 33 | + FunctionArgList &args) { |
| 34 | + if (cgm.getLangOpts().HIP) |
| 35 | + llvm_unreachable("NYI"); |
| 36 | + |
| 37 | + // This requires arguments to be sent to kernels in a different way. |
| 38 | + if (cgm.getLangOpts().OffloadViaLLVM) |
| 39 | + llvm_unreachable("NYI"); |
| 40 | + |
| 41 | + auto &builder = cgm.getBuilder(); |
| 42 | + |
| 43 | + // For cudaLaunchKernel, we must add another layer of indirection |
| 44 | + // to arguments. For example, for function `add(int a, float b)`, |
| 45 | + // we need to pass it as `void *args[2] = { &a, &b }`. |
| 46 | + |
| 47 | + auto loc = fn.getLoc(); |
| 48 | + auto voidPtrArrayTy = |
| 49 | + cir::ArrayType::get(&cgm.getMLIRContext(), cgm.VoidPtrTy, args.size()); |
| 50 | + mlir::Value kernelArgs = builder.createAlloca( |
| 51 | + loc, cir::PointerType::get(voidPtrArrayTy), voidPtrArrayTy, "kernel_args", |
| 52 | + CharUnits::fromQuantity(16)); |
| 53 | + |
| 54 | + // Store arguments into kernelArgs |
| 55 | + for (auto [i, arg] : llvm::enumerate(args)) { |
| 56 | + mlir::Value index = |
| 57 | + builder.getConstInt(loc, llvm::APInt(/*numBits=*/32, i)); |
| 58 | + mlir::Value storePos = builder.createPtrStride(loc, kernelArgs, index); |
| 59 | + builder.CIRBaseBuilderTy::createStore( |
| 60 | + loc, cgf.GetAddrOfLocalVar(arg).getPointer(), storePos); |
| 61 | + } |
| 62 | + |
| 63 | + // We retrieve dim3 type by looking into the second argument of |
| 64 | + // cudaLaunchKernel, as is done in OG. |
| 65 | + TranslationUnitDecl *tuDecl = cgm.getASTContext().getTranslationUnitDecl(); |
| 66 | + DeclContext *dc = TranslationUnitDecl::castToDeclContext(tuDecl); |
| 67 | + |
| 68 | + // The default stream is usually stream 0 (the legacy default stream). |
| 69 | + // For per-thread default stream, we need a different LaunchKernel function. |
| 70 | + if (cgm.getLangOpts().GPUDefaultStream == |
| 71 | + LangOptions::GPUDefaultStreamKind::PerThread) |
| 72 | + llvm_unreachable("NYI"); |
| 73 | + |
| 74 | + std::string launchAPI = "cudaLaunchKernel"; |
| 75 | + const IdentifierInfo &launchII = cgm.getASTContext().Idents.get(launchAPI); |
| 76 | + FunctionDecl *launchFD = nullptr; |
| 77 | + for (auto *result : dc->lookup(&launchII)) { |
| 78 | + if (FunctionDecl *fd = dyn_cast<FunctionDecl>(result)) |
| 79 | + launchFD = fd; |
| 80 | + } |
| 81 | + |
| 82 | + if (launchFD == nullptr) { |
| 83 | + cgm.Error(cgf.CurFuncDecl->getLocation(), |
| 84 | + "Can't find declaration for " + launchAPI); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + // Use this function to retrieve arguments for cudaLaunchKernel: |
| 89 | + // int __cudaPopCallConfiguration(dim3 *gridDim, dim3 *blockDim, size_t |
| 90 | + // *sharedMem, cudaStream_t *stream) |
| 91 | + // |
| 92 | + // Here cudaStream_t, while also being the 6th argument of cudaLaunchKernel, |
| 93 | + // is a pointer to some opaque struct. |
| 94 | + |
| 95 | + mlir::Type dim3Ty = |
| 96 | + cgf.getTypes().convertType(launchFD->getParamDecl(1)->getType()); |
| 97 | + mlir::Type streamTy = |
| 98 | + cgf.getTypes().convertType(launchFD->getParamDecl(5)->getType()); |
| 99 | + |
| 100 | + mlir::Value gridDim = |
| 101 | + builder.createAlloca(loc, cir::PointerType::get(dim3Ty), dim3Ty, |
| 102 | + "grid_dim", CharUnits::fromQuantity(8)); |
| 103 | + mlir::Value blockDim = |
| 104 | + builder.createAlloca(loc, cir::PointerType::get(dim3Ty), dim3Ty, |
| 105 | + "block_dim", CharUnits::fromQuantity(8)); |
| 106 | + mlir::Value sharedMem = |
| 107 | + builder.createAlloca(loc, cir::PointerType::get(cgm.SizeTy), cgm.SizeTy, |
| 108 | + "shared_mem", cgm.getSizeAlign()); |
| 109 | + mlir::Value stream = |
| 110 | + builder.createAlloca(loc, cir::PointerType::get(streamTy), streamTy, |
| 111 | + "stream", cgm.getPointerAlign()); |
| 112 | + |
| 113 | + cir::FuncOp popConfig = cgm.createRuntimeFunction( |
| 114 | + cir::FuncType::get({gridDim.getType(), blockDim.getType(), |
| 115 | + sharedMem.getType(), stream.getType()}, |
| 116 | + cgm.SInt32Ty), |
| 117 | + "__cudaPopCallConfiguration"); |
| 118 | + cgf.emitRuntimeCall(loc, popConfig, {gridDim, blockDim, sharedMem, stream}); |
| 119 | + |
| 120 | + // Now emit the call to cudaLaunchKernel |
| 121 | + // cudaError_t cudaLaunchKernel(const void *func, dim3 gridDim, dim3 blockDim, |
| 122 | + // void **args, size_t sharedMem, |
| 123 | + // cudaStream_t stream); |
| 124 | + auto kernelTy = |
| 125 | + cir::PointerType::get(&cgm.getMLIRContext(), fn.getFunctionType()); |
| 126 | + |
| 127 | + mlir::Value kernel = |
| 128 | + builder.create<cir::GetGlobalOp>(loc, kernelTy, fn.getSymName()); |
| 129 | + mlir::Value func = builder.createBitcast(kernel, cgm.VoidPtrTy); |
| 130 | + CallArgList launchArgs; |
| 131 | + |
| 132 | + mlir::Value kernelArgsDecayed = |
| 133 | + builder.createCast(cir::CastKind::array_to_ptrdecay, kernelArgs, |
| 134 | + cir::PointerType::get(cgm.VoidPtrTy)); |
| 135 | + |
| 136 | + launchArgs.add(RValue::get(func), launchFD->getParamDecl(0)->getType()); |
| 137 | + launchArgs.add( |
| 138 | + RValue::getAggregate(Address(gridDim, CharUnits::fromQuantity(8))), |
| 139 | + launchFD->getParamDecl(1)->getType()); |
| 140 | + launchArgs.add( |
| 141 | + RValue::getAggregate(Address(blockDim, CharUnits::fromQuantity(8))), |
| 142 | + launchFD->getParamDecl(2)->getType()); |
| 143 | + launchArgs.add(RValue::get(kernelArgsDecayed), |
| 144 | + launchFD->getParamDecl(3)->getType()); |
| 145 | + launchArgs.add( |
| 146 | + RValue::get(builder.CIRBaseBuilderTy::createLoad(loc, sharedMem)), |
| 147 | + launchFD->getParamDecl(4)->getType()); |
| 148 | + launchArgs.add(RValue::get(stream), launchFD->getParamDecl(5)->getType()); |
| 149 | + |
| 150 | + mlir::Type launchTy = cgm.getTypes().convertType(launchFD->getType()); |
| 151 | + mlir::Operation *launchFn = |
| 152 | + cgm.createRuntimeFunction(cast<cir::FuncType>(launchTy), launchAPI); |
| 153 | + const auto &callInfo = cgm.getTypes().arrangeFunctionDeclaration(launchFD); |
| 154 | + cgf.emitCall(callInfo, CIRGenCallee::forDirect(launchFn), ReturnValueSlot(), |
| 155 | + launchArgs); |
| 156 | +} |
| 157 | + |
| 158 | +void CIRGenCUDARuntime::emitDeviceStub(CIRGenFunction &cgf, cir::FuncOp fn, |
| 159 | + FunctionArgList &args) { |
| 160 | + // Device stub and its handle might be different. |
| 161 | + if (cgm.getLangOpts().HIP) |
| 162 | + llvm_unreachable("NYI"); |
| 163 | + |
| 164 | + // CUDA 9.0 changed the way to launch kernels. |
| 165 | + if (CudaFeatureEnabled(cgm.getTarget().getSDKVersion(), |
| 166 | + CudaFeature::CUDA_USES_NEW_LAUNCH) || |
| 167 | + cgm.getLangOpts().OffloadViaLLVM) |
| 168 | + emitDeviceStubBodyNew(cgf, fn, args); |
| 169 | + else |
| 170 | + emitDeviceStubBodyLegacy(cgf, fn, args); |
| 171 | +} |
0 commit comments