blob: 4d4038dae9cfcde267314d2dcd20e8f75fddeae5 [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;
Michael Liaoe40f8792019-06-17 12:51:36 +0000135 /// Construct and return the stub name of a kernel.
136 std::string getDeviceStubName(llvm::StringRef Name) const override;
Peter Collingbournefe883422011-10-06 18:29:37 +0000137};
138
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000139}
Peter Collingbournefe883422011-10-06 18:29:37 +0000140
Yaxun Liu887c5692018-04-25 01:10:37 +0000141std::string CGNVCUDARuntime::addPrefixToName(StringRef FuncName) const {
142 if (CGM.getLangOpts().HIP)
143 return ((Twine("hip") + Twine(FuncName)).str());
144 return ((Twine("cuda") + Twine(FuncName)).str());
145}
146std::string
147CGNVCUDARuntime::addUnderscoredPrefixToName(StringRef FuncName) const {
148 if (CGM.getLangOpts().HIP)
149 return ((Twine("__hip") + Twine(FuncName)).str());
150 return ((Twine("__cuda") + Twine(FuncName)).str());
151}
152
Artem Belevich52cc4872015-05-07 19:34:16 +0000153CGNVCUDARuntime::CGNVCUDARuntime(CodeGenModule &CGM)
154 : CGCUDARuntime(CGM), Context(CGM.getLLVMContext()),
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000155 TheModule(CGM.getModule()),
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000156 RelocatableDeviceCode(CGM.getLangOpts().GPURelocatableDeviceCode),
157 DeviceMC(CGM.getContext().createMangleContext(
158 CGM.getContext().getAuxTargetInfo())) {
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000159 CodeGen::CodeGenTypes &Types = CGM.getTypes();
160 ASTContext &Ctx = CGM.getContext();
161
John McCall6c9f1fdb2016-11-19 08:17:24 +0000162 IntTy = CGM.IntTy;
163 SizeTy = CGM.SizeTy;
164 VoidTy = CGM.VoidTy;
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000165
166 CharPtrTy = llvm::PointerType::getUnqual(Types.ConvertType(Ctx.CharTy));
167 VoidPtrTy = cast<llvm::PointerType>(Types.ConvertType(Ctx.VoidPtrTy));
Artem Belevich52cc4872015-05-07 19:34:16 +0000168 VoidPtrPtrTy = VoidPtrTy->getPointerTo();
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000169}
170
James Y Knight9871db02019-02-05 16:42:33 +0000171llvm::FunctionCallee CGNVCUDARuntime::getSetupArgumentFn() const {
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000172 // cudaError_t cudaSetupArgument(void *, size_t, size_t)
Benjamin Kramer30934732016-07-02 11:41:41 +0000173 llvm::Type *Params[] = {VoidPtrTy, SizeTy, SizeTy};
Yaxun Liu887c5692018-04-25 01:10:37 +0000174 return CGM.CreateRuntimeFunction(
175 llvm::FunctionType::get(IntTy, Params, false),
176 addPrefixToName("SetupArgument"));
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000177}
178
James Y Knight9871db02019-02-05 16:42:33 +0000179llvm::FunctionCallee CGNVCUDARuntime::getLaunchFn() const {
Yaxun Liu887c5692018-04-25 01:10:37 +0000180 if (CGM.getLangOpts().HIP) {
181 // hipError_t hipLaunchByPtr(char *);
182 return CGM.CreateRuntimeFunction(
183 llvm::FunctionType::get(IntTy, CharPtrTy, false), "hipLaunchByPtr");
184 } else {
185 // cudaError_t cudaLaunch(char *);
186 return CGM.CreateRuntimeFunction(
187 llvm::FunctionType::get(IntTy, CharPtrTy, false), "cudaLaunch");
188 }
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000189}
190
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000191llvm::FunctionType *CGNVCUDARuntime::getRegisterGlobalsFnTy() const {
192 return llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false);
193}
194
195llvm::FunctionType *CGNVCUDARuntime::getCallbackFnTy() const {
196 return llvm::FunctionType::get(VoidTy, VoidPtrTy, false);
197}
198
199llvm::FunctionType *CGNVCUDARuntime::getRegisterLinkedBinaryFnTy() const {
200 auto CallbackFnTy = getCallbackFnTy();
201 auto RegisterGlobalsFnTy = getRegisterGlobalsFnTy();
202 llvm::Type *Params[] = {RegisterGlobalsFnTy->getPointerTo(), VoidPtrTy,
203 VoidPtrTy, CallbackFnTy->getPointerTo()};
204 return llvm::FunctionType::get(VoidTy, Params, false);
205}
206
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000207std::string CGNVCUDARuntime::getDeviceSideName(const Decl *D) {
208 auto *ND = cast<const NamedDecl>(D);
209 std::string DeviceSideName;
210 if (DeviceMC->shouldMangleDeclName(ND)) {
211 SmallString<256> Buffer;
212 llvm::raw_svector_ostream Out(Buffer);
213 DeviceMC->mangleName(ND, Out);
214 DeviceSideName = Out.str();
215 } else
216 DeviceSideName = ND->getIdentifier()->getName();
217 return DeviceSideName;
218}
219
Artem Belevich52cc4872015-05-07 19:34:16 +0000220void CGNVCUDARuntime::emitDeviceStub(CodeGenFunction &CGF,
221 FunctionArgList &Args) {
Michael Liaoe40f8792019-06-17 12:51:36 +0000222 // Ensure either we have different ABIs between host and device compilations,
223 // says host compilation following MSVC ABI but device compilation follows
224 // Itanium C++ ABI or, if they follow the same ABI, kernel names after
225 // mangling should be the same after name stubbing. The later checking is
226 // very important as the device kernel name being mangled in host-compilation
227 // is used to resolve the device binaries to be executed. Inconsistent naming
228 // result in undefined behavior. Even though we cannot check that naming
229 // directly between host- and device-compilations, the host- and
230 // device-mangling in host compilation could help catching certain ones.
231 assert((CGF.CGM.getContext().getAuxTargetInfo() &&
232 (CGF.CGM.getContext().getAuxTargetInfo()->getCXXABI() !=
233 CGF.CGM.getContext().getTargetInfo().getCXXABI())) ||
234 getDeviceStubName(getDeviceSideName(CGF.CurFuncDecl)) ==
235 CGF.CurFn->getName());
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000236
237 EmittedKernels.push_back({CGF.CurFn, CGF.CurFuncDecl});
Artem Belevichc62214d2019-01-31 21:34:03 +0000238 if (CudaFeatureEnabled(CGM.getTarget().getSDKVersion(),
239 CudaFeature::CUDA_USES_NEW_LAUNCH))
240 emitDeviceStubBodyNew(CGF, Args);
241 else
242 emitDeviceStubBodyLegacy(CGF, Args);
Artem Belevich52cc4872015-05-07 19:34:16 +0000243}
244
Artem Belevichc62214d2019-01-31 21:34:03 +0000245// CUDA 9.0+ uses new way to launch kernels. Parameters are packed in a local
246// array and kernels are launched using cudaLaunchKernel().
247void CGNVCUDARuntime::emitDeviceStubBodyNew(CodeGenFunction &CGF,
248 FunctionArgList &Args) {
249 // Build the shadow stack entry at the very start of the function.
250
251 // Calculate amount of space we will need for all arguments. If we have no
252 // args, allocate a single pointer so we still have a valid pointer to the
253 // argument array that we can pass to runtime, even if it will be unused.
254 Address KernelArgs = CGF.CreateTempAlloca(
255 VoidPtrTy, CharUnits::fromQuantity(16), "kernel_args",
256 llvm::ConstantInt::get(SizeTy, std::max<size_t>(1, Args.size())));
257 // Store pointers to the arguments in a locally allocated launch_args.
258 for (unsigned i = 0; i < Args.size(); ++i) {
259 llvm::Value* VarPtr = CGF.GetAddrOfLocalVar(Args[i]).getPointer();
260 llvm::Value *VoidVarPtr = CGF.Builder.CreatePointerCast(VarPtr, VoidPtrTy);
261 CGF.Builder.CreateDefaultAlignedStore(
262 VoidVarPtr, CGF.Builder.CreateConstGEP1_32(KernelArgs.getPointer(), i));
263 }
264
265 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("setup.end");
266
267 // Lookup cudaLaunchKernel function.
268 // cudaError_t cudaLaunchKernel(const void *func, dim3 gridDim, dim3 blockDim,
269 // void **args, size_t sharedMem,
270 // cudaStream_t stream);
271 TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl();
272 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
273 IdentifierInfo &cudaLaunchKernelII =
274 CGM.getContext().Idents.get("cudaLaunchKernel");
275 FunctionDecl *cudaLaunchKernelFD = nullptr;
276 for (const auto &Result : DC->lookup(&cudaLaunchKernelII)) {
277 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Result))
278 cudaLaunchKernelFD = FD;
279 }
280
281 if (cudaLaunchKernelFD == nullptr) {
282 CGM.Error(CGF.CurFuncDecl->getLocation(),
283 "Can't find declaration for cudaLaunchKernel()");
284 return;
285 }
286 // Create temporary dim3 grid_dim, block_dim.
287 ParmVarDecl *GridDimParam = cudaLaunchKernelFD->getParamDecl(1);
288 QualType Dim3Ty = GridDimParam->getType();
289 Address GridDim =
290 CGF.CreateMemTemp(Dim3Ty, CharUnits::fromQuantity(8), "grid_dim");
291 Address BlockDim =
292 CGF.CreateMemTemp(Dim3Ty, CharUnits::fromQuantity(8), "block_dim");
293 Address ShmemSize =
294 CGF.CreateTempAlloca(SizeTy, CGM.getSizeAlign(), "shmem_size");
295 Address Stream =
296 CGF.CreateTempAlloca(VoidPtrTy, CGM.getPointerAlign(), "stream");
James Y Knight9871db02019-02-05 16:42:33 +0000297 llvm::FunctionCallee cudaPopConfigFn = CGM.CreateRuntimeFunction(
Artem Belevichc62214d2019-01-31 21:34:03 +0000298 llvm::FunctionType::get(IntTy,
299 {/*gridDim=*/GridDim.getType(),
300 /*blockDim=*/BlockDim.getType(),
301 /*ShmemSize=*/ShmemSize.getType(),
302 /*Stream=*/Stream.getType()},
303 /*isVarArg=*/false),
304 "__cudaPopCallConfiguration");
305
306 CGF.EmitRuntimeCallOrInvoke(cudaPopConfigFn,
307 {GridDim.getPointer(), BlockDim.getPointer(),
308 ShmemSize.getPointer(), Stream.getPointer()});
309
310 // Emit the call to cudaLaunch
311 llvm::Value *Kernel = CGF.Builder.CreatePointerCast(CGF.CurFn, VoidPtrTy);
312 CallArgList LaunchKernelArgs;
313 LaunchKernelArgs.add(RValue::get(Kernel),
314 cudaLaunchKernelFD->getParamDecl(0)->getType());
315 LaunchKernelArgs.add(RValue::getAggregate(GridDim), Dim3Ty);
316 LaunchKernelArgs.add(RValue::getAggregate(BlockDim), Dim3Ty);
317 LaunchKernelArgs.add(RValue::get(KernelArgs.getPointer()),
318 cudaLaunchKernelFD->getParamDecl(3)->getType());
319 LaunchKernelArgs.add(RValue::get(CGF.Builder.CreateLoad(ShmemSize)),
320 cudaLaunchKernelFD->getParamDecl(4)->getType());
321 LaunchKernelArgs.add(RValue::get(CGF.Builder.CreateLoad(Stream)),
322 cudaLaunchKernelFD->getParamDecl(5)->getType());
323
324 QualType QT = cudaLaunchKernelFD->getType();
325 QualType CQT = QT.getCanonicalType();
James Y Knight916db652019-02-02 01:48:23 +0000326 llvm::Type *Ty = CGM.getTypes().ConvertType(CQT);
Artem Belevichc62214d2019-01-31 21:34:03 +0000327 llvm::FunctionType *FTy = dyn_cast<llvm::FunctionType>(Ty);
328
329 const CGFunctionInfo &FI =
330 CGM.getTypes().arrangeFunctionDeclaration(cudaLaunchKernelFD);
James Y Knight9871db02019-02-05 16:42:33 +0000331 llvm::FunctionCallee cudaLaunchKernelFn =
Artem Belevichc62214d2019-01-31 21:34:03 +0000332 CGM.CreateRuntimeFunction(FTy, "cudaLaunchKernel");
333 CGF.EmitCall(FI, CGCallee::forDirect(cudaLaunchKernelFn), ReturnValueSlot(),
334 LaunchKernelArgs);
335 CGF.EmitBranch(EndBlock);
336
337 CGF.EmitBlock(EndBlock);
338}
339
340void CGNVCUDARuntime::emitDeviceStubBodyLegacy(CodeGenFunction &CGF,
341 FunctionArgList &Args) {
Justin Lebare56360a2016-07-27 22:36:21 +0000342 // Emit a call to cudaSetupArgument for each arg in Args.
James Y Knight9871db02019-02-05 16:42:33 +0000343 llvm::FunctionCallee cudaSetupArgFn = getSetupArgumentFn();
Justin Lebare56360a2016-07-27 22:36:21 +0000344 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("setup.end");
345 CharUnits Offset = CharUnits::Zero();
346 for (const VarDecl *A : Args) {
347 CharUnits TyWidth, TyAlign;
348 std::tie(TyWidth, TyAlign) =
349 CGM.getContext().getTypeInfoInChars(A->getType());
350 Offset = Offset.alignTo(TyAlign);
351 llvm::Value *Args[] = {
352 CGF.Builder.CreatePointerCast(CGF.GetAddrOfLocalVar(A).getPointer(),
353 VoidPtrTy),
354 llvm::ConstantInt::get(SizeTy, TyWidth.getQuantity()),
355 llvm::ConstantInt::get(SizeTy, Offset.getQuantity()),
356 };
James Y Knight3933add2019-01-30 02:54:28 +0000357 llvm::CallBase *CB = CGF.EmitRuntimeCallOrInvoke(cudaSetupArgFn, Args);
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000358 llvm::Constant *Zero = llvm::ConstantInt::get(IntTy, 0);
James Y Knight3933add2019-01-30 02:54:28 +0000359 llvm::Value *CBZero = CGF.Builder.CreateICmpEQ(CB, Zero);
Justin Lebare56360a2016-07-27 22:36:21 +0000360 llvm::BasicBlock *NextBlock = CGF.createBasicBlock("setup.next");
James Y Knight3933add2019-01-30 02:54:28 +0000361 CGF.Builder.CreateCondBr(CBZero, NextBlock, EndBlock);
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000362 CGF.EmitBlock(NextBlock);
Justin Lebare56360a2016-07-27 22:36:21 +0000363 Offset += TyWidth;
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000364 }
365
366 // Emit the call to cudaLaunch
James Y Knight9871db02019-02-05 16:42:33 +0000367 llvm::FunctionCallee cudaLaunchFn = getLaunchFn();
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000368 llvm::Value *Arg = CGF.Builder.CreatePointerCast(CGF.CurFn, CharPtrTy);
John McCall882987f2013-02-28 19:01:20 +0000369 CGF.EmitRuntimeCallOrInvoke(cudaLaunchFn, Arg);
Peter Collingbournefa4d6032011-10-06 18:51:56 +0000370 CGF.EmitBranch(EndBlock);
371
372 CGF.EmitBlock(EndBlock);
Peter Collingbournefe883422011-10-06 18:29:37 +0000373}
374
Artem Belevich42e19492016-03-02 18:28:50 +0000375/// Creates a function that sets up state on the host side for CUDA objects that
376/// have a presence on both the host and device sides. Specifically, registers
377/// the host side of kernel functions and device global variables with the CUDA
378/// runtime.
Artem Belevich52cc4872015-05-07 19:34:16 +0000379/// \code
Artem Belevich42e19492016-03-02 18:28:50 +0000380/// void __cuda_register_globals(void** GpuBinaryHandle) {
Artem Belevich52cc4872015-05-07 19:34:16 +0000381/// __cudaRegisterFunction(GpuBinaryHandle,Kernel0,...);
382/// ...
383/// __cudaRegisterFunction(GpuBinaryHandle,KernelM,...);
Artem Belevich42e19492016-03-02 18:28:50 +0000384/// __cudaRegisterVar(GpuBinaryHandle, GlobalVar0, ...);
385/// ...
386/// __cudaRegisterVar(GpuBinaryHandle, GlobalVarN, ...);
Artem Belevich52cc4872015-05-07 19:34:16 +0000387/// }
388/// \endcode
Artem Belevich42e19492016-03-02 18:28:50 +0000389llvm::Function *CGNVCUDARuntime::makeRegisterGlobalsFn() {
Artem Belevich8c1ec1e2016-03-02 18:28:53 +0000390 // No need to register anything
391 if (EmittedKernels.empty() && DeviceVars.empty())
392 return nullptr;
393
Artem Belevich52cc4872015-05-07 19:34:16 +0000394 llvm::Function *RegisterKernelsFunc = llvm::Function::Create(
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000395 getRegisterGlobalsFnTy(), llvm::GlobalValue::InternalLinkage,
Yaxun Liu887c5692018-04-25 01:10:37 +0000396 addUnderscoredPrefixToName("_register_globals"), &TheModule);
Artem Belevich52cc4872015-05-07 19:34:16 +0000397 llvm::BasicBlock *EntryBB =
398 llvm::BasicBlock::Create(Context, "entry", RegisterKernelsFunc);
John McCall7f416cc2015-09-08 08:05:57 +0000399 CGBuilderTy Builder(CGM, Context);
Artem Belevich52cc4872015-05-07 19:34:16 +0000400 Builder.SetInsertPoint(EntryBB);
401
402 // void __cudaRegisterFunction(void **, const char *, char *, const char *,
403 // int, uint3*, uint3*, dim3*, dim3*, int*)
Benjamin Kramer6d1c10b2016-07-02 12:03:57 +0000404 llvm::Type *RegisterFuncParams[] = {
Artem Belevich52cc4872015-05-07 19:34:16 +0000405 VoidPtrPtrTy, CharPtrTy, CharPtrTy, CharPtrTy, IntTy,
406 VoidPtrTy, VoidPtrTy, VoidPtrTy, VoidPtrTy, IntTy->getPointerTo()};
James Y Knight9871db02019-02-05 16:42:33 +0000407 llvm::FunctionCallee RegisterFunc = CGM.CreateRuntimeFunction(
Artem Belevich52cc4872015-05-07 19:34:16 +0000408 llvm::FunctionType::get(IntTy, RegisterFuncParams, false),
Yaxun Liu887c5692018-04-25 01:10:37 +0000409 addUnderscoredPrefixToName("RegisterFunction"));
Artem Belevich52cc4872015-05-07 19:34:16 +0000410
411 // Extract GpuBinaryHandle passed as the first argument passed to
Artem Belevich42e19492016-03-02 18:28:50 +0000412 // __cuda_register_globals() and generate __cudaRegisterFunction() call for
Artem Belevich52cc4872015-05-07 19:34:16 +0000413 // each emitted kernel.
414 llvm::Argument &GpuBinaryHandlePtr = *RegisterKernelsFunc->arg_begin();
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000415 for (auto &&I : EmittedKernels) {
416 llvm::Constant *KernelName = makeConstantString(getDeviceSideName(I.D));
Artem Belevich52cc4872015-05-07 19:34:16 +0000417 llvm::Constant *NullPtr = llvm::ConstantPointerNull::get(VoidPtrTy);
Artem Belevich42e19492016-03-02 18:28:50 +0000418 llvm::Value *Args[] = {
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000419 &GpuBinaryHandlePtr,
420 Builder.CreateBitCast(I.Kernel, VoidPtrTy),
421 KernelName,
422 KernelName,
423 llvm::ConstantInt::get(IntTy, -1),
424 NullPtr,
425 NullPtr,
426 NullPtr,
427 NullPtr,
Artem Belevich52cc4872015-05-07 19:34:16 +0000428 llvm::ConstantPointerNull::get(IntTy->getPointerTo())};
Artem Belevich42e19492016-03-02 18:28:50 +0000429 Builder.CreateCall(RegisterFunc, Args);
430 }
431
432 // void __cudaRegisterVar(void **, char *, char *, const char *,
433 // int, int, int, int)
Benjamin Kramer6d1c10b2016-07-02 12:03:57 +0000434 llvm::Type *RegisterVarParams[] = {VoidPtrPtrTy, CharPtrTy, CharPtrTy,
435 CharPtrTy, IntTy, IntTy,
436 IntTy, IntTy};
James Y Knight9871db02019-02-05 16:42:33 +0000437 llvm::FunctionCallee RegisterVar = CGM.CreateRuntimeFunction(
Artem Belevich42e19492016-03-02 18:28:50 +0000438 llvm::FunctionType::get(IntTy, RegisterVarParams, false),
Yaxun Liu887c5692018-04-25 01:10:37 +0000439 addUnderscoredPrefixToName("RegisterVar"));
Yaxun Liuc18e9ec2019-02-14 02:00:09 +0000440 for (auto &&Info : DeviceVars) {
441 llvm::GlobalVariable *Var = Info.Var;
442 unsigned Flags = Info.Flag;
443 llvm::Constant *VarName = makeConstantString(getDeviceSideName(Info.D));
Artem Belevich42e19492016-03-02 18:28:50 +0000444 uint64_t VarSize =
445 CGM.getDataLayout().getTypeAllocSize(Var->getValueType());
446 llvm::Value *Args[] = {
447 &GpuBinaryHandlePtr,
448 Builder.CreateBitCast(Var, VoidPtrTy),
449 VarName,
450 VarName,
451 llvm::ConstantInt::get(IntTy, (Flags & ExternDeviceVar) ? 1 : 0),
452 llvm::ConstantInt::get(IntTy, VarSize),
453 llvm::ConstantInt::get(IntTy, (Flags & ConstantDeviceVar) ? 1 : 0),
454 llvm::ConstantInt::get(IntTy, 0)};
455 Builder.CreateCall(RegisterVar, Args);
Artem Belevich52cc4872015-05-07 19:34:16 +0000456 }
457
458 Builder.CreateRetVoid();
459 return RegisterKernelsFunc;
460}
461
462/// Creates a global constructor function for the module:
Yaxun Liuf99752b2018-07-20 22:45:24 +0000463///
464/// For CUDA:
Artem Belevich52cc4872015-05-07 19:34:16 +0000465/// \code
466/// void __cuda_module_ctor(void*) {
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000467/// Handle = __cudaRegisterFatBinary(GpuBinaryBlob);
468/// __cuda_register_globals(Handle);
Artem Belevich52cc4872015-05-07 19:34:16 +0000469/// }
470/// \endcode
Yaxun Liuf99752b2018-07-20 22:45:24 +0000471///
472/// For HIP:
473/// \code
474/// void __hip_module_ctor(void*) {
475/// if (__hip_gpubin_handle == 0) {
476/// __hip_gpubin_handle = __hipRegisterFatBinary(GpuBinaryBlob);
477/// __hip_register_globals(__hip_gpubin_handle);
478/// }
479/// }
480/// \endcode
Artem Belevich52cc4872015-05-07 19:34:16 +0000481llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
Yaxun Liu29155b02018-05-18 15:07:56 +0000482 bool IsHIP = CGM.getLangOpts().HIP;
Aaron Enye Shi81295212019-04-02 20:49:41 +0000483 bool IsCUDA = CGM.getLangOpts().CUDA;
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000484 // No need to generate ctors/dtors if there is no GPU binary.
Yaxun Liu29155b02018-05-18 15:07:56 +0000485 StringRef CudaGpuBinaryFileName = CGM.getCodeGenOpts().CudaGpuBinaryFileName;
486 if (CudaGpuBinaryFileName.empty() && !IsHIP)
Artem Belevich8c1ec1e2016-03-02 18:28:53 +0000487 return nullptr;
Aaron Enye Shi6d37f322019-04-02 21:54:41 +0000488 if ((IsHIP || (IsCUDA && !RelocatableDeviceCode)) && EmittedKernels.empty() &&
489 DeviceVars.empty())
Aaron Enye Shi13d8e922019-04-02 20:10:18 +0000490 return nullptr;
Artem Belevich8c1ec1e2016-03-02 18:28:53 +0000491
Yaxun Liu29155b02018-05-18 15:07:56 +0000492 // void __{cuda|hip}_register_globals(void* handle);
Artem Belevich42e19492016-03-02 18:28:50 +0000493 llvm::Function *RegisterGlobalsFunc = makeRegisterGlobalsFn();
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000494 // We always need a function to pass in as callback. Create a dummy
495 // implementation if we don't need to register anything.
496 if (RelocatableDeviceCode && !RegisterGlobalsFunc)
497 RegisterGlobalsFunc = makeDummyFunction(getRegisterGlobalsFnTy());
498
Yaxun Liu29155b02018-05-18 15:07:56 +0000499 // void ** __{cuda|hip}RegisterFatBinary(void *);
James Y Knight9871db02019-02-05 16:42:33 +0000500 llvm::FunctionCallee RegisterFatbinFunc = CGM.CreateRuntimeFunction(
Artem Belevich52cc4872015-05-07 19:34:16 +0000501 llvm::FunctionType::get(VoidPtrPtrTy, VoidPtrTy, false),
Yaxun Liu887c5692018-04-25 01:10:37 +0000502 addUnderscoredPrefixToName("RegisterFatBinary"));
Artem Belevich52cc4872015-05-07 19:34:16 +0000503 // struct { int magic, int version, void * gpu_binary, void * dont_care };
504 llvm::StructType *FatbinWrapperTy =
Serge Guelton1d993272017-05-09 19:31:30 +0000505 llvm::StructType::get(IntTy, IntTy, VoidPtrTy, VoidPtrTy);
Artem Belevich52cc4872015-05-07 19:34:16 +0000506
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000507 // Register GPU binary with the CUDA runtime, store returned handle in a
508 // global variable and save a reference in GpuBinaryHandle to be cleaned up
509 // in destructor on exit. Then associate all known kernels with the GPU binary
510 // handle so CUDA runtime can figure out what to call on the GPU side.
Yaxun Liu97670892018-10-02 17:48:54 +0000511 std::unique_ptr<llvm::MemoryBuffer> CudaGpuBinary = nullptr;
512 if (!CudaGpuBinaryFileName.empty()) {
Yaxun Liu29155b02018-05-18 15:07:56 +0000513 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CudaGpuBinaryOrErr =
514 llvm::MemoryBuffer::getFileOrSTDIN(CudaGpuBinaryFileName);
515 if (std::error_code EC = CudaGpuBinaryOrErr.getError()) {
516 CGM.getDiags().Report(diag::err_cannot_open_file)
517 << CudaGpuBinaryFileName << EC.message();
518 return nullptr;
519 }
520 CudaGpuBinary = std::move(CudaGpuBinaryOrErr.get());
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000521 }
522
Artem Belevich52cc4872015-05-07 19:34:16 +0000523 llvm::Function *ModuleCtorFunc = llvm::Function::Create(
524 llvm::FunctionType::get(VoidTy, VoidPtrTy, false),
Yaxun Liu887c5692018-04-25 01:10:37 +0000525 llvm::GlobalValue::InternalLinkage,
526 addUnderscoredPrefixToName("_module_ctor"), &TheModule);
Artem Belevich52cc4872015-05-07 19:34:16 +0000527 llvm::BasicBlock *CtorEntryBB =
528 llvm::BasicBlock::Create(Context, "entry", ModuleCtorFunc);
John McCall7f416cc2015-09-08 08:05:57 +0000529 CGBuilderTy CtorBuilder(CGM, Context);
Artem Belevich52cc4872015-05-07 19:34:16 +0000530
531 CtorBuilder.SetInsertPoint(CtorEntryBB);
532
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000533 const char *FatbinConstantName;
Yaxun Liu29155b02018-05-18 15:07:56 +0000534 const char *FatbinSectionName;
535 const char *ModuleIDSectionName;
536 StringRef ModuleIDPrefix;
537 llvm::Constant *FatBinStr;
538 unsigned FatMagic;
539 if (IsHIP) {
540 FatbinConstantName = ".hip_fatbin";
541 FatbinSectionName = ".hipFatBinSegment";
542
543 ModuleIDSectionName = "__hip_module_id";
544 ModuleIDPrefix = "__hip_";
545
Yaxun Liu97670892018-10-02 17:48:54 +0000546 if (CudaGpuBinary) {
547 // If fatbin is available from early finalization, create a string
548 // literal containing the fat binary loaded from the given file.
549 FatBinStr = makeConstantString(CudaGpuBinary->getBuffer(), "",
550 FatbinConstantName, 8);
551 } else {
552 // If fatbin is not available, create an external symbol
553 // __hip_fatbin in section .hip_fatbin. The external symbol is supposed
554 // to contain the fat binary but will be populated somewhere else,
555 // e.g. by lld through link script.
556 FatBinStr = new llvm::GlobalVariable(
Yaxun Liu29155b02018-05-18 15:07:56 +0000557 CGM.getModule(), CGM.Int8Ty,
558 /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, nullptr,
559 "__hip_fatbin", nullptr,
560 llvm::GlobalVariable::NotThreadLocal);
Yaxun Liu97670892018-10-02 17:48:54 +0000561 cast<llvm::GlobalVariable>(FatBinStr)->setSection(FatbinConstantName);
562 }
Yaxun Liu29155b02018-05-18 15:07:56 +0000563
564 FatMagic = HIPFatMagic;
565 } else {
566 if (RelocatableDeviceCode)
Artem Belevich5ce0a082018-06-28 17:15:52 +0000567 FatbinConstantName = CGM.getTriple().isMacOSX()
568 ? "__NV_CUDA,__nv_relfatbin"
569 : "__nv_relfatbin";
Yaxun Liu29155b02018-05-18 15:07:56 +0000570 else
571 FatbinConstantName =
572 CGM.getTriple().isMacOSX() ? "__NV_CUDA,__nv_fatbin" : ".nv_fatbin";
573 // NVIDIA's cuobjdump looks for fatbins in this section.
574 FatbinSectionName =
575 CGM.getTriple().isMacOSX() ? "__NV_CUDA,__fatbin" : ".nvFatBinSegment";
576
Artem Belevich5ce0a082018-06-28 17:15:52 +0000577 ModuleIDSectionName = CGM.getTriple().isMacOSX()
578 ? "__NV_CUDA,__nv_module_id"
579 : "__nv_module_id";
Yaxun Liu29155b02018-05-18 15:07:56 +0000580 ModuleIDPrefix = "__nv_";
581
582 // For CUDA, create a string literal containing the fat binary loaded from
583 // the given file.
584 FatBinStr = makeConstantString(CudaGpuBinary->getBuffer(), "",
585 FatbinConstantName, 8);
586 FatMagic = CudaFatMagic;
587 }
Artem Belevich52cc4872015-05-07 19:34:16 +0000588
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000589 // Create initialized wrapper structure that points to the loaded GPU binary
590 ConstantInitBuilder Builder(CGM);
591 auto Values = Builder.beginStruct(FatbinWrapperTy);
592 // Fatbin wrapper magic.
Yaxun Liu29155b02018-05-18 15:07:56 +0000593 Values.addInt(IntTy, FatMagic);
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000594 // Fatbin version.
595 Values.addInt(IntTy, 1);
596 // Data.
Yaxun Liu29155b02018-05-18 15:07:56 +0000597 Values.add(FatBinStr);
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000598 // Unused in fatbin v1.
599 Values.add(llvm::ConstantPointerNull::get(VoidPtrTy));
600 llvm::GlobalVariable *FatbinWrapper = Values.finishAndCreateGlobal(
Yaxun Liu887c5692018-04-25 01:10:37 +0000601 addUnderscoredPrefixToName("_fatbin_wrapper"), CGM.getPointerAlign(),
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000602 /*constant*/ true);
603 FatbinWrapper->setSection(FatbinSectionName);
Justin Lebard14fe882016-11-18 00:41:31 +0000604
Yaxun Liuf99752b2018-07-20 22:45:24 +0000605 // There is only one HIP fat binary per linked module, however there are
606 // multiple constructor functions. Make sure the fat binary is registered
607 // only once. The constructor functions are executed by the dynamic loader
608 // before the program gains control. The dynamic loader cannot execute the
609 // constructor functions concurrently since doing that would not guarantee
610 // thread safety of the loaded program. Therefore we can assume sequential
611 // execution of constructor functions here.
612 if (IsHIP) {
Yaxun Liu97670892018-10-02 17:48:54 +0000613 auto Linkage = CudaGpuBinary ? llvm::GlobalValue::InternalLinkage :
614 llvm::GlobalValue::LinkOnceAnyLinkage;
Yaxun Liuf99752b2018-07-20 22:45:24 +0000615 llvm::BasicBlock *IfBlock =
616 llvm::BasicBlock::Create(Context, "if", ModuleCtorFunc);
617 llvm::BasicBlock *ExitBlock =
618 llvm::BasicBlock::Create(Context, "exit", ModuleCtorFunc);
619 // The name, size, and initialization pattern of this variable is part
620 // of HIP ABI.
621 GpuBinaryHandle = new llvm::GlobalVariable(
622 TheModule, VoidPtrPtrTy, /*isConstant=*/false,
Yaxun Liu97670892018-10-02 17:48:54 +0000623 Linkage,
Yaxun Liuf99752b2018-07-20 22:45:24 +0000624 /*Initializer=*/llvm::ConstantPointerNull::get(VoidPtrPtrTy),
625 "__hip_gpubin_handle");
626 GpuBinaryHandle->setAlignment(CGM.getPointerAlign().getQuantity());
Yaxun Liu94ff57f2018-08-17 17:47:31 +0000627 // Prevent the weak symbol in different shared libraries being merged.
Yaxun Liu97670892018-10-02 17:48:54 +0000628 if (Linkage != llvm::GlobalValue::InternalLinkage)
629 GpuBinaryHandle->setVisibility(llvm::GlobalValue::HiddenVisibility);
Yaxun Liuf99752b2018-07-20 22:45:24 +0000630 Address GpuBinaryAddr(
631 GpuBinaryHandle,
632 CharUnits::fromQuantity(GpuBinaryHandle->getAlignment()));
633 {
634 auto HandleValue = CtorBuilder.CreateLoad(GpuBinaryAddr);
635 llvm::Constant *Zero =
636 llvm::Constant::getNullValue(HandleValue->getType());
637 llvm::Value *EQZero = CtorBuilder.CreateICmpEQ(HandleValue, Zero);
638 CtorBuilder.CreateCondBr(EQZero, IfBlock, ExitBlock);
639 }
640 {
641 CtorBuilder.SetInsertPoint(IfBlock);
642 // GpuBinaryHandle = __hipRegisterFatBinary(&FatbinWrapper);
643 llvm::CallInst *RegisterFatbinCall = CtorBuilder.CreateCall(
644 RegisterFatbinFunc,
645 CtorBuilder.CreateBitCast(FatbinWrapper, VoidPtrTy));
646 CtorBuilder.CreateStore(RegisterFatbinCall, GpuBinaryAddr);
647 CtorBuilder.CreateBr(ExitBlock);
648 }
649 {
650 CtorBuilder.SetInsertPoint(ExitBlock);
651 // Call __hip_register_globals(GpuBinaryHandle);
652 if (RegisterGlobalsFunc) {
653 auto HandleValue = CtorBuilder.CreateLoad(GpuBinaryAddr);
654 CtorBuilder.CreateCall(RegisterGlobalsFunc, HandleValue);
655 }
656 }
657 } else if (!RelocatableDeviceCode) {
658 // Register binary with CUDA runtime. This is substantially different in
659 // default mode vs. separate compilation!
660 // GpuBinaryHandle = __cudaRegisterFatBinary(&FatbinWrapper);
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000661 llvm::CallInst *RegisterFatbinCall = CtorBuilder.CreateCall(
662 RegisterFatbinFunc,
663 CtorBuilder.CreateBitCast(FatbinWrapper, VoidPtrTy));
664 GpuBinaryHandle = new llvm::GlobalVariable(
665 TheModule, VoidPtrPtrTy, false, llvm::GlobalValue::InternalLinkage,
Yaxun Liuf99752b2018-07-20 22:45:24 +0000666 llvm::ConstantPointerNull::get(VoidPtrPtrTy), "__cuda_gpubin_handle");
667 GpuBinaryHandle->setAlignment(CGM.getPointerAlign().getQuantity());
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000668 CtorBuilder.CreateAlignedStore(RegisterFatbinCall, GpuBinaryHandle,
669 CGM.getPointerAlign());
Artem Belevich52cc4872015-05-07 19:34:16 +0000670
Yaxun Liuf99752b2018-07-20 22:45:24 +0000671 // Call __cuda_register_globals(GpuBinaryHandle);
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000672 if (RegisterGlobalsFunc)
673 CtorBuilder.CreateCall(RegisterGlobalsFunc, RegisterFatbinCall);
Artem Belevich40717632019-02-05 22:38:58 +0000674
675 // Call __cudaRegisterFatBinaryEnd(Handle) if this CUDA version needs it.
676 if (CudaFeatureEnabled(CGM.getTarget().getSDKVersion(),
677 CudaFeature::CUDA_USES_FATBIN_REGISTER_END)) {
678 // void __cudaRegisterFatBinaryEnd(void **);
679 llvm::FunctionCallee RegisterFatbinEndFunc = CGM.CreateRuntimeFunction(
680 llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false),
681 "__cudaRegisterFatBinaryEnd");
682 CtorBuilder.CreateCall(RegisterFatbinEndFunc, RegisterFatbinCall);
683 }
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000684 } else {
685 // Generate a unique module ID.
Yaxun Liu29155b02018-05-18 15:07:56 +0000686 SmallString<64> ModuleID;
687 llvm::raw_svector_ostream OS(ModuleID);
Artem Belevich93552b32018-10-05 18:39:58 +0000688 OS << ModuleIDPrefix << llvm::format("%" PRIx64, FatbinWrapper->getGUID());
Yaxun Liu29155b02018-05-18 15:07:56 +0000689 llvm::Constant *ModuleIDConstant =
690 makeConstantString(ModuleID.str(), "", ModuleIDSectionName, 32);
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000691
Yaxun Liuf99752b2018-07-20 22:45:24 +0000692 // Create an alias for the FatbinWrapper that nvcc will look for.
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000693 llvm::GlobalAlias::create(llvm::GlobalValue::ExternalLinkage,
Yaxun Liu29155b02018-05-18 15:07:56 +0000694 Twine("__fatbinwrap") + ModuleID, FatbinWrapper);
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000695
Yaxun Liuf99752b2018-07-20 22:45:24 +0000696 // void __cudaRegisterLinkedBinary%ModuleID%(void (*)(void *), void *,
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000697 // void *, void (*)(void **))
Yaxun Liuf99752b2018-07-20 22:45:24 +0000698 SmallString<128> RegisterLinkedBinaryName("__cudaRegisterLinkedBinary");
Yaxun Liu29155b02018-05-18 15:07:56 +0000699 RegisterLinkedBinaryName += ModuleID;
James Y Knight9871db02019-02-05 16:42:33 +0000700 llvm::FunctionCallee RegisterLinkedBinaryFunc = CGM.CreateRuntimeFunction(
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000701 getRegisterLinkedBinaryFnTy(), RegisterLinkedBinaryName);
702
703 assert(RegisterGlobalsFunc && "Expecting at least dummy function!");
704 llvm::Value *Args[] = {RegisterGlobalsFunc,
705 CtorBuilder.CreateBitCast(FatbinWrapper, VoidPtrTy),
Yaxun Liu29155b02018-05-18 15:07:56 +0000706 ModuleIDConstant,
Jonas Hahnfeldf5527c22018-04-20 13:04:45 +0000707 makeDummyFunction(getCallbackFnTy())};
708 CtorBuilder.CreateCall(RegisterLinkedBinaryFunc, Args);
709 }
Artem Belevich52cc4872015-05-07 19:34:16 +0000710
Artem Belevichc66d2542018-06-27 18:32:51 +0000711 // Create destructor and register it with atexit() the way NVCC does it. Doing
712 // it during regular destructor phase worked in CUDA before 9.2 but results in
713 // double-free in 9.2.
714 if (llvm::Function *CleanupFn = makeModuleDtorFunction()) {
715 // extern "C" int atexit(void (*f)(void));
716 llvm::FunctionType *AtExitTy =
717 llvm::FunctionType::get(IntTy, CleanupFn->getType(), false);
James Y Knight9871db02019-02-05 16:42:33 +0000718 llvm::FunctionCallee AtExitFunc =
Artem Belevichc66d2542018-06-27 18:32:51 +0000719 CGM.CreateRuntimeFunction(AtExitTy, "atexit", llvm::AttributeList(),
720 /*Local=*/true);
721 CtorBuilder.CreateCall(AtExitFunc, CleanupFn);
722 }
723
Artem Belevich52cc4872015-05-07 19:34:16 +0000724 CtorBuilder.CreateRetVoid();
725 return ModuleCtorFunc;
726}
727
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000728/// Creates a global destructor function that unregisters the GPU code blob
Artem Belevich52cc4872015-05-07 19:34:16 +0000729/// registered by constructor.
Yaxun Liuf99752b2018-07-20 22:45:24 +0000730///
731/// For CUDA:
Artem Belevich52cc4872015-05-07 19:34:16 +0000732/// \code
733/// void __cuda_module_dtor(void*) {
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000734/// __cudaUnregisterFatBinary(Handle);
Artem Belevich52cc4872015-05-07 19:34:16 +0000735/// }
736/// \endcode
Yaxun Liuf99752b2018-07-20 22:45:24 +0000737///
738/// For HIP:
739/// \code
740/// void __hip_module_dtor(void*) {
741/// if (__hip_gpubin_handle) {
742/// __hipUnregisterFatBinary(__hip_gpubin_handle);
743/// __hip_gpubin_handle = 0;
744/// }
745/// }
746/// \endcode
Artem Belevich52cc4872015-05-07 19:34:16 +0000747llvm::Function *CGNVCUDARuntime::makeModuleDtorFunction() {
Jonas Hahnfelde7681322018-02-28 17:53:46 +0000748 // No need for destructor if we don't have a handle to unregister.
749 if (!GpuBinaryHandle)
Artem Belevich8c1ec1e2016-03-02 18:28:53 +0000750 return nullptr;
751
Artem Belevich52cc4872015-05-07 19:34:16 +0000752 // void __cudaUnregisterFatBinary(void ** handle);
James Y Knight9871db02019-02-05 16:42:33 +0000753 llvm::FunctionCallee UnregisterFatbinFunc = CGM.CreateRuntimeFunction(
Artem Belevich52cc4872015-05-07 19:34:16 +0000754 llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false),
Yaxun Liu887c5692018-04-25 01:10:37 +0000755 addUnderscoredPrefixToName("UnregisterFatBinary"));
Artem Belevich52cc4872015-05-07 19:34:16 +0000756
757 llvm::Function *ModuleDtorFunc = llvm::Function::Create(
758 llvm::FunctionType::get(VoidTy, VoidPtrTy, false),
Yaxun Liu887c5692018-04-25 01:10:37 +0000759 llvm::GlobalValue::InternalLinkage,
760 addUnderscoredPrefixToName("_module_dtor"), &TheModule);
761
Artem Belevich52cc4872015-05-07 19:34:16 +0000762 llvm::BasicBlock *DtorEntryBB =
763 llvm::BasicBlock::Create(Context, "entry", ModuleDtorFunc);
John McCall7f416cc2015-09-08 08:05:57 +0000764 CGBuilderTy DtorBuilder(CGM, Context);
Artem Belevich52cc4872015-05-07 19:34:16 +0000765 DtorBuilder.SetInsertPoint(DtorEntryBB);
766
Yaxun Liuf99752b2018-07-20 22:45:24 +0000767 Address GpuBinaryAddr(GpuBinaryHandle, CharUnits::fromQuantity(
768 GpuBinaryHandle->getAlignment()));
769 auto HandleValue = DtorBuilder.CreateLoad(GpuBinaryAddr);
770 // There is only one HIP fat binary per linked module, however there are
771 // multiple destructor functions. Make sure the fat binary is unregistered
772 // only once.
773 if (CGM.getLangOpts().HIP) {
774 llvm::BasicBlock *IfBlock =
775 llvm::BasicBlock::Create(Context, "if", ModuleDtorFunc);
776 llvm::BasicBlock *ExitBlock =
777 llvm::BasicBlock::Create(Context, "exit", ModuleDtorFunc);
778 llvm::Constant *Zero = llvm::Constant::getNullValue(HandleValue->getType());
779 llvm::Value *NEZero = DtorBuilder.CreateICmpNE(HandleValue, Zero);
780 DtorBuilder.CreateCondBr(NEZero, IfBlock, ExitBlock);
Artem Belevich52cc4872015-05-07 19:34:16 +0000781
Yaxun Liuf99752b2018-07-20 22:45:24 +0000782 DtorBuilder.SetInsertPoint(IfBlock);
783 DtorBuilder.CreateCall(UnregisterFatbinFunc, HandleValue);
784 DtorBuilder.CreateStore(Zero, GpuBinaryAddr);
785 DtorBuilder.CreateBr(ExitBlock);
786
787 DtorBuilder.SetInsertPoint(ExitBlock);
788 } else {
789 DtorBuilder.CreateCall(UnregisterFatbinFunc, HandleValue);
790 }
Artem Belevich52cc4872015-05-07 19:34:16 +0000791 DtorBuilder.CreateRetVoid();
792 return ModuleDtorFunc;
793}
794
Michael Liaoe40f8792019-06-17 12:51:36 +0000795std::string CGNVCUDARuntime::getDeviceStubName(llvm::StringRef Name) const {
796 if (!CGM.getLangOpts().HIP)
797 return Name;
Kadir Cetinkaya3c9391a2019-06-17 14:23:06 +0000798 return (Name + ".stub").str();
Michael Liaoe40f8792019-06-17 12:51:36 +0000799}
800
Peter Collingbournefe883422011-10-06 18:29:37 +0000801CGCUDARuntime *CodeGen::CreateNVCUDARuntime(CodeGenModule &CGM) {
802 return new CGNVCUDARuntime(CGM);
803}