blob: b598330abe44863b7f330e7ccf7b989b31523830 [file] [log] [blame]
Peter Collingbournefe883422011-10-06 18:29:37 +00001//===----- 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 Collingbournefa4d6032011-10-06 18:51:56 +000016#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
18#include "clang/AST/Decl.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000019#include "llvm/IR/BasicBlock.h"
Chandler Carruthc80ceea2014-03-04 11:02:08 +000020#include "llvm/IR/CallSite.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DerivedTypes.h"
Peter Collingbournefe883422011-10-06 18:29:37 +000023
24using namespace clang;
25using namespace CodeGen;
26
27namespace {
28
29class CGNVCUDARuntime : public CGCUDARuntime {
Peter Collingbournefa4d6032011-10-06 18:51:56 +000030
31private:
Artem Belevich52cc4872015-05-07 19:34:16 +000032 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 Belevich42e19492016-03-02 18:28:50 +000041 llvm::SmallVector<std::pair<llvm::GlobalVariable *, unsigned>, 16> DeviceVars;
Artem Belevich52cc4872015-05-07 19:34:16 +000042 /// 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 Collingbournefa4d6032011-10-06 18:51:56 +000046
47 llvm::Constant *getSetupArgumentFn() const;
48 llvm::Constant *getLaunchFn() const;
49
Artem Belevich52cc4872015-05-07 19:34:16 +000050 /// Creates a function to register all kernel stubs generated in this module.
Artem Belevich42e19492016-03-02 18:28:50 +000051 llvm::Function *makeRegisterGlobalsFn();
Artem Belevich52cc4872015-05-07 19:34:16 +000052
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 Belevich4c093182016-08-12 18:44:01 +000058 const std::string &SectionName = "",
Artem Belevich52cc4872015-05-07 19:34:16 +000059 unsigned Alignment = 0) {
60 llvm::Constant *Zeros[] = {llvm::ConstantInt::get(SizeTy, 0),
61 llvm::ConstantInt::get(SizeTy, 0)};
John McCall7f416cc2015-09-08 08:05:57 +000062 auto ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
Artem Belevich4c093182016-08-12 18:44:01 +000063 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 McCall7f416cc2015-09-08 08:05:57 +000070 return llvm::ConstantExpr::getGetElementPtr(ConstStr.getElementType(),
71 ConstStr.getPointer(), Zeros);
Artem Belevich52cc4872015-05-07 19:34:16 +000072 }
73
74 void emitDeviceStubBody(CodeGenFunction &CGF, FunctionArgList &Args);
75
Peter Collingbournefe883422011-10-06 18:29:37 +000076public:
77 CGNVCUDARuntime(CodeGenModule &CGM);
Peter Collingbournefa4d6032011-10-06 18:51:56 +000078
Artem Belevich52cc4872015-05-07 19:34:16 +000079 void emitDeviceStub(CodeGenFunction &CGF, FunctionArgList &Args) override;
Artem Belevich42e19492016-03-02 18:28:50 +000080 void registerDeviceVar(llvm::GlobalVariable &Var, unsigned Flags) override {
81 DeviceVars.push_back(std::make_pair(&Var, Flags));
82 }
83
Artem Belevich52cc4872015-05-07 19:34:16 +000084 /// Creates module constructor function
85 llvm::Function *makeModuleCtorFunction() override;
86 /// Creates module destructor function
87 llvm::Function *makeModuleDtorFunction() override;
Peter Collingbournefe883422011-10-06 18:29:37 +000088};
89
Alexander Kornienkoab9db512015-06-22 23:07:51 +000090}
Peter Collingbournefe883422011-10-06 18:29:37 +000091
Artem Belevich52cc4872015-05-07 19:34:16 +000092CGNVCUDARuntime::CGNVCUDARuntime(CodeGenModule &CGM)
93 : CGCUDARuntime(CGM), Context(CGM.getLLVMContext()),
94 TheModule(CGM.getModule()) {
Peter Collingbournefa4d6032011-10-06 18:51:56 +000095 CodeGen::CodeGenTypes &Types = CGM.getTypes();
96 ASTContext &Ctx = CGM.getContext();
97
98 IntTy = Types.ConvertType(Ctx.IntTy);
99 SizeTy = Types.ConvertType(Ctx.getSizeType());
Artem Belevich52cc4872015-05-07 19:34:16 +0000100 VoidTy = llvm::Type::getVoidTy(Context);
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000101
102 CharPtrTy = llvm::PointerType::getUnqual(Types.ConvertType(Ctx.CharTy));
103 VoidPtrTy = cast<llvm::PointerType>(Types.ConvertType(Ctx.VoidPtrTy));
Artem Belevich52cc4872015-05-07 19:34:16 +0000104 VoidPtrPtrTy = VoidPtrTy->getPointerTo();
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000105}
106
107llvm::Constant *CGNVCUDARuntime::getSetupArgumentFn() const {
108 // cudaError_t cudaSetupArgument(void *, size_t, size_t)
Benjamin Kramer30934732016-07-02 11:41:41 +0000109 llvm::Type *Params[] = {VoidPtrTy, SizeTy, SizeTy};
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000110 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(IntTy,
111 Params, false),
112 "cudaSetupArgument");
113}
114
115llvm::Constant *CGNVCUDARuntime::getLaunchFn() const {
116 // cudaError_t cudaLaunch(char *)
Artem Belevich52cc4872015-05-07 19:34:16 +0000117 return CGM.CreateRuntimeFunction(
118 llvm::FunctionType::get(IntTy, CharPtrTy, false), "cudaLaunch");
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000119}
120
Artem Belevich52cc4872015-05-07 19:34:16 +0000121void CGNVCUDARuntime::emitDeviceStub(CodeGenFunction &CGF,
122 FunctionArgList &Args) {
123 EmittedKernels.push_back(CGF.CurFn);
124 emitDeviceStubBody(CGF, Args);
125}
126
127void CGNVCUDARuntime::emitDeviceStubBody(CodeGenFunction &CGF,
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000128 FunctionArgList &Args) {
Justin Lebare56360a2016-07-27 22:36:21 +0000129 // Emit a call to cudaSetupArgument for each arg in Args.
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000130 llvm::Constant *cudaSetupArgFn = getSetupArgumentFn();
Justin Lebare56360a2016-07-27 22:36:21 +0000131 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 McCall882987f2013-02-28 19:01:20 +0000144 llvm::CallSite CS = CGF.EmitRuntimeCallOrInvoke(cudaSetupArgFn, Args);
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000145 llvm::Constant *Zero = llvm::ConstantInt::get(IntTy, 0);
146 llvm::Value *CSZero = CGF.Builder.CreateICmpEQ(CS.getInstruction(), Zero);
Justin Lebare56360a2016-07-27 22:36:21 +0000147 llvm::BasicBlock *NextBlock = CGF.createBasicBlock("setup.next");
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000148 CGF.Builder.CreateCondBr(CSZero, NextBlock, EndBlock);
149 CGF.EmitBlock(NextBlock);
Justin Lebare56360a2016-07-27 22:36:21 +0000150 Offset += TyWidth;
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000151 }
152
153 // Emit the call to cudaLaunch
154 llvm::Constant *cudaLaunchFn = getLaunchFn();
155 llvm::Value *Arg = CGF.Builder.CreatePointerCast(CGF.CurFn, CharPtrTy);
John McCall882987f2013-02-28 19:01:20 +0000156 CGF.EmitRuntimeCallOrInvoke(cudaLaunchFn, Arg);
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000157 CGF.EmitBranch(EndBlock);
158
159 CGF.EmitBlock(EndBlock);
Peter Collingbournefe883422011-10-06 18:29:37 +0000160}
161
Artem Belevich42e19492016-03-02 18:28:50 +0000162/// 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 Belevich52cc4872015-05-07 19:34:16 +0000166/// \code
Artem Belevich42e19492016-03-02 18:28:50 +0000167/// void __cuda_register_globals(void** GpuBinaryHandle) {
Artem Belevich52cc4872015-05-07 19:34:16 +0000168/// __cudaRegisterFunction(GpuBinaryHandle,Kernel0,...);
169/// ...
170/// __cudaRegisterFunction(GpuBinaryHandle,KernelM,...);
Artem Belevich42e19492016-03-02 18:28:50 +0000171/// __cudaRegisterVar(GpuBinaryHandle, GlobalVar0, ...);
172/// ...
173/// __cudaRegisterVar(GpuBinaryHandle, GlobalVarN, ...);
Artem Belevich52cc4872015-05-07 19:34:16 +0000174/// }
175/// \endcode
Artem Belevich42e19492016-03-02 18:28:50 +0000176llvm::Function *CGNVCUDARuntime::makeRegisterGlobalsFn() {
Artem Belevich8c1ec1e2016-03-02 18:28:53 +0000177 // No need to register anything
178 if (EmittedKernels.empty() && DeviceVars.empty())
179 return nullptr;
180
Artem Belevich52cc4872015-05-07 19:34:16 +0000181 llvm::Function *RegisterKernelsFunc = llvm::Function::Create(
182 llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false),
Artem Belevich42e19492016-03-02 18:28:50 +0000183 llvm::GlobalValue::InternalLinkage, "__cuda_register_globals", &TheModule);
Artem Belevich52cc4872015-05-07 19:34:16 +0000184 llvm::BasicBlock *EntryBB =
185 llvm::BasicBlock::Create(Context, "entry", RegisterKernelsFunc);
John McCall7f416cc2015-09-08 08:05:57 +0000186 CGBuilderTy Builder(CGM, Context);
Artem Belevich52cc4872015-05-07 19:34:16 +0000187 Builder.SetInsertPoint(EntryBB);
188
189 // void __cudaRegisterFunction(void **, const char *, char *, const char *,
190 // int, uint3*, uint3*, dim3*, dim3*, int*)
Benjamin Kramer6d1c10b2016-07-02 12:03:57 +0000191 llvm::Type *RegisterFuncParams[] = {
Artem Belevich52cc4872015-05-07 19:34:16 +0000192 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 Belevich42e19492016-03-02 18:28:50 +0000199 // __cuda_register_globals() and generate __cudaRegisterFunction() call for
Artem Belevich52cc4872015-05-07 19:34:16 +0000200 // 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 Belevich42e19492016-03-02 18:28:50 +0000205 llvm::Value *Args[] = {
Artem Belevich52cc4872015-05-07 19:34:16 +0000206 &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 Belevich42e19492016-03-02 18:28:50 +0000210 Builder.CreateCall(RegisterFunc, Args);
211 }
212
213 // void __cudaRegisterVar(void **, char *, char *, const char *,
214 // int, int, int, int)
Benjamin Kramer6d1c10b2016-07-02 12:03:57 +0000215 llvm::Type *RegisterVarParams[] = {VoidPtrPtrTy, CharPtrTy, CharPtrTy,
216 CharPtrTy, IntTy, IntTy,
217 IntTy, IntTy};
Artem Belevich42e19492016-03-02 18:28:50 +0000218 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 Belevich52cc4872015-05-07 19:34:16 +0000237 }
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 Belevich42e19492016-03-02 18:28:50 +0000247/// __cuda_register_globals(Handle0);
Artem Belevich52cc4872015-05-07 19:34:16 +0000248/// ...
249/// HandleN = __cudaRegisterFatBinary(GpuBinaryBlobN);
Artem Belevich42e19492016-03-02 18:28:50 +0000250/// __cuda_register_globals(HandleN);
Artem Belevich52cc4872015-05-07 19:34:16 +0000251/// }
252/// \endcode
253llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
Artem Belevich8c1ec1e2016-03-02 18:28:53 +0000254 // No need to generate ctors/dtors if there are no GPU binaries.
255 if (CGM.getCodeGenOpts().CudaGpuBinaryFileNames.empty())
256 return nullptr;
257
Artem Belevich42e19492016-03-02 18:28:50 +0000258 // void __cuda_register_globals(void* handle);
259 llvm::Function *RegisterGlobalsFunc = makeRegisterGlobalsFn();
Artem Belevich52cc4872015-05-07 19:34:16 +0000260 // 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 McCall7f416cc2015-09-08 08:05:57 +0000273 CGBuilderTy CtorBuilder(CGM, Context);
Artem Belevich52cc4872015-05-07 19:34:16 +0000274
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 Lebard14fe882016-11-18 00:41:31 +0000292 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 Belevich52cc4872015-05-07 19:34:16 +0000298 // 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 Belevich4c093182016-08-12 18:44:01 +0000302 makeConstantString(GpuBinaryOrErr.get()->getBuffer(), // Data.
Justin Lebard14fe882016-11-18 00:41:31 +0000303 "", FatbinConstantName, 8),
Artem Belevich52cc4872015-05-07 19:34:16 +0000304 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 Lebard14fe882016-11-18 00:41:31 +0000309 FatbinWrapper->setSection(FatbinSectionName);
Artem Belevich52cc4872015-05-07 19:34:16 +0000310
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 McCall7f416cc2015-09-08 08:05:57 +0000318 CtorBuilder.CreateAlignedStore(RegisterFatbinCall, GpuBinaryHandle,
319 CGM.getPointerAlign());
Artem Belevich52cc4872015-05-07 19:34:16 +0000320
Artem Belevich42e19492016-03-02 18:28:50 +0000321 // Call __cuda_register_globals(GpuBinaryHandle);
Artem Belevich8c1ec1e2016-03-02 18:28:53 +0000322 if (RegisterGlobalsFunc)
323 CtorBuilder.CreateCall(RegisterGlobalsFunc, RegisterFatbinCall);
Artem Belevich52cc4872015-05-07 19:34:16 +0000324
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
342llvm::Function *CGNVCUDARuntime::makeModuleDtorFunction() {
Artem Belevich8c1ec1e2016-03-02 18:28:53 +0000343 // No need for destructor if we don't have handles to unregister.
344 if (GpuBinaryHandles.empty())
345 return nullptr;
346
Artem Belevich52cc4872015-05-07 19:34:16 +0000347 // 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 McCall7f416cc2015-09-08 08:05:57 +0000357 CGBuilderTy DtorBuilder(CGM, Context);
Artem Belevich52cc4872015-05-07 19:34:16 +0000358 DtorBuilder.SetInsertPoint(DtorEntryBB);
359
360 for (llvm::GlobalVariable *GpuBinaryHandle : GpuBinaryHandles) {
John McCall7f416cc2015-09-08 08:05:57 +0000361 auto HandleValue =
362 DtorBuilder.CreateAlignedLoad(GpuBinaryHandle, CGM.getPointerAlign());
363 DtorBuilder.CreateCall(UnregisterFatbinFunc, HandleValue);
Artem Belevich52cc4872015-05-07 19:34:16 +0000364 }
365
366 DtorBuilder.CreateRetVoid();
367 return ModuleDtorFunc;
368}
369
Peter Collingbournefe883422011-10-06 18:29:37 +0000370CGCUDARuntime *CodeGen::CreateNVCUDARuntime(CodeGenModule &CGM) {
371 return new CGNVCUDARuntime(CGM);
372}