LLVM API Documentation

NVPTXUtilities.cpp
Go to the documentation of this file.
00001 //===- NVPTXUtilities.cpp - Utility Functions -----------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains miscellaneous utility functions
00011 //===----------------------------------------------------------------------===//
00012 
00013 #include "NVPTXUtilities.h"
00014 #include "NVPTX.h"
00015 #include "llvm/IR/Constants.h"
00016 #include "llvm/IR/Function.h"
00017 #include "llvm/IR/GlobalVariable.h"
00018 #include "llvm/IR/Module.h"
00019 #include "llvm/IR/Operator.h"
00020 #include <algorithm>
00021 #include <cstring>
00022 #include <map>
00023 #include <string>
00024 #include <vector>
00025 #include "llvm/Support/ManagedStatic.h"
00026 #include "llvm/IR/InstIterator.h"
00027 #include "llvm/Support/MutexGuard.h"
00028 
00029 using namespace llvm;
00030 
00031 typedef std::map<std::string, std::vector<unsigned> > key_val_pair_t;
00032 typedef std::map<const GlobalValue *, key_val_pair_t> global_val_annot_t;
00033 typedef std::map<const Module *, global_val_annot_t> per_module_annot_t;
00034 
00035 ManagedStatic<per_module_annot_t> annotationCache;
00036 static sys::Mutex Lock;
00037 
00038 void llvm::clearAnnotationCache(const llvm::Module *Mod) {
00039   MutexGuard Guard(Lock);
00040   annotationCache->erase(Mod);
00041 }
00042 
00043 static void cacheAnnotationFromMD(const MDNode *md, key_val_pair_t &retval) {
00044   MutexGuard Guard(Lock);
00045   assert(md && "Invalid mdnode for annotation");
00046   assert((md->getNumOperands() % 2) == 1 && "Invalid number of operands");
00047   // start index = 1, to skip the global variable key
00048   // increment = 2, to skip the value for each property-value pairs
00049   for (unsigned i = 1, e = md->getNumOperands(); i != e; i += 2) {
00050     // property
00051     const MDString *prop = dyn_cast<MDString>(md->getOperand(i));
00052     assert(prop && "Annotation property not a string");
00053 
00054     // value
00055     ConstantInt *Val = mdconst::dyn_extract<ConstantInt>(md->getOperand(i + 1));
00056     assert(Val && "Value operand not a constant int");
00057 
00058     std::string keyname = prop->getString().str();
00059     if (retval.find(keyname) != retval.end())
00060       retval[keyname].push_back(Val->getZExtValue());
00061     else {
00062       std::vector<unsigned> tmp;
00063       tmp.push_back(Val->getZExtValue());
00064       retval[keyname] = tmp;
00065     }
00066   }
00067 }
00068 
00069 static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) {
00070   MutexGuard Guard(Lock);
00071   NamedMDNode *NMD = m->getNamedMetadata(llvm::NamedMDForAnnotations);
00072   if (!NMD)
00073     return;
00074   key_val_pair_t tmp;
00075   for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
00076     const MDNode *elem = NMD->getOperand(i);
00077 
00078     GlobalValue *entity =
00079         mdconst::dyn_extract_or_null<GlobalValue>(elem->getOperand(0));
00080     // entity may be null due to DCE
00081     if (!entity)
00082       continue;
00083     if (entity != gv)
00084       continue;
00085 
00086     // accumulate annotations for entity in tmp
00087     cacheAnnotationFromMD(elem, tmp);
00088   }
00089 
00090   if (tmp.empty()) // no annotations for this gv
00091     return;
00092 
00093   if ((*annotationCache).find(m) != (*annotationCache).end())
00094     (*annotationCache)[m][gv] = std::move(tmp);
00095   else {
00096     global_val_annot_t tmp1;
00097     tmp1[gv] = std::move(tmp);
00098     (*annotationCache)[m] = std::move(tmp1);
00099   }
00100 }
00101 
00102 bool llvm::findOneNVVMAnnotation(const GlobalValue *gv, std::string prop,
00103                                  unsigned &retval) {
00104   MutexGuard Guard(Lock);
00105   const Module *m = gv->getParent();
00106   if ((*annotationCache).find(m) == (*annotationCache).end())
00107     cacheAnnotationFromMD(m, gv);
00108   else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
00109     cacheAnnotationFromMD(m, gv);
00110   if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
00111     return false;
00112   retval = (*annotationCache)[m][gv][prop][0];
00113   return true;
00114 }
00115 
00116 bool llvm::findAllNVVMAnnotation(const GlobalValue *gv, std::string prop,
00117                                  std::vector<unsigned> &retval) {
00118   MutexGuard Guard(Lock);
00119   const Module *m = gv->getParent();
00120   if ((*annotationCache).find(m) == (*annotationCache).end())
00121     cacheAnnotationFromMD(m, gv);
00122   else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
00123     cacheAnnotationFromMD(m, gv);
00124   if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
00125     return false;
00126   retval = (*annotationCache)[m][gv][prop];
00127   return true;
00128 }
00129 
00130 bool llvm::isTexture(const llvm::Value &val) {
00131   if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
00132     unsigned annot;
00133     if (llvm::findOneNVVMAnnotation(
00134             gv, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISTEXTURE],
00135             annot)) {
00136       assert((annot == 1) && "Unexpected annotation on a texture symbol");
00137       return true;
00138     }
00139   }
00140   return false;
00141 }
00142 
00143 bool llvm::isSurface(const llvm::Value &val) {
00144   if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
00145     unsigned annot;
00146     if (llvm::findOneNVVMAnnotation(
00147             gv, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISSURFACE],
00148             annot)) {
00149       assert((annot == 1) && "Unexpected annotation on a surface symbol");
00150       return true;
00151     }
00152   }
00153   return false;
00154 }
00155 
00156 bool llvm::isSampler(const llvm::Value &val) {
00157   if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
00158     unsigned annot;
00159     if (llvm::findOneNVVMAnnotation(
00160             gv, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISSAMPLER],
00161             annot)) {
00162       assert((annot == 1) && "Unexpected annotation on a sampler symbol");
00163       return true;
00164     }
00165   }
00166   if (const Argument *arg = dyn_cast<Argument>(&val)) {
00167     const Function *func = arg->getParent();
00168     std::vector<unsigned> annot;
00169     if (llvm::findAllNVVMAnnotation(
00170             func, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISSAMPLER],
00171             annot)) {
00172       if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
00173         return true;
00174     }
00175   }
00176   return false;
00177 }
00178 
00179 bool llvm::isImageReadOnly(const llvm::Value &val) {
00180   if (const Argument *arg = dyn_cast<Argument>(&val)) {
00181     const Function *func = arg->getParent();
00182     std::vector<unsigned> annot;
00183     if (llvm::findAllNVVMAnnotation(func,
00184                                     llvm::PropertyAnnotationNames[
00185                                         llvm::PROPERTY_ISREADONLY_IMAGE_PARAM],
00186                                     annot)) {
00187       if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
00188         return true;
00189     }
00190   }
00191   return false;
00192 }
00193 
00194 bool llvm::isImageWriteOnly(const llvm::Value &val) {
00195   if (const Argument *arg = dyn_cast<Argument>(&val)) {
00196     const Function *func = arg->getParent();
00197     std::vector<unsigned> annot;
00198     if (llvm::findAllNVVMAnnotation(func,
00199                                     llvm::PropertyAnnotationNames[
00200                                         llvm::PROPERTY_ISWRITEONLY_IMAGE_PARAM],
00201                                     annot)) {
00202       if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
00203         return true;
00204     }
00205   }
00206   return false;
00207 }
00208 
00209 bool llvm::isImageReadWrite(const llvm::Value &val) {
00210   if (const Argument *arg = dyn_cast<Argument>(&val)) {
00211     const Function *func = arg->getParent();
00212     std::vector<unsigned> annot;
00213     if (llvm::findAllNVVMAnnotation(func,
00214                                     llvm::PropertyAnnotationNames[
00215                                         llvm::PROPERTY_ISREADWRITE_IMAGE_PARAM],
00216                                     annot)) {
00217       if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
00218         return true;
00219     }
00220   }
00221   return false;
00222 }
00223 
00224 bool llvm::isImage(const llvm::Value &val) {
00225   return llvm::isImageReadOnly(val) || llvm::isImageWriteOnly(val) ||
00226          llvm::isImageReadWrite(val);
00227 }
00228 
00229 bool llvm::isManaged(const llvm::Value &val) {
00230   if(const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
00231     unsigned annot;
00232     if(llvm::findOneNVVMAnnotation(gv,
00233                           llvm::PropertyAnnotationNames[llvm::PROPERTY_MANAGED],
00234                                    annot)) {
00235       assert((annot == 1) && "Unexpected annotation on a managed symbol");
00236       return true;
00237     }
00238   }
00239   return false;
00240 }
00241 
00242 std::string llvm::getTextureName(const llvm::Value &val) {
00243   assert(val.hasName() && "Found texture variable with no name");
00244   return val.getName();
00245 }
00246 
00247 std::string llvm::getSurfaceName(const llvm::Value &val) {
00248   assert(val.hasName() && "Found surface variable with no name");
00249   return val.getName();
00250 }
00251 
00252 std::string llvm::getSamplerName(const llvm::Value &val) {
00253   assert(val.hasName() && "Found sampler variable with no name");
00254   return val.getName();
00255 }
00256 
00257 bool llvm::getMaxNTIDx(const Function &F, unsigned &x) {
00258   return (llvm::findOneNVVMAnnotation(
00259       &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MAXNTID_X], x));
00260 }
00261 
00262 bool llvm::getMaxNTIDy(const Function &F, unsigned &y) {
00263   return (llvm::findOneNVVMAnnotation(
00264       &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MAXNTID_Y], y));
00265 }
00266 
00267 bool llvm::getMaxNTIDz(const Function &F, unsigned &z) {
00268   return (llvm::findOneNVVMAnnotation(
00269       &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MAXNTID_Z], z));
00270 }
00271 
00272 bool llvm::getReqNTIDx(const Function &F, unsigned &x) {
00273   return (llvm::findOneNVVMAnnotation(
00274       &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_REQNTID_X], x));
00275 }
00276 
00277 bool llvm::getReqNTIDy(const Function &F, unsigned &y) {
00278   return (llvm::findOneNVVMAnnotation(
00279       &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_REQNTID_Y], y));
00280 }
00281 
00282 bool llvm::getReqNTIDz(const Function &F, unsigned &z) {
00283   return (llvm::findOneNVVMAnnotation(
00284       &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_REQNTID_Z], z));
00285 }
00286 
00287 bool llvm::getMinCTASm(const Function &F, unsigned &x) {
00288   return (llvm::findOneNVVMAnnotation(
00289       &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MINNCTAPERSM], x));
00290 }
00291 
00292 bool llvm::isKernelFunction(const Function &F) {
00293   unsigned x = 0;
00294   bool retval = llvm::findOneNVVMAnnotation(
00295       &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISKERNEL_FUNCTION], x);
00296   if (retval == false) {
00297     // There is no NVVM metadata, check the calling convention
00298     if (F.getCallingConv() == llvm::CallingConv::PTX_Kernel)
00299       return true;
00300     else
00301       return false;
00302   }
00303   return (x == 1);
00304 }
00305 
00306 bool llvm::getAlign(const Function &F, unsigned index, unsigned &align) {
00307   std::vector<unsigned> Vs;
00308   bool retval = llvm::findAllNVVMAnnotation(
00309       &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_ALIGN], Vs);
00310   if (retval == false)
00311     return false;
00312   for (int i = 0, e = Vs.size(); i < e; i++) {
00313     unsigned v = Vs[i];
00314     if ((v >> 16) == index) {
00315       align = v & 0xFFFF;
00316       return true;
00317     }
00318   }
00319   return false;
00320 }
00321 
00322 bool llvm::getAlign(const CallInst &I, unsigned index, unsigned &align) {
00323   if (MDNode *alignNode = I.getMetadata("callalign")) {
00324     for (int i = 0, n = alignNode->getNumOperands(); i < n; i++) {
00325       if (const ConstantInt *CI =
00326               mdconst::dyn_extract<ConstantInt>(alignNode->getOperand(i))) {
00327         unsigned v = CI->getZExtValue();
00328         if ((v >> 16) == index) {
00329           align = v & 0xFFFF;
00330           return true;
00331         }
00332         if ((v >> 16) > index) {
00333           return false;
00334         }
00335       }
00336     }
00337   }
00338   return false;
00339 }
00340 
00341 bool llvm::isBarrierIntrinsic(Intrinsic::ID id) {
00342   if ((id == Intrinsic::nvvm_barrier0) ||
00343       (id == Intrinsic::nvvm_barrier0_popc) ||
00344       (id == Intrinsic::nvvm_barrier0_and) ||
00345       (id == Intrinsic::nvvm_barrier0_or) ||
00346       (id == Intrinsic::cuda_syncthreads))
00347     return true;
00348   return false;
00349 }
00350 
00351 // Interface for checking all memory space transfer related intrinsics
00352 bool llvm::isMemorySpaceTransferIntrinsic(Intrinsic::ID id) {
00353   if (id == Intrinsic::nvvm_ptr_local_to_gen ||
00354       id == Intrinsic::nvvm_ptr_shared_to_gen ||
00355       id == Intrinsic::nvvm_ptr_global_to_gen ||
00356       id == Intrinsic::nvvm_ptr_constant_to_gen ||
00357       id == Intrinsic::nvvm_ptr_gen_to_global ||
00358       id == Intrinsic::nvvm_ptr_gen_to_shared ||
00359       id == Intrinsic::nvvm_ptr_gen_to_local ||
00360       id == Intrinsic::nvvm_ptr_gen_to_constant ||
00361       id == Intrinsic::nvvm_ptr_gen_to_param) {
00362     return true;
00363   }
00364 
00365   return false;
00366 }
00367 
00368 // consider several special intrinsics in striping pointer casts, and
00369 // provide an option to ignore GEP indicies for find out the base address only
00370 // which could be used in simple alias disambigurate.
00371 const Value *
00372 llvm::skipPointerTransfer(const Value *V, bool ignore_GEP_indices) {
00373   V = V->stripPointerCasts();
00374   while (true) {
00375     if (const IntrinsicInst *IS = dyn_cast<IntrinsicInst>(V)) {
00376       if (isMemorySpaceTransferIntrinsic(IS->getIntrinsicID())) {
00377         V = IS->getArgOperand(0)->stripPointerCasts();
00378         continue;
00379       }
00380     } else if (ignore_GEP_indices)
00381       if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
00382         V = GEP->getPointerOperand()->stripPointerCasts();
00383         continue;
00384       }
00385     break;
00386   }
00387   return V;
00388 }
00389 
00390 // consider several special intrinsics in striping pointer casts, and
00391 // - ignore GEP indicies for find out the base address only, and
00392 // - tracking PHINode
00393 // which could be used in simple alias disambigurate.
00394 const Value *
00395 llvm::skipPointerTransfer(const Value *V, std::set<const Value *> &processed) {
00396   if (processed.find(V) != processed.end())
00397     return nullptr;
00398   processed.insert(V);
00399 
00400   const Value *V2 = V->stripPointerCasts();
00401   if (V2 != V && processed.find(V2) != processed.end())
00402     return nullptr;
00403   processed.insert(V2);
00404 
00405   V = V2;
00406 
00407   while (true) {
00408     if (const IntrinsicInst *IS = dyn_cast<IntrinsicInst>(V)) {
00409       if (isMemorySpaceTransferIntrinsic(IS->getIntrinsicID())) {
00410         V = IS->getArgOperand(0)->stripPointerCasts();
00411         continue;
00412       }
00413     } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
00414       V = GEP->getPointerOperand()->stripPointerCasts();
00415       continue;
00416     } else if (const PHINode *PN = dyn_cast<PHINode>(V)) {
00417       if (V != V2 && processed.find(V) != processed.end())
00418         return nullptr;
00419       processed.insert(PN);
00420       const Value *common = nullptr;
00421       for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
00422         const Value *pv = PN->getIncomingValue(i);
00423         const Value *base = skipPointerTransfer(pv, processed);
00424         if (base) {
00425           if (!common)
00426             common = base;
00427           else if (common != base)
00428             return PN;
00429         }
00430       }
00431       if (!common)
00432         return PN;
00433       V = common;
00434     }
00435     break;
00436   }
00437   return V;
00438 }
00439 
00440 // The following are some useful utilities for debuggung
00441 
00442 BasicBlock *llvm::getParentBlock(Value *v) {
00443   if (BasicBlock *B = dyn_cast<BasicBlock>(v))
00444     return B;
00445 
00446   if (Instruction *I = dyn_cast<Instruction>(v))
00447     return I->getParent();
00448 
00449   return nullptr;
00450 }
00451 
00452 Function *llvm::getParentFunction(Value *v) {
00453   if (Function *F = dyn_cast<Function>(v))
00454     return F;
00455 
00456   if (Instruction *I = dyn_cast<Instruction>(v))
00457     return I->getParent()->getParent();
00458 
00459   if (BasicBlock *B = dyn_cast<BasicBlock>(v))
00460     return B->getParent();
00461 
00462   return nullptr;
00463 }
00464 
00465 // Dump a block by name
00466 void llvm::dumpBlock(Value *v, char *blockName) {
00467   Function *F = getParentFunction(v);
00468   if (!F)
00469     return;
00470 
00471   for (Function::iterator it = F->begin(), ie = F->end(); it != ie; ++it) {
00472     BasicBlock *B = it;
00473     if (strcmp(B->getName().data(), blockName) == 0) {
00474       B->dump();
00475       return;
00476     }
00477   }
00478 }
00479 
00480 // Find an instruction by name
00481 Instruction *llvm::getInst(Value *base, char *instName) {
00482   Function *F = getParentFunction(base);
00483   if (!F)
00484     return nullptr;
00485 
00486   for (inst_iterator it = inst_begin(F), ie = inst_end(F); it != ie; ++it) {
00487     Instruction *I = &*it;
00488     if (strcmp(I->getName().data(), instName) == 0) {
00489       return I;
00490     }
00491   }
00492 
00493   return nullptr;
00494 }
00495 
00496 // Dump an instruction by nane
00497 void llvm::dumpInst(Value *base, char *instName) {
00498   Instruction *I = getInst(base, instName);
00499   if (I)
00500     I->dump();
00501 }
00502 
00503 // Dump an instruction and all dependent instructions
00504 void llvm::dumpInstRec(Value *v, std::set<Instruction *> *visited) {
00505   if (Instruction *I = dyn_cast<Instruction>(v)) {
00506 
00507     if (visited->find(I) != visited->end())
00508       return;
00509 
00510     visited->insert(I);
00511 
00512     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
00513       dumpInstRec(I->getOperand(i), visited);
00514 
00515     I->dump();
00516   }
00517 }
00518 
00519 // Dump an instruction and all dependent instructions
00520 void llvm::dumpInstRec(Value *v) {
00521   std::set<Instruction *> visited;
00522 
00523   //BasicBlock *B = getParentBlock(v);
00524 
00525   dumpInstRec(v, &visited);
00526 }
00527 
00528 // Dump the parent for Instruction, block or function
00529 void llvm::dumpParent(Value *v) {
00530   if (Instruction *I = dyn_cast<Instruction>(v)) {
00531     I->getParent()->dump();
00532     return;
00533   }
00534 
00535   if (BasicBlock *B = dyn_cast<BasicBlock>(v)) {
00536     B->getParent()->dump();
00537     return;
00538   }
00539 
00540   if (Function *F = dyn_cast<Function>(v)) {
00541     F->getParent()->dump();
00542     return;
00543   }
00544 }