Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 1 | //===----- CGCUDANV.cpp - Interface to NVIDIA CUDA Runtime ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This provides a class for CUDA code generation targeting the NVIDIA CUDA |
| 11 | // runtime library. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "CGCUDARuntime.h" |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 16 | #include "CodeGenFunction.h" |
| 17 | #include "CodeGenModule.h" |
| 18 | #include "clang/AST/Decl.h" |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 19 | #include "clang/CodeGen/ConstantInitBuilder.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 20 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | c80ceea | 2014-03-04 11:02:08 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 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 |
| 45 | llvm::SmallVector<llvm::Function *, 16> EmittedKernels; |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 46 | llvm::SmallVector<std::pair<llvm::GlobalVariable *, unsigned>, 16> DeviceVars; |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 47 | /// Keeps track of variable containing handle of GPU binary. Populated by |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 48 | /// ModuleCtorFunction() and used to create corresponding cleanup calls in |
| 49 | /// ModuleDtorFunction() |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 50 | llvm::GlobalVariable *GpuBinaryHandle = nullptr; |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 51 | /// Whether we generate relocatable device code. |
| 52 | bool RelocatableDeviceCode; |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 53 | |
| 54 | llvm::Constant *getSetupArgumentFn() const; |
| 55 | llvm::Constant *getLaunchFn() const; |
| 56 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 57 | llvm::FunctionType *getRegisterGlobalsFnTy() const; |
| 58 | llvm::FunctionType *getCallbackFnTy() const; |
| 59 | llvm::FunctionType *getRegisterLinkedBinaryFnTy() const; |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 60 | std::string addPrefixToName(StringRef FuncName) const; |
| 61 | std::string addUnderscoredPrefixToName(StringRef FuncName) const; |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 62 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 63 | /// Creates a function to register all kernel stubs generated in this module. |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 64 | llvm::Function *makeRegisterGlobalsFn(); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 65 | |
| 66 | /// Helper function that generates a constant string and returns a pointer to |
| 67 | /// the start of the string. The result of this function can be used anywhere |
| 68 | /// where the C code specifies const char*. |
| 69 | llvm::Constant *makeConstantString(const std::string &Str, |
| 70 | const std::string &Name = "", |
Artem Belevich | 4c09318 | 2016-08-12 18:44:01 +0000 | [diff] [blame] | 71 | const std::string &SectionName = "", |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 72 | unsigned Alignment = 0) { |
| 73 | llvm::Constant *Zeros[] = {llvm::ConstantInt::get(SizeTy, 0), |
| 74 | llvm::ConstantInt::get(SizeTy, 0)}; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 75 | auto ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str()); |
Artem Belevich | 4c09318 | 2016-08-12 18:44:01 +0000 | [diff] [blame] | 76 | llvm::GlobalVariable *GV = |
| 77 | cast<llvm::GlobalVariable>(ConstStr.getPointer()); |
| 78 | if (!SectionName.empty()) |
| 79 | GV->setSection(SectionName); |
| 80 | if (Alignment) |
| 81 | GV->setAlignment(Alignment); |
| 82 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 83 | return llvm::ConstantExpr::getGetElementPtr(ConstStr.getElementType(), |
| 84 | ConstStr.getPointer(), Zeros); |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /// Helper function that generates an empty dummy function returning void. |
| 88 | llvm::Function *makeDummyFunction(llvm::FunctionType *FnTy) { |
| 89 | assert(FnTy->getReturnType()->isVoidTy() && |
| 90 | "Can only generate dummy functions returning void!"); |
| 91 | llvm::Function *DummyFunc = llvm::Function::Create( |
| 92 | FnTy, llvm::GlobalValue::InternalLinkage, "dummy", &TheModule); |
| 93 | |
| 94 | llvm::BasicBlock *DummyBlock = |
| 95 | llvm::BasicBlock::Create(Context, "", DummyFunc); |
| 96 | CGBuilderTy FuncBuilder(CGM, Context); |
| 97 | FuncBuilder.SetInsertPoint(DummyBlock); |
| 98 | FuncBuilder.CreateRetVoid(); |
| 99 | |
| 100 | return DummyFunc; |
| 101 | } |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 102 | |
| 103 | void emitDeviceStubBody(CodeGenFunction &CGF, FunctionArgList &Args); |
| 104 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 105 | public: |
| 106 | CGNVCUDARuntime(CodeGenModule &CGM); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 107 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 108 | void emitDeviceStub(CodeGenFunction &CGF, FunctionArgList &Args) override; |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 109 | void registerDeviceVar(llvm::GlobalVariable &Var, unsigned Flags) override { |
| 110 | DeviceVars.push_back(std::make_pair(&Var, Flags)); |
| 111 | } |
| 112 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 113 | /// Creates module constructor function |
| 114 | llvm::Function *makeModuleCtorFunction() override; |
| 115 | /// Creates module destructor function |
| 116 | llvm::Function *makeModuleDtorFunction() override; |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 119 | } |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 120 | |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 121 | std::string CGNVCUDARuntime::addPrefixToName(StringRef FuncName) const { |
| 122 | if (CGM.getLangOpts().HIP) |
| 123 | return ((Twine("hip") + Twine(FuncName)).str()); |
| 124 | return ((Twine("cuda") + Twine(FuncName)).str()); |
| 125 | } |
| 126 | std::string |
| 127 | CGNVCUDARuntime::addUnderscoredPrefixToName(StringRef FuncName) const { |
| 128 | if (CGM.getLangOpts().HIP) |
| 129 | return ((Twine("__hip") + Twine(FuncName)).str()); |
| 130 | return ((Twine("__cuda") + Twine(FuncName)).str()); |
| 131 | } |
| 132 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 133 | CGNVCUDARuntime::CGNVCUDARuntime(CodeGenModule &CGM) |
| 134 | : CGCUDARuntime(CGM), Context(CGM.getLLVMContext()), |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 135 | TheModule(CGM.getModule()), |
| 136 | RelocatableDeviceCode(CGM.getLangOpts().CUDARelocatableDeviceCode) { |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 137 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 138 | ASTContext &Ctx = CGM.getContext(); |
| 139 | |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 140 | IntTy = CGM.IntTy; |
| 141 | SizeTy = CGM.SizeTy; |
| 142 | VoidTy = CGM.VoidTy; |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 143 | |
| 144 | CharPtrTy = llvm::PointerType::getUnqual(Types.ConvertType(Ctx.CharTy)); |
| 145 | VoidPtrTy = cast<llvm::PointerType>(Types.ConvertType(Ctx.VoidPtrTy)); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 146 | VoidPtrPtrTy = VoidPtrTy->getPointerTo(); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | llvm::Constant *CGNVCUDARuntime::getSetupArgumentFn() const { |
| 150 | // cudaError_t cudaSetupArgument(void *, size_t, size_t) |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 151 | llvm::Type *Params[] = {VoidPtrTy, SizeTy, SizeTy}; |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 152 | return CGM.CreateRuntimeFunction( |
| 153 | llvm::FunctionType::get(IntTy, Params, false), |
| 154 | addPrefixToName("SetupArgument")); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | llvm::Constant *CGNVCUDARuntime::getLaunchFn() const { |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 158 | if (CGM.getLangOpts().HIP) { |
| 159 | // hipError_t hipLaunchByPtr(char *); |
| 160 | return CGM.CreateRuntimeFunction( |
| 161 | llvm::FunctionType::get(IntTy, CharPtrTy, false), "hipLaunchByPtr"); |
| 162 | } else { |
| 163 | // cudaError_t cudaLaunch(char *); |
| 164 | return CGM.CreateRuntimeFunction( |
| 165 | llvm::FunctionType::get(IntTy, CharPtrTy, false), "cudaLaunch"); |
| 166 | } |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 169 | llvm::FunctionType *CGNVCUDARuntime::getRegisterGlobalsFnTy() const { |
| 170 | return llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false); |
| 171 | } |
| 172 | |
| 173 | llvm::FunctionType *CGNVCUDARuntime::getCallbackFnTy() const { |
| 174 | return llvm::FunctionType::get(VoidTy, VoidPtrTy, false); |
| 175 | } |
| 176 | |
| 177 | llvm::FunctionType *CGNVCUDARuntime::getRegisterLinkedBinaryFnTy() const { |
| 178 | auto CallbackFnTy = getCallbackFnTy(); |
| 179 | auto RegisterGlobalsFnTy = getRegisterGlobalsFnTy(); |
| 180 | llvm::Type *Params[] = {RegisterGlobalsFnTy->getPointerTo(), VoidPtrTy, |
| 181 | VoidPtrTy, CallbackFnTy->getPointerTo()}; |
| 182 | return llvm::FunctionType::get(VoidTy, Params, false); |
| 183 | } |
| 184 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 185 | void CGNVCUDARuntime::emitDeviceStub(CodeGenFunction &CGF, |
| 186 | FunctionArgList &Args) { |
| 187 | EmittedKernels.push_back(CGF.CurFn); |
| 188 | emitDeviceStubBody(CGF, Args); |
| 189 | } |
| 190 | |
| 191 | void CGNVCUDARuntime::emitDeviceStubBody(CodeGenFunction &CGF, |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 192 | FunctionArgList &Args) { |
Justin Lebar | e56360a | 2016-07-27 22:36:21 +0000 | [diff] [blame] | 193 | // Emit a call to cudaSetupArgument for each arg in Args. |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 194 | llvm::Constant *cudaSetupArgFn = getSetupArgumentFn(); |
Justin Lebar | e56360a | 2016-07-27 22:36:21 +0000 | [diff] [blame] | 195 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("setup.end"); |
| 196 | CharUnits Offset = CharUnits::Zero(); |
| 197 | for (const VarDecl *A : Args) { |
| 198 | CharUnits TyWidth, TyAlign; |
| 199 | std::tie(TyWidth, TyAlign) = |
| 200 | CGM.getContext().getTypeInfoInChars(A->getType()); |
| 201 | Offset = Offset.alignTo(TyAlign); |
| 202 | llvm::Value *Args[] = { |
| 203 | CGF.Builder.CreatePointerCast(CGF.GetAddrOfLocalVar(A).getPointer(), |
| 204 | VoidPtrTy), |
| 205 | llvm::ConstantInt::get(SizeTy, TyWidth.getQuantity()), |
| 206 | llvm::ConstantInt::get(SizeTy, Offset.getQuantity()), |
| 207 | }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 208 | llvm::CallSite CS = CGF.EmitRuntimeCallOrInvoke(cudaSetupArgFn, Args); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 209 | llvm::Constant *Zero = llvm::ConstantInt::get(IntTy, 0); |
| 210 | llvm::Value *CSZero = CGF.Builder.CreateICmpEQ(CS.getInstruction(), Zero); |
Justin Lebar | e56360a | 2016-07-27 22:36:21 +0000 | [diff] [blame] | 211 | llvm::BasicBlock *NextBlock = CGF.createBasicBlock("setup.next"); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 212 | CGF.Builder.CreateCondBr(CSZero, NextBlock, EndBlock); |
| 213 | CGF.EmitBlock(NextBlock); |
Justin Lebar | e56360a | 2016-07-27 22:36:21 +0000 | [diff] [blame] | 214 | Offset += TyWidth; |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // Emit the call to cudaLaunch |
| 218 | llvm::Constant *cudaLaunchFn = getLaunchFn(); |
| 219 | llvm::Value *Arg = CGF.Builder.CreatePointerCast(CGF.CurFn, CharPtrTy); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 220 | CGF.EmitRuntimeCallOrInvoke(cudaLaunchFn, Arg); |
Peter Collingbourne | fa4d603 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 221 | CGF.EmitBranch(EndBlock); |
| 222 | |
| 223 | CGF.EmitBlock(EndBlock); |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 226 | /// Creates a function that sets up state on the host side for CUDA objects that |
| 227 | /// have a presence on both the host and device sides. Specifically, registers |
| 228 | /// the host side of kernel functions and device global variables with the CUDA |
| 229 | /// runtime. |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 230 | /// \code |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 231 | /// void __cuda_register_globals(void** GpuBinaryHandle) { |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 232 | /// __cudaRegisterFunction(GpuBinaryHandle,Kernel0,...); |
| 233 | /// ... |
| 234 | /// __cudaRegisterFunction(GpuBinaryHandle,KernelM,...); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 235 | /// __cudaRegisterVar(GpuBinaryHandle, GlobalVar0, ...); |
| 236 | /// ... |
| 237 | /// __cudaRegisterVar(GpuBinaryHandle, GlobalVarN, ...); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 238 | /// } |
| 239 | /// \endcode |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 240 | llvm::Function *CGNVCUDARuntime::makeRegisterGlobalsFn() { |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 241 | // No need to register anything |
| 242 | if (EmittedKernels.empty() && DeviceVars.empty()) |
| 243 | return nullptr; |
| 244 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 245 | llvm::Function *RegisterKernelsFunc = llvm::Function::Create( |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 246 | getRegisterGlobalsFnTy(), llvm::GlobalValue::InternalLinkage, |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 247 | addUnderscoredPrefixToName("_register_globals"), &TheModule); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 248 | llvm::BasicBlock *EntryBB = |
| 249 | llvm::BasicBlock::Create(Context, "entry", RegisterKernelsFunc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 250 | CGBuilderTy Builder(CGM, Context); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 251 | Builder.SetInsertPoint(EntryBB); |
| 252 | |
| 253 | // void __cudaRegisterFunction(void **, const char *, char *, const char *, |
| 254 | // int, uint3*, uint3*, dim3*, dim3*, int*) |
Benjamin Kramer | 6d1c10b | 2016-07-02 12:03:57 +0000 | [diff] [blame] | 255 | llvm::Type *RegisterFuncParams[] = { |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 256 | VoidPtrPtrTy, CharPtrTy, CharPtrTy, CharPtrTy, IntTy, |
| 257 | VoidPtrTy, VoidPtrTy, VoidPtrTy, VoidPtrTy, IntTy->getPointerTo()}; |
| 258 | llvm::Constant *RegisterFunc = CGM.CreateRuntimeFunction( |
| 259 | llvm::FunctionType::get(IntTy, RegisterFuncParams, false), |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 260 | addUnderscoredPrefixToName("RegisterFunction")); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 261 | |
| 262 | // Extract GpuBinaryHandle passed as the first argument passed to |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 263 | // __cuda_register_globals() and generate __cudaRegisterFunction() call for |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 264 | // each emitted kernel. |
| 265 | llvm::Argument &GpuBinaryHandlePtr = *RegisterKernelsFunc->arg_begin(); |
| 266 | for (llvm::Function *Kernel : EmittedKernels) { |
| 267 | llvm::Constant *KernelName = makeConstantString(Kernel->getName()); |
| 268 | llvm::Constant *NullPtr = llvm::ConstantPointerNull::get(VoidPtrTy); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 269 | llvm::Value *Args[] = { |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 270 | &GpuBinaryHandlePtr, Builder.CreateBitCast(Kernel, VoidPtrTy), |
| 271 | KernelName, KernelName, llvm::ConstantInt::get(IntTy, -1), NullPtr, |
| 272 | NullPtr, NullPtr, NullPtr, |
| 273 | llvm::ConstantPointerNull::get(IntTy->getPointerTo())}; |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 274 | Builder.CreateCall(RegisterFunc, Args); |
| 275 | } |
| 276 | |
| 277 | // void __cudaRegisterVar(void **, char *, char *, const char *, |
| 278 | // int, int, int, int) |
Benjamin Kramer | 6d1c10b | 2016-07-02 12:03:57 +0000 | [diff] [blame] | 279 | llvm::Type *RegisterVarParams[] = {VoidPtrPtrTy, CharPtrTy, CharPtrTy, |
| 280 | CharPtrTy, IntTy, IntTy, |
| 281 | IntTy, IntTy}; |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 282 | llvm::Constant *RegisterVar = CGM.CreateRuntimeFunction( |
| 283 | llvm::FunctionType::get(IntTy, RegisterVarParams, false), |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 284 | addUnderscoredPrefixToName("RegisterVar")); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 285 | for (auto &Pair : DeviceVars) { |
| 286 | llvm::GlobalVariable *Var = Pair.first; |
| 287 | unsigned Flags = Pair.second; |
| 288 | llvm::Constant *VarName = makeConstantString(Var->getName()); |
| 289 | uint64_t VarSize = |
| 290 | CGM.getDataLayout().getTypeAllocSize(Var->getValueType()); |
| 291 | llvm::Value *Args[] = { |
| 292 | &GpuBinaryHandlePtr, |
| 293 | Builder.CreateBitCast(Var, VoidPtrTy), |
| 294 | VarName, |
| 295 | VarName, |
| 296 | llvm::ConstantInt::get(IntTy, (Flags & ExternDeviceVar) ? 1 : 0), |
| 297 | llvm::ConstantInt::get(IntTy, VarSize), |
| 298 | llvm::ConstantInt::get(IntTy, (Flags & ConstantDeviceVar) ? 1 : 0), |
| 299 | llvm::ConstantInt::get(IntTy, 0)}; |
| 300 | Builder.CreateCall(RegisterVar, Args); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | Builder.CreateRetVoid(); |
| 304 | return RegisterKernelsFunc; |
| 305 | } |
| 306 | |
| 307 | /// Creates a global constructor function for the module: |
| 308 | /// \code |
| 309 | /// void __cuda_module_ctor(void*) { |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 310 | /// Handle = __cudaRegisterFatBinary(GpuBinaryBlob); |
| 311 | /// __cuda_register_globals(Handle); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 312 | /// } |
| 313 | /// \endcode |
| 314 | llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() { |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 315 | bool IsHIP = CGM.getLangOpts().HIP; |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 316 | // No need to generate ctors/dtors if there is no GPU binary. |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 317 | StringRef CudaGpuBinaryFileName = CGM.getCodeGenOpts().CudaGpuBinaryFileName; |
| 318 | if (CudaGpuBinaryFileName.empty() && !IsHIP) |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 319 | return nullptr; |
| 320 | |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 321 | // void __{cuda|hip}_register_globals(void* handle); |
Artem Belevich | 42e1949 | 2016-03-02 18:28:50 +0000 | [diff] [blame] | 322 | llvm::Function *RegisterGlobalsFunc = makeRegisterGlobalsFn(); |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 323 | // We always need a function to pass in as callback. Create a dummy |
| 324 | // implementation if we don't need to register anything. |
| 325 | if (RelocatableDeviceCode && !RegisterGlobalsFunc) |
| 326 | RegisterGlobalsFunc = makeDummyFunction(getRegisterGlobalsFnTy()); |
| 327 | |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 328 | // void ** __{cuda|hip}RegisterFatBinary(void *); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 329 | llvm::Constant *RegisterFatbinFunc = CGM.CreateRuntimeFunction( |
| 330 | llvm::FunctionType::get(VoidPtrPtrTy, VoidPtrTy, false), |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 331 | addUnderscoredPrefixToName("RegisterFatBinary")); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 332 | // struct { int magic, int version, void * gpu_binary, void * dont_care }; |
| 333 | llvm::StructType *FatbinWrapperTy = |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 334 | llvm::StructType::get(IntTy, IntTy, VoidPtrTy, VoidPtrTy); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 335 | |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 336 | // Register GPU binary with the CUDA runtime, store returned handle in a |
| 337 | // global variable and save a reference in GpuBinaryHandle to be cleaned up |
| 338 | // in destructor on exit. Then associate all known kernels with the GPU binary |
| 339 | // handle so CUDA runtime can figure out what to call on the GPU side. |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 340 | std::unique_ptr<llvm::MemoryBuffer> CudaGpuBinary; |
| 341 | if (!IsHIP) { |
| 342 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CudaGpuBinaryOrErr = |
| 343 | llvm::MemoryBuffer::getFileOrSTDIN(CudaGpuBinaryFileName); |
| 344 | if (std::error_code EC = CudaGpuBinaryOrErr.getError()) { |
| 345 | CGM.getDiags().Report(diag::err_cannot_open_file) |
| 346 | << CudaGpuBinaryFileName << EC.message(); |
| 347 | return nullptr; |
| 348 | } |
| 349 | CudaGpuBinary = std::move(CudaGpuBinaryOrErr.get()); |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 352 | llvm::Function *ModuleCtorFunc = llvm::Function::Create( |
| 353 | llvm::FunctionType::get(VoidTy, VoidPtrTy, false), |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 354 | llvm::GlobalValue::InternalLinkage, |
| 355 | addUnderscoredPrefixToName("_module_ctor"), &TheModule); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 356 | llvm::BasicBlock *CtorEntryBB = |
| 357 | llvm::BasicBlock::Create(Context, "entry", ModuleCtorFunc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 358 | CGBuilderTy CtorBuilder(CGM, Context); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 359 | |
| 360 | CtorBuilder.SetInsertPoint(CtorEntryBB); |
| 361 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 362 | const char *FatbinConstantName; |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 363 | const char *FatbinSectionName; |
| 364 | const char *ModuleIDSectionName; |
| 365 | StringRef ModuleIDPrefix; |
| 366 | llvm::Constant *FatBinStr; |
| 367 | unsigned FatMagic; |
| 368 | if (IsHIP) { |
| 369 | FatbinConstantName = ".hip_fatbin"; |
| 370 | FatbinSectionName = ".hipFatBinSegment"; |
| 371 | |
| 372 | ModuleIDSectionName = "__hip_module_id"; |
| 373 | ModuleIDPrefix = "__hip_"; |
| 374 | |
| 375 | // For HIP, create an external symbol __hip_fatbin in section .hip_fatbin. |
| 376 | // The external symbol is supposed to contain the fat binary but will be |
| 377 | // populated somewhere else, e.g. by lld through link script. |
| 378 | FatBinStr = new llvm::GlobalVariable( |
| 379 | CGM.getModule(), CGM.Int8Ty, |
| 380 | /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, nullptr, |
| 381 | "__hip_fatbin", nullptr, |
| 382 | llvm::GlobalVariable::NotThreadLocal); |
| 383 | cast<llvm::GlobalVariable>(FatBinStr)->setSection(FatbinConstantName); |
| 384 | |
| 385 | FatMagic = HIPFatMagic; |
| 386 | } else { |
| 387 | if (RelocatableDeviceCode) |
| 388 | // TODO: Figure out how this is called on mac OS! |
| 389 | FatbinConstantName = "__nv_relfatbin"; |
| 390 | else |
| 391 | FatbinConstantName = |
| 392 | CGM.getTriple().isMacOSX() ? "__NV_CUDA,__nv_fatbin" : ".nv_fatbin"; |
| 393 | // NVIDIA's cuobjdump looks for fatbins in this section. |
| 394 | FatbinSectionName = |
| 395 | CGM.getTriple().isMacOSX() ? "__NV_CUDA,__fatbin" : ".nvFatBinSegment"; |
| 396 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 397 | // TODO: Figure out how this is called on mac OS! |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 398 | ModuleIDSectionName = "__nv_module_id"; |
| 399 | ModuleIDPrefix = "__nv_"; |
| 400 | |
| 401 | // For CUDA, create a string literal containing the fat binary loaded from |
| 402 | // the given file. |
| 403 | FatBinStr = makeConstantString(CudaGpuBinary->getBuffer(), "", |
| 404 | FatbinConstantName, 8); |
| 405 | FatMagic = CudaFatMagic; |
| 406 | } |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 407 | |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 408 | // Create initialized wrapper structure that points to the loaded GPU binary |
| 409 | ConstantInitBuilder Builder(CGM); |
| 410 | auto Values = Builder.beginStruct(FatbinWrapperTy); |
| 411 | // Fatbin wrapper magic. |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 412 | Values.addInt(IntTy, FatMagic); |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 413 | // Fatbin version. |
| 414 | Values.addInt(IntTy, 1); |
| 415 | // Data. |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 416 | Values.add(FatBinStr); |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 417 | // Unused in fatbin v1. |
| 418 | Values.add(llvm::ConstantPointerNull::get(VoidPtrTy)); |
| 419 | llvm::GlobalVariable *FatbinWrapper = Values.finishAndCreateGlobal( |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 420 | addUnderscoredPrefixToName("_fatbin_wrapper"), CGM.getPointerAlign(), |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 421 | /*constant*/ true); |
| 422 | FatbinWrapper->setSection(FatbinSectionName); |
Justin Lebar | d14fe88 | 2016-11-18 00:41:31 +0000 | [diff] [blame] | 423 | |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 424 | // Register binary with CUDA/HIP runtime. This is substantially different in |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 425 | // default mode vs. separate compilation! |
| 426 | if (!RelocatableDeviceCode) { |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 427 | // GpuBinaryHandle = __{cuda|hip}RegisterFatBinary(&FatbinWrapper); |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 428 | llvm::CallInst *RegisterFatbinCall = CtorBuilder.CreateCall( |
| 429 | RegisterFatbinFunc, |
| 430 | CtorBuilder.CreateBitCast(FatbinWrapper, VoidPtrTy)); |
| 431 | GpuBinaryHandle = new llvm::GlobalVariable( |
| 432 | TheModule, VoidPtrPtrTy, false, llvm::GlobalValue::InternalLinkage, |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 433 | llvm::ConstantPointerNull::get(VoidPtrPtrTy), |
| 434 | addUnderscoredPrefixToName("_gpubin_handle")); |
| 435 | |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 436 | CtorBuilder.CreateAlignedStore(RegisterFatbinCall, GpuBinaryHandle, |
| 437 | CGM.getPointerAlign()); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 438 | |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 439 | // Call __{cuda|hip}_register_globals(GpuBinaryHandle); |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 440 | if (RegisterGlobalsFunc) |
| 441 | CtorBuilder.CreateCall(RegisterGlobalsFunc, RegisterFatbinCall); |
| 442 | } else { |
| 443 | // Generate a unique module ID. |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 444 | SmallString<64> ModuleID; |
| 445 | llvm::raw_svector_ostream OS(ModuleID); |
| 446 | OS << ModuleIDPrefix << llvm::format("%x", FatbinWrapper->getGUID()); |
| 447 | llvm::Constant *ModuleIDConstant = |
| 448 | makeConstantString(ModuleID.str(), "", ModuleIDSectionName, 32); |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 449 | |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 450 | // Create an alias for the FatbinWrapper that nvcc or hip backend will |
| 451 | // look for. |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 452 | llvm::GlobalAlias::create(llvm::GlobalValue::ExternalLinkage, |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 453 | Twine("__fatbinwrap") + ModuleID, FatbinWrapper); |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 454 | |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 455 | // void __{cuda|hip}RegisterLinkedBinary%ModuleID%(void (*)(void *), void *, |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 456 | // void *, void (*)(void **)) |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 457 | SmallString<128> RegisterLinkedBinaryName( |
| 458 | addUnderscoredPrefixToName("RegisterLinkedBinary")); |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 459 | RegisterLinkedBinaryName += ModuleID; |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 460 | llvm::Constant *RegisterLinkedBinaryFunc = CGM.CreateRuntimeFunction( |
| 461 | getRegisterLinkedBinaryFnTy(), RegisterLinkedBinaryName); |
| 462 | |
| 463 | assert(RegisterGlobalsFunc && "Expecting at least dummy function!"); |
| 464 | llvm::Value *Args[] = {RegisterGlobalsFunc, |
| 465 | CtorBuilder.CreateBitCast(FatbinWrapper, VoidPtrTy), |
Yaxun Liu | 29155b0 | 2018-05-18 15:07:56 +0000 | [diff] [blame^] | 466 | ModuleIDConstant, |
Jonas Hahnfeld | f5527c2 | 2018-04-20 13:04:45 +0000 | [diff] [blame] | 467 | makeDummyFunction(getCallbackFnTy())}; |
| 468 | CtorBuilder.CreateCall(RegisterLinkedBinaryFunc, Args); |
| 469 | } |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 470 | |
| 471 | CtorBuilder.CreateRetVoid(); |
| 472 | return ModuleCtorFunc; |
| 473 | } |
| 474 | |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 475 | /// Creates a global destructor function that unregisters the GPU code blob |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 476 | /// registered by constructor. |
| 477 | /// \code |
| 478 | /// void __cuda_module_dtor(void*) { |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 479 | /// __cudaUnregisterFatBinary(Handle); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 480 | /// } |
| 481 | /// \endcode |
| 482 | llvm::Function *CGNVCUDARuntime::makeModuleDtorFunction() { |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 483 | // No need for destructor if we don't have a handle to unregister. |
| 484 | if (!GpuBinaryHandle) |
Artem Belevich | 8c1ec1e | 2016-03-02 18:28:53 +0000 | [diff] [blame] | 485 | return nullptr; |
| 486 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 487 | // void __cudaUnregisterFatBinary(void ** handle); |
| 488 | llvm::Constant *UnregisterFatbinFunc = CGM.CreateRuntimeFunction( |
| 489 | llvm::FunctionType::get(VoidTy, VoidPtrPtrTy, false), |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 490 | addUnderscoredPrefixToName("UnregisterFatBinary")); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 491 | |
| 492 | llvm::Function *ModuleDtorFunc = llvm::Function::Create( |
| 493 | llvm::FunctionType::get(VoidTy, VoidPtrTy, false), |
Yaxun Liu | 887c569 | 2018-04-25 01:10:37 +0000 | [diff] [blame] | 494 | llvm::GlobalValue::InternalLinkage, |
| 495 | addUnderscoredPrefixToName("_module_dtor"), &TheModule); |
| 496 | |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 497 | llvm::BasicBlock *DtorEntryBB = |
| 498 | llvm::BasicBlock::Create(Context, "entry", ModuleDtorFunc); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 499 | CGBuilderTy DtorBuilder(CGM, Context); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 500 | DtorBuilder.SetInsertPoint(DtorEntryBB); |
| 501 | |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 502 | auto HandleValue = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 503 | DtorBuilder.CreateAlignedLoad(GpuBinaryHandle, CGM.getPointerAlign()); |
Jonas Hahnfeld | e768132 | 2018-02-28 17:53:46 +0000 | [diff] [blame] | 504 | DtorBuilder.CreateCall(UnregisterFatbinFunc, HandleValue); |
Artem Belevich | 52cc487 | 2015-05-07 19:34:16 +0000 | [diff] [blame] | 505 | |
| 506 | DtorBuilder.CreateRetVoid(); |
| 507 | return ModuleDtorFunc; |
| 508 | } |
| 509 | |
Peter Collingbourne | fe88342 | 2011-10-06 18:29:37 +0000 | [diff] [blame] | 510 | CGCUDARuntime *CodeGen::CreateNVCUDARuntime(CodeGenModule &CGM) { |
| 511 | return new CGNVCUDARuntime(CGM); |
| 512 | } |