blob: 62661039a32a1a089b287d44cbb17aa41b807482 [file] [log] [blame]
Peter Collingbournefe883422011-10-06 18:29:37 +00001//===----- CGCUDANV.cpp - Interface to NVIDIA CUDA Runtime ----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Peter Collingbournefe883422011-10-06 18:29:37 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This provides a class for CUDA code generation targeting the NVIDIA CUDA
10// runtime library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGCUDARuntime.h"
Peter Collingbournefa4d6032011-10-06 18:51:56 +000015#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
17#include "clang/AST/Decl.h"
Artem Belevichc62214d2019-01-31 21:34:03 +000018#include "clang/Basic/Cuda.h"
19#include "clang/CodeGen/CodeGenABITypes.h"
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000020#include "clang/CodeGen/ConstantInitBuilder.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000021#include "llvm/IR/BasicBlock.h"
22#include "llvm/IR/Constants.h"
23#include "llvm/IR/DerivedTypes.h"
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000024#include "llvm/Support/Format.h"
Peter Collingbournefe883422011-10-06 18:29:37 +000025
26using namespace clang;
27using namespace CodeGen;
28
29namespace {
Yaxun Liu29155b02018-05-18 15:07:56 +000030constexpr unsigned CudaFatMagic = 0x466243b1;
31constexpr unsigned HIPFatMagic = 0x48495046; // "HIPF"
Peter Collingbournefe883422011-10-06 18:29:37 +000032
33class CGNVCUDARuntime : public CGCUDARuntime {
Peter Collingbournefa4d6032011-10-06 18:51:56 +000034
35private:
John McCall6c9f1fdb2016-11-19 08:17:24 +000036 llvm::IntegerType *IntTy, *SizeTy;
37 llvm::Type *VoidTy;
Artem Belevich52cc4872015-05-07 19:34:16 +000038 llvm::PointerType *CharPtrTy, *VoidPtrTy, *VoidPtrPtrTy;
39
40 /// Convenience reference to LLVM Context
41 llvm::LLVMContext &Context;
42 /// Convenience reference to the current module
43 llvm::Module &TheModule;
44 /// Keeps track of kernel launch stubs emitted in this module
Yaxun Liuc18e9ec2019-02-14 02:00:09 +000045 struct KernelInfo {
46 llvm::Function *Kernel;
47 const Decl *D;
48 };
49 llvm::SmallVector<KernelInfo, 16> EmittedKernels;
50 struct VarInfo {
51 llvm::GlobalVariable *Var;
52 const VarDecl *D;
53 unsigned Flag;
54 };
55 llvm::SmallVector<VarInfo, 16> DeviceVars;
Jonas Hahnfelde7681322018-02-28 17:53:46 +000056 /// Keeps track of variable containing handle of GPU binary. Populated by
Artem Belevich52cc4872015-05-07 19:34:16 +000057 /// ModuleCtorFunction() and used to create corresponding cleanup calls in
58 /// ModuleDtorFunction()
Jonas Hahnfelde7681322018-02-28 17:53:46 +000059 llvm::GlobalVariable *GpuBinaryHandle = nullptr;
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000060 /// Whether we generate relocatable device code.
61 bool RelocatableDeviceCode;
Yaxun Liuc18e9ec2019-02-14 02:00:09 +000062 /// Mangle context for device.
63 std::unique_ptr<MangleContext> DeviceMC;
Peter Collingbournefa4d6032011-10-06 18:51:56 +000064
James Y Knight9871db02019-02-05 16:42:33 +000065 llvm::FunctionCallee getSetupArgumentFn() const;
66 llvm::FunctionCallee getLaunchFn() const;
Peter Collingbournefa4d6032011-10-06 18:51:56 +000067
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000068 llvm::FunctionType *getRegisterGlobalsFnTy() const;
69 llvm::FunctionType *getCallbackFnTy() const;
70 llvm::FunctionType *getRegisterLinkedBinaryFnTy() const;
Yaxun Liu887c5692018-04-25 01:10:37 +000071 std::string addPrefixToName(StringRef FuncName) const;
72 std::string addUnderscoredPrefixToName(StringRef FuncName) const;
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +000073
Artem Belevich52cc4872015-05-07 19:34:16 +000074 /// Creates a function to register all kernel stubs generated in this module.
Artem Belevich42e19492016-03-02 18:28:50 +000075 llvm::Function *makeRegisterGlobalsFn();
Artem Belevich52cc4872015-05-07 19:34:16 +000076
77 /// Helper function that generates a constant string and returns a pointer to
78 /// the start of the string. The result of this function can be used anywhere
79 /// where the C code specifies const char*.
80 llvm::Constant *makeConstantString(const std::string &Str,
81 const std::string &Name = "",
Artem Belevich4c093182016-08-12 18:44:01 +000082 const std::string &SectionName = "",
Artem Belevich52cc4872015-05-07 19:34:16 +000083 unsigned Alignment = 0) {
84 llvm::Constant *Zeros[] = {llvm::ConstantInt::get(SizeTy, 0),
85 llvm::ConstantInt::get(SizeTy, 0)};
John McCall7f416cc2015-09-08 08:05:57 +000086 auto ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
Artem Belevich4c093182016-08-12 18:44:01 +000087 llvm::GlobalVariable *GV =
88 cast<llvm::GlobalVariable>(ConstStr.getPointer());
Jonas Hahnfeld3b9cbba92018-06-08 11:17:08 +000089 if (!SectionName.empty()) {
Artem Belevich4c093182016-08-12 18:44:01 +000090 GV->setSection(SectionName);
Jonas Hahnfeld3b9cbba92018-06-08 11:17:08 +000091 // Mark the address as used which make sure that this section isn't
92 // merged and we will really have it in the object file.
93 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::None);
94 }
Artem Belevich4c093182016-08-12 18:44:01 +000095 if (Alignment)
96 GV->setAlignment(Alignment);
97
John McCall7f416cc2015-09-08 08:05:57 +000098 return llvm::ConstantExpr::getGetElementPtr(ConstStr.getElementType(),
99 ConstStr.getPointer(), Zeros);
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000100 }
101
102 /// Helper function that generates an empty dummy function returning void.
103 llvm::Function *makeDummyFunction(llvm::FunctionType *FnTy) {
104 assert(FnTy->getReturnType()->isVoidTy() &&
105 "Can only generate dummy functions returning void!");
106 llvm::Function *DummyFunc = llvm::Function::Create(
107 FnTy, llvm::GlobalValue::InternalLinkage, "dummy", &TheModule);
108
109 llvm::BasicBlock *DummyBlock =
110 llvm::BasicBlock::Create(Context, "", DummyFunc);
111 CGBuilderTy FuncBuilder(CGM, Context);
112 FuncBuilder.SetInsertPoint(DummyBlock);
113 FuncBuilder.CreateRetVoid();
114
115 return DummyFunc;
116 }
Artem Belevich52cc4872015-05-07 19:34:16 +0000117
Artem Belevichc62214d2019-01-31 21:34:03 +0000118 void emitDeviceStubBodyLegacy(CodeGenFunction &CGF, FunctionArgList &Args);
119 void emitDeviceStubBodyNew(CodeGenFunction &CGF, FunctionArgList &Args);
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000120 std::string getDeviceSideName(const Decl *ND);
Artem Belevich52cc4872015-05-07 19:34:16 +0000121
Peter Collingbournefe883422011-10-06 18:29:37 +0000122public:
123 CGNVCUDARuntime(CodeGenModule &CGM);
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000124
Artem Belevich52cc4872015-05-07 19:34:16 +0000125 void emitDeviceStub(CodeGenFunction &CGF, FunctionArgList &Args) override;
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000126 void registerDeviceVar(const VarDecl *VD, llvm::GlobalVariable &Var,
127 unsigned Flags) override {
128 DeviceVars.push_back({&Var, VD, Flags});
Artem Belevich42e19492016-03-02 18:28:50 +0000129 }
130
Artem Belevich52cc4872015-05-07 19:34:16 +0000131 /// Creates module constructor function
132 llvm::Function *makeModuleCtorFunction() override;
133 /// Creates module destructor function
134 llvm::Function *makeModuleDtorFunction() override;
Peter Collingbournefe883422011-10-06 18:29:37 +0000135};
136
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000137}
Peter Collingbournefe883422011-10-06 18:29:37 +0000138
Yaxun Liu887c5692018-04-25 01:10:37 +0000139std::string CGNVCUDARuntime::addPrefixToName(StringRef FuncName) const {
140 if (CGM.getLangOpts().HIP)
141 return ((Twine("hip") + Twine(FuncName)).str());
142 return ((Twine("cuda") + Twine(FuncName)).str());
143}
144std::string
145CGNVCUDARuntime::addUnderscoredPrefixToName(StringRef FuncName) const {
146 if (CGM.getLangOpts().HIP)
147 return ((Twine("__hip") + Twine(FuncName)).str());
148 return ((Twine("__cuda") + Twine(FuncName)).str());
149}
150
Artem Belevich52cc4872015-05-07 19:34:16 +0000151CGNVCUDARuntime::CGNVCUDARuntime(CodeGenModule &CGM)
152 : CGCUDARuntime(CGM), Context(CGM.getLLVMContext()),
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000153 TheModule(CGM.getModule()),
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000154 RelocatableDeviceCode(CGM.getLangOpts().GPURelocatableDeviceCode),
155 DeviceMC(CGM.getContext().createMangleContext(
156 CGM.getContext().getAuxTargetInfo())) {
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000157 CodeGen::CodeGenTypes &Types = CGM.getTypes();
158 ASTContext &Ctx = CGM.getContext();
159
John McCall6c9f1fdb2016-11-19 08:17:24 +0000160 IntTy = CGM.IntTy;
161 SizeTy = CGM.SizeTy;
162 VoidTy = CGM.VoidTy;
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000163
164 CharPtrTy = llvm::PointerType::getUnqual(Types.ConvertType(Ctx.CharTy));
165 VoidPtrTy = cast<llvm::PointerType>(Types.ConvertType(Ctx.VoidPtrTy));
Artem Belevich52cc4872015-05-07 19:34:16 +0000166 VoidPtrPtrTy = VoidPtrTy->getPointerTo();
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000167}
168
James Y Knight9871db02019-02-05 16:42:33 +0000169llvm::FunctionCallee CGNVCUDARuntime::getSetupArgumentFn() const {
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000170 // cudaError_t cudaSetupArgument(void *, size_t, size_t)
Benjamin Kramer30934732016-07-02 11:41:41 +0000171 llvm::Type *Params[] = {VoidPtrTy, SizeTy, SizeTy};
Yaxun Liu887c5692018-04-25 01:10:37 +0000172 return CGM.CreateRuntimeFunction(
173 llvm::FunctionType::get(IntTy, Params, false),
174 addPrefixToName("SetupArgument"));
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000175}
176
James Y Knight9871db02019-02-05 16:42:33 +0000177llvm::FunctionCallee CGNVCUDARuntime::getLaunchFn() const {
Yaxun Liu887c5692018-04-25 01:10:37 +0000178 if (CGM.getLangOpts().HIP) {
179 // hipError_t hipLaunchByPtr(char *);
180 return CGM.CreateRuntimeFunction(
181 llvm::FunctionType::get(IntTy, CharPtrTy, false), "hipLaunchByPtr");
182 } else {
183 // cudaError_t cudaLaunch(char *);
184 return CGM.CreateRuntimeFunction(
185 llvm::FunctionType::get(IntTy, CharPtrTy, false), "cudaLaunch");
186 }
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000187}
188
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000189llvm::FunctionType *CGNVCUDARuntime::getRegisterGlobalsFnTy() const {
190 return llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false);
191}
192
193llvm::FunctionType *CGNVCUDARuntime::getCallbackFnTy() const {
194 return llvm::FunctionType::get(VoidTy, VoidPtrTy, false);
195}
196
197llvm::FunctionType *CGNVCUDARuntime::getRegisterLinkedBinaryFnTy() const {
198 auto CallbackFnTy = getCallbackFnTy();
199 auto RegisterGlobalsFnTy = getRegisterGlobalsFnTy();
200 llvm::Type *Params[] = {RegisterGlobalsFnTy->getPointerTo(), VoidPtrTy,
201 VoidPtrTy, CallbackFnTy->getPointerTo()};
202 return llvm::FunctionType::get(VoidTy, Params, false);
203}
204
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000205std::string CGNVCUDARuntime::getDeviceSideName(const Decl *D) {
206 auto *ND = cast<const NamedDecl>(D);
207 std::string DeviceSideName;
208 if (DeviceMC->shouldMangleDeclName(ND)) {
209 SmallString<256> Buffer;
210 llvm::raw_svector_ostream Out(Buffer);
211 DeviceMC->mangleName(ND, Out);
212 DeviceSideName = Out.str();
213 } else
214 DeviceSideName = ND->getIdentifier()->getName();
215 return DeviceSideName;
216}
217
Artem Belevich52cc4872015-05-07 19:34:16 +0000218void CGNVCUDARuntime::emitDeviceStub(CodeGenFunction &CGF,
219 FunctionArgList &Args) {
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000220 assert(getDeviceSideName(CGF.CurFuncDecl) == CGF.CurFn->getName() ||
221 CGF.CGM.getContext().getTargetInfo().getCXXABI() !=
222 CGF.CGM.getContext().getAuxTargetInfo()->getCXXABI());
223
224 EmittedKernels.push_back({CGF.CurFn, CGF.CurFuncDecl});
Artem Belevichc62214d2019-01-31 21:34:03 +0000225 if (CudaFeatureEnabled(CGM.getTarget().getSDKVersion(),
226 CudaFeature::CUDA_USES_NEW_LAUNCH))
227 emitDeviceStubBodyNew(CGF, Args);
228 else
229 emitDeviceStubBodyLegacy(CGF, Args);
Artem Belevich52cc4872015-05-07 19:34:16 +0000230}
231
Artem Belevichc62214d2019-01-31 21:34:03 +0000232// CUDA 9.0+ uses new way to launch kernels. Parameters are packed in a local
233// array and kernels are launched using cudaLaunchKernel().
234void CGNVCUDARuntime::emitDeviceStubBodyNew(CodeGenFunction &CGF,
235 FunctionArgList &Args) {
236 // Build the shadow stack entry at the very start of the function.
237
238 // Calculate amount of space we will need for all arguments. If we have no
239 // args, allocate a single pointer so we still have a valid pointer to the
240 // argument array that we can pass to runtime, even if it will be unused.
241 Address KernelArgs = CGF.CreateTempAlloca(
242 VoidPtrTy, CharUnits::fromQuantity(16), "kernel_args",
243 llvm::ConstantInt::get(SizeTy, std::max<size_t>(1, Args.size())));
244 // Store pointers to the arguments in a locally allocated launch_args.
245 for (unsigned i = 0; i < Args.size(); ++i) {
246 llvm::Value* VarPtr = CGF.GetAddrOfLocalVar(Args[i]).getPointer();
247 llvm::Value *VoidVarPtr = CGF.Builder.CreatePointerCast(VarPtr, VoidPtrTy);
248 CGF.Builder.CreateDefaultAlignedStore(
249 VoidVarPtr, CGF.Builder.CreateConstGEP1_32(KernelArgs.getPointer(), i));
250 }
251
252 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("setup.end");
253
254 // Lookup cudaLaunchKernel function.
255 // cudaError_t cudaLaunchKernel(const void *func, dim3 gridDim, dim3 blockDim,
256 // void **args, size_t sharedMem,
257 // cudaStream_t stream);
258 TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl();
259 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
260 IdentifierInfo &cudaLaunchKernelII =
261 CGM.getContext().Idents.get("cudaLaunchKernel");
262 FunctionDecl *cudaLaunchKernelFD = nullptr;
263 for (const auto &Result : DC->lookup(&cudaLaunchKernelII)) {
264 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Result))
265 cudaLaunchKernelFD = FD;
266 }
267
268 if (cudaLaunchKernelFD == nullptr) {
269 CGM.Error(CGF.CurFuncDecl->getLocation(),
270 "Can't find declaration for cudaLaunchKernel()");
271 return;
272 }
273 // Create temporary dim3 grid_dim, block_dim.
274 ParmVarDecl *GridDimParam = cudaLaunchKernelFD->getParamDecl(1);
275 QualType Dim3Ty = GridDimParam->getType();
276 Address GridDim =
277 CGF.CreateMemTemp(Dim3Ty, CharUnits::fromQuantity(8), "grid_dim");
278 Address BlockDim =
279 CGF.CreateMemTemp(Dim3Ty, CharUnits::fromQuantity(8), "block_dim");
280 Address ShmemSize =
281 CGF.CreateTempAlloca(SizeTy, CGM.getSizeAlign(), "shmem_size");
282 Address Stream =
283 CGF.CreateTempAlloca(VoidPtrTy, CGM.getPointerAlign(), "stream");
James Y Knight9871db02019-02-05 16:42:33 +0000284 llvm::FunctionCallee cudaPopConfigFn = CGM.CreateRuntimeFunction(
Artem Belevichc62214d2019-01-31 21:34:03 +0000285 llvm::FunctionType::get(IntTy,
286 {/*gridDim=*/GridDim.getType(),
287 /*blockDim=*/BlockDim.getType(),
288 /*ShmemSize=*/ShmemSize.getType(),
289 /*Stream=*/Stream.getType()},
290 /*isVarArg=*/false),
291 "__cudaPopCallConfiguration");
292
293 CGF.EmitRuntimeCallOrInvoke(cudaPopConfigFn,
294 {GridDim.getPointer(), BlockDim.getPointer(),
295 ShmemSize.getPointer(), Stream.getPointer()});
296
297 // Emit the call to cudaLaunch
298 llvm::Value *Kernel = CGF.Builder.CreatePointerCast(CGF.CurFn, VoidPtrTy);
299 CallArgList LaunchKernelArgs;
300 LaunchKernelArgs.add(RValue::get(Kernel),
301 cudaLaunchKernelFD->getParamDecl(0)->getType());
302 LaunchKernelArgs.add(RValue::getAggregate(GridDim), Dim3Ty);
303 LaunchKernelArgs.add(RValue::getAggregate(BlockDim), Dim3Ty);
304 LaunchKernelArgs.add(RValue::get(KernelArgs.getPointer()),
305 cudaLaunchKernelFD->getParamDecl(3)->getType());
306 LaunchKernelArgs.add(RValue::get(CGF.Builder.CreateLoad(ShmemSize)),
307 cudaLaunchKernelFD->getParamDecl(4)->getType());
308 LaunchKernelArgs.add(RValue::get(CGF.Builder.CreateLoad(Stream)),
309 cudaLaunchKernelFD->getParamDecl(5)->getType());
310
311 QualType QT = cudaLaunchKernelFD->getType();
312 QualType CQT = QT.getCanonicalType();
James Y Knight916db652019-02-02 01:48:23 +0000313 llvm::Type *Ty = CGM.getTypes().ConvertType(CQT);
Artem Belevichc62214d2019-01-31 21:34:03 +0000314 llvm::FunctionType *FTy = dyn_cast<llvm::FunctionType>(Ty);
315
316 const CGFunctionInfo &FI =
317 CGM.getTypes().arrangeFunctionDeclaration(cudaLaunchKernelFD);
James Y Knight9871db02019-02-05 16:42:33 +0000318 llvm::FunctionCallee cudaLaunchKernelFn =
Artem Belevichc62214d2019-01-31 21:34:03 +0000319 CGM.CreateRuntimeFunction(FTy, "cudaLaunchKernel");
320 CGF.EmitCall(FI, CGCallee::forDirect(cudaLaunchKernelFn), ReturnValueSlot(),
321 LaunchKernelArgs);
322 CGF.EmitBranch(EndBlock);
323
324 CGF.EmitBlock(EndBlock);
325}
326
327void CGNVCUDARuntime::emitDeviceStubBodyLegacy(CodeGenFunction &CGF,
328 FunctionArgList &Args) {
Justin Lebare56360a2016-07-27 22:36:21 +0000329 // Emit a call to cudaSetupArgument for each arg in Args.
James Y Knight9871db02019-02-05 16:42:33 +0000330 llvm::FunctionCallee cudaSetupArgFn = getSetupArgumentFn();
Justin Lebare56360a2016-07-27 22:36:21 +0000331 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("setup.end");
332 CharUnits Offset = CharUnits::Zero();
333 for (const VarDecl *A : Args) {
334 CharUnits TyWidth, TyAlign;
335 std::tie(TyWidth, TyAlign) =
336 CGM.getContext().getTypeInfoInChars(A->getType());
337 Offset = Offset.alignTo(TyAlign);
338 llvm::Value *Args[] = {
339 CGF.Builder.CreatePointerCast(CGF.GetAddrOfLocalVar(A).getPointer(),
340 VoidPtrTy),
341 llvm::ConstantInt::get(SizeTy, TyWidth.getQuantity()),
342 llvm::ConstantInt::get(SizeTy, Offset.getQuantity()),
343 };
James Y Knight3933add2019-01-30 02:54:28 +0000344 llvm::CallBase *CB = CGF.EmitRuntimeCallOrInvoke(cudaSetupArgFn, Args);
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000345 llvm::Constant *Zero = llvm::ConstantInt::get(IntTy, 0);
James Y Knight3933add2019-01-30 02:54:28 +0000346 llvm::Value *CBZero = CGF.Builder.CreateICmpEQ(CB, Zero);
Justin Lebare56360a2016-07-27 22:36:21 +0000347 llvm::BasicBlock *NextBlock = CGF.createBasicBlock("setup.next");
James Y Knight3933add2019-01-30 02:54:28 +0000348 CGF.Builder.CreateCondBr(CBZero, NextBlock, EndBlock);
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000349 CGF.EmitBlock(NextBlock);
Justin Lebare56360a2016-07-27 22:36:21 +0000350 Offset += TyWidth;
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000351 }
352
353 // Emit the call to cudaLaunch
James Y Knight9871db02019-02-05 16:42:33 +0000354 llvm::FunctionCallee cudaLaunchFn = getLaunchFn();
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000355 llvm::Value *Arg = CGF.Builder.CreatePointerCast(CGF.CurFn, CharPtrTy);
John McCall882987f2013-02-28 19:01:20 +0000356 CGF.EmitRuntimeCallOrInvoke(cudaLaunchFn, Arg);
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000357 CGF.EmitBranch(EndBlock);
358
359 CGF.EmitBlock(EndBlock);
Peter Collingbournefe883422011-10-06 18:29:37 +0000360}
361
Artem Belevich42e19492016-03-02 18:28:50 +0000362/// Creates a function that sets up state on the host side for CUDA objects that
363/// have a presence on both the host and device sides. Specifically, registers
364/// the host side of kernel functions and device global variables with the CUDA
365/// runtime.
Artem Belevich52cc4872015-05-07 19:34:16 +0000366/// \code
Artem Belevich42e19492016-03-02 18:28:50 +0000367/// void __cuda_register_globals(void** GpuBinaryHandle) {
Artem Belevich52cc4872015-05-07 19:34:16 +0000368/// __cudaRegisterFunction(GpuBinaryHandle,Kernel0,...);
369/// ...
370/// __cudaRegisterFunction(GpuBinaryHandle,KernelM,...);
Artem Belevich42e19492016-03-02 18:28:50 +0000371/// __cudaRegisterVar(GpuBinaryHandle, GlobalVar0, ...);
372/// ...
373/// __cudaRegisterVar(GpuBinaryHandle, GlobalVarN, ...);
Artem Belevich52cc4872015-05-07 19:34:16 +0000374/// }
375/// \endcode
Artem Belevich42e19492016-03-02 18:28:50 +0000376llvm::Function *CGNVCUDARuntime::makeRegisterGlobalsFn() {
Artem Belevich8c1ec1e2016-03-02 18:28:53 +0000377 // No need to register anything
378 if (EmittedKernels.empty() && DeviceVars.empty())
379 return nullptr;
380
Artem Belevich52cc4872015-05-07 19:34:16 +0000381 llvm::Function *RegisterKernelsFunc = llvm::Function::Create(
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000382 getRegisterGlobalsFnTy(), llvm::GlobalValue::InternalLinkage,
Yaxun Liu887c5692018-04-25 01:10:37 +0000383 addUnderscoredPrefixToName("_register_globals"), &TheModule);
Artem Belevich52cc4872015-05-07 19:34:16 +0000384 llvm::BasicBlock *EntryBB =
385 llvm::BasicBlock::Create(Context, "entry", RegisterKernelsFunc);
John McCall7f416cc2015-09-08 08:05:57 +0000386 CGBuilderTy Builder(CGM, Context);
Artem Belevich52cc4872015-05-07 19:34:16 +0000387 Builder.SetInsertPoint(EntryBB);
388
389 // void __cudaRegisterFunction(void **, const char *, char *, const char *,
390 // int, uint3*, uint3*, dim3*, dim3*, int*)
Benjamin Kramer6d1c10b2016-07-02 12:03:57 +0000391 llvm::Type *RegisterFuncParams[] = {
Artem Belevich52cc4872015-05-07 19:34:16 +0000392 VoidPtrPtrTy, CharPtrTy, CharPtrTy, CharPtrTy, IntTy,
393 VoidPtrTy, VoidPtrTy, VoidPtrTy, VoidPtrTy, IntTy->getPointerTo()};
James Y Knight9871db02019-02-05 16:42:33 +0000394 llvm::FunctionCallee RegisterFunc = CGM.CreateRuntimeFunction(
Artem Belevich52cc4872015-05-07 19:34:16 +0000395 llvm::FunctionType::get(IntTy, RegisterFuncParams, false),
Yaxun Liu887c5692018-04-25 01:10:37 +0000396 addUnderscoredPrefixToName("RegisterFunction"));
Artem Belevich52cc4872015-05-07 19:34:16 +0000397
398 // Extract GpuBinaryHandle passed as the first argument passed to
Artem Belevich42e19492016-03-02 18:28:50 +0000399 // __cuda_register_globals() and generate __cudaRegisterFunction() call for
Artem Belevich52cc4872015-05-07 19:34:16 +0000400 // each emitted kernel.
401 llvm::Argument &GpuBinaryHandlePtr = *RegisterKernelsFunc->arg_begin();
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000402 for (auto &&I : EmittedKernels) {
403 llvm::Constant *KernelName = makeConstantString(getDeviceSideName(I.D));
Artem Belevich52cc4872015-05-07 19:34:16 +0000404 llvm::Constant *NullPtr = llvm::ConstantPointerNull::get(VoidPtrTy);
Artem Belevich42e19492016-03-02 18:28:50 +0000405 llvm::Value *Args[] = {
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000406 &GpuBinaryHandlePtr,
407 Builder.CreateBitCast(I.Kernel, VoidPtrTy),
408 KernelName,
409 KernelName,
410 llvm::ConstantInt::get(IntTy, -1),
411 NullPtr,
412 NullPtr,
413 NullPtr,
414 NullPtr,
Artem Belevich52cc4872015-05-07 19:34:16 +0000415 llvm::ConstantPointerNull::get(IntTy->getPointerTo())};
Artem Belevich42e19492016-03-02 18:28:50 +0000416 Builder.CreateCall(RegisterFunc, Args);
417 }
418
419 // void __cudaRegisterVar(void **, char *, char *, const char *,
420 // int, int, int, int)
Benjamin Kramer6d1c10b2016-07-02 12:03:57 +0000421 llvm::Type *RegisterVarParams[] = {VoidPtrPtrTy, CharPtrTy, CharPtrTy,
422 CharPtrTy, IntTy, IntTy,
423 IntTy, IntTy};
James Y Knight9871db02019-02-05 16:42:33 +0000424 llvm::FunctionCallee RegisterVar = CGM.CreateRuntimeFunction(
Artem Belevich42e19492016-03-02 18:28:50 +0000425 llvm::FunctionType::get(IntTy, RegisterVarParams, false),
Yaxun Liu887c5692018-04-25 01:10:37 +0000426 addUnderscoredPrefixToName("RegisterVar"));
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000427 for (auto &&Info : DeviceVars) {
428 llvm::GlobalVariable *Var = Info.Var;
429 unsigned Flags = Info.Flag;
430 llvm::Constant *VarName = makeConstantString(getDeviceSideName(Info.D));
Artem Belevich42e19492016-03-02 18:28:50 +0000431 uint64_t VarSize =
432 CGM.getDataLayout().getTypeAllocSize(Var->getValueType());
433 llvm::Value *Args[] = {
434 &GpuBinaryHandlePtr,
435 Builder.CreateBitCast(Var, VoidPtrTy),
436 VarName,
437 VarName,
438 llvm::ConstantInt::get(IntTy, (Flags & ExternDeviceVar) ? 1 : 0),
439 llvm::ConstantInt::get(IntTy, VarSize),
440 llvm::ConstantInt::get(IntTy, (Flags & ConstantDeviceVar) ? 1 : 0),
441 llvm::ConstantInt::get(IntTy, 0)};
442 Builder.CreateCall(RegisterVar, Args);
Artem Belevich52cc4872015-05-07 19:34:16 +0000443 }
444
445 Builder.CreateRetVoid();
446 return RegisterKernelsFunc;
447}
448
449/// Creates a global constructor function for the module:
Yaxun Liuf99752b2018-07-20 22:45:24 +0000450///
451/// For CUDA:
Artem Belevich52cc4872015-05-07 19:34:16 +0000452/// \code
453/// void __cuda_module_ctor(void*) {
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000454/// Handle = __cudaRegisterFatBinary(GpuBinaryBlob);
455/// __cuda_register_globals(Handle);
Artem Belevich52cc4872015-05-07 19:34:16 +0000456/// }
457/// \endcode
Yaxun Liuf99752b2018-07-20 22:45:24 +0000458///
459/// For HIP:
460/// \code
461/// void __hip_module_ctor(void*) {
462/// if (__hip_gpubin_handle == 0) {
463/// __hip_gpubin_handle = __hipRegisterFatBinary(GpuBinaryBlob);
464/// __hip_register_globals(__hip_gpubin_handle);
465/// }
466/// }
467/// \endcode
Artem Belevich52cc4872015-05-07 19:34:16 +0000468llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
Yaxun Liu29155b02018-05-18 15:07:56 +0000469 bool IsHIP = CGM.getLangOpts().HIP;
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000470 // No need to generate ctors/dtors if there is no GPU binary.
Yaxun Liu29155b02018-05-18 15:07:56 +0000471 StringRef CudaGpuBinaryFileName = CGM.getCodeGenOpts().CudaGpuBinaryFileName;
472 if (CudaGpuBinaryFileName.empty() && !IsHIP)
Artem Belevich8c1ec1e2016-03-02 18:28:53 +0000473 return nullptr;
474
Yaxun Liu29155b02018-05-18 15:07:56 +0000475 // void __{cuda|hip}_register_globals(void* handle);
Artem Belevich42e19492016-03-02 18:28:50 +0000476 llvm::Function *RegisterGlobalsFunc = makeRegisterGlobalsFn();
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000477 // We always need a function to pass in as callback. Create a dummy
478 // implementation if we don't need to register anything.
479 if (RelocatableDeviceCode && !RegisterGlobalsFunc)
480 RegisterGlobalsFunc = makeDummyFunction(getRegisterGlobalsFnTy());
481
Yaxun Liu29155b02018-05-18 15:07:56 +0000482 // void ** __{cuda|hip}RegisterFatBinary(void *);
James Y Knight9871db02019-02-05 16:42:33 +0000483 llvm::FunctionCallee RegisterFatbinFunc = CGM.CreateRuntimeFunction(
Artem Belevich52cc4872015-05-07 19:34:16 +0000484 llvm::FunctionType::get(VoidPtrPtrTy, VoidPtrTy, false),
Yaxun Liu887c5692018-04-25 01:10:37 +0000485 addUnderscoredPrefixToName("RegisterFatBinary"));
Artem Belevich52cc4872015-05-07 19:34:16 +0000486 // struct { int magic, int version, void * gpu_binary, void * dont_care };
487 llvm::StructType *FatbinWrapperTy =
Serge Guelton1d993272017-05-09 19:31:30 +0000488 llvm::StructType::get(IntTy, IntTy, VoidPtrTy, VoidPtrTy);
Artem Belevich52cc4872015-05-07 19:34:16 +0000489
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000490 // Register GPU binary with the CUDA runtime, store returned handle in a
491 // global variable and save a reference in GpuBinaryHandle to be cleaned up
492 // in destructor on exit. Then associate all known kernels with the GPU binary
493 // handle so CUDA runtime can figure out what to call on the GPU side.
Yaxun Liu97670892018-10-02 17:48:54 +0000494 std::unique_ptr<llvm::MemoryBuffer> CudaGpuBinary = nullptr;
495 if (!CudaGpuBinaryFileName.empty()) {
Yaxun Liu29155b02018-05-18 15:07:56 +0000496 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CudaGpuBinaryOrErr =
497 llvm::MemoryBuffer::getFileOrSTDIN(CudaGpuBinaryFileName);
498 if (std::error_code EC = CudaGpuBinaryOrErr.getError()) {
499 CGM.getDiags().Report(diag::err_cannot_open_file)
500 << CudaGpuBinaryFileName << EC.message();
501 return nullptr;
502 }
503 CudaGpuBinary = std::move(CudaGpuBinaryOrErr.get());
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000504 }
505
Artem Belevich52cc4872015-05-07 19:34:16 +0000506 llvm::Function *ModuleCtorFunc = llvm::Function::Create(
507 llvm::FunctionType::get(VoidTy, VoidPtrTy, false),
Yaxun Liu887c5692018-04-25 01:10:37 +0000508 llvm::GlobalValue::InternalLinkage,
509 addUnderscoredPrefixToName("_module_ctor"), &TheModule);
Artem Belevich52cc4872015-05-07 19:34:16 +0000510 llvm::BasicBlock *CtorEntryBB =
511 llvm::BasicBlock::Create(Context, "entry", ModuleCtorFunc);
John McCall7f416cc2015-09-08 08:05:57 +0000512 CGBuilderTy CtorBuilder(CGM, Context);
Artem Belevich52cc4872015-05-07 19:34:16 +0000513
514 CtorBuilder.SetInsertPoint(CtorEntryBB);
515
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000516 const char *FatbinConstantName;
Yaxun Liu29155b02018-05-18 15:07:56 +0000517 const char *FatbinSectionName;
518 const char *ModuleIDSectionName;
519 StringRef ModuleIDPrefix;
520 llvm::Constant *FatBinStr;
521 unsigned FatMagic;
522 if (IsHIP) {
523 FatbinConstantName = ".hip_fatbin";
524 FatbinSectionName = ".hipFatBinSegment";
525
526 ModuleIDSectionName = "__hip_module_id";
527 ModuleIDPrefix = "__hip_";
528
Yaxun Liu97670892018-10-02 17:48:54 +0000529 if (CudaGpuBinary) {
530 // If fatbin is available from early finalization, create a string
531 // literal containing the fat binary loaded from the given file.
532 FatBinStr = makeConstantString(CudaGpuBinary->getBuffer(), "",
533 FatbinConstantName, 8);
534 } else {
535 // If fatbin is not available, create an external symbol
536 // __hip_fatbin in section .hip_fatbin. The external symbol is supposed
537 // to contain the fat binary but will be populated somewhere else,
538 // e.g. by lld through link script.
539 FatBinStr = new llvm::GlobalVariable(
Yaxun Liu29155b02018-05-18 15:07:56 +0000540 CGM.getModule(), CGM.Int8Ty,
541 /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, nullptr,
542 "__hip_fatbin", nullptr,
543 llvm::GlobalVariable::NotThreadLocal);
Yaxun Liu97670892018-10-02 17:48:54 +0000544 cast<llvm::GlobalVariable>(FatBinStr)->setSection(FatbinConstantName);
545 }
Yaxun Liu29155b02018-05-18 15:07:56 +0000546
547 FatMagic = HIPFatMagic;
548 } else {
549 if (RelocatableDeviceCode)
Artem Belevich5ce0a082018-06-28 17:15:52 +0000550 FatbinConstantName = CGM.getTriple().isMacOSX()
551 ? "__NV_CUDA,__nv_relfatbin"
552 : "__nv_relfatbin";
Yaxun Liu29155b02018-05-18 15:07:56 +0000553 else
554 FatbinConstantName =
555 CGM.getTriple().isMacOSX() ? "__NV_CUDA,__nv_fatbin" : ".nv_fatbin";
556 // NVIDIA's cuobjdump looks for fatbins in this section.
557 FatbinSectionName =
558 CGM.getTriple().isMacOSX() ? "__NV_CUDA,__fatbin" : ".nvFatBinSegment";
559
Artem Belevich5ce0a082018-06-28 17:15:52 +0000560 ModuleIDSectionName = CGM.getTriple().isMacOSX()
561 ? "__NV_CUDA,__nv_module_id"
562 : "__nv_module_id";
Yaxun Liu29155b02018-05-18 15:07:56 +0000563 ModuleIDPrefix = "__nv_";
564
565 // For CUDA, create a string literal containing the fat binary loaded from
566 // the given file.
567 FatBinStr = makeConstantString(CudaGpuBinary->getBuffer(), "",
568 FatbinConstantName, 8);
569 FatMagic = CudaFatMagic;
570 }
Artem Belevich52cc4872015-05-07 19:34:16 +0000571
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000572 // Create initialized wrapper structure that points to the loaded GPU binary
573 ConstantInitBuilder Builder(CGM);
574 auto Values = Builder.beginStruct(FatbinWrapperTy);
575 // Fatbin wrapper magic.
Yaxun Liu29155b02018-05-18 15:07:56 +0000576 Values.addInt(IntTy, FatMagic);
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000577 // Fatbin version.
578 Values.addInt(IntTy, 1);
579 // Data.
Yaxun Liu29155b02018-05-18 15:07:56 +0000580 Values.add(FatBinStr);
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000581 // Unused in fatbin v1.
582 Values.add(llvm::ConstantPointerNull::get(VoidPtrTy));
583 llvm::GlobalVariable *FatbinWrapper = Values.finishAndCreateGlobal(
Yaxun Liu887c5692018-04-25 01:10:37 +0000584 addUnderscoredPrefixToName("_fatbin_wrapper"), CGM.getPointerAlign(),
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000585 /*constant*/ true);
586 FatbinWrapper->setSection(FatbinSectionName);
Justin Lebard14fe882016-11-18 00:41:31 +0000587
Yaxun Liuf99752b2018-07-20 22:45:24 +0000588 // There is only one HIP fat binary per linked module, however there are
589 // multiple constructor functions. Make sure the fat binary is registered
590 // only once. The constructor functions are executed by the dynamic loader
591 // before the program gains control. The dynamic loader cannot execute the
592 // constructor functions concurrently since doing that would not guarantee
593 // thread safety of the loaded program. Therefore we can assume sequential
594 // execution of constructor functions here.
595 if (IsHIP) {
Yaxun Liu97670892018-10-02 17:48:54 +0000596 auto Linkage = CudaGpuBinary ? llvm::GlobalValue::InternalLinkage :
597 llvm::GlobalValue::LinkOnceAnyLinkage;
Yaxun Liuf99752b2018-07-20 22:45:24 +0000598 llvm::BasicBlock *IfBlock =
599 llvm::BasicBlock::Create(Context, "if", ModuleCtorFunc);
600 llvm::BasicBlock *ExitBlock =
601 llvm::BasicBlock::Create(Context, "exit", ModuleCtorFunc);
602 // The name, size, and initialization pattern of this variable is part
603 // of HIP ABI.
604 GpuBinaryHandle = new llvm::GlobalVariable(
605 TheModule, VoidPtrPtrTy, /*isConstant=*/false,
Yaxun Liu97670892018-10-02 17:48:54 +0000606 Linkage,
Yaxun Liuf99752b2018-07-20 22:45:24 +0000607 /*Initializer=*/llvm::ConstantPointerNull::get(VoidPtrPtrTy),
608 "__hip_gpubin_handle");
609 GpuBinaryHandle->setAlignment(CGM.getPointerAlign().getQuantity());
Yaxun Liu94ff57f2018-08-17 17:47:31 +0000610 // Prevent the weak symbol in different shared libraries being merged.
Yaxun Liu97670892018-10-02 17:48:54 +0000611 if (Linkage != llvm::GlobalValue::InternalLinkage)
612 GpuBinaryHandle->setVisibility(llvm::GlobalValue::HiddenVisibility);
Yaxun Liuf99752b2018-07-20 22:45:24 +0000613 Address GpuBinaryAddr(
614 GpuBinaryHandle,
615 CharUnits::fromQuantity(GpuBinaryHandle->getAlignment()));
616 {
617 auto HandleValue = CtorBuilder.CreateLoad(GpuBinaryAddr);
618 llvm::Constant *Zero =
619 llvm::Constant::getNullValue(HandleValue->getType());
620 llvm::Value *EQZero = CtorBuilder.CreateICmpEQ(HandleValue, Zero);
621 CtorBuilder.CreateCondBr(EQZero, IfBlock, ExitBlock);
622 }
623 {
624 CtorBuilder.SetInsertPoint(IfBlock);
625 // GpuBinaryHandle = __hipRegisterFatBinary(&FatbinWrapper);
626 llvm::CallInst *RegisterFatbinCall = CtorBuilder.CreateCall(
627 RegisterFatbinFunc,
628 CtorBuilder.CreateBitCast(FatbinWrapper, VoidPtrTy));
629 CtorBuilder.CreateStore(RegisterFatbinCall, GpuBinaryAddr);
630 CtorBuilder.CreateBr(ExitBlock);
631 }
632 {
633 CtorBuilder.SetInsertPoint(ExitBlock);
634 // Call __hip_register_globals(GpuBinaryHandle);
635 if (RegisterGlobalsFunc) {
636 auto HandleValue = CtorBuilder.CreateLoad(GpuBinaryAddr);
637 CtorBuilder.CreateCall(RegisterGlobalsFunc, HandleValue);
638 }
639 }
640 } else if (!RelocatableDeviceCode) {
641 // Register binary with CUDA runtime. This is substantially different in
642 // default mode vs. separate compilation!
643 // GpuBinaryHandle = __cudaRegisterFatBinary(&FatbinWrapper);
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000644 llvm::CallInst *RegisterFatbinCall = CtorBuilder.CreateCall(
645 RegisterFatbinFunc,
646 CtorBuilder.CreateBitCast(FatbinWrapper, VoidPtrTy));
647 GpuBinaryHandle = new llvm::GlobalVariable(
648 TheModule, VoidPtrPtrTy, false, llvm::GlobalValue::InternalLinkage,
Yaxun Liuf99752b2018-07-20 22:45:24 +0000649 llvm::ConstantPointerNull::get(VoidPtrPtrTy), "__cuda_gpubin_handle");
650 GpuBinaryHandle->setAlignment(CGM.getPointerAlign().getQuantity());
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000651 CtorBuilder.CreateAlignedStore(RegisterFatbinCall, GpuBinaryHandle,
652 CGM.getPointerAlign());
Artem Belevich52cc4872015-05-07 19:34:16 +0000653
Yaxun Liuf99752b2018-07-20 22:45:24 +0000654 // Call __cuda_register_globals(GpuBinaryHandle);
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000655 if (RegisterGlobalsFunc)
656 CtorBuilder.CreateCall(RegisterGlobalsFunc, RegisterFatbinCall);
Artem Belevich40717632019-02-05 22:38:58 +0000657
658 // Call __cudaRegisterFatBinaryEnd(Handle) if this CUDA version needs it.
659 if (CudaFeatureEnabled(CGM.getTarget().getSDKVersion(),
660 CudaFeature::CUDA_USES_FATBIN_REGISTER_END)) {
661 // void __cudaRegisterFatBinaryEnd(void **);
662 llvm::FunctionCallee RegisterFatbinEndFunc = CGM.CreateRuntimeFunction(
663 llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false),
664 "__cudaRegisterFatBinaryEnd");
665 CtorBuilder.CreateCall(RegisterFatbinEndFunc, RegisterFatbinCall);
666 }
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000667 } else {
668 // Generate a unique module ID.
Yaxun Liu29155b02018-05-18 15:07:56 +0000669 SmallString<64> ModuleID;
670 llvm::raw_svector_ostream OS(ModuleID);
Artem Belevich93552b32018-10-05 18:39:58 +0000671 OS << ModuleIDPrefix << llvm::format("%" PRIx64, FatbinWrapper->getGUID());
Yaxun Liu29155b02018-05-18 15:07:56 +0000672 llvm::Constant *ModuleIDConstant =
673 makeConstantString(ModuleID.str(), "", ModuleIDSectionName, 32);
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000674
Yaxun Liuf99752b2018-07-20 22:45:24 +0000675 // Create an alias for the FatbinWrapper that nvcc will look for.
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000676 llvm::GlobalAlias::create(llvm::GlobalValue::ExternalLinkage,
Yaxun Liu29155b02018-05-18 15:07:56 +0000677 Twine("__fatbinwrap") + ModuleID, FatbinWrapper);
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000678
Yaxun Liuf99752b2018-07-20 22:45:24 +0000679 // void __cudaRegisterLinkedBinary%ModuleID%(void (*)(void *), void *,
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000680 // void *, void (*)(void **))
Yaxun Liuf99752b2018-07-20 22:45:24 +0000681 SmallString<128> RegisterLinkedBinaryName("__cudaRegisterLinkedBinary");
Yaxun Liu29155b02018-05-18 15:07:56 +0000682 RegisterLinkedBinaryName += ModuleID;
James Y Knight9871db02019-02-05 16:42:33 +0000683 llvm::FunctionCallee RegisterLinkedBinaryFunc = CGM.CreateRuntimeFunction(
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000684 getRegisterLinkedBinaryFnTy(), RegisterLinkedBinaryName);
685
686 assert(RegisterGlobalsFunc && "Expecting at least dummy function!");
687 llvm::Value *Args[] = {RegisterGlobalsFunc,
688 CtorBuilder.CreateBitCast(FatbinWrapper, VoidPtrTy),
Yaxun Liu29155b02018-05-18 15:07:56 +0000689 ModuleIDConstant,
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000690 makeDummyFunction(getCallbackFnTy())};
691 CtorBuilder.CreateCall(RegisterLinkedBinaryFunc, Args);
692 }
Artem Belevich52cc4872015-05-07 19:34:16 +0000693
Artem Belevichc66d2542018-06-27 18:32:51 +0000694 // Create destructor and register it with atexit() the way NVCC does it. Doing
695 // it during regular destructor phase worked in CUDA before 9.2 but results in
696 // double-free in 9.2.
697 if (llvm::Function *CleanupFn = makeModuleDtorFunction()) {
698 // extern "C" int atexit(void (*f)(void));
699 llvm::FunctionType *AtExitTy =
700 llvm::FunctionType::get(IntTy, CleanupFn->getType(), false);
James Y Knight9871db02019-02-05 16:42:33 +0000701 llvm::FunctionCallee AtExitFunc =
Artem Belevichc66d2542018-06-27 18:32:51 +0000702 CGM.CreateRuntimeFunction(AtExitTy, "atexit", llvm::AttributeList(),
703 /*Local=*/true);
704 CtorBuilder.CreateCall(AtExitFunc, CleanupFn);
705 }
706
Artem Belevich52cc4872015-05-07 19:34:16 +0000707 CtorBuilder.CreateRetVoid();
708 return ModuleCtorFunc;
709}
710
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000711/// Creates a global destructor function that unregisters the GPU code blob
Artem Belevich52cc4872015-05-07 19:34:16 +0000712/// registered by constructor.
Yaxun Liuf99752b2018-07-20 22:45:24 +0000713///
714/// For CUDA:
Artem Belevich52cc4872015-05-07 19:34:16 +0000715/// \code
716/// void __cuda_module_dtor(void*) {
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000717/// __cudaUnregisterFatBinary(Handle);
Artem Belevich52cc4872015-05-07 19:34:16 +0000718/// }
719/// \endcode
Yaxun Liuf99752b2018-07-20 22:45:24 +0000720///
721/// For HIP:
722/// \code
723/// void __hip_module_dtor(void*) {
724/// if (__hip_gpubin_handle) {
725/// __hipUnregisterFatBinary(__hip_gpubin_handle);
726/// __hip_gpubin_handle = 0;
727/// }
728/// }
729/// \endcode
Artem Belevich52cc4872015-05-07 19:34:16 +0000730llvm::Function *CGNVCUDARuntime::makeModuleDtorFunction() {
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000731 // No need for destructor if we don't have a handle to unregister.
732 if (!GpuBinaryHandle)
Artem Belevich8c1ec1e2016-03-02 18:28:53 +0000733 return nullptr;
734
Artem Belevich52cc4872015-05-07 19:34:16 +0000735 // void __cudaUnregisterFatBinary(void ** handle);
James Y Knight9871db02019-02-05 16:42:33 +0000736 llvm::FunctionCallee UnregisterFatbinFunc = CGM.CreateRuntimeFunction(
Artem Belevich52cc4872015-05-07 19:34:16 +0000737 llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false),
Yaxun Liu887c5692018-04-25 01:10:37 +0000738 addUnderscoredPrefixToName("UnregisterFatBinary"));
Artem Belevich52cc4872015-05-07 19:34:16 +0000739
740 llvm::Function *ModuleDtorFunc = llvm::Function::Create(
741 llvm::FunctionType::get(VoidTy, VoidPtrTy, false),
Yaxun Liu887c5692018-04-25 01:10:37 +0000742 llvm::GlobalValue::InternalLinkage,
743 addUnderscoredPrefixToName("_module_dtor"), &TheModule);
744
Artem Belevich52cc4872015-05-07 19:34:16 +0000745 llvm::BasicBlock *DtorEntryBB =
746 llvm::BasicBlock::Create(Context, "entry", ModuleDtorFunc);
John McCall7f416cc2015-09-08 08:05:57 +0000747 CGBuilderTy DtorBuilder(CGM, Context);
Artem Belevich52cc4872015-05-07 19:34:16 +0000748 DtorBuilder.SetInsertPoint(DtorEntryBB);
749
Yaxun Liuf99752b2018-07-20 22:45:24 +0000750 Address GpuBinaryAddr(GpuBinaryHandle, CharUnits::fromQuantity(
751 GpuBinaryHandle->getAlignment()));
752 auto HandleValue = DtorBuilder.CreateLoad(GpuBinaryAddr);
753 // There is only one HIP fat binary per linked module, however there are
754 // multiple destructor functions. Make sure the fat binary is unregistered
755 // only once.
756 if (CGM.getLangOpts().HIP) {
757 llvm::BasicBlock *IfBlock =
758 llvm::BasicBlock::Create(Context, "if", ModuleDtorFunc);
759 llvm::BasicBlock *ExitBlock =
760 llvm::BasicBlock::Create(Context, "exit", ModuleDtorFunc);
761 llvm::Constant *Zero = llvm::Constant::getNullValue(HandleValue->getType());
762 llvm::Value *NEZero = DtorBuilder.CreateICmpNE(HandleValue, Zero);
763 DtorBuilder.CreateCondBr(NEZero, IfBlock, ExitBlock);
Artem Belevich52cc4872015-05-07 19:34:16 +0000764
Yaxun Liuf99752b2018-07-20 22:45:24 +0000765 DtorBuilder.SetInsertPoint(IfBlock);
766 DtorBuilder.CreateCall(UnregisterFatbinFunc, HandleValue);
767 DtorBuilder.CreateStore(Zero, GpuBinaryAddr);
768 DtorBuilder.CreateBr(ExitBlock);
769
770 DtorBuilder.SetInsertPoint(ExitBlock);
771 } else {
772 DtorBuilder.CreateCall(UnregisterFatbinFunc, HandleValue);
773 }
Artem Belevich52cc4872015-05-07 19:34:16 +0000774 DtorBuilder.CreateRetVoid();
775 return ModuleDtorFunc;
776}
777
Peter Collingbournefe883422011-10-06 18:29:37 +0000778CGCUDARuntime *CodeGen::CreateNVCUDARuntime(CodeGenModule &CGM) {
779 return new CGNVCUDARuntime(CGM);
780}