NVPTXUtilities.cpp 9.3 KB

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