106f32e7eSjoerg //===- NVPTXUtilities.cpp - Utility Functions -----------------------------===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This file contains miscellaneous utility functions
1006f32e7eSjoerg //
1106f32e7eSjoerg //===----------------------------------------------------------------------===//
1206f32e7eSjoerg 
1306f32e7eSjoerg #include "NVPTXUtilities.h"
1406f32e7eSjoerg #include "NVPTX.h"
1506f32e7eSjoerg #include "llvm/IR/Constants.h"
1606f32e7eSjoerg #include "llvm/IR/Function.h"
1706f32e7eSjoerg #include "llvm/IR/GlobalVariable.h"
1806f32e7eSjoerg #include "llvm/IR/InstIterator.h"
1906f32e7eSjoerg #include "llvm/IR/Module.h"
2006f32e7eSjoerg #include "llvm/IR/Operator.h"
2106f32e7eSjoerg #include "llvm/Support/ManagedStatic.h"
2206f32e7eSjoerg #include "llvm/Support/Mutex.h"
2306f32e7eSjoerg #include <algorithm>
2406f32e7eSjoerg #include <cstring>
2506f32e7eSjoerg #include <map>
2606f32e7eSjoerg #include <mutex>
2706f32e7eSjoerg #include <string>
2806f32e7eSjoerg #include <vector>
2906f32e7eSjoerg 
3006f32e7eSjoerg namespace llvm {
3106f32e7eSjoerg 
3206f32e7eSjoerg namespace {
3306f32e7eSjoerg typedef std::map<std::string, std::vector<unsigned> > key_val_pair_t;
3406f32e7eSjoerg typedef std::map<const GlobalValue *, key_val_pair_t> global_val_annot_t;
3506f32e7eSjoerg typedef std::map<const Module *, global_val_annot_t> per_module_annot_t;
3606f32e7eSjoerg } // anonymous namespace
3706f32e7eSjoerg 
3806f32e7eSjoerg static ManagedStatic<per_module_annot_t> annotationCache;
3906f32e7eSjoerg static sys::Mutex Lock;
4006f32e7eSjoerg 
clearAnnotationCache(const Module * Mod)4106f32e7eSjoerg void clearAnnotationCache(const Module *Mod) {
4206f32e7eSjoerg   std::lock_guard<sys::Mutex> Guard(Lock);
4306f32e7eSjoerg   annotationCache->erase(Mod);
4406f32e7eSjoerg }
4506f32e7eSjoerg 
cacheAnnotationFromMD(const MDNode * md,key_val_pair_t & retval)4606f32e7eSjoerg static void cacheAnnotationFromMD(const MDNode *md, key_val_pair_t &retval) {
4706f32e7eSjoerg   std::lock_guard<sys::Mutex> Guard(Lock);
4806f32e7eSjoerg   assert(md && "Invalid mdnode for annotation");
4906f32e7eSjoerg   assert((md->getNumOperands() % 2) == 1 && "Invalid number of operands");
5006f32e7eSjoerg   // start index = 1, to skip the global variable key
5106f32e7eSjoerg   // increment = 2, to skip the value for each property-value pairs
5206f32e7eSjoerg   for (unsigned i = 1, e = md->getNumOperands(); i != e; i += 2) {
5306f32e7eSjoerg     // property
5406f32e7eSjoerg     const MDString *prop = dyn_cast<MDString>(md->getOperand(i));
5506f32e7eSjoerg     assert(prop && "Annotation property not a string");
5606f32e7eSjoerg 
5706f32e7eSjoerg     // value
5806f32e7eSjoerg     ConstantInt *Val = mdconst::dyn_extract<ConstantInt>(md->getOperand(i + 1));
5906f32e7eSjoerg     assert(Val && "Value operand not a constant int");
6006f32e7eSjoerg 
6106f32e7eSjoerg     std::string keyname = prop->getString().str();
6206f32e7eSjoerg     if (retval.find(keyname) != retval.end())
6306f32e7eSjoerg       retval[keyname].push_back(Val->getZExtValue());
6406f32e7eSjoerg     else {
6506f32e7eSjoerg       std::vector<unsigned> tmp;
6606f32e7eSjoerg       tmp.push_back(Val->getZExtValue());
6706f32e7eSjoerg       retval[keyname] = tmp;
6806f32e7eSjoerg     }
6906f32e7eSjoerg   }
7006f32e7eSjoerg }
7106f32e7eSjoerg 
cacheAnnotationFromMD(const Module * m,const GlobalValue * gv)7206f32e7eSjoerg static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) {
7306f32e7eSjoerg   std::lock_guard<sys::Mutex> Guard(Lock);
7406f32e7eSjoerg   NamedMDNode *NMD = m->getNamedMetadata("nvvm.annotations");
7506f32e7eSjoerg   if (!NMD)
7606f32e7eSjoerg     return;
7706f32e7eSjoerg   key_val_pair_t tmp;
7806f32e7eSjoerg   for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
7906f32e7eSjoerg     const MDNode *elem = NMD->getOperand(i);
8006f32e7eSjoerg 
8106f32e7eSjoerg     GlobalValue *entity =
8206f32e7eSjoerg         mdconst::dyn_extract_or_null<GlobalValue>(elem->getOperand(0));
8306f32e7eSjoerg     // entity may be null due to DCE
8406f32e7eSjoerg     if (!entity)
8506f32e7eSjoerg       continue;
8606f32e7eSjoerg     if (entity != gv)
8706f32e7eSjoerg       continue;
8806f32e7eSjoerg 
8906f32e7eSjoerg     // accumulate annotations for entity in tmp
9006f32e7eSjoerg     cacheAnnotationFromMD(elem, tmp);
9106f32e7eSjoerg   }
9206f32e7eSjoerg 
9306f32e7eSjoerg   if (tmp.empty()) // no annotations for this gv
9406f32e7eSjoerg     return;
9506f32e7eSjoerg 
9606f32e7eSjoerg   if ((*annotationCache).find(m) != (*annotationCache).end())
9706f32e7eSjoerg     (*annotationCache)[m][gv] = std::move(tmp);
9806f32e7eSjoerg   else {
9906f32e7eSjoerg     global_val_annot_t tmp1;
10006f32e7eSjoerg     tmp1[gv] = std::move(tmp);
10106f32e7eSjoerg     (*annotationCache)[m] = std::move(tmp1);
10206f32e7eSjoerg   }
10306f32e7eSjoerg }
10406f32e7eSjoerg 
findOneNVVMAnnotation(const GlobalValue * gv,const std::string & prop,unsigned & retval)10506f32e7eSjoerg bool findOneNVVMAnnotation(const GlobalValue *gv, const std::string &prop,
10606f32e7eSjoerg                            unsigned &retval) {
10706f32e7eSjoerg   std::lock_guard<sys::Mutex> Guard(Lock);
10806f32e7eSjoerg   const Module *m = gv->getParent();
10906f32e7eSjoerg   if ((*annotationCache).find(m) == (*annotationCache).end())
11006f32e7eSjoerg     cacheAnnotationFromMD(m, gv);
11106f32e7eSjoerg   else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
11206f32e7eSjoerg     cacheAnnotationFromMD(m, gv);
11306f32e7eSjoerg   if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
11406f32e7eSjoerg     return false;
11506f32e7eSjoerg   retval = (*annotationCache)[m][gv][prop][0];
11606f32e7eSjoerg   return true;
11706f32e7eSjoerg }
11806f32e7eSjoerg 
findAllNVVMAnnotation(const GlobalValue * gv,const std::string & prop,std::vector<unsigned> & retval)11906f32e7eSjoerg bool findAllNVVMAnnotation(const GlobalValue *gv, const std::string &prop,
12006f32e7eSjoerg                            std::vector<unsigned> &retval) {
12106f32e7eSjoerg   std::lock_guard<sys::Mutex> Guard(Lock);
12206f32e7eSjoerg   const Module *m = gv->getParent();
12306f32e7eSjoerg   if ((*annotationCache).find(m) == (*annotationCache).end())
12406f32e7eSjoerg     cacheAnnotationFromMD(m, gv);
12506f32e7eSjoerg   else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
12606f32e7eSjoerg     cacheAnnotationFromMD(m, gv);
12706f32e7eSjoerg   if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
12806f32e7eSjoerg     return false;
12906f32e7eSjoerg   retval = (*annotationCache)[m][gv][prop];
13006f32e7eSjoerg   return true;
13106f32e7eSjoerg }
13206f32e7eSjoerg 
isTexture(const Value & val)13306f32e7eSjoerg bool isTexture(const Value &val) {
13406f32e7eSjoerg   if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
13506f32e7eSjoerg     unsigned annot;
13606f32e7eSjoerg     if (findOneNVVMAnnotation(gv, "texture", annot)) {
13706f32e7eSjoerg       assert((annot == 1) && "Unexpected annotation on a texture symbol");
13806f32e7eSjoerg       return true;
13906f32e7eSjoerg     }
14006f32e7eSjoerg   }
14106f32e7eSjoerg   return false;
14206f32e7eSjoerg }
14306f32e7eSjoerg 
isSurface(const Value & val)14406f32e7eSjoerg bool isSurface(const Value &val) {
14506f32e7eSjoerg   if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
14606f32e7eSjoerg     unsigned annot;
14706f32e7eSjoerg     if (findOneNVVMAnnotation(gv, "surface", annot)) {
14806f32e7eSjoerg       assert((annot == 1) && "Unexpected annotation on a surface symbol");
14906f32e7eSjoerg       return true;
15006f32e7eSjoerg     }
15106f32e7eSjoerg   }
15206f32e7eSjoerg   return false;
15306f32e7eSjoerg }
15406f32e7eSjoerg 
isSampler(const Value & val)15506f32e7eSjoerg bool isSampler(const Value &val) {
15606f32e7eSjoerg   const char *AnnotationName = "sampler";
15706f32e7eSjoerg 
15806f32e7eSjoerg   if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
15906f32e7eSjoerg     unsigned annot;
16006f32e7eSjoerg     if (findOneNVVMAnnotation(gv, AnnotationName, annot)) {
16106f32e7eSjoerg       assert((annot == 1) && "Unexpected annotation on a sampler symbol");
16206f32e7eSjoerg       return true;
16306f32e7eSjoerg     }
16406f32e7eSjoerg   }
16506f32e7eSjoerg   if (const Argument *arg = dyn_cast<Argument>(&val)) {
16606f32e7eSjoerg     const Function *func = arg->getParent();
16706f32e7eSjoerg     std::vector<unsigned> annot;
16806f32e7eSjoerg     if (findAllNVVMAnnotation(func, AnnotationName, annot)) {
16906f32e7eSjoerg       if (is_contained(annot, arg->getArgNo()))
17006f32e7eSjoerg         return true;
17106f32e7eSjoerg     }
17206f32e7eSjoerg   }
17306f32e7eSjoerg   return false;
17406f32e7eSjoerg }
17506f32e7eSjoerg 
isImageReadOnly(const Value & val)17606f32e7eSjoerg bool isImageReadOnly(const Value &val) {
17706f32e7eSjoerg   if (const Argument *arg = dyn_cast<Argument>(&val)) {
17806f32e7eSjoerg     const Function *func = arg->getParent();
17906f32e7eSjoerg     std::vector<unsigned> annot;
18006f32e7eSjoerg     if (findAllNVVMAnnotation(func, "rdoimage", annot)) {
18106f32e7eSjoerg       if (is_contained(annot, arg->getArgNo()))
18206f32e7eSjoerg         return true;
18306f32e7eSjoerg     }
18406f32e7eSjoerg   }
18506f32e7eSjoerg   return false;
18606f32e7eSjoerg }
18706f32e7eSjoerg 
isImageWriteOnly(const Value & val)18806f32e7eSjoerg bool isImageWriteOnly(const Value &val) {
18906f32e7eSjoerg   if (const Argument *arg = dyn_cast<Argument>(&val)) {
19006f32e7eSjoerg     const Function *func = arg->getParent();
19106f32e7eSjoerg     std::vector<unsigned> annot;
19206f32e7eSjoerg     if (findAllNVVMAnnotation(func, "wroimage", annot)) {
19306f32e7eSjoerg       if (is_contained(annot, arg->getArgNo()))
19406f32e7eSjoerg         return true;
19506f32e7eSjoerg     }
19606f32e7eSjoerg   }
19706f32e7eSjoerg   return false;
19806f32e7eSjoerg }
19906f32e7eSjoerg 
isImageReadWrite(const Value & val)20006f32e7eSjoerg bool isImageReadWrite(const Value &val) {
20106f32e7eSjoerg   if (const Argument *arg = dyn_cast<Argument>(&val)) {
20206f32e7eSjoerg     const Function *func = arg->getParent();
20306f32e7eSjoerg     std::vector<unsigned> annot;
20406f32e7eSjoerg     if (findAllNVVMAnnotation(func, "rdwrimage", annot)) {
20506f32e7eSjoerg       if (is_contained(annot, arg->getArgNo()))
20606f32e7eSjoerg         return true;
20706f32e7eSjoerg     }
20806f32e7eSjoerg   }
20906f32e7eSjoerg   return false;
21006f32e7eSjoerg }
21106f32e7eSjoerg 
isImage(const Value & val)21206f32e7eSjoerg bool isImage(const Value &val) {
21306f32e7eSjoerg   return isImageReadOnly(val) || isImageWriteOnly(val) || isImageReadWrite(val);
21406f32e7eSjoerg }
21506f32e7eSjoerg 
isManaged(const Value & val)21606f32e7eSjoerg bool isManaged(const Value &val) {
21706f32e7eSjoerg   if(const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
21806f32e7eSjoerg     unsigned annot;
21906f32e7eSjoerg     if (findOneNVVMAnnotation(gv, "managed", annot)) {
22006f32e7eSjoerg       assert((annot == 1) && "Unexpected annotation on a managed symbol");
22106f32e7eSjoerg       return true;
22206f32e7eSjoerg     }
22306f32e7eSjoerg   }
22406f32e7eSjoerg   return false;
22506f32e7eSjoerg }
22606f32e7eSjoerg 
getTextureName(const Value & val)22706f32e7eSjoerg std::string getTextureName(const Value &val) {
22806f32e7eSjoerg   assert(val.hasName() && "Found texture variable with no name");
229*da58b97aSjoerg   return std::string(val.getName());
23006f32e7eSjoerg }
23106f32e7eSjoerg 
getSurfaceName(const Value & val)23206f32e7eSjoerg std::string getSurfaceName(const Value &val) {
23306f32e7eSjoerg   assert(val.hasName() && "Found surface variable with no name");
234*da58b97aSjoerg   return std::string(val.getName());
23506f32e7eSjoerg }
23606f32e7eSjoerg 
getSamplerName(const Value & val)23706f32e7eSjoerg std::string getSamplerName(const Value &val) {
23806f32e7eSjoerg   assert(val.hasName() && "Found sampler variable with no name");
239*da58b97aSjoerg   return std::string(val.getName());
24006f32e7eSjoerg }
24106f32e7eSjoerg 
getMaxNTIDx(const Function & F,unsigned & x)24206f32e7eSjoerg bool getMaxNTIDx(const Function &F, unsigned &x) {
24306f32e7eSjoerg   return findOneNVVMAnnotation(&F, "maxntidx", x);
24406f32e7eSjoerg }
24506f32e7eSjoerg 
getMaxNTIDy(const Function & F,unsigned & y)24606f32e7eSjoerg bool getMaxNTIDy(const Function &F, unsigned &y) {
24706f32e7eSjoerg   return findOneNVVMAnnotation(&F, "maxntidy", y);
24806f32e7eSjoerg }
24906f32e7eSjoerg 
getMaxNTIDz(const Function & F,unsigned & z)25006f32e7eSjoerg bool getMaxNTIDz(const Function &F, unsigned &z) {
25106f32e7eSjoerg   return findOneNVVMAnnotation(&F, "maxntidz", z);
25206f32e7eSjoerg }
25306f32e7eSjoerg 
getReqNTIDx(const Function & F,unsigned & x)25406f32e7eSjoerg bool getReqNTIDx(const Function &F, unsigned &x) {
25506f32e7eSjoerg   return findOneNVVMAnnotation(&F, "reqntidx", x);
25606f32e7eSjoerg }
25706f32e7eSjoerg 
getReqNTIDy(const Function & F,unsigned & y)25806f32e7eSjoerg bool getReqNTIDy(const Function &F, unsigned &y) {
25906f32e7eSjoerg   return findOneNVVMAnnotation(&F, "reqntidy", y);
26006f32e7eSjoerg }
26106f32e7eSjoerg 
getReqNTIDz(const Function & F,unsigned & z)26206f32e7eSjoerg bool getReqNTIDz(const Function &F, unsigned &z) {
26306f32e7eSjoerg   return findOneNVVMAnnotation(&F, "reqntidz", z);
26406f32e7eSjoerg }
26506f32e7eSjoerg 
getMinCTASm(const Function & F,unsigned & x)26606f32e7eSjoerg bool getMinCTASm(const Function &F, unsigned &x) {
26706f32e7eSjoerg   return findOneNVVMAnnotation(&F, "minctasm", x);
26806f32e7eSjoerg }
26906f32e7eSjoerg 
getMaxNReg(const Function & F,unsigned & x)27006f32e7eSjoerg bool getMaxNReg(const Function &F, unsigned &x) {
27106f32e7eSjoerg   return findOneNVVMAnnotation(&F, "maxnreg", x);
27206f32e7eSjoerg }
27306f32e7eSjoerg 
isKernelFunction(const Function & F)27406f32e7eSjoerg bool isKernelFunction(const Function &F) {
27506f32e7eSjoerg   unsigned x = 0;
27606f32e7eSjoerg   bool retval = findOneNVVMAnnotation(&F, "kernel", x);
27706f32e7eSjoerg   if (!retval) {
27806f32e7eSjoerg     // There is no NVVM metadata, check the calling convention
27906f32e7eSjoerg     return F.getCallingConv() == CallingConv::PTX_Kernel;
28006f32e7eSjoerg   }
28106f32e7eSjoerg   return (x == 1);
28206f32e7eSjoerg }
28306f32e7eSjoerg 
getAlign(const Function & F,unsigned index,unsigned & align)28406f32e7eSjoerg bool getAlign(const Function &F, unsigned index, unsigned &align) {
28506f32e7eSjoerg   std::vector<unsigned> Vs;
28606f32e7eSjoerg   bool retval = findAllNVVMAnnotation(&F, "align", Vs);
28706f32e7eSjoerg   if (!retval)
28806f32e7eSjoerg     return false;
28906f32e7eSjoerg   for (int i = 0, e = Vs.size(); i < e; i++) {
29006f32e7eSjoerg     unsigned v = Vs[i];
29106f32e7eSjoerg     if ((v >> 16) == index) {
29206f32e7eSjoerg       align = v & 0xFFFF;
29306f32e7eSjoerg       return true;
29406f32e7eSjoerg     }
29506f32e7eSjoerg   }
29606f32e7eSjoerg   return false;
29706f32e7eSjoerg }
29806f32e7eSjoerg 
getAlign(const CallInst & I,unsigned index,unsigned & align)29906f32e7eSjoerg bool getAlign(const CallInst &I, unsigned index, unsigned &align) {
30006f32e7eSjoerg   if (MDNode *alignNode = I.getMetadata("callalign")) {
30106f32e7eSjoerg     for (int i = 0, n = alignNode->getNumOperands(); i < n; i++) {
30206f32e7eSjoerg       if (const ConstantInt *CI =
30306f32e7eSjoerg               mdconst::dyn_extract<ConstantInt>(alignNode->getOperand(i))) {
30406f32e7eSjoerg         unsigned v = CI->getZExtValue();
30506f32e7eSjoerg         if ((v >> 16) == index) {
30606f32e7eSjoerg           align = v & 0xFFFF;
30706f32e7eSjoerg           return true;
30806f32e7eSjoerg         }
30906f32e7eSjoerg         if ((v >> 16) > index) {
31006f32e7eSjoerg           return false;
31106f32e7eSjoerg         }
31206f32e7eSjoerg       }
31306f32e7eSjoerg     }
31406f32e7eSjoerg   }
31506f32e7eSjoerg   return false;
31606f32e7eSjoerg }
31706f32e7eSjoerg 
31806f32e7eSjoerg } // namespace llvm
319