Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 1 | //===----- CGCUDANV.cpp - Interface to NVIDIA CUDA Runtime ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This provides a class for CUDA code generation targeting the NVIDIA CUDA |
| 11 | // runtime library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "CGCUDARuntime.h" |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 16 | #include "CodeGenFunction.h" |
| 17 | #include "CodeGenModule.h" |
| 18 | #include "clang/AST/Decl.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 19 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | c80ceea | 2014-03-04 11:02:08 +0000 | [diff] [blame] | 20 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
| 22 | #include "llvm/IR/DerivedTypes.h" |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | class CGNVCUDARuntime : public CGCUDARuntime { |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 30 | |
| 31 | private: |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 32 | llvm::Type *IntTy, *SizeTy, *VoidTy; |
| 33 | llvm::PointerType *CharPtrTy, *VoidPtrTy, *VoidPtrPtrTy; |
| 34 | |
| 35 | /// Convenience reference to LLVM Context |
| 36 | llvm::LLVMContext &Context; |
| 37 | /// Convenience reference to the current module |
| 38 | llvm::Module &TheModule; |
| 39 | /// Keeps track of kernel launch stubs emitted in this module |
| 40 | llvm::SmallVector<llvm::Function *, 16> EmittedKernels; |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 41 | llvm::SmallVector<std::pair<llvm::GlobalVariable *, unsigned>, 16> DeviceVars; |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 42 | /// Keeps track of variables containing handles of GPU binaries. Populated by |
| 43 | /// ModuleCtorFunction() and used to create corresponding cleanup calls in |
| 44 | /// ModuleDtorFunction() |
| 45 | llvm::SmallVector<llvm::GlobalVariable *, 16> GpuBinaryHandles; |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 46 | |
| 47 | llvm::Constant *getSetupArgumentFn() const; |
| 48 | llvm::Constant *getLaunchFn() const; |
| 49 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 50 | /// Creates a function to register all kernel stubs generated in this module. |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 51 | llvm::Function *makeRegisterGlobalsFn(); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 52 | |
| 53 | /// Helper function that generates a constant string and returns a pointer to |
| 54 | /// the start of the string. The result of this function can be used anywhere |
| 55 | /// where the C code specifies const char*. |
| 56 | llvm::Constant *makeConstantString(const std::string &Str, |
| 57 | const std::string &Name = "", |
Artem Belevich | 4c09318 | 2016-08-12 18:44:01 +0000 | [diff] [blame] | 58 | const std::string &SectionName = "", |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 59 | unsigned Alignment = 0) { |
| 60 | llvm::Constant *Zeros[] = {llvm::ConstantInt::get(SizeTy, 0), |
| 61 | llvm::ConstantInt::get(SizeTy, 0)}; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 62 | auto ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str()); |
Artem Belevich | 4c09318 | 2016-08-12 18:44:01 +0000 | [diff] [blame] | 63 | llvm::GlobalVariable *GV = |
| 64 | cast<llvm::GlobalVariable>(ConstStr.getPointer()); |
| 65 | if (!SectionName.empty()) |
| 66 | GV->setSection(SectionName); |
| 67 | if (Alignment) |
| 68 | GV->setAlignment(Alignment); |
| 69 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 70 | return llvm::ConstantExpr::getGetElementPtr(ConstStr.getElementType(), |
| 71 | ConstStr.getPointer(), Zeros); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void emitDeviceStubBody(CodeGenFunction &CGF, FunctionArgList &Args); |
| 75 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 76 | public: |
| 77 | CGNVCUDARuntime(CodeGenModule &CGM); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 78 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 79 | void emitDeviceStub(CodeGenFunction &CGF, FunctionArgList &Args) override; |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 80 | void registerDeviceVar(llvm::GlobalVariable &Var, unsigned Flags) override { |
| 81 | DeviceVars.push_back(std::make_pair(&Var, Flags)); |
| 82 | } |
| 83 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 84 | /// Creates module constructor function |
| 85 | llvm::Function *makeModuleCtorFunction() override; |
| 86 | /// Creates module destructor function |
| 87 | llvm::Function *makeModuleDtorFunction() override; |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 90 | } |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 91 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 92 | CGNVCUDARuntime::CGNVCUDARuntime(CodeGenModule &CGM) |
| 93 | : CGCUDARuntime(CGM), Context(CGM.getLLVMContext()), |
| 94 | TheModule(CGM.getModule()) { |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 95 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 96 | ASTContext &Ctx = CGM.getContext(); |
| 97 | |
| 98 | IntTy = Types.ConvertType(Ctx.IntTy); |
| 99 | SizeTy = Types.ConvertType(Ctx.getSizeType()); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 100 | VoidTy = llvm::Type::getVoidTy(Context); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 101 | |
| 102 | CharPtrTy = llvm::PointerType::getUnqual(Types.ConvertType(Ctx.CharTy)); |
| 103 | VoidPtrTy = cast<llvm::PointerType>(Types.ConvertType(Ctx.VoidPtrTy)); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 104 | VoidPtrPtrTy = VoidPtrTy->getPointerTo(); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | llvm::Constant *CGNVCUDARuntime::getSetupArgumentFn() const { |
| 108 | // cudaError_t cudaSetupArgument(void *, size_t, size_t) |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 109 | llvm::Type *Params[] = {VoidPtrTy, SizeTy, SizeTy}; |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 110 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(IntTy, |
| 111 | Params, false), |
| 112 | "cudaSetupArgument"); |
| 113 | } |
| 114 | |
| 115 | llvm::Constant *CGNVCUDARuntime::getLaunchFn() const { |
| 116 | // cudaError_t cudaLaunch(char *) |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 117 | return CGM.CreateRuntimeFunction( |
| 118 | llvm::FunctionType::get(IntTy, CharPtrTy, false), "cudaLaunch"); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 121 | void CGNVCUDARuntime::emitDeviceStub(CodeGenFunction &CGF, |
| 122 | FunctionArgList &Args) { |
| 123 | EmittedKernels.push_back(CGF.CurFn); |
| 124 | emitDeviceStubBody(CGF, Args); |
| 125 | } |
| 126 | |
| 127 | void CGNVCUDARuntime::emitDeviceStubBody(CodeGenFunction &CGF, |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 128 | FunctionArgList &Args) { |
Justin Lebar | e56360a | 2016-07-27 22:36:21 +0000 | [diff] [blame] | 129 | // Emit a call to cudaSetupArgument for each arg in Args. |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 130 | llvm::Constant *cudaSetupArgFn = getSetupArgumentFn(); |
Justin Lebar | e56360a | 2016-07-27 22:36:21 +0000 | [diff] [blame] | 131 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("setup.end"); |
| 132 | CharUnits Offset = CharUnits::Zero(); |
| 133 | for (const VarDecl *A : Args) { |
| 134 | CharUnits TyWidth, TyAlign; |
| 135 | std::tie(TyWidth, TyAlign) = |
| 136 | CGM.getContext().getTypeInfoInChars(A->getType()); |
| 137 | Offset = Offset.alignTo(TyAlign); |
| 138 | llvm::Value *Args[] = { |
| 139 | CGF.Builder.CreatePointerCast(CGF.GetAddrOfLocalVar(A).getPointer(), |
| 140 | VoidPtrTy), |
| 141 | llvm::ConstantInt::get(SizeTy, TyWidth.getQuantity()), |
| 142 | llvm::ConstantInt::get(SizeTy, Offset.getQuantity()), |
| 143 | }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 144 | llvm::CallSite CS = CGF.EmitRuntimeCallOrInvoke(cudaSetupArgFn, Args); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 145 | llvm::Constant *Zero = llvm::ConstantInt::get(IntTy, 0); |
| 146 | llvm::Value *CSZero = CGF.Builder.CreateICmpEQ(CS.getInstruction(), Zero); |
Justin Lebar | e56360a | 2016-07-27 22:36:21 +0000 | [diff] [blame] | 147 | llvm::BasicBlock *NextBlock = CGF.createBasicBlock("setup.next"); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 148 | CGF.Builder.CreateCondBr(CSZero, NextBlock, EndBlock); |
| 149 | CGF.EmitBlock(NextBlock); |
Justin Lebar | e56360a | 2016-07-27 22:36:21 +0000 | [diff] [blame] | 150 | Offset += TyWidth; |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Emit the call to cudaLaunch |
| 154 | llvm::Constant *cudaLaunchFn = getLaunchFn(); |
| 155 | llvm::Value *Arg = CGF.Builder.CreatePointerCast(CGF.CurFn, CharPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 156 | CGF.EmitRuntimeCallOrInvoke(cudaLaunchFn, Arg); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 157 | CGF.EmitBranch(EndBlock); |
| 158 | |
| 159 | CGF.EmitBlock(EndBlock); |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 162 | /// Creates a function that sets up state on the host side for CUDA objects that |
| 163 | /// have a presence on both the host and device sides. Specifically, registers |
| 164 | /// the host side of kernel functions and device global variables with the CUDA |
| 165 | /// runtime. |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 166 | /// \code |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 167 | /// void __cuda_register_globals(void** GpuBinaryHandle) { |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 168 | /// __cudaRegisterFunction(GpuBinaryHandle,Kernel0,...); |
| 169 | /// ... |
| 170 | /// __cudaRegisterFunction(GpuBinaryHandle,KernelM,...); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 171 | /// __cudaRegisterVar(GpuBinaryHandle, GlobalVar0, ...); |
| 172 | /// ... |
| 173 | /// __cudaRegisterVar(GpuBinaryHandle, GlobalVarN, ...); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 174 | /// } |
| 175 | /// \endcode |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 176 | llvm::Function *CGNVCUDARuntime::makeRegisterGlobalsFn() { |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 177 | // No need to register anything |
| 178 | if (EmittedKernels.empty() && DeviceVars.empty()) |
| 179 | return nullptr; |
| 180 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 181 | llvm::Function *RegisterKernelsFunc = llvm::Function::Create( |
| 182 | llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false), |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 183 | llvm::GlobalValue::InternalLinkage, "__cuda_register_globals", &TheModule); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 184 | llvm::BasicBlock *EntryBB = |
| 185 | llvm::BasicBlock::Create(Context, "entry", RegisterKernelsFunc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 186 | CGBuilderTy Builder(CGM, Context); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 187 | Builder.SetInsertPoint(EntryBB); |
| 188 | |
| 189 | // void __cudaRegisterFunction(void **, const char *, char *, const char *, |
| 190 | // int, uint3*, uint3*, dim3*, dim3*, int*) |
Benjamin Kramer | 6d1c10b | 2016-07-02 12:03:57 +0000 | [diff] [blame] | 191 | llvm::Type *RegisterFuncParams[] = { |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 192 | VoidPtrPtrTy, CharPtrTy, CharPtrTy, CharPtrTy, IntTy, |
| 193 | VoidPtrTy, VoidPtrTy, VoidPtrTy, VoidPtrTy, IntTy->getPointerTo()}; |
| 194 | llvm::Constant *RegisterFunc = CGM.CreateRuntimeFunction( |
| 195 | llvm::FunctionType::get(IntTy, RegisterFuncParams, false), |
| 196 | "__cudaRegisterFunction"); |
| 197 | |
| 198 | // Extract GpuBinaryHandle passed as the first argument passed to |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 199 | // __cuda_register_globals() and generate __cudaRegisterFunction() call for |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 200 | // each emitted kernel. |
| 201 | llvm::Argument &GpuBinaryHandlePtr = *RegisterKernelsFunc->arg_begin(); |
| 202 | for (llvm::Function *Kernel : EmittedKernels) { |
| 203 | llvm::Constant *KernelName = makeConstantString(Kernel->getName()); |
| 204 | llvm::Constant *NullPtr = llvm::ConstantPointerNull::get(VoidPtrTy); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 205 | llvm::Value *Args[] = { |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 206 | &GpuBinaryHandlePtr, Builder.CreateBitCast(Kernel, VoidPtrTy), |
| 207 | KernelName, KernelName, llvm::ConstantInt::get(IntTy, -1), NullPtr, |
| 208 | NullPtr, NullPtr, NullPtr, |
| 209 | llvm::ConstantPointerNull::get(IntTy->getPointerTo())}; |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 210 | Builder.CreateCall(RegisterFunc, Args); |
| 211 | } |
| 212 | |
| 213 | // void __cudaRegisterVar(void **, char *, char *, const char *, |
| 214 | // int, int, int, int) |
Benjamin Kramer | 6d1c10b | 2016-07-02 12:03:57 +0000 | [diff] [blame] | 215 | llvm::Type *RegisterVarParams[] = {VoidPtrPtrTy, CharPtrTy, CharPtrTy, |
| 216 | CharPtrTy, IntTy, IntTy, |
| 217 | IntTy, IntTy}; |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 218 | llvm::Constant *RegisterVar = CGM.CreateRuntimeFunction( |
| 219 | llvm::FunctionType::get(IntTy, RegisterVarParams, false), |
| 220 | "__cudaRegisterVar"); |
| 221 | for (auto &Pair : DeviceVars) { |
| 222 | llvm::GlobalVariable *Var = Pair.first; |
| 223 | unsigned Flags = Pair.second; |
| 224 | llvm::Constant *VarName = makeConstantString(Var->getName()); |
| 225 | uint64_t VarSize = |
| 226 | CGM.getDataLayout().getTypeAllocSize(Var->getValueType()); |
| 227 | llvm::Value *Args[] = { |
| 228 | &GpuBinaryHandlePtr, |
| 229 | Builder.CreateBitCast(Var, VoidPtrTy), |
| 230 | VarName, |
| 231 | VarName, |
| 232 | llvm::ConstantInt::get(IntTy, (Flags & ExternDeviceVar) ? 1 : 0), |
| 233 | llvm::ConstantInt::get(IntTy, VarSize), |
| 234 | llvm::ConstantInt::get(IntTy, (Flags & ConstantDeviceVar) ? 1 : 0), |
| 235 | llvm::ConstantInt::get(IntTy, 0)}; |
| 236 | Builder.CreateCall(RegisterVar, Args); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | Builder.CreateRetVoid(); |
| 240 | return RegisterKernelsFunc; |
| 241 | } |
| 242 | |
| 243 | /// Creates a global constructor function for the module: |
| 244 | /// \code |
| 245 | /// void __cuda_module_ctor(void*) { |
| 246 | /// Handle0 = __cudaRegisterFatBinary(GpuBinaryBlob0); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 247 | /// __cuda_register_globals(Handle0); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 248 | /// ... |
| 249 | /// HandleN = __cudaRegisterFatBinary(GpuBinaryBlobN); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 250 | /// __cuda_register_globals(HandleN); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 251 | /// } |
| 252 | /// \endcode |
| 253 | llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() { |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 254 | // No need to generate ctors/dtors if there are no GPU binaries. |
| 255 | if (CGM.getCodeGenOpts().CudaGpuBinaryFileNames.empty()) |
| 256 | return nullptr; |
| 257 | |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 258 | // void __cuda_register_globals(void* handle); |
| 259 | llvm::Function *RegisterGlobalsFunc = makeRegisterGlobalsFn(); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 260 | // void ** __cudaRegisterFatBinary(void *); |
| 261 | llvm::Constant *RegisterFatbinFunc = CGM.CreateRuntimeFunction( |
| 262 | llvm::FunctionType::get(VoidPtrPtrTy, VoidPtrTy, false), |
| 263 | "__cudaRegisterFatBinary"); |
| 264 | // struct { int magic, int version, void * gpu_binary, void * dont_care }; |
| 265 | llvm::StructType *FatbinWrapperTy = |
| 266 | llvm::StructType::get(IntTy, IntTy, VoidPtrTy, VoidPtrTy, nullptr); |
| 267 | |
| 268 | llvm::Function *ModuleCtorFunc = llvm::Function::Create( |
| 269 | llvm::FunctionType::get(VoidTy, VoidPtrTy, false), |
| 270 | llvm::GlobalValue::InternalLinkage, "__cuda_module_ctor", &TheModule); |
| 271 | llvm::BasicBlock *CtorEntryBB = |
| 272 | llvm::BasicBlock::Create(Context, "entry", ModuleCtorFunc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 273 | CGBuilderTy CtorBuilder(CGM, Context); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 274 | |
| 275 | CtorBuilder.SetInsertPoint(CtorEntryBB); |
| 276 | |
| 277 | // For each GPU binary, register it with the CUDA runtime and store returned |
| 278 | // handle in a global variable and save the handle in GpuBinaryHandles vector |
| 279 | // to be cleaned up in destructor on exit. Then associate all known kernels |
| 280 | // with the GPU binary handle so CUDA runtime can figure out what to call on |
| 281 | // the GPU side. |
| 282 | for (const std::string &GpuBinaryFileName : |
| 283 | CGM.getCodeGenOpts().CudaGpuBinaryFileNames) { |
| 284 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> GpuBinaryOrErr = |
| 285 | llvm::MemoryBuffer::getFileOrSTDIN(GpuBinaryFileName); |
| 286 | if (std::error_code EC = GpuBinaryOrErr.getError()) { |
| 287 | CGM.getDiags().Report(diag::err_cannot_open_file) << GpuBinaryFileName |
| 288 | << EC.message(); |
| 289 | continue; |
| 290 | } |
| 291 | |
Justin Lebar | d14fe88 | 2016-11-18 00:41:31 +0000 | [diff] [blame] | 292 | const char *FatbinConstantName = |
| 293 | CGM.getTriple().isMacOSX() ? "__NV_CUDA,__nv_fatbin" : ".nv_fatbin"; |
| 294 | // NVIDIA's cuobjdump looks for fatbins in this section. |
| 295 | const char *FatbinSectionName = |
| 296 | CGM.getTriple().isMacOSX() ? "__NV_CUDA,__fatbin" : ".nvFatBinSegment"; |
| 297 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 298 | // Create initialized wrapper structure that points to the loaded GPU binary |
| 299 | llvm::Constant *Values[] = { |
| 300 | llvm::ConstantInt::get(IntTy, 0x466243b1), // Fatbin wrapper magic. |
| 301 | llvm::ConstantInt::get(IntTy, 1), // Fatbin version. |
Artem Belevich | 4c09318 | 2016-08-12 18:44:01 +0000 | [diff] [blame] | 302 | makeConstantString(GpuBinaryOrErr.get()->getBuffer(), // Data. |
Justin Lebar | d14fe88 | 2016-11-18 00:41:31 +0000 | [diff] [blame] | 303 | "", FatbinConstantName, 8), |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 304 | llvm::ConstantPointerNull::get(VoidPtrTy)}; // Unused in fatbin v1. |
| 305 | llvm::GlobalVariable *FatbinWrapper = new llvm::GlobalVariable( |
| 306 | TheModule, FatbinWrapperTy, true, llvm::GlobalValue::InternalLinkage, |
| 307 | llvm::ConstantStruct::get(FatbinWrapperTy, Values), |
| 308 | "__cuda_fatbin_wrapper"); |
Justin Lebar | d14fe88 | 2016-11-18 00:41:31 +0000 | [diff] [blame] | 309 | FatbinWrapper->setSection(FatbinSectionName); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 310 | |
| 311 | // GpuBinaryHandle = __cudaRegisterFatBinary(&FatbinWrapper); |
| 312 | llvm::CallInst *RegisterFatbinCall = CtorBuilder.CreateCall( |
| 313 | RegisterFatbinFunc, |
| 314 | CtorBuilder.CreateBitCast(FatbinWrapper, VoidPtrTy)); |
| 315 | llvm::GlobalVariable *GpuBinaryHandle = new llvm::GlobalVariable( |
| 316 | TheModule, VoidPtrPtrTy, false, llvm::GlobalValue::InternalLinkage, |
| 317 | llvm::ConstantPointerNull::get(VoidPtrPtrTy), "__cuda_gpubin_handle"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 318 | CtorBuilder.CreateAlignedStore(RegisterFatbinCall, GpuBinaryHandle, |
| 319 | CGM.getPointerAlign()); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 320 | |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 321 | // Call __cuda_register_globals(GpuBinaryHandle); |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 322 | if (RegisterGlobalsFunc) |
| 323 | CtorBuilder.CreateCall(RegisterGlobalsFunc, RegisterFatbinCall); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 324 | |
| 325 | // Save GpuBinaryHandle so we can unregister it in destructor. |
| 326 | GpuBinaryHandles.push_back(GpuBinaryHandle); |
| 327 | } |
| 328 | |
| 329 | CtorBuilder.CreateRetVoid(); |
| 330 | return ModuleCtorFunc; |
| 331 | } |
| 332 | |
| 333 | /// Creates a global destructor function that unregisters all GPU code blobs |
| 334 | /// registered by constructor. |
| 335 | /// \code |
| 336 | /// void __cuda_module_dtor(void*) { |
| 337 | /// __cudaUnregisterFatBinary(Handle0); |
| 338 | /// ... |
| 339 | /// __cudaUnregisterFatBinary(HandleN); |
| 340 | /// } |
| 341 | /// \endcode |
| 342 | llvm::Function *CGNVCUDARuntime::makeModuleDtorFunction() { |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 343 | // No need for destructor if we don't have handles to unregister. |
| 344 | if (GpuBinaryHandles.empty()) |
| 345 | return nullptr; |
| 346 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 347 | // void __cudaUnregisterFatBinary(void ** handle); |
| 348 | llvm::Constant *UnregisterFatbinFunc = CGM.CreateRuntimeFunction( |
| 349 | llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false), |
| 350 | "__cudaUnregisterFatBinary"); |
| 351 | |
| 352 | llvm::Function *ModuleDtorFunc = llvm::Function::Create( |
| 353 | llvm::FunctionType::get(VoidTy, VoidPtrTy, false), |
| 354 | llvm::GlobalValue::InternalLinkage, "__cuda_module_dtor", &TheModule); |
| 355 | llvm::BasicBlock *DtorEntryBB = |
| 356 | llvm::BasicBlock::Create(Context, "entry", ModuleDtorFunc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 357 | CGBuilderTy DtorBuilder(CGM, Context); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 358 | DtorBuilder.SetInsertPoint(DtorEntryBB); |
| 359 | |
| 360 | for (llvm::GlobalVariable *GpuBinaryHandle : GpuBinaryHandles) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 361 | auto HandleValue = |
| 362 | DtorBuilder.CreateAlignedLoad(GpuBinaryHandle, CGM.getPointerAlign()); |
| 363 | DtorBuilder.CreateCall(UnregisterFatbinFunc, HandleValue); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | DtorBuilder.CreateRetVoid(); |
| 367 | return ModuleDtorFunc; |
| 368 | } |
| 369 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 370 | CGCUDARuntime *CodeGen::CreateNVCUDARuntime(CodeGenModule &CGM) { |
| 371 | return new CGNVCUDARuntime(CGM); |
| 372 | } |