NVPTXUtilities.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //===- NVPTXUtilities.cpp - Utility Functions -----------------------------===//
  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. //
  9. // This file contains miscellaneous utility functions
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "NVPTXUtilities.h"
  13. #include "NVPTX.h"
  14. #include "NVPTXTargetMachine.h"
  15. #include "llvm/IR/Constants.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/GlobalVariable.h"
  18. #include "llvm/IR/InstIterator.h"
  19. #include "llvm/IR/Module.h"
  20. #include "llvm/IR/Operator.h"
  21. #include "llvm/Support/Mutex.h"
  22. #include <algorithm>
  23. #include <cstring>
  24. #include <map>
  25. #include <mutex>
  26. #include <string>
  27. #include <vector>
  28. namespace llvm {
  29. namespace {
  30. typedef std::map<std::string, std::vector<unsigned> > key_val_pair_t;
  31. typedef std::map<const GlobalValue *, key_val_pair_t> global_val_annot_t;
  32. struct AnnotationCache {
  33. sys::Mutex Lock;
  34. std::map<const Module *, global_val_annot_t> Cache;
  35. };
  36. AnnotationCache &getAnnotationCache() {
  37. static AnnotationCache AC;
  38. return AC;
  39. }
  40. } // anonymous namespace
  41. void clearAnnotationCache(const Module *Mod) {
  42. auto &AC = getAnnotationCache();
  43. std::lock_guard<sys::Mutex> Guard(AC.Lock);
  44. AC.Cache.erase(Mod);
  45. }
  46. static void cacheAnnotationFromMD(const MDNode *md, key_val_pair_t &retval) {
  47. auto &AC = getAnnotationCache();
  48. std::lock_guard<sys::Mutex> Guard(AC.Lock);
  49. assert(md && "Invalid mdnode for annotation");
  50. assert((md->getNumOperands() % 2) == 1 && "Invalid number of operands");
  51. // start index = 1, to skip the global variable key
  52. // increment = 2, to skip the value for each property-value pairs
  53. for (unsigned i = 1, e = md->getNumOperands(); i != e; i += 2) {
  54. // property
  55. const MDString *prop = dyn_cast<MDString>(md->getOperand(i));
  56. assert(prop && "Annotation property not a string");
  57. // value
  58. ConstantInt *Val = mdconst::dyn_extract<ConstantInt>(md->getOperand(i + 1));
  59. assert(Val && "Value operand not a constant int");
  60. std::string keyname = prop->getString().str();
  61. if (retval.find(keyname) != retval.end())
  62. retval[keyname].push_back(Val->getZExtValue());
  63. else {
  64. std::vector<unsigned> tmp;
  65. tmp.push_back(Val->getZExtValue());
  66. retval[keyname] = tmp;
  67. }
  68. }
  69. }
  70. static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) {
  71. auto &AC = getAnnotationCache();
  72. std::lock_guard<sys::Mutex> Guard(AC.Lock);
  73. NamedMDNode *NMD = m->getNamedMetadata("nvvm.annotations");
  74. if (!NMD)
  75. return;
  76. key_val_pair_t tmp;
  77. for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
  78. const MDNode *elem = NMD->getOperand(i);
  79. GlobalValue *entity =
  80. mdconst::dyn_extract_or_null<GlobalValue>(elem->getOperand(0));
  81. // entity may be null due to DCE
  82. if (!entity)
  83. continue;
  84. if (entity != gv)
  85. continue;
  86. // accumulate annotations for entity in tmp
  87. cacheAnnotationFromMD(elem, tmp);
  88. }
  89. if (tmp.empty()) // no annotations for this gv
  90. return;
  91. if (AC.Cache.find(m) != AC.Cache.end())
  92. AC.Cache[m][gv] = std::move(tmp);
  93. else {
  94. global_val_annot_t tmp1;
  95. tmp1[gv] = std::move(tmp);
  96. AC.Cache[m] = std::move(tmp1);
  97. }
  98. }
  99. bool findOneNVVMAnnotation(const GlobalValue *gv, const std::string &prop,
  100. unsigned &retval) {
  101. auto &AC = getAnnotationCache();
  102. std::lock_guard<sys::Mutex> Guard(AC.Lock);
  103. const Module *m = gv->getParent();
  104. if (AC.Cache.find(m) == AC.Cache.end())
  105. cacheAnnotationFromMD(m, gv);
  106. else if (AC.Cache[m].find(gv) == AC.Cache[m].end())
  107. cacheAnnotationFromMD(m, gv);
  108. if (AC.Cache[m][gv].find(prop) == AC.Cache[m][gv].end())
  109. return false;
  110. retval = AC.Cache[m][gv][prop][0];
  111. return true;
  112. }
  113. bool findAllNVVMAnnotation(const GlobalValue *gv, const std::string &prop,
  114. std::vector<unsigned> &retval) {
  115. auto &AC = getAnnotationCache();
  116. std::lock_guard<sys::Mutex> Guard(AC.Lock);
  117. const Module *m = gv->getParent();
  118. if (AC.Cache.find(m) == AC.Cache.end())
  119. cacheAnnotationFromMD(m, gv);
  120. else if (AC.Cache[m].find(gv) == AC.Cache[m].end())
  121. cacheAnnotationFromMD(m, gv);
  122. if (AC.Cache[m][gv].find(prop) == AC.Cache[m][gv].end())
  123. return false;
  124. retval = AC.Cache[m][gv][prop];
  125. return true;
  126. }
  127. bool isTexture(const Value &val) {
  128. if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
  129. unsigned annot;
  130. if (findOneNVVMAnnotation(gv, "texture", annot)) {
  131. assert((annot == 1) && "Unexpected annotation on a texture symbol");
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. bool isSurface(const Value &val) {
  138. if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
  139. unsigned annot;
  140. if (findOneNVVMAnnotation(gv, "surface", annot)) {
  141. assert((annot == 1) && "Unexpected annotation on a surface symbol");
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. bool isSampler(const Value &val) {
  148. const char *AnnotationName = "sampler";
  149. if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
  150. unsigned annot;
  151. if (findOneNVVMAnnotation(gv, AnnotationName, annot)) {
  152. assert((annot == 1) && "Unexpected annotation on a sampler symbol");
  153. return true;
  154. }
  155. }
  156. if (const Argument *arg = dyn_cast<Argument>(&val)) {
  157. const Function *func = arg->getParent();
  158. std::vector<unsigned> annot;
  159. if (findAllNVVMAnnotation(func, AnnotationName, annot)) {
  160. if (is_contained(annot, arg->getArgNo()))
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. bool isImageReadOnly(const Value &val) {
  167. if (const Argument *arg = dyn_cast<Argument>(&val)) {
  168. const Function *func = arg->getParent();
  169. std::vector<unsigned> annot;
  170. if (findAllNVVMAnnotation(func, "rdoimage", annot)) {
  171. if (is_contained(annot, arg->getArgNo()))
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. bool isImageWriteOnly(const Value &val) {
  178. if (const Argument *arg = dyn_cast<Argument>(&val)) {
  179. const Function *func = arg->getParent();
  180. std::vector<unsigned> annot;
  181. if (findAllNVVMAnnotation(func, "wroimage", annot)) {
  182. if (is_contained(annot, arg->getArgNo()))
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. bool isImageReadWrite(const Value &val) {
  189. if (const Argument *arg = dyn_cast<Argument>(&val)) {
  190. const Function *func = arg->getParent();
  191. std::vector<unsigned> annot;
  192. if (findAllNVVMAnnotation(func, "rdwrimage", annot)) {
  193. if (is_contained(annot, arg->getArgNo()))
  194. return true;
  195. }
  196. }
  197. return false;
  198. }
  199. bool isImage(const Value &val) {
  200. return isImageReadOnly(val) || isImageWriteOnly(val) || isImageReadWrite(val);
  201. }
  202. bool isManaged(const Value &val) {
  203. if(const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
  204. unsigned annot;
  205. if (findOneNVVMAnnotation(gv, "managed", annot)) {
  206. assert((annot == 1) && "Unexpected annotation on a managed symbol");
  207. return true;
  208. }
  209. }
  210. return false;
  211. }
  212. std::string getTextureName(const Value &val) {
  213. assert(val.hasName() && "Found texture variable with no name");
  214. return std::string(val.getName());
  215. }
  216. std::string getSurfaceName(const Value &val) {
  217. assert(val.hasName() && "Found surface variable with no name");
  218. return std::string(val.getName());
  219. }
  220. std::string getSamplerName(const Value &val) {
  221. assert(val.hasName() && "Found sampler variable with no name");
  222. return std::string(val.getName());
  223. }
  224. bool getMaxNTIDx(const Function &F, unsigned &x) {
  225. return findOneNVVMAnnotation(&F, "maxntidx", x);
  226. }
  227. bool getMaxNTIDy(const Function &F, unsigned &y) {
  228. return findOneNVVMAnnotation(&F, "maxntidy", y);
  229. }
  230. bool getMaxNTIDz(const Function &F, unsigned &z) {
  231. return findOneNVVMAnnotation(&F, "maxntidz", z);
  232. }
  233. bool getReqNTIDx(const Function &F, unsigned &x) {
  234. return findOneNVVMAnnotation(&F, "reqntidx", x);
  235. }
  236. bool getReqNTIDy(const Function &F, unsigned &y) {
  237. return findOneNVVMAnnotation(&F, "reqntidy", y);
  238. }
  239. bool getReqNTIDz(const Function &F, unsigned &z) {
  240. return findOneNVVMAnnotation(&F, "reqntidz", z);
  241. }
  242. bool getMinCTASm(const Function &F, unsigned &x) {
  243. return findOneNVVMAnnotation(&F, "minctasm", x);
  244. }
  245. bool getMaxNReg(const Function &F, unsigned &x) {
  246. return findOneNVVMAnnotation(&F, "maxnreg", x);
  247. }
  248. bool isKernelFunction(const Function &F) {
  249. unsigned x = 0;
  250. bool retval = findOneNVVMAnnotation(&F, "kernel", x);
  251. if (!retval) {
  252. // There is no NVVM metadata, check the calling convention
  253. return F.getCallingConv() == CallingConv::PTX_Kernel;
  254. }
  255. return (x == 1);
  256. }
  257. bool getAlign(const Function &F, unsigned index, unsigned &align) {
  258. std::vector<unsigned> Vs;
  259. bool retval = findAllNVVMAnnotation(&F, "align", Vs);
  260. if (!retval)
  261. return false;
  262. for (unsigned v : Vs) {
  263. if ((v >> 16) == index) {
  264. align = v & 0xFFFF;
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. bool getAlign(const CallInst &I, unsigned index, unsigned &align) {
  271. if (MDNode *alignNode = I.getMetadata("callalign")) {
  272. for (int i = 0, n = alignNode->getNumOperands(); i < n; i++) {
  273. if (const ConstantInt *CI =
  274. mdconst::dyn_extract<ConstantInt>(alignNode->getOperand(i))) {
  275. unsigned v = CI->getZExtValue();
  276. if ((v >> 16) == index) {
  277. align = v & 0xFFFF;
  278. return true;
  279. }
  280. if ((v >> 16) > index) {
  281. return false;
  282. }
  283. }
  284. }
  285. }
  286. return false;
  287. }
  288. Function *getMaybeBitcastedCallee(const CallBase *CB) {
  289. return dyn_cast<Function>(CB->getCalledOperand()->stripPointerCasts());
  290. }
  291. bool shouldEmitPTXNoReturn(const Value *V, const TargetMachine &TM) {
  292. const auto &ST =
  293. *static_cast<const NVPTXTargetMachine &>(TM).getSubtargetImpl();
  294. if (!ST.hasNoReturn())
  295. return false;
  296. assert((isa<Function>(V) || isa<CallInst>(V)) &&
  297. "Expect either a call instruction or a function");
  298. if (const CallInst *CallI = dyn_cast<CallInst>(V))
  299. return CallI->doesNotReturn() &&
  300. CallI->getFunctionType()->getReturnType()->isVoidTy();
  301. const Function *F = cast<Function>(V);
  302. return F->doesNotReturn() &&
  303. F->getFunctionType()->getReturnType()->isVoidTy() &&
  304. !isKernelFunction(*F);
  305. }
  306. } // namespace llvm