OffloadWrapper.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //===- OffloadWrapper.cpp ---------------------------------------*- C++ -*-===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "OffloadWrapper.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/ADT/Triple.h"
  11. #include "llvm/IR/Constants.h"
  12. #include "llvm/IR/GlobalVariable.h"
  13. #include "llvm/IR/IRBuilder.h"
  14. #include "llvm/IR/LLVMContext.h"
  15. #include "llvm/IR/Module.h"
  16. #include "llvm/Transforms/Utils/ModuleUtils.h"
  17. using namespace llvm;
  18. namespace {
  19. IntegerType *getSizeTTy(Module &M) {
  20. LLVMContext &C = M.getContext();
  21. switch (M.getDataLayout().getPointerTypeSize(Type::getInt8PtrTy(C))) {
  22. case 4u:
  23. return Type::getInt32Ty(C);
  24. case 8u:
  25. return Type::getInt64Ty(C);
  26. }
  27. llvm_unreachable("unsupported pointer type size");
  28. }
  29. // struct __tgt_offload_entry {
  30. // void *addr;
  31. // char *name;
  32. // size_t size;
  33. // int32_t flags;
  34. // int32_t reserved;
  35. // };
  36. StructType *getEntryTy(Module &M) {
  37. LLVMContext &C = M.getContext();
  38. StructType *EntryTy = StructType::getTypeByName(C, "__tgt_offload_entry");
  39. if (!EntryTy)
  40. EntryTy = StructType::create("__tgt_offload_entry", Type::getInt8PtrTy(C),
  41. Type::getInt8PtrTy(C), getSizeTTy(M),
  42. Type::getInt32Ty(C), Type::getInt32Ty(C));
  43. return EntryTy;
  44. }
  45. PointerType *getEntryPtrTy(Module &M) {
  46. return PointerType::getUnqual(getEntryTy(M));
  47. }
  48. // struct __tgt_device_image {
  49. // void *ImageStart;
  50. // void *ImageEnd;
  51. // __tgt_offload_entry *EntriesBegin;
  52. // __tgt_offload_entry *EntriesEnd;
  53. // };
  54. StructType *getDeviceImageTy(Module &M) {
  55. LLVMContext &C = M.getContext();
  56. StructType *ImageTy = StructType::getTypeByName(C, "__tgt_device_image");
  57. if (!ImageTy)
  58. ImageTy = StructType::create("__tgt_device_image", Type::getInt8PtrTy(C),
  59. Type::getInt8PtrTy(C), getEntryPtrTy(M),
  60. getEntryPtrTy(M));
  61. return ImageTy;
  62. }
  63. PointerType *getDeviceImagePtrTy(Module &M) {
  64. return PointerType::getUnqual(getDeviceImageTy(M));
  65. }
  66. // struct __tgt_bin_desc {
  67. // int32_t NumDeviceImages;
  68. // __tgt_device_image *DeviceImages;
  69. // __tgt_offload_entry *HostEntriesBegin;
  70. // __tgt_offload_entry *HostEntriesEnd;
  71. // };
  72. StructType *getBinDescTy(Module &M) {
  73. LLVMContext &C = M.getContext();
  74. StructType *DescTy = StructType::getTypeByName(C, "__tgt_bin_desc");
  75. if (!DescTy)
  76. DescTy = StructType::create("__tgt_bin_desc", Type::getInt32Ty(C),
  77. getDeviceImagePtrTy(M), getEntryPtrTy(M),
  78. getEntryPtrTy(M));
  79. return DescTy;
  80. }
  81. PointerType *getBinDescPtrTy(Module &M) {
  82. return PointerType::getUnqual(getBinDescTy(M));
  83. }
  84. /// Creates binary descriptor for the given device images. Binary descriptor
  85. /// is an object that is passed to the offloading runtime at program startup
  86. /// and it describes all device images available in the executable or shared
  87. /// library. It is defined as follows
  88. ///
  89. /// __attribute__((visibility("hidden")))
  90. /// extern __tgt_offload_entry *__start_omp_offloading_entries;
  91. /// __attribute__((visibility("hidden")))
  92. /// extern __tgt_offload_entry *__stop_omp_offloading_entries;
  93. ///
  94. /// static const char Image0[] = { <Bufs.front() contents> };
  95. /// ...
  96. /// static const char ImageN[] = { <Bufs.back() contents> };
  97. ///
  98. /// static const __tgt_device_image Images[] = {
  99. /// {
  100. /// Image0, /*ImageStart*/
  101. /// Image0 + sizeof(Image0), /*ImageEnd*/
  102. /// __start_omp_offloading_entries, /*EntriesBegin*/
  103. /// __stop_omp_offloading_entries /*EntriesEnd*/
  104. /// },
  105. /// ...
  106. /// {
  107. /// ImageN, /*ImageStart*/
  108. /// ImageN + sizeof(ImageN), /*ImageEnd*/
  109. /// __start_omp_offloading_entries, /*EntriesBegin*/
  110. /// __stop_omp_offloading_entries /*EntriesEnd*/
  111. /// }
  112. /// };
  113. ///
  114. /// static const __tgt_bin_desc BinDesc = {
  115. /// sizeof(Images) / sizeof(Images[0]), /*NumDeviceImages*/
  116. /// Images, /*DeviceImages*/
  117. /// __start_omp_offloading_entries, /*HostEntriesBegin*/
  118. /// __stop_omp_offloading_entries /*HostEntriesEnd*/
  119. /// };
  120. ///
  121. /// Global variable that represents BinDesc is returned.
  122. GlobalVariable *createBinDesc(Module &M, ArrayRef<ArrayRef<char>> Bufs) {
  123. LLVMContext &C = M.getContext();
  124. // Create external begin/end symbols for the offload entries table.
  125. auto *EntriesB = new GlobalVariable(
  126. M, getEntryTy(M), /*isConstant*/ true, GlobalValue::ExternalLinkage,
  127. /*Initializer*/ nullptr, "__start_omp_offloading_entries");
  128. EntriesB->setVisibility(GlobalValue::HiddenVisibility);
  129. auto *EntriesE = new GlobalVariable(
  130. M, getEntryTy(M), /*isConstant*/ true, GlobalValue::ExternalLinkage,
  131. /*Initializer*/ nullptr, "__stop_omp_offloading_entries");
  132. EntriesE->setVisibility(GlobalValue::HiddenVisibility);
  133. // We assume that external begin/end symbols that we have created above will
  134. // be defined by the linker. But linker will do that only if linker inputs
  135. // have section with "omp_offloading_entries" name which is not guaranteed.
  136. // So, we just create dummy zero sized object in the offload entries section
  137. // to force linker to define those symbols.
  138. auto *DummyInit =
  139. ConstantAggregateZero::get(ArrayType::get(getEntryTy(M), 0u));
  140. auto *DummyEntry = new GlobalVariable(
  141. M, DummyInit->getType(), true, GlobalVariable::ExternalLinkage, DummyInit,
  142. "__dummy.omp_offloading.entry");
  143. DummyEntry->setSection("omp_offloading_entries");
  144. DummyEntry->setVisibility(GlobalValue::HiddenVisibility);
  145. auto *Zero = ConstantInt::get(getSizeTTy(M), 0u);
  146. Constant *ZeroZero[] = {Zero, Zero};
  147. // Create initializer for the images array.
  148. SmallVector<Constant *, 4u> ImagesInits;
  149. ImagesInits.reserve(Bufs.size());
  150. for (ArrayRef<char> Buf : Bufs) {
  151. auto *Data = ConstantDataArray::get(C, Buf);
  152. auto *Image = new GlobalVariable(M, Data->getType(), /*isConstant*/ true,
  153. GlobalVariable::InternalLinkage, Data,
  154. ".omp_offloading.device_image");
  155. Image->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
  156. auto *Size = ConstantInt::get(getSizeTTy(M), Buf.size());
  157. Constant *ZeroSize[] = {Zero, Size};
  158. auto *ImageB =
  159. ConstantExpr::getGetElementPtr(Image->getValueType(), Image, ZeroZero);
  160. auto *ImageE =
  161. ConstantExpr::getGetElementPtr(Image->getValueType(), Image, ZeroSize);
  162. ImagesInits.push_back(ConstantStruct::get(getDeviceImageTy(M), ImageB,
  163. ImageE, EntriesB, EntriesE));
  164. }
  165. // Then create images array.
  166. auto *ImagesData = ConstantArray::get(
  167. ArrayType::get(getDeviceImageTy(M), ImagesInits.size()), ImagesInits);
  168. auto *Images =
  169. new GlobalVariable(M, ImagesData->getType(), /*isConstant*/ true,
  170. GlobalValue::InternalLinkage, ImagesData,
  171. ".omp_offloading.device_images");
  172. Images->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
  173. auto *ImagesB =
  174. ConstantExpr::getGetElementPtr(Images->getValueType(), Images, ZeroZero);
  175. // And finally create the binary descriptor object.
  176. auto *DescInit = ConstantStruct::get(
  177. getBinDescTy(M),
  178. ConstantInt::get(Type::getInt32Ty(C), ImagesInits.size()), ImagesB,
  179. EntriesB, EntriesE);
  180. return new GlobalVariable(M, DescInit->getType(), /*isConstant*/ true,
  181. GlobalValue::InternalLinkage, DescInit,
  182. ".omp_offloading.descriptor");
  183. }
  184. void createRegisterFunction(Module &M, GlobalVariable *BinDesc) {
  185. LLVMContext &C = M.getContext();
  186. auto *FuncTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg*/ false);
  187. auto *Func = Function::Create(FuncTy, GlobalValue::InternalLinkage,
  188. ".omp_offloading.descriptor_reg", &M);
  189. Func->setSection(".text.startup");
  190. // Get __tgt_register_lib function declaration.
  191. auto *RegFuncTy = FunctionType::get(Type::getVoidTy(C), getBinDescPtrTy(M),
  192. /*isVarArg*/ false);
  193. FunctionCallee RegFuncC =
  194. M.getOrInsertFunction("__tgt_register_lib", RegFuncTy);
  195. // Construct function body
  196. IRBuilder<> Builder(BasicBlock::Create(C, "entry", Func));
  197. Builder.CreateCall(RegFuncC, BinDesc);
  198. Builder.CreateRetVoid();
  199. // Add this function to constructors.
  200. // Set priority to 1 so that __tgt_register_lib is executed AFTER
  201. // __tgt_register_requires (we want to know what requirements have been
  202. // asked for before we load a libomptarget plugin so that by the time the
  203. // plugin is loaded it can report how many devices there are which can
  204. // satisfy these requirements).
  205. appendToGlobalCtors(M, Func, /*Priority*/ 1);
  206. }
  207. void createUnregisterFunction(Module &M, GlobalVariable *BinDesc) {
  208. LLVMContext &C = M.getContext();
  209. auto *FuncTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg*/ false);
  210. auto *Func = Function::Create(FuncTy, GlobalValue::InternalLinkage,
  211. ".omp_offloading.descriptor_unreg", &M);
  212. Func->setSection(".text.startup");
  213. // Get __tgt_unregister_lib function declaration.
  214. auto *UnRegFuncTy = FunctionType::get(Type::getVoidTy(C), getBinDescPtrTy(M),
  215. /*isVarArg*/ false);
  216. FunctionCallee UnRegFuncC =
  217. M.getOrInsertFunction("__tgt_unregister_lib", UnRegFuncTy);
  218. // Construct function body
  219. IRBuilder<> Builder(BasicBlock::Create(C, "entry", Func));
  220. Builder.CreateCall(UnRegFuncC, BinDesc);
  221. Builder.CreateRetVoid();
  222. // Add this function to global destructors.
  223. // Match priority of __tgt_register_lib
  224. appendToGlobalDtors(M, Func, /*Priority*/ 1);
  225. }
  226. } // namespace
  227. Error wrapBinaries(Module &M, ArrayRef<ArrayRef<char>> Images) {
  228. GlobalVariable *Desc = createBinDesc(M, Images);
  229. if (!Desc)
  230. return createStringError(inconvertibleErrorCode(),
  231. "No binary descriptors created.");
  232. createRegisterFunction(M, Desc);
  233. createUnregisterFunction(M, Desc);
  234. return Error::success();
  235. }