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