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/InstIterator.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/Operator.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/MutexGuard.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;
36 static sys::Mutex Lock;
38 void llvm::clearAnnotationCache(const llvm::Module *Mod) {
39 MutexGuard Guard(Lock);
40 annotationCache->erase(Mod);
43 static void cacheAnnotationFromMD(const MDNode *md, key_val_pair_t &retval) {
44 MutexGuard Guard(Lock);
45 assert(md && "Invalid mdnode for annotation");
46 assert((md->getNumOperands() % 2) == 1 && "Invalid number of operands");
47 // start index = 1, to skip the global variable key
48 // increment = 2, to skip the value for each property-value pairs
49 for (unsigned i = 1, e = md->getNumOperands(); i != e; i += 2) {
51 const MDString *prop = dyn_cast<MDString>(md->getOperand(i));
52 assert(prop && "Annotation property not a string");
55 ConstantInt *Val = mdconst::dyn_extract<ConstantInt>(md->getOperand(i + 1));
56 assert(Val && "Value operand not a constant int");
58 std::string keyname = prop->getString().str();
59 if (retval.find(keyname) != retval.end())
60 retval[keyname].push_back(Val->getZExtValue());
62 std::vector<unsigned> tmp;
63 tmp.push_back(Val->getZExtValue());
64 retval[keyname] = tmp;
69 static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) {
70 MutexGuard Guard(Lock);
71 NamedMDNode *NMD = m->getNamedMetadata(llvm::NamedMDForAnnotations);
75 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
76 const MDNode *elem = NMD->getOperand(i);
79 mdconst::dyn_extract_or_null<GlobalValue>(elem->getOperand(0));
80 // entity may be null due to DCE
86 // accumulate annotations for entity in tmp
87 cacheAnnotationFromMD(elem, tmp);
90 if (tmp.empty()) // no annotations for this gv
93 if ((*annotationCache).find(m) != (*annotationCache).end())
94 (*annotationCache)[m][gv] = std::move(tmp);
96 global_val_annot_t tmp1;
97 tmp1[gv] = std::move(tmp);
98 (*annotationCache)[m] = std::move(tmp1);
102 bool llvm::findOneNVVMAnnotation(const GlobalValue *gv, std::string prop,
104 MutexGuard Guard(Lock);
105 const Module *m = gv->getParent();
106 if ((*annotationCache).find(m) == (*annotationCache).end())
107 cacheAnnotationFromMD(m, gv);
108 else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
109 cacheAnnotationFromMD(m, gv);
110 if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
112 retval = (*annotationCache)[m][gv][prop][0];
116 bool llvm::findAllNVVMAnnotation(const GlobalValue *gv, std::string prop,
117 std::vector<unsigned> &retval) {
118 MutexGuard Guard(Lock);
119 const Module *m = gv->getParent();
120 if ((*annotationCache).find(m) == (*annotationCache).end())
121 cacheAnnotationFromMD(m, gv);
122 else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
123 cacheAnnotationFromMD(m, gv);
124 if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
126 retval = (*annotationCache)[m][gv][prop];
130 bool llvm::isTexture(const llvm::Value &val) {
131 if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
133 if (llvm::findOneNVVMAnnotation(
134 gv, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISTEXTURE],
136 assert((annot == 1) && "Unexpected annotation on a texture symbol");
143 bool llvm::isSurface(const llvm::Value &val) {
144 if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
146 if (llvm::findOneNVVMAnnotation(
147 gv, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISSURFACE],
149 assert((annot == 1) && "Unexpected annotation on a surface symbol");
156 bool llvm::isSampler(const llvm::Value &val) {
157 if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
159 if (llvm::findOneNVVMAnnotation(
160 gv, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISSAMPLER],
162 assert((annot == 1) && "Unexpected annotation on a sampler symbol");
166 if (const Argument *arg = dyn_cast<Argument>(&val)) {
167 const Function *func = arg->getParent();
168 std::vector<unsigned> annot;
169 if (llvm::findAllNVVMAnnotation(
170 func, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISSAMPLER],
172 if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
179 bool llvm::isImageReadOnly(const llvm::Value &val) {
180 if (const Argument *arg = dyn_cast<Argument>(&val)) {
181 const Function *func = arg->getParent();
182 std::vector<unsigned> annot;
183 if (llvm::findAllNVVMAnnotation(func,
184 llvm::PropertyAnnotationNames[
185 llvm::PROPERTY_ISREADONLY_IMAGE_PARAM],
187 if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
194 bool llvm::isImageWriteOnly(const llvm::Value &val) {
195 if (const Argument *arg = dyn_cast<Argument>(&val)) {
196 const Function *func = arg->getParent();
197 std::vector<unsigned> annot;
198 if (llvm::findAllNVVMAnnotation(func,
199 llvm::PropertyAnnotationNames[
200 llvm::PROPERTY_ISWRITEONLY_IMAGE_PARAM],
202 if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
209 bool llvm::isImageReadWrite(const llvm::Value &val) {
210 if (const Argument *arg = dyn_cast<Argument>(&val)) {
211 const Function *func = arg->getParent();
212 std::vector<unsigned> annot;
213 if (llvm::findAllNVVMAnnotation(func,
214 llvm::PropertyAnnotationNames[
215 llvm::PROPERTY_ISREADWRITE_IMAGE_PARAM],
217 if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
224 bool llvm::isImage(const llvm::Value &val) {
225 return llvm::isImageReadOnly(val) || llvm::isImageWriteOnly(val) ||
226 llvm::isImageReadWrite(val);
229 bool llvm::isManaged(const llvm::Value &val) {
230 if(const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
232 if(llvm::findOneNVVMAnnotation(gv,
233 llvm::PropertyAnnotationNames[llvm::PROPERTY_MANAGED],
235 assert((annot == 1) && "Unexpected annotation on a managed symbol");
242 std::string llvm::getTextureName(const llvm::Value &val) {
243 assert(val.hasName() && "Found texture variable with no name");
244 return val.getName();
247 std::string llvm::getSurfaceName(const llvm::Value &val) {
248 assert(val.hasName() && "Found surface variable with no name");
249 return val.getName();
252 std::string llvm::getSamplerName(const llvm::Value &val) {
253 assert(val.hasName() && "Found sampler variable with no name");
254 return val.getName();
257 bool llvm::getMaxNTIDx(const Function &F, unsigned &x) {
258 return (llvm::findOneNVVMAnnotation(
259 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MAXNTID_X], x));
262 bool llvm::getMaxNTIDy(const Function &F, unsigned &y) {
263 return (llvm::findOneNVVMAnnotation(
264 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MAXNTID_Y], y));
267 bool llvm::getMaxNTIDz(const Function &F, unsigned &z) {
268 return (llvm::findOneNVVMAnnotation(
269 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MAXNTID_Z], z));
272 bool llvm::getReqNTIDx(const Function &F, unsigned &x) {
273 return (llvm::findOneNVVMAnnotation(
274 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_REQNTID_X], x));
277 bool llvm::getReqNTIDy(const Function &F, unsigned &y) {
278 return (llvm::findOneNVVMAnnotation(
279 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_REQNTID_Y], y));
282 bool llvm::getReqNTIDz(const Function &F, unsigned &z) {
283 return (llvm::findOneNVVMAnnotation(
284 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_REQNTID_Z], z));
287 bool llvm::getMinCTASm(const Function &F, unsigned &x) {
288 return (llvm::findOneNVVMAnnotation(
289 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MINNCTAPERSM], x));
292 bool llvm::isKernelFunction(const Function &F) {
294 bool retval = llvm::findOneNVVMAnnotation(
295 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISKERNEL_FUNCTION], x);
297 // There is no NVVM metadata, check the calling convention
298 return F.getCallingConv() == llvm::CallingConv::PTX_Kernel;
303 bool llvm::getAlign(const Function &F, unsigned index, unsigned &align) {
304 std::vector<unsigned> Vs;
305 bool retval = llvm::findAllNVVMAnnotation(
306 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_ALIGN], Vs);
309 for (int i = 0, e = Vs.size(); i < e; i++) {
311 if ((v >> 16) == index) {
319 bool llvm::getAlign(const CallInst &I, unsigned index, unsigned &align) {
320 if (MDNode *alignNode = I.getMetadata("callalign")) {
321 for (int i = 0, n = alignNode->getNumOperands(); i < n; i++) {
322 if (const ConstantInt *CI =
323 mdconst::dyn_extract<ConstantInt>(alignNode->getOperand(i))) {
324 unsigned v = CI->getZExtValue();
325 if ((v >> 16) == index) {
329 if ((v >> 16) > index) {
338 // The following are some useful utilities for debugging
340 BasicBlock *llvm::getParentBlock(Value *v) {
341 if (BasicBlock *B = dyn_cast<BasicBlock>(v))
344 if (Instruction *I = dyn_cast<Instruction>(v))
345 return I->getParent();
350 Function *llvm::getParentFunction(Value *v) {
351 if (Function *F = dyn_cast<Function>(v))
354 if (Instruction *I = dyn_cast<Instruction>(v))
355 return I->getParent()->getParent();
357 if (BasicBlock *B = dyn_cast<BasicBlock>(v))
358 return B->getParent();
363 // Dump a block by name
364 void llvm::dumpBlock(Value *v, char *blockName) {
365 Function *F = getParentFunction(v);
369 for (Function::iterator it = F->begin(), ie = F->end(); it != ie; ++it) {
370 BasicBlock *B = &*it;
371 if (strcmp(B->getName().data(), blockName) == 0) {
378 // Find an instruction by name
379 Instruction *llvm::getInst(Value *base, char *instName) {
380 Function *F = getParentFunction(base);
384 for (inst_iterator it = inst_begin(F), ie = inst_end(F); it != ie; ++it) {
385 Instruction *I = &*it;
386 if (strcmp(I->getName().data(), instName) == 0) {
394 // Dump an instruction by name
395 void llvm::dumpInst(Value *base, char *instName) {
396 Instruction *I = getInst(base, instName);
401 // Dump an instruction and all dependent instructions
402 void llvm::dumpInstRec(Value *v, std::set<Instruction *> *visited) {
403 if (Instruction *I = dyn_cast<Instruction>(v)) {
405 if (visited->find(I) != visited->end())
410 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
411 dumpInstRec(I->getOperand(i), visited);
417 // Dump an instruction and all dependent instructions
418 void llvm::dumpInstRec(Value *v) {
419 std::set<Instruction *> visited;
421 //BasicBlock *B = getParentBlock(v);
423 dumpInstRec(v, &visited);
426 // Dump the parent for Instruction, block or function
427 void llvm::dumpParent(Value *v) {
428 if (Instruction *I = dyn_cast<Instruction>(v)) {
429 I->getParent()->dump();
433 if (BasicBlock *B = dyn_cast<BasicBlock>(v)) {
434 B->getParent()->dump();
438 if (Function *F = dyn_cast<Function>(v)) {
439 F->getParent()->dump();