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