1 //===- NVPTXUtilities.cpp - Utility Functions -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains miscellaneous utility functions
11 //===----------------------------------------------------------------------===//
13 #include "NVPTXUtilities.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/GlobalVariable.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/Operator.h"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/InstIterator.h"
31 typedef std::map<std::string, std::vector<unsigned> > key_val_pair_t;
32 typedef std::map<const GlobalValue *, key_val_pair_t> global_val_annot_t;
33 typedef std::map<const Module *, global_val_annot_t> per_module_annot_t;
35 ManagedStatic<per_module_annot_t> annotationCache;
37 static void cacheAnnotationFromMD(const MDNode *md, key_val_pair_t &retval) {
38 assert(md && "Invalid mdnode for annotation");
39 assert((md->getNumOperands() % 2) == 1 && "Invalid number of operands");
40 // start index = 1, to skip the global variable key
41 // increment = 2, to skip the value for each property-value pairs
42 for (unsigned i = 1, e = md->getNumOperands(); i != e; i += 2) {
44 const MDString *prop = dyn_cast<MDString>(md->getOperand(i));
45 assert(prop && "Annotation property not a string");
48 ConstantInt *Val = dyn_cast<ConstantInt>(md->getOperand(i + 1));
49 assert(Val && "Value operand not a constant int");
51 std::string keyname = prop->getString().str();
52 if (retval.find(keyname) != retval.end())
53 retval[keyname].push_back(Val->getZExtValue());
55 std::vector<unsigned> tmp;
56 tmp.push_back(Val->getZExtValue());
57 retval[keyname] = tmp;
62 static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) {
63 NamedMDNode *NMD = m->getNamedMetadata(llvm::NamedMDForAnnotations);
67 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
68 const MDNode *elem = NMD->getOperand(i);
70 Value *entity = elem->getOperand(0);
71 // entity may be null due to DCE
77 // accumulate annotations for entity in tmp
78 cacheAnnotationFromMD(elem, tmp);
81 if (tmp.empty()) // no annotations for this gv
84 if ((*annotationCache).find(m) != (*annotationCache).end())
85 (*annotationCache)[m][gv] = tmp;
87 global_val_annot_t tmp1;
89 (*annotationCache)[m] = tmp1;
93 bool llvm::findOneNVVMAnnotation(const GlobalValue *gv, std::string prop,
95 const Module *m = gv->getParent();
96 if ((*annotationCache).find(m) == (*annotationCache).end())
97 cacheAnnotationFromMD(m, gv);
98 else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
99 cacheAnnotationFromMD(m, gv);
100 if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
102 retval = (*annotationCache)[m][gv][prop][0];
106 bool llvm::findAllNVVMAnnotation(const GlobalValue *gv, std::string prop,
107 std::vector<unsigned> &retval) {
108 const Module *m = gv->getParent();
109 if ((*annotationCache).find(m) == (*annotationCache).end())
110 cacheAnnotationFromMD(m, gv);
111 else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
112 cacheAnnotationFromMD(m, gv);
113 if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
115 retval = (*annotationCache)[m][gv][prop];
119 bool llvm::isTexture(const llvm::Value &val) {
120 if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
122 if (llvm::findOneNVVMAnnotation(
123 gv, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISTEXTURE],
125 assert((annot == 1) && "Unexpected annotation on a texture symbol");
132 bool llvm::isSurface(const llvm::Value &val) {
133 if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
135 if (llvm::findOneNVVMAnnotation(
136 gv, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISSURFACE],
138 assert((annot == 1) && "Unexpected annotation on a surface symbol");
145 bool llvm::isSampler(const llvm::Value &val) {
146 if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
148 if (llvm::findOneNVVMAnnotation(
149 gv, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISSAMPLER],
151 assert((annot == 1) && "Unexpected annotation on a sampler symbol");
155 if (const Argument *arg = dyn_cast<Argument>(&val)) {
156 const Function *func = arg->getParent();
157 std::vector<unsigned> annot;
158 if (llvm::findAllNVVMAnnotation(
159 func, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISSAMPLER],
161 if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
168 bool llvm::isImageReadOnly(const llvm::Value &val) {
169 if (const Argument *arg = dyn_cast<Argument>(&val)) {
170 const Function *func = arg->getParent();
171 std::vector<unsigned> annot;
172 if (llvm::findAllNVVMAnnotation(func,
173 llvm::PropertyAnnotationNames[
174 llvm::PROPERTY_ISREADONLY_IMAGE_PARAM],
176 if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
183 bool llvm::isImageWriteOnly(const llvm::Value &val) {
184 if (const Argument *arg = dyn_cast<Argument>(&val)) {
185 const Function *func = arg->getParent();
186 std::vector<unsigned> annot;
187 if (llvm::findAllNVVMAnnotation(func,
188 llvm::PropertyAnnotationNames[
189 llvm::PROPERTY_ISWRITEONLY_IMAGE_PARAM],
191 if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
198 bool llvm::isImage(const llvm::Value &val) {
199 return llvm::isImageReadOnly(val) || llvm::isImageWriteOnly(val);
202 std::string llvm::getTextureName(const llvm::Value &val) {
203 assert(val.hasName() && "Found texture variable with no name");
204 return val.getName();
207 std::string llvm::getSurfaceName(const llvm::Value &val) {
208 assert(val.hasName() && "Found surface variable with no name");
209 return val.getName();
212 std::string llvm::getSamplerName(const llvm::Value &val) {
213 assert(val.hasName() && "Found sampler variable with no name");
214 return val.getName();
217 bool llvm::getMaxNTIDx(const Function &F, unsigned &x) {
218 return (llvm::findOneNVVMAnnotation(
219 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MAXNTID_X], x));
222 bool llvm::getMaxNTIDy(const Function &F, unsigned &y) {
223 return (llvm::findOneNVVMAnnotation(
224 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MAXNTID_Y], y));
227 bool llvm::getMaxNTIDz(const Function &F, unsigned &z) {
228 return (llvm::findOneNVVMAnnotation(
229 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MAXNTID_Z], z));
232 bool llvm::getReqNTIDx(const Function &F, unsigned &x) {
233 return (llvm::findOneNVVMAnnotation(
234 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_REQNTID_X], x));
237 bool llvm::getReqNTIDy(const Function &F, unsigned &y) {
238 return (llvm::findOneNVVMAnnotation(
239 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_REQNTID_Y], y));
242 bool llvm::getReqNTIDz(const Function &F, unsigned &z) {
243 return (llvm::findOneNVVMAnnotation(
244 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_REQNTID_Z], z));
247 bool llvm::getMinCTASm(const Function &F, unsigned &x) {
248 return (llvm::findOneNVVMAnnotation(
249 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MINNCTAPERSM], x));
252 bool llvm::isKernelFunction(const Function &F) {
254 bool retval = llvm::findOneNVVMAnnotation(
255 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISKERNEL_FUNCTION], x);
256 if (retval == false) {
257 // There is no NVVM metadata, check the calling convention
258 if (F.getCallingConv() == llvm::CallingConv::PTX_Kernel)
266 bool llvm::getAlign(const Function &F, unsigned index, unsigned &align) {
267 std::vector<unsigned> Vs;
268 bool retval = llvm::findAllNVVMAnnotation(
269 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_ALIGN], Vs);
272 for (int i = 0, e = Vs.size(); i < e; i++) {
274 if ((v >> 16) == index) {
282 bool llvm::getAlign(const CallInst &I, unsigned index, unsigned &align) {
283 if (MDNode *alignNode = I.getMetadata("callalign")) {
284 for (int i = 0, n = alignNode->getNumOperands(); i < n; i++) {
285 if (const ConstantInt *CI =
286 dyn_cast<ConstantInt>(alignNode->getOperand(i))) {
287 unsigned v = CI->getZExtValue();
288 if ((v >> 16) == index) {
292 if ((v >> 16) > index) {
301 bool llvm::isBarrierIntrinsic(Intrinsic::ID id) {
302 if ((id == Intrinsic::nvvm_barrier0) ||
303 (id == Intrinsic::nvvm_barrier0_popc) ||
304 (id == Intrinsic::nvvm_barrier0_and) ||
305 (id == Intrinsic::nvvm_barrier0_or) ||
306 (id == Intrinsic::cuda_syncthreads))
311 // Interface for checking all memory space transfer related intrinsics
312 bool llvm::isMemorySpaceTransferIntrinsic(Intrinsic::ID id) {
313 if (id == Intrinsic::nvvm_ptr_local_to_gen ||
314 id == Intrinsic::nvvm_ptr_shared_to_gen ||
315 id == Intrinsic::nvvm_ptr_global_to_gen ||
316 id == Intrinsic::nvvm_ptr_constant_to_gen ||
317 id == Intrinsic::nvvm_ptr_gen_to_global ||
318 id == Intrinsic::nvvm_ptr_gen_to_shared ||
319 id == Intrinsic::nvvm_ptr_gen_to_local ||
320 id == Intrinsic::nvvm_ptr_gen_to_constant ||
321 id == Intrinsic::nvvm_ptr_gen_to_param) {
328 // consider several special intrinsics in striping pointer casts, and
329 // provide an option to ignore GEP indicies for find out the base address only
330 // which could be used in simple alias disambigurate.
332 llvm::skipPointerTransfer(const Value *V, bool ignore_GEP_indices) {
333 V = V->stripPointerCasts();
335 if (const IntrinsicInst *IS = dyn_cast<IntrinsicInst>(V)) {
336 if (isMemorySpaceTransferIntrinsic(IS->getIntrinsicID())) {
337 V = IS->getArgOperand(0)->stripPointerCasts();
340 } else if (ignore_GEP_indices)
341 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
342 V = GEP->getPointerOperand()->stripPointerCasts();
350 // consider several special intrinsics in striping pointer casts, and
351 // - ignore GEP indicies for find out the base address only, and
352 // - tracking PHINode
353 // which could be used in simple alias disambigurate.
355 llvm::skipPointerTransfer(const Value *V, std::set<const Value *> &processed) {
356 if (processed.find(V) != processed.end())
360 const Value *V2 = V->stripPointerCasts();
361 if (V2 != V && processed.find(V2) != processed.end())
363 processed.insert(V2);
368 if (const IntrinsicInst *IS = dyn_cast<IntrinsicInst>(V)) {
369 if (isMemorySpaceTransferIntrinsic(IS->getIntrinsicID())) {
370 V = IS->getArgOperand(0)->stripPointerCasts();
373 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
374 V = GEP->getPointerOperand()->stripPointerCasts();
376 } else if (const PHINode *PN = dyn_cast<PHINode>(V)) {
377 if (V != V2 && processed.find(V) != processed.end())
379 processed.insert(PN);
380 const Value *common = 0;
381 for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
382 const Value *pv = PN->getIncomingValue(i);
383 const Value *base = skipPointerTransfer(pv, processed);
387 else if (common != base)
400 // The following are some useful utilities for debuggung
402 BasicBlock *llvm::getParentBlock(Value *v) {
403 if (BasicBlock *B = dyn_cast<BasicBlock>(v))
406 if (Instruction *I = dyn_cast<Instruction>(v))
407 return I->getParent();
412 Function *llvm::getParentFunction(Value *v) {
413 if (Function *F = dyn_cast<Function>(v))
416 if (Instruction *I = dyn_cast<Instruction>(v))
417 return I->getParent()->getParent();
419 if (BasicBlock *B = dyn_cast<BasicBlock>(v))
420 return B->getParent();
425 // Dump a block by name
426 void llvm::dumpBlock(Value *v, char *blockName) {
427 Function *F = getParentFunction(v);
431 for (Function::iterator it = F->begin(), ie = F->end(); it != ie; ++it) {
433 if (strcmp(B->getName().data(), blockName) == 0) {
440 // Find an instruction by name
441 Instruction *llvm::getInst(Value *base, char *instName) {
442 Function *F = getParentFunction(base);
446 for (inst_iterator it = inst_begin(F), ie = inst_end(F); it != ie; ++it) {
447 Instruction *I = &*it;
448 if (strcmp(I->getName().data(), instName) == 0) {
456 // Dump an instruction by nane
457 void llvm::dumpInst(Value *base, char *instName) {
458 Instruction *I = getInst(base, instName);
463 // Dump an instruction and all dependent instructions
464 void llvm::dumpInstRec(Value *v, std::set<Instruction *> *visited) {
465 if (Instruction *I = dyn_cast<Instruction>(v)) {
467 if (visited->find(I) != visited->end())
472 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
473 dumpInstRec(I->getOperand(i), visited);
479 // Dump an instruction and all dependent instructions
480 void llvm::dumpInstRec(Value *v) {
481 std::set<Instruction *> visited;
483 //BasicBlock *B = getParentBlock(v);
485 dumpInstRec(v, &visited);
488 // Dump the parent for Instruction, block or function
489 void llvm::dumpParent(Value *v) {
490 if (Instruction *I = dyn_cast<Instruction>(v)) {
491 I->getParent()->dump();
495 if (BasicBlock *B = dyn_cast<BasicBlock>(v)) {
496 B->getParent()->dump();
500 if (Function *F = dyn_cast<Function>(v)) {
501 F->getParent()->dump();