Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 1 | //===----- CGCUDANV.cpp - Interface to NVIDIA CUDA Runtime ----------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 6 | // |
| 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 Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
| 16 | #include "CodeGenModule.h" |
| 17 | #include "clang/AST/Decl.h" |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Cuda.h" |
| 19 | #include "clang/CodeGen/CodeGenABITypes.h" |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 20 | #include "clang/CodeGen/ConstantInitBuilder.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 21 | #include "llvm/IR/BasicBlock.h" |
| 22 | #include "llvm/IR/Constants.h" |
| 23 | #include "llvm/IR/DerivedTypes.h" |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Format.h" |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
| 29 | namespace { |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 30 | constexpr unsigned CudaFatMagic = 0x466243b1; |
| 31 | constexpr unsigned HIPFatMagic = 0x48495046; // "HIPF" |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 32 | |
| 33 | class CGNVCUDARuntime : public CGCUDARuntime { |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 34 | |
| 35 | private: |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 36 | llvm::IntegerType *IntTy, *SizeTy; |
| 37 | llvm::Type *VoidTy; |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 38 | 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 Liu | c18e9ec | 2019-02-14 02:00:09 +0000 | [diff] [blame] | 45 | 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 Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 56 | /// Keeps track of variable containing handle of GPU binary. Populated by |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 57 | /// ModuleCtorFunction() and used to create corresponding cleanup calls in |
| 58 | /// ModuleDtorFunction() |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 59 | llvm::GlobalVariable *GpuBinaryHandle = nullptr; |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 60 | /// Whether we generate relocatable device code. |
| 61 | bool RelocatableDeviceCode; |
Yaxun Liu | c18e9ec | 2019-02-14 02:00:09 +0000 | [diff] [blame] | 62 | /// Mangle context for device. |
| 63 | std::unique_ptr<MangleContext> DeviceMC; |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 64 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 65 | llvm::FunctionCallee getSetupArgumentFn() const; |
| 66 | llvm::FunctionCallee getLaunchFn() const; |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 67 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 68 | llvm::FunctionType *getRegisterGlobalsFnTy() const; |
| 69 | llvm::FunctionType *getCallbackFnTy() const; |
| 70 | llvm::FunctionType *getRegisterLinkedBinaryFnTy() const; |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 71 | std::string addPrefixToName(StringRef FuncName) const; |
| 72 | std::string addUnderscoredPrefixToName(StringRef FuncName) const; |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 73 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 74 | /// Creates a function to register all kernel stubs generated in this module. |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 75 | llvm::Function *makeRegisterGlobalsFn(); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 76 | |
| 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 Belevich | 4c09318 | 2016-08-12 18:44:01 +0000 | [diff] [blame] | 82 | const std::string &SectionName = "", |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 83 | unsigned Alignment = 0) { |
| 84 | llvm::Constant *Zeros[] = {llvm::ConstantInt::get(SizeTy, 0), |
| 85 | llvm::ConstantInt::get(SizeTy, 0)}; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 86 | auto ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str()); |
Artem Belevich | 4c09318 | 2016-08-12 18:44:01 +0000 | [diff] [blame] | 87 | llvm::GlobalVariable *GV = |
| 88 | cast<llvm::GlobalVariable>(ConstStr.getPointer()); |
Jonas Hahnfeld | 3b9cbba9 | 2018-06-08 11:17:08 +0000 | [diff] [blame] | 89 | if (!SectionName.empty()) { |
Artem Belevich | 4c09318 | 2016-08-12 18:44:01 +0000 | [diff] [blame] | 90 | GV->setSection(SectionName); |
Jonas Hahnfeld | 3b9cbba9 | 2018-06-08 11:17:08 +0000 | [diff] [blame] | 91 | // 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 Belevich | 4c09318 | 2016-08-12 18:44:01 +0000 | [diff] [blame] | 95 | if (Alignment) |
Guillaume Chatelet | c79099e | 2019-10-03 13:00:29 +0000 | [diff] [blame^] | 96 | GV->setAlignment(llvm::Align(Alignment)); |
Artem Belevich | 4c09318 | 2016-08-12 18:44:01 +0000 | [diff] [blame] | 97 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 98 | return llvm::ConstantExpr::getGetElementPtr(ConstStr.getElementType(), |
| 99 | ConstStr.getPointer(), Zeros); |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 100 | } |
| 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 Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 117 | |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 118 | void emitDeviceStubBodyLegacy(CodeGenFunction &CGF, FunctionArgList &Args); |
| 119 | void emitDeviceStubBodyNew(CodeGenFunction &CGF, FunctionArgList &Args); |
Yaxun Liu | c18e9ec | 2019-02-14 02:00:09 +0000 | [diff] [blame] | 120 | std::string getDeviceSideName(const Decl *ND); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 121 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 122 | public: |
| 123 | CGNVCUDARuntime(CodeGenModule &CGM); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 124 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 125 | void emitDeviceStub(CodeGenFunction &CGF, FunctionArgList &Args) override; |
Yaxun Liu | c18e9ec | 2019-02-14 02:00:09 +0000 | [diff] [blame] | 126 | void registerDeviceVar(const VarDecl *VD, llvm::GlobalVariable &Var, |
| 127 | unsigned Flags) override { |
| 128 | DeviceVars.push_back({&Var, VD, Flags}); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 131 | /// Creates module constructor function |
| 132 | llvm::Function *makeModuleCtorFunction() override; |
| 133 | /// Creates module destructor function |
| 134 | llvm::Function *makeModuleDtorFunction() override; |
Michael Liao | e40f879 | 2019-06-17 12:51:36 +0000 | [diff] [blame] | 135 | /// Construct and return the stub name of a kernel. |
| 136 | std::string getDeviceStubName(llvm::StringRef Name) const override; |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 139 | } |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 140 | |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 141 | std::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 | } |
| 146 | std::string |
| 147 | CGNVCUDARuntime::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 Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 153 | CGNVCUDARuntime::CGNVCUDARuntime(CodeGenModule &CGM) |
| 154 | : CGCUDARuntime(CGM), Context(CGM.getLLVMContext()), |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 155 | TheModule(CGM.getModule()), |
Yaxun Liu | c18e9ec | 2019-02-14 02:00:09 +0000 | [diff] [blame] | 156 | RelocatableDeviceCode(CGM.getLangOpts().GPURelocatableDeviceCode), |
| 157 | DeviceMC(CGM.getContext().createMangleContext( |
| 158 | CGM.getContext().getAuxTargetInfo())) { |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 159 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 160 | ASTContext &Ctx = CGM.getContext(); |
| 161 | |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 162 | IntTy = CGM.IntTy; |
| 163 | SizeTy = CGM.SizeTy; |
| 164 | VoidTy = CGM.VoidTy; |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 165 | |
| 166 | CharPtrTy = llvm::PointerType::getUnqual(Types.ConvertType(Ctx.CharTy)); |
| 167 | VoidPtrTy = cast<llvm::PointerType>(Types.ConvertType(Ctx.VoidPtrTy)); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 168 | VoidPtrPtrTy = VoidPtrTy->getPointerTo(); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 169 | } |
| 170 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 171 | llvm::FunctionCallee CGNVCUDARuntime::getSetupArgumentFn() const { |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 172 | // cudaError_t cudaSetupArgument(void *, size_t, size_t) |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 173 | llvm::Type *Params[] = {VoidPtrTy, SizeTy, SizeTy}; |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 174 | return CGM.CreateRuntimeFunction( |
| 175 | llvm::FunctionType::get(IntTy, Params, false), |
| 176 | addPrefixToName("SetupArgument")); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 177 | } |
| 178 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 179 | llvm::FunctionCallee CGNVCUDARuntime::getLaunchFn() const { |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 180 | 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 Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 191 | llvm::FunctionType *CGNVCUDARuntime::getRegisterGlobalsFnTy() const { |
| 192 | return llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false); |
| 193 | } |
| 194 | |
| 195 | llvm::FunctionType *CGNVCUDARuntime::getCallbackFnTy() const { |
| 196 | return llvm::FunctionType::get(VoidTy, VoidPtrTy, false); |
| 197 | } |
| 198 | |
| 199 | llvm::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 Liu | c18e9ec | 2019-02-14 02:00:09 +0000 | [diff] [blame] | 207 | std::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 Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 220 | void CGNVCUDARuntime::emitDeviceStub(CodeGenFunction &CGF, |
| 221 | FunctionArgList &Args) { |
Michael Liao | e40f879 | 2019-06-17 12:51:36 +0000 | [diff] [blame] | 222 | // 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 Liu | c18e9ec | 2019-02-14 02:00:09 +0000 | [diff] [blame] | 236 | |
| 237 | EmittedKernels.push_back({CGF.CurFn, CGF.CurFuncDecl}); |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 238 | if (CudaFeatureEnabled(CGM.getTarget().getSDKVersion(), |
Yaxun Liu | 1282889 | 2019-09-24 19:16:40 +0000 | [diff] [blame] | 239 | CudaFeature::CUDA_USES_NEW_LAUNCH) || |
| 240 | CGF.getLangOpts().HIPUseNewLaunchAPI) |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 241 | emitDeviceStubBodyNew(CGF, Args); |
| 242 | else |
| 243 | emitDeviceStubBodyLegacy(CGF, Args); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 246 | // CUDA 9.0+ uses new way to launch kernels. Parameters are packed in a local |
| 247 | // array and kernels are launched using cudaLaunchKernel(). |
| 248 | void CGNVCUDARuntime::emitDeviceStubBodyNew(CodeGenFunction &CGF, |
| 249 | FunctionArgList &Args) { |
| 250 | // Build the shadow stack entry at the very start of the function. |
| 251 | |
| 252 | // Calculate amount of space we will need for all arguments. If we have no |
| 253 | // args, allocate a single pointer so we still have a valid pointer to the |
| 254 | // argument array that we can pass to runtime, even if it will be unused. |
| 255 | Address KernelArgs = CGF.CreateTempAlloca( |
| 256 | VoidPtrTy, CharUnits::fromQuantity(16), "kernel_args", |
| 257 | llvm::ConstantInt::get(SizeTy, std::max<size_t>(1, Args.size()))); |
| 258 | // Store pointers to the arguments in a locally allocated launch_args. |
| 259 | for (unsigned i = 0; i < Args.size(); ++i) { |
| 260 | llvm::Value* VarPtr = CGF.GetAddrOfLocalVar(Args[i]).getPointer(); |
| 261 | llvm::Value *VoidVarPtr = CGF.Builder.CreatePointerCast(VarPtr, VoidPtrTy); |
| 262 | CGF.Builder.CreateDefaultAlignedStore( |
| 263 | VoidVarPtr, CGF.Builder.CreateConstGEP1_32(KernelArgs.getPointer(), i)); |
| 264 | } |
| 265 | |
| 266 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("setup.end"); |
| 267 | |
Yaxun Liu | 1282889 | 2019-09-24 19:16:40 +0000 | [diff] [blame] | 268 | // Lookup cudaLaunchKernel/hipLaunchKernel function. |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 269 | // cudaError_t cudaLaunchKernel(const void *func, dim3 gridDim, dim3 blockDim, |
| 270 | // void **args, size_t sharedMem, |
| 271 | // cudaStream_t stream); |
Yaxun Liu | 1282889 | 2019-09-24 19:16:40 +0000 | [diff] [blame] | 272 | // hipError_t hipLaunchKernel(const void *func, dim3 gridDim, dim3 blockDim, |
| 273 | // void **args, size_t sharedMem, |
| 274 | // hipStream_t stream); |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 275 | TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl(); |
| 276 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
Yaxun Liu | 1282889 | 2019-09-24 19:16:40 +0000 | [diff] [blame] | 277 | auto LaunchKernelName = addPrefixToName("LaunchKernel"); |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 278 | IdentifierInfo &cudaLaunchKernelII = |
Yaxun Liu | 1282889 | 2019-09-24 19:16:40 +0000 | [diff] [blame] | 279 | CGM.getContext().Idents.get(LaunchKernelName); |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 280 | FunctionDecl *cudaLaunchKernelFD = nullptr; |
| 281 | for (const auto &Result : DC->lookup(&cudaLaunchKernelII)) { |
| 282 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Result)) |
| 283 | cudaLaunchKernelFD = FD; |
| 284 | } |
| 285 | |
| 286 | if (cudaLaunchKernelFD == nullptr) { |
| 287 | CGM.Error(CGF.CurFuncDecl->getLocation(), |
Yaxun Liu | 1282889 | 2019-09-24 19:16:40 +0000 | [diff] [blame] | 288 | "Can't find declaration for " + LaunchKernelName); |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 289 | return; |
| 290 | } |
| 291 | // Create temporary dim3 grid_dim, block_dim. |
| 292 | ParmVarDecl *GridDimParam = cudaLaunchKernelFD->getParamDecl(1); |
| 293 | QualType Dim3Ty = GridDimParam->getType(); |
| 294 | Address GridDim = |
| 295 | CGF.CreateMemTemp(Dim3Ty, CharUnits::fromQuantity(8), "grid_dim"); |
| 296 | Address BlockDim = |
| 297 | CGF.CreateMemTemp(Dim3Ty, CharUnits::fromQuantity(8), "block_dim"); |
| 298 | Address ShmemSize = |
| 299 | CGF.CreateTempAlloca(SizeTy, CGM.getSizeAlign(), "shmem_size"); |
| 300 | Address Stream = |
| 301 | CGF.CreateTempAlloca(VoidPtrTy, CGM.getPointerAlign(), "stream"); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 302 | llvm::FunctionCallee cudaPopConfigFn = CGM.CreateRuntimeFunction( |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 303 | llvm::FunctionType::get(IntTy, |
| 304 | {/*gridDim=*/GridDim.getType(), |
| 305 | /*blockDim=*/BlockDim.getType(), |
| 306 | /*ShmemSize=*/ShmemSize.getType(), |
| 307 | /*Stream=*/Stream.getType()}, |
| 308 | /*isVarArg=*/false), |
Yaxun Liu | 1282889 | 2019-09-24 19:16:40 +0000 | [diff] [blame] | 309 | addUnderscoredPrefixToName("PopCallConfiguration")); |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 310 | |
| 311 | CGF.EmitRuntimeCallOrInvoke(cudaPopConfigFn, |
| 312 | {GridDim.getPointer(), BlockDim.getPointer(), |
| 313 | ShmemSize.getPointer(), Stream.getPointer()}); |
| 314 | |
| 315 | // Emit the call to cudaLaunch |
| 316 | llvm::Value *Kernel = CGF.Builder.CreatePointerCast(CGF.CurFn, VoidPtrTy); |
| 317 | CallArgList LaunchKernelArgs; |
| 318 | LaunchKernelArgs.add(RValue::get(Kernel), |
| 319 | cudaLaunchKernelFD->getParamDecl(0)->getType()); |
| 320 | LaunchKernelArgs.add(RValue::getAggregate(GridDim), Dim3Ty); |
| 321 | LaunchKernelArgs.add(RValue::getAggregate(BlockDim), Dim3Ty); |
| 322 | LaunchKernelArgs.add(RValue::get(KernelArgs.getPointer()), |
| 323 | cudaLaunchKernelFD->getParamDecl(3)->getType()); |
| 324 | LaunchKernelArgs.add(RValue::get(CGF.Builder.CreateLoad(ShmemSize)), |
| 325 | cudaLaunchKernelFD->getParamDecl(4)->getType()); |
| 326 | LaunchKernelArgs.add(RValue::get(CGF.Builder.CreateLoad(Stream)), |
| 327 | cudaLaunchKernelFD->getParamDecl(5)->getType()); |
| 328 | |
| 329 | QualType QT = cudaLaunchKernelFD->getType(); |
| 330 | QualType CQT = QT.getCanonicalType(); |
James Y Knight | 916db65 | 2019-02-02 01:48:23 +0000 | [diff] [blame] | 331 | llvm::Type *Ty = CGM.getTypes().ConvertType(CQT); |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 332 | llvm::FunctionType *FTy = dyn_cast<llvm::FunctionType>(Ty); |
| 333 | |
| 334 | const CGFunctionInfo &FI = |
| 335 | CGM.getTypes().arrangeFunctionDeclaration(cudaLaunchKernelFD); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 336 | llvm::FunctionCallee cudaLaunchKernelFn = |
Yaxun Liu | 1282889 | 2019-09-24 19:16:40 +0000 | [diff] [blame] | 337 | CGM.CreateRuntimeFunction(FTy, LaunchKernelName); |
Artem Belevich | c62214d | 2019-01-31 21:34:03 +0000 | [diff] [blame] | 338 | CGF.EmitCall(FI, CGCallee::forDirect(cudaLaunchKernelFn), ReturnValueSlot(), |
| 339 | LaunchKernelArgs); |
| 340 | CGF.EmitBranch(EndBlock); |
| 341 | |
| 342 | CGF.EmitBlock(EndBlock); |
| 343 | } |
| 344 | |
| 345 | void CGNVCUDARuntime::emitDeviceStubBodyLegacy(CodeGenFunction &CGF, |
| 346 | FunctionArgList &Args) { |
Justin Lebar | e56360a | 2016-07-27 22:36:21 +0000 | [diff] [blame] | 347 | // Emit a call to cudaSetupArgument for each arg in Args. |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 348 | llvm::FunctionCallee cudaSetupArgFn = getSetupArgumentFn(); |
Justin Lebar | e56360a | 2016-07-27 22:36:21 +0000 | [diff] [blame] | 349 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("setup.end"); |
| 350 | CharUnits Offset = CharUnits::Zero(); |
| 351 | for (const VarDecl *A : Args) { |
| 352 | CharUnits TyWidth, TyAlign; |
| 353 | std::tie(TyWidth, TyAlign) = |
| 354 | CGM.getContext().getTypeInfoInChars(A->getType()); |
| 355 | Offset = Offset.alignTo(TyAlign); |
| 356 | llvm::Value *Args[] = { |
| 357 | CGF.Builder.CreatePointerCast(CGF.GetAddrOfLocalVar(A).getPointer(), |
| 358 | VoidPtrTy), |
| 359 | llvm::ConstantInt::get(SizeTy, TyWidth.getQuantity()), |
| 360 | llvm::ConstantInt::get(SizeTy, Offset.getQuantity()), |
| 361 | }; |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 362 | llvm::CallBase *CB = CGF.EmitRuntimeCallOrInvoke(cudaSetupArgFn, Args); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 363 | llvm::Constant *Zero = llvm::ConstantInt::get(IntTy, 0); |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 364 | llvm::Value *CBZero = CGF.Builder.CreateICmpEQ(CB, Zero); |
Justin Lebar | e56360a | 2016-07-27 22:36:21 +0000 | [diff] [blame] | 365 | llvm::BasicBlock *NextBlock = CGF.createBasicBlock("setup.next"); |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 366 | CGF.Builder.CreateCondBr(CBZero, NextBlock, EndBlock); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 367 | CGF.EmitBlock(NextBlock); |
Justin Lebar | e56360a | 2016-07-27 22:36:21 +0000 | [diff] [blame] | 368 | Offset += TyWidth; |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // Emit the call to cudaLaunch |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 372 | llvm::FunctionCallee cudaLaunchFn = getLaunchFn(); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 373 | llvm::Value *Arg = CGF.Builder.CreatePointerCast(CGF.CurFn, CharPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 374 | CGF.EmitRuntimeCallOrInvoke(cudaLaunchFn, Arg); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 375 | CGF.EmitBranch(EndBlock); |
| 376 | |
| 377 | CGF.EmitBlock(EndBlock); |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 380 | /// Creates a function that sets up state on the host side for CUDA objects that |
| 381 | /// have a presence on both the host and device sides. Specifically, registers |
| 382 | /// the host side of kernel functions and device global variables with the CUDA |
| 383 | /// runtime. |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 384 | /// \code |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 385 | /// void __cuda_register_globals(void** GpuBinaryHandle) { |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 386 | /// __cudaRegisterFunction(GpuBinaryHandle,Kernel0,...); |
| 387 | /// ... |
| 388 | /// __cudaRegisterFunction(GpuBinaryHandle,KernelM,...); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 389 | /// __cudaRegisterVar(GpuBinaryHandle, GlobalVar0, ...); |
| 390 | /// ... |
| 391 | /// __cudaRegisterVar(GpuBinaryHandle, GlobalVarN, ...); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 392 | /// } |
| 393 | /// \endcode |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 394 | llvm::Function *CGNVCUDARuntime::makeRegisterGlobalsFn() { |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 395 | // No need to register anything |
| 396 | if (EmittedKernels.empty() && DeviceVars.empty()) |
| 397 | return nullptr; |
| 398 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 399 | llvm::Function *RegisterKernelsFunc = llvm::Function::Create( |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 400 | getRegisterGlobalsFnTy(), llvm::GlobalValue::InternalLinkage, |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 401 | addUnderscoredPrefixToName("_register_globals"), &TheModule); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 402 | llvm::BasicBlock *EntryBB = |
| 403 | llvm::BasicBlock::Create(Context, "entry", RegisterKernelsFunc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 404 | CGBuilderTy Builder(CGM, Context); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 405 | Builder.SetInsertPoint(EntryBB); |
| 406 | |
| 407 | // void __cudaRegisterFunction(void **, const char *, char *, const char *, |
| 408 | // int, uint3*, uint3*, dim3*, dim3*, int*) |
Benjamin Kramer | 6d1c10b | 2016-07-02 12:03:57 +0000 | [diff] [blame] | 409 | llvm::Type *RegisterFuncParams[] = { |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 410 | VoidPtrPtrTy, CharPtrTy, CharPtrTy, CharPtrTy, IntTy, |
| 411 | VoidPtrTy, VoidPtrTy, VoidPtrTy, VoidPtrTy, IntTy->getPointerTo()}; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 412 | llvm::FunctionCallee RegisterFunc = CGM.CreateRuntimeFunction( |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 413 | llvm::FunctionType::get(IntTy, RegisterFuncParams, false), |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 414 | addUnderscoredPrefixToName("RegisterFunction")); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 415 | |
| 416 | // Extract GpuBinaryHandle passed as the first argument passed to |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 417 | // __cuda_register_globals() and generate __cudaRegisterFunction() call for |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 418 | // each emitted kernel. |
| 419 | llvm::Argument &GpuBinaryHandlePtr = *RegisterKernelsFunc->arg_begin(); |
Yaxun Liu | c18e9ec | 2019-02-14 02:00:09 +0000 | [diff] [blame] | 420 | for (auto &&I : EmittedKernels) { |
| 421 | llvm::Constant *KernelName = makeConstantString(getDeviceSideName(I.D)); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 422 | llvm::Constant *NullPtr = llvm::ConstantPointerNull::get(VoidPtrTy); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 423 | llvm::Value *Args[] = { |
Yaxun Liu | c18e9ec | 2019-02-14 02:00:09 +0000 | [diff] [blame] | 424 | &GpuBinaryHandlePtr, |
| 425 | Builder.CreateBitCast(I.Kernel, VoidPtrTy), |
| 426 | KernelName, |
| 427 | KernelName, |
| 428 | llvm::ConstantInt::get(IntTy, -1), |
| 429 | NullPtr, |
| 430 | NullPtr, |
| 431 | NullPtr, |
| 432 | NullPtr, |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 433 | llvm::ConstantPointerNull::get(IntTy->getPointerTo())}; |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 434 | Builder.CreateCall(RegisterFunc, Args); |
| 435 | } |
| 436 | |
| 437 | // void __cudaRegisterVar(void **, char *, char *, const char *, |
| 438 | // int, int, int, int) |
Benjamin Kramer | 6d1c10b | 2016-07-02 12:03:57 +0000 | [diff] [blame] | 439 | llvm::Type *RegisterVarParams[] = {VoidPtrPtrTy, CharPtrTy, CharPtrTy, |
| 440 | CharPtrTy, IntTy, IntTy, |
| 441 | IntTy, IntTy}; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 442 | llvm::FunctionCallee RegisterVar = CGM.CreateRuntimeFunction( |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 443 | llvm::FunctionType::get(IntTy, RegisterVarParams, false), |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 444 | addUnderscoredPrefixToName("RegisterVar")); |
Yaxun Liu | c18e9ec | 2019-02-14 02:00:09 +0000 | [diff] [blame] | 445 | for (auto &&Info : DeviceVars) { |
| 446 | llvm::GlobalVariable *Var = Info.Var; |
| 447 | unsigned Flags = Info.Flag; |
| 448 | llvm::Constant *VarName = makeConstantString(getDeviceSideName(Info.D)); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 449 | uint64_t VarSize = |
| 450 | CGM.getDataLayout().getTypeAllocSize(Var->getValueType()); |
| 451 | llvm::Value *Args[] = { |
| 452 | &GpuBinaryHandlePtr, |
| 453 | Builder.CreateBitCast(Var, VoidPtrTy), |
| 454 | VarName, |
| 455 | VarName, |
| 456 | llvm::ConstantInt::get(IntTy, (Flags & ExternDeviceVar) ? 1 : 0), |
| 457 | llvm::ConstantInt::get(IntTy, VarSize), |
| 458 | llvm::ConstantInt::get(IntTy, (Flags & ConstantDeviceVar) ? 1 : 0), |
| 459 | llvm::ConstantInt::get(IntTy, 0)}; |
| 460 | Builder.CreateCall(RegisterVar, Args); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | Builder.CreateRetVoid(); |
| 464 | return RegisterKernelsFunc; |
| 465 | } |
| 466 | |
| 467 | /// Creates a global constructor function for the module: |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 468 | /// |
| 469 | /// For CUDA: |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 470 | /// \code |
| 471 | /// void __cuda_module_ctor(void*) { |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 472 | /// Handle = __cudaRegisterFatBinary(GpuBinaryBlob); |
| 473 | /// __cuda_register_globals(Handle); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 474 | /// } |
| 475 | /// \endcode |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 476 | /// |
| 477 | /// For HIP: |
| 478 | /// \code |
| 479 | /// void __hip_module_ctor(void*) { |
| 480 | /// if (__hip_gpubin_handle == 0) { |
| 481 | /// __hip_gpubin_handle = __hipRegisterFatBinary(GpuBinaryBlob); |
| 482 | /// __hip_register_globals(__hip_gpubin_handle); |
| 483 | /// } |
| 484 | /// } |
| 485 | /// \endcode |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 486 | llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() { |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 487 | bool IsHIP = CGM.getLangOpts().HIP; |
Aaron Enye Shi | 8129521 | 2019-04-02 20:49:41 +0000 | [diff] [blame] | 488 | bool IsCUDA = CGM.getLangOpts().CUDA; |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 489 | // No need to generate ctors/dtors if there is no GPU binary. |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 490 | StringRef CudaGpuBinaryFileName = CGM.getCodeGenOpts().CudaGpuBinaryFileName; |
| 491 | if (CudaGpuBinaryFileName.empty() && !IsHIP) |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 492 | return nullptr; |
Aaron Enye Shi | 6d37f32 | 2019-04-02 21:54:41 +0000 | [diff] [blame] | 493 | if ((IsHIP || (IsCUDA && !RelocatableDeviceCode)) && EmittedKernels.empty() && |
| 494 | DeviceVars.empty()) |
Aaron Enye Shi | 13d8e92 | 2019-04-02 20:10:18 +0000 | [diff] [blame] | 495 | return nullptr; |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 496 | |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 497 | // void __{cuda|hip}_register_globals(void* handle); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 498 | llvm::Function *RegisterGlobalsFunc = makeRegisterGlobalsFn(); |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 499 | // We always need a function to pass in as callback. Create a dummy |
| 500 | // implementation if we don't need to register anything. |
| 501 | if (RelocatableDeviceCode && !RegisterGlobalsFunc) |
| 502 | RegisterGlobalsFunc = makeDummyFunction(getRegisterGlobalsFnTy()); |
| 503 | |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 504 | // void ** __{cuda|hip}RegisterFatBinary(void *); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 505 | llvm::FunctionCallee RegisterFatbinFunc = CGM.CreateRuntimeFunction( |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 506 | llvm::FunctionType::get(VoidPtrPtrTy, VoidPtrTy, false), |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 507 | addUnderscoredPrefixToName("RegisterFatBinary")); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 508 | // struct { int magic, int version, void * gpu_binary, void * dont_care }; |
| 509 | llvm::StructType *FatbinWrapperTy = |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 510 | llvm::StructType::get(IntTy, IntTy, VoidPtrTy, VoidPtrTy); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 511 | |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 512 | // Register GPU binary with the CUDA runtime, store returned handle in a |
| 513 | // global variable and save a reference in GpuBinaryHandle to be cleaned up |
| 514 | // in destructor on exit. Then associate all known kernels with the GPU binary |
| 515 | // handle so CUDA runtime can figure out what to call on the GPU side. |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 516 | std::unique_ptr<llvm::MemoryBuffer> CudaGpuBinary = nullptr; |
| 517 | if (!CudaGpuBinaryFileName.empty()) { |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 518 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CudaGpuBinaryOrErr = |
| 519 | llvm::MemoryBuffer::getFileOrSTDIN(CudaGpuBinaryFileName); |
| 520 | if (std::error_code EC = CudaGpuBinaryOrErr.getError()) { |
| 521 | CGM.getDiags().Report(diag::err_cannot_open_file) |
| 522 | << CudaGpuBinaryFileName << EC.message(); |
| 523 | return nullptr; |
| 524 | } |
| 525 | CudaGpuBinary = std::move(CudaGpuBinaryOrErr.get()); |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 528 | llvm::Function *ModuleCtorFunc = llvm::Function::Create( |
| 529 | llvm::FunctionType::get(VoidTy, VoidPtrTy, false), |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 530 | llvm::GlobalValue::InternalLinkage, |
| 531 | addUnderscoredPrefixToName("_module_ctor"), &TheModule); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 532 | llvm::BasicBlock *CtorEntryBB = |
| 533 | llvm::BasicBlock::Create(Context, "entry", ModuleCtorFunc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 534 | CGBuilderTy CtorBuilder(CGM, Context); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 535 | |
| 536 | CtorBuilder.SetInsertPoint(CtorEntryBB); |
| 537 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 538 | const char *FatbinConstantName; |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 539 | const char *FatbinSectionName; |
| 540 | const char *ModuleIDSectionName; |
| 541 | StringRef ModuleIDPrefix; |
| 542 | llvm::Constant *FatBinStr; |
| 543 | unsigned FatMagic; |
| 544 | if (IsHIP) { |
| 545 | FatbinConstantName = ".hip_fatbin"; |
| 546 | FatbinSectionName = ".hipFatBinSegment"; |
| 547 | |
| 548 | ModuleIDSectionName = "__hip_module_id"; |
| 549 | ModuleIDPrefix = "__hip_"; |
| 550 | |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 551 | if (CudaGpuBinary) { |
| 552 | // If fatbin is available from early finalization, create a string |
| 553 | // literal containing the fat binary loaded from the given file. |
| 554 | FatBinStr = makeConstantString(CudaGpuBinary->getBuffer(), "", |
| 555 | FatbinConstantName, 8); |
| 556 | } else { |
| 557 | // If fatbin is not available, create an external symbol |
| 558 | // __hip_fatbin in section .hip_fatbin. The external symbol is supposed |
| 559 | // to contain the fat binary but will be populated somewhere else, |
| 560 | // e.g. by lld through link script. |
| 561 | FatBinStr = new llvm::GlobalVariable( |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 562 | CGM.getModule(), CGM.Int8Ty, |
| 563 | /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, nullptr, |
| 564 | "__hip_fatbin", nullptr, |
| 565 | llvm::GlobalVariable::NotThreadLocal); |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 566 | cast<llvm::GlobalVariable>(FatBinStr)->setSection(FatbinConstantName); |
| 567 | } |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 568 | |
| 569 | FatMagic = HIPFatMagic; |
| 570 | } else { |
| 571 | if (RelocatableDeviceCode) |
Artem Belevich | 5ce0a08 | 2018-06-28 17:15:52 +0000 | [diff] [blame] | 572 | FatbinConstantName = CGM.getTriple().isMacOSX() |
| 573 | ? "__NV_CUDA,__nv_relfatbin" |
| 574 | : "__nv_relfatbin"; |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 575 | else |
| 576 | FatbinConstantName = |
| 577 | CGM.getTriple().isMacOSX() ? "__NV_CUDA,__nv_fatbin" : ".nv_fatbin"; |
| 578 | // NVIDIA's cuobjdump looks for fatbins in this section. |
| 579 | FatbinSectionName = |
| 580 | CGM.getTriple().isMacOSX() ? "__NV_CUDA,__fatbin" : ".nvFatBinSegment"; |
| 581 | |
Artem Belevich | 5ce0a08 | 2018-06-28 17:15:52 +0000 | [diff] [blame] | 582 | ModuleIDSectionName = CGM.getTriple().isMacOSX() |
| 583 | ? "__NV_CUDA,__nv_module_id" |
| 584 | : "__nv_module_id"; |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 585 | ModuleIDPrefix = "__nv_"; |
| 586 | |
| 587 | // For CUDA, create a string literal containing the fat binary loaded from |
| 588 | // the given file. |
| 589 | FatBinStr = makeConstantString(CudaGpuBinary->getBuffer(), "", |
| 590 | FatbinConstantName, 8); |
| 591 | FatMagic = CudaFatMagic; |
| 592 | } |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 593 | |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 594 | // Create initialized wrapper structure that points to the loaded GPU binary |
| 595 | ConstantInitBuilder Builder(CGM); |
| 596 | auto Values = Builder.beginStruct(FatbinWrapperTy); |
| 597 | // Fatbin wrapper magic. |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 598 | Values.addInt(IntTy, FatMagic); |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 599 | // Fatbin version. |
| 600 | Values.addInt(IntTy, 1); |
| 601 | // Data. |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 602 | Values.add(FatBinStr); |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 603 | // Unused in fatbin v1. |
| 604 | Values.add(llvm::ConstantPointerNull::get(VoidPtrTy)); |
| 605 | llvm::GlobalVariable *FatbinWrapper = Values.finishAndCreateGlobal( |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 606 | addUnderscoredPrefixToName("_fatbin_wrapper"), CGM.getPointerAlign(), |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 607 | /*constant*/ true); |
| 608 | FatbinWrapper->setSection(FatbinSectionName); |
Justin Lebar | d14fe88 | 2016-11-18 00:41:31 +0000 | [diff] [blame] | 609 | |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 610 | // There is only one HIP fat binary per linked module, however there are |
| 611 | // multiple constructor functions. Make sure the fat binary is registered |
| 612 | // only once. The constructor functions are executed by the dynamic loader |
| 613 | // before the program gains control. The dynamic loader cannot execute the |
| 614 | // constructor functions concurrently since doing that would not guarantee |
| 615 | // thread safety of the loaded program. Therefore we can assume sequential |
| 616 | // execution of constructor functions here. |
| 617 | if (IsHIP) { |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 618 | auto Linkage = CudaGpuBinary ? llvm::GlobalValue::InternalLinkage : |
| 619 | llvm::GlobalValue::LinkOnceAnyLinkage; |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 620 | llvm::BasicBlock *IfBlock = |
| 621 | llvm::BasicBlock::Create(Context, "if", ModuleCtorFunc); |
| 622 | llvm::BasicBlock *ExitBlock = |
| 623 | llvm::BasicBlock::Create(Context, "exit", ModuleCtorFunc); |
| 624 | // The name, size, and initialization pattern of this variable is part |
| 625 | // of HIP ABI. |
| 626 | GpuBinaryHandle = new llvm::GlobalVariable( |
| 627 | TheModule, VoidPtrPtrTy, /*isConstant=*/false, |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 628 | Linkage, |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 629 | /*Initializer=*/llvm::ConstantPointerNull::get(VoidPtrPtrTy), |
| 630 | "__hip_gpubin_handle"); |
Guillaume Chatelet | c79099e | 2019-10-03 13:00:29 +0000 | [diff] [blame^] | 631 | GpuBinaryHandle->setAlignment(CGM.getPointerAlign().getAsAlign()); |
Yaxun Liu | 94ff57f | 2018-08-17 17:47:31 +0000 | [diff] [blame] | 632 | // Prevent the weak symbol in different shared libraries being merged. |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 633 | if (Linkage != llvm::GlobalValue::InternalLinkage) |
| 634 | GpuBinaryHandle->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 635 | Address GpuBinaryAddr( |
| 636 | GpuBinaryHandle, |
| 637 | CharUnits::fromQuantity(GpuBinaryHandle->getAlignment())); |
| 638 | { |
| 639 | auto HandleValue = CtorBuilder.CreateLoad(GpuBinaryAddr); |
| 640 | llvm::Constant *Zero = |
| 641 | llvm::Constant::getNullValue(HandleValue->getType()); |
| 642 | llvm::Value *EQZero = CtorBuilder.CreateICmpEQ(HandleValue, Zero); |
| 643 | CtorBuilder.CreateCondBr(EQZero, IfBlock, ExitBlock); |
| 644 | } |
| 645 | { |
| 646 | CtorBuilder.SetInsertPoint(IfBlock); |
| 647 | // GpuBinaryHandle = __hipRegisterFatBinary(&FatbinWrapper); |
| 648 | llvm::CallInst *RegisterFatbinCall = CtorBuilder.CreateCall( |
| 649 | RegisterFatbinFunc, |
| 650 | CtorBuilder.CreateBitCast(FatbinWrapper, VoidPtrTy)); |
| 651 | CtorBuilder.CreateStore(RegisterFatbinCall, GpuBinaryAddr); |
| 652 | CtorBuilder.CreateBr(ExitBlock); |
| 653 | } |
| 654 | { |
| 655 | CtorBuilder.SetInsertPoint(ExitBlock); |
| 656 | // Call __hip_register_globals(GpuBinaryHandle); |
| 657 | if (RegisterGlobalsFunc) { |
| 658 | auto HandleValue = CtorBuilder.CreateLoad(GpuBinaryAddr); |
| 659 | CtorBuilder.CreateCall(RegisterGlobalsFunc, HandleValue); |
| 660 | } |
| 661 | } |
| 662 | } else if (!RelocatableDeviceCode) { |
| 663 | // Register binary with CUDA runtime. This is substantially different in |
| 664 | // default mode vs. separate compilation! |
| 665 | // GpuBinaryHandle = __cudaRegisterFatBinary(&FatbinWrapper); |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 666 | llvm::CallInst *RegisterFatbinCall = CtorBuilder.CreateCall( |
| 667 | RegisterFatbinFunc, |
| 668 | CtorBuilder.CreateBitCast(FatbinWrapper, VoidPtrTy)); |
| 669 | GpuBinaryHandle = new llvm::GlobalVariable( |
| 670 | TheModule, VoidPtrPtrTy, false, llvm::GlobalValue::InternalLinkage, |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 671 | llvm::ConstantPointerNull::get(VoidPtrPtrTy), "__cuda_gpubin_handle"); |
Guillaume Chatelet | c79099e | 2019-10-03 13:00:29 +0000 | [diff] [blame^] | 672 | GpuBinaryHandle->setAlignment(CGM.getPointerAlign().getAsAlign()); |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 673 | CtorBuilder.CreateAlignedStore(RegisterFatbinCall, GpuBinaryHandle, |
| 674 | CGM.getPointerAlign()); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 675 | |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 676 | // Call __cuda_register_globals(GpuBinaryHandle); |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 677 | if (RegisterGlobalsFunc) |
| 678 | CtorBuilder.CreateCall(RegisterGlobalsFunc, RegisterFatbinCall); |
Artem Belevich | 4071763 | 2019-02-05 22:38:58 +0000 | [diff] [blame] | 679 | |
| 680 | // Call __cudaRegisterFatBinaryEnd(Handle) if this CUDA version needs it. |
| 681 | if (CudaFeatureEnabled(CGM.getTarget().getSDKVersion(), |
| 682 | CudaFeature::CUDA_USES_FATBIN_REGISTER_END)) { |
| 683 | // void __cudaRegisterFatBinaryEnd(void **); |
| 684 | llvm::FunctionCallee RegisterFatbinEndFunc = CGM.CreateRuntimeFunction( |
| 685 | llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false), |
| 686 | "__cudaRegisterFatBinaryEnd"); |
| 687 | CtorBuilder.CreateCall(RegisterFatbinEndFunc, RegisterFatbinCall); |
| 688 | } |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 689 | } else { |
| 690 | // Generate a unique module ID. |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 691 | SmallString<64> ModuleID; |
| 692 | llvm::raw_svector_ostream OS(ModuleID); |
Artem Belevich | 93552b3 | 2018-10-05 18:39:58 +0000 | [diff] [blame] | 693 | OS << ModuleIDPrefix << llvm::format("%" PRIx64, FatbinWrapper->getGUID()); |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 694 | llvm::Constant *ModuleIDConstant = |
| 695 | makeConstantString(ModuleID.str(), "", ModuleIDSectionName, 32); |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 696 | |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 697 | // Create an alias for the FatbinWrapper that nvcc will look for. |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 698 | llvm::GlobalAlias::create(llvm::GlobalValue::ExternalLinkage, |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 699 | Twine("__fatbinwrap") + ModuleID, FatbinWrapper); |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 700 | |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 701 | // void __cudaRegisterLinkedBinary%ModuleID%(void (*)(void *), void *, |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 702 | // void *, void (*)(void **)) |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 703 | SmallString<128> RegisterLinkedBinaryName("__cudaRegisterLinkedBinary"); |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 704 | RegisterLinkedBinaryName += ModuleID; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 705 | llvm::FunctionCallee RegisterLinkedBinaryFunc = CGM.CreateRuntimeFunction( |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 706 | getRegisterLinkedBinaryFnTy(), RegisterLinkedBinaryName); |
| 707 | |
| 708 | assert(RegisterGlobalsFunc && "Expecting at least dummy function!"); |
| 709 | llvm::Value *Args[] = {RegisterGlobalsFunc, |
| 710 | CtorBuilder.CreateBitCast(FatbinWrapper, VoidPtrTy), |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame] | 711 | ModuleIDConstant, |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 712 | makeDummyFunction(getCallbackFnTy())}; |
| 713 | CtorBuilder.CreateCall(RegisterLinkedBinaryFunc, Args); |
| 714 | } |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 715 | |
Artem Belevich | c66d254 | 2018-06-27 18:32:51 +0000 | [diff] [blame] | 716 | // Create destructor and register it with atexit() the way NVCC does it. Doing |
| 717 | // it during regular destructor phase worked in CUDA before 9.2 but results in |
| 718 | // double-free in 9.2. |
| 719 | if (llvm::Function *CleanupFn = makeModuleDtorFunction()) { |
| 720 | // extern "C" int atexit(void (*f)(void)); |
| 721 | llvm::FunctionType *AtExitTy = |
| 722 | llvm::FunctionType::get(IntTy, CleanupFn->getType(), false); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 723 | llvm::FunctionCallee AtExitFunc = |
Artem Belevich | c66d254 | 2018-06-27 18:32:51 +0000 | [diff] [blame] | 724 | CGM.CreateRuntimeFunction(AtExitTy, "atexit", llvm::AttributeList(), |
| 725 | /*Local=*/true); |
| 726 | CtorBuilder.CreateCall(AtExitFunc, CleanupFn); |
| 727 | } |
| 728 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 729 | CtorBuilder.CreateRetVoid(); |
| 730 | return ModuleCtorFunc; |
| 731 | } |
| 732 | |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 733 | /// Creates a global destructor function that unregisters the GPU code blob |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 734 | /// registered by constructor. |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 735 | /// |
| 736 | /// For CUDA: |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 737 | /// \code |
| 738 | /// void __cuda_module_dtor(void*) { |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 739 | /// __cudaUnregisterFatBinary(Handle); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 740 | /// } |
| 741 | /// \endcode |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 742 | /// |
| 743 | /// For HIP: |
| 744 | /// \code |
| 745 | /// void __hip_module_dtor(void*) { |
| 746 | /// if (__hip_gpubin_handle) { |
| 747 | /// __hipUnregisterFatBinary(__hip_gpubin_handle); |
| 748 | /// __hip_gpubin_handle = 0; |
| 749 | /// } |
| 750 | /// } |
| 751 | /// \endcode |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 752 | llvm::Function *CGNVCUDARuntime::makeModuleDtorFunction() { |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 753 | // No need for destructor if we don't have a handle to unregister. |
| 754 | if (!GpuBinaryHandle) |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 755 | return nullptr; |
| 756 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 757 | // void __cudaUnregisterFatBinary(void ** handle); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 758 | llvm::FunctionCallee UnregisterFatbinFunc = CGM.CreateRuntimeFunction( |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 759 | llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false), |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 760 | addUnderscoredPrefixToName("UnregisterFatBinary")); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 761 | |
| 762 | llvm::Function *ModuleDtorFunc = llvm::Function::Create( |
| 763 | llvm::FunctionType::get(VoidTy, VoidPtrTy, false), |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 764 | llvm::GlobalValue::InternalLinkage, |
| 765 | addUnderscoredPrefixToName("_module_dtor"), &TheModule); |
| 766 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 767 | llvm::BasicBlock *DtorEntryBB = |
| 768 | llvm::BasicBlock::Create(Context, "entry", ModuleDtorFunc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 769 | CGBuilderTy DtorBuilder(CGM, Context); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 770 | DtorBuilder.SetInsertPoint(DtorEntryBB); |
| 771 | |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 772 | Address GpuBinaryAddr(GpuBinaryHandle, CharUnits::fromQuantity( |
| 773 | GpuBinaryHandle->getAlignment())); |
| 774 | auto HandleValue = DtorBuilder.CreateLoad(GpuBinaryAddr); |
| 775 | // There is only one HIP fat binary per linked module, however there are |
| 776 | // multiple destructor functions. Make sure the fat binary is unregistered |
| 777 | // only once. |
| 778 | if (CGM.getLangOpts().HIP) { |
| 779 | llvm::BasicBlock *IfBlock = |
| 780 | llvm::BasicBlock::Create(Context, "if", ModuleDtorFunc); |
| 781 | llvm::BasicBlock *ExitBlock = |
| 782 | llvm::BasicBlock::Create(Context, "exit", ModuleDtorFunc); |
| 783 | llvm::Constant *Zero = llvm::Constant::getNullValue(HandleValue->getType()); |
| 784 | llvm::Value *NEZero = DtorBuilder.CreateICmpNE(HandleValue, Zero); |
| 785 | DtorBuilder.CreateCondBr(NEZero, IfBlock, ExitBlock); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 786 | |
Yaxun Liu | f99752b | 2018-07-20 22:45:24 +0000 | [diff] [blame] | 787 | DtorBuilder.SetInsertPoint(IfBlock); |
| 788 | DtorBuilder.CreateCall(UnregisterFatbinFunc, HandleValue); |
| 789 | DtorBuilder.CreateStore(Zero, GpuBinaryAddr); |
| 790 | DtorBuilder.CreateBr(ExitBlock); |
| 791 | |
| 792 | DtorBuilder.SetInsertPoint(ExitBlock); |
| 793 | } else { |
| 794 | DtorBuilder.CreateCall(UnregisterFatbinFunc, HandleValue); |
| 795 | } |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 796 | DtorBuilder.CreateRetVoid(); |
| 797 | return ModuleDtorFunc; |
| 798 | } |
| 799 | |
Michael Liao | e40f879 | 2019-06-17 12:51:36 +0000 | [diff] [blame] | 800 | std::string CGNVCUDARuntime::getDeviceStubName(llvm::StringRef Name) const { |
| 801 | if (!CGM.getLangOpts().HIP) |
| 802 | return Name; |
Kadir Cetinkaya | 3c9391a | 2019-06-17 14:23:06 +0000 | [diff] [blame] | 803 | return (Name + ".stub").str(); |
Michael Liao | e40f879 | 2019-06-17 12:51:36 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 806 | CGCUDARuntime *CodeGen::CreateNVCUDARuntime(CodeGenModule &CGM) { |
| 807 | return new CGNVCUDARuntime(CGM); |
| 808 | } |