blob: e1264d0defb89d41f280ef912331685e423059dc [file] [log] [blame]
Justin Holewinskiae556d32012-05-04 20:18:50 +00001//===- NVPTXUtilities.cpp - Utility Functions -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains miscellaneous utility functions
11//===----------------------------------------------------------------------===//
12
13#include "NVPTXUtilities.h"
14#include "NVPTX.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Constants.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/GlobalVariable.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Module.h"
20#include "llvm/IR/Operator.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000021#include "llvm/Support/ManagedStatic.h"
22#include "llvm/Support/MutexGuard.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000023#include <algorithm>
24#include <cstring>
25#include <map>
26#include <string>
27#include <vector>
Justin Holewinskiae556d32012-05-04 20:18:50 +000028
Justin Lebare6867082016-12-14 22:32:44 +000029namespace llvm {
Justin Holewinskiae556d32012-05-04 20:18:50 +000030
Justin Lebare6867082016-12-14 22:32:44 +000031namespace {
Justin Holewinskiae556d32012-05-04 20:18:50 +000032typedef std::map<std::string, std::vector<unsigned> > key_val_pair_t;
33typedef std::map<const GlobalValue *, key_val_pair_t> global_val_annot_t;
34typedef std::map<const Module *, global_val_annot_t> per_module_annot_t;
Justin Lebare6867082016-12-14 22:32:44 +000035} // anonymous namespace
Justin Holewinskiae556d32012-05-04 20:18:50 +000036
Justin Lebare6867082016-12-14 22:32:44 +000037static ManagedStatic<per_module_annot_t> annotationCache;
Justin Holewinski59596952014-04-09 15:38:52 +000038static sys::Mutex Lock;
39
Justin Lebare6867082016-12-14 22:32:44 +000040void clearAnnotationCache(const Module *Mod) {
Justin Holewinski59596952014-04-09 15:38:52 +000041 MutexGuard Guard(Lock);
42 annotationCache->erase(Mod);
43}
Justin Holewinskiae556d32012-05-04 20:18:50 +000044
Justin Holewinskiae556d32012-05-04 20:18:50 +000045static void cacheAnnotationFromMD(const MDNode *md, key_val_pair_t &retval) {
Justin Holewinski59596952014-04-09 15:38:52 +000046 MutexGuard Guard(Lock);
Justin Holewinskiae556d32012-05-04 20:18:50 +000047 assert(md && "Invalid mdnode for annotation");
48 assert((md->getNumOperands() % 2) == 1 && "Invalid number of operands");
49 // start index = 1, to skip the global variable key
50 // increment = 2, to skip the value for each property-value pairs
51 for (unsigned i = 1, e = md->getNumOperands(); i != e; i += 2) {
52 // property
53 const MDString *prop = dyn_cast<MDString>(md->getOperand(i));
54 assert(prop && "Annotation property not a string");
55
56 // value
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000057 ConstantInt *Val = mdconst::dyn_extract<ConstantInt>(md->getOperand(i + 1));
Justin Holewinskiae556d32012-05-04 20:18:50 +000058 assert(Val && "Value operand not a constant int");
59
60 std::string keyname = prop->getString().str();
61 if (retval.find(keyname) != retval.end())
62 retval[keyname].push_back(Val->getZExtValue());
63 else {
64 std::vector<unsigned> tmp;
65 tmp.push_back(Val->getZExtValue());
66 retval[keyname] = tmp;
67 }
68 }
69}
70
71static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) {
Justin Holewinski59596952014-04-09 15:38:52 +000072 MutexGuard Guard(Lock);
Justin Lebare6867082016-12-14 22:32:44 +000073 NamedMDNode *NMD = m->getNamedMetadata("nvvm.annotations");
Justin Holewinskiae556d32012-05-04 20:18:50 +000074 if (!NMD)
75 return;
76 key_val_pair_t tmp;
77 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +000078 const MDNode *elem = NMD->getOperand(i);
Justin Holewinskiae556d32012-05-04 20:18:50 +000079
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000080 GlobalValue *entity =
81 mdconst::dyn_extract_or_null<GlobalValue>(elem->getOperand(0));
Justin Holewinskiae556d32012-05-04 20:18:50 +000082 // entity may be null due to DCE
83 if (!entity)
84 continue;
85 if (entity != gv)
86 continue;
87
88 // accumulate annotations for entity in tmp
89 cacheAnnotationFromMD(elem, tmp);
90 }
91
92 if (tmp.empty()) // no annotations for this gv
93 return;
94
95 if ((*annotationCache).find(m) != (*annotationCache).end())
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +000096 (*annotationCache)[m][gv] = std::move(tmp);
Justin Holewinskiae556d32012-05-04 20:18:50 +000097 else {
98 global_val_annot_t tmp1;
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +000099 tmp1[gv] = std::move(tmp);
100 (*annotationCache)[m] = std::move(tmp1);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000101 }
102}
103
Justin Lebare6867082016-12-14 22:32:44 +0000104bool findOneNVVMAnnotation(const GlobalValue *gv, const std::string &prop,
105 unsigned &retval) {
Justin Holewinski59596952014-04-09 15:38:52 +0000106 MutexGuard Guard(Lock);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000107 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][0];
115 return true;
116}
117
Justin Lebare6867082016-12-14 22:32:44 +0000118bool findAllNVVMAnnotation(const GlobalValue *gv, const std::string &prop,
119 std::vector<unsigned> &retval) {
Justin Holewinski59596952014-04-09 15:38:52 +0000120 MutexGuard Guard(Lock);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000121 const Module *m = gv->getParent();
122 if ((*annotationCache).find(m) == (*annotationCache).end())
123 cacheAnnotationFromMD(m, gv);
124 else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
125 cacheAnnotationFromMD(m, gv);
126 if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
127 return false;
128 retval = (*annotationCache)[m][gv][prop];
129 return true;
130}
131
Justin Lebare6867082016-12-14 22:32:44 +0000132bool isTexture(const Value &val) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000133 if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
134 unsigned annot;
Justin Lebare6867082016-12-14 22:32:44 +0000135 if (findOneNVVMAnnotation(gv, "texture", annot)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000136 assert((annot == 1) && "Unexpected annotation on a texture symbol");
137 return true;
138 }
139 }
140 return false;
141}
142
Justin Lebare6867082016-12-14 22:32:44 +0000143bool isSurface(const Value &val) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000144 if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
145 unsigned annot;
Justin Lebare6867082016-12-14 22:32:44 +0000146 if (findOneNVVMAnnotation(gv, "surface", annot)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000147 assert((annot == 1) && "Unexpected annotation on a surface symbol");
148 return true;
149 }
150 }
151 return false;
152}
153
Justin Lebare6867082016-12-14 22:32:44 +0000154bool isSampler(const Value &val) {
155 const char *AnnotationName = "sampler";
156
Justin Holewinskiae556d32012-05-04 20:18:50 +0000157 if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
158 unsigned annot;
Justin Lebare6867082016-12-14 22:32:44 +0000159 if (findOneNVVMAnnotation(gv, AnnotationName, annot)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000160 assert((annot == 1) && "Unexpected annotation on a sampler symbol");
161 return true;
162 }
163 }
164 if (const Argument *arg = dyn_cast<Argument>(&val)) {
165 const Function *func = arg->getParent();
166 std::vector<unsigned> annot;
Justin Lebare6867082016-12-14 22:32:44 +0000167 if (findAllNVVMAnnotation(func, AnnotationName, annot)) {
David Majnemer0d955d02016-08-11 22:21:41 +0000168 if (is_contained(annot, arg->getArgNo()))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000169 return true;
170 }
171 }
172 return false;
173}
174
Justin Lebare6867082016-12-14 22:32:44 +0000175bool isImageReadOnly(const Value &val) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000176 if (const Argument *arg = dyn_cast<Argument>(&val)) {
177 const Function *func = arg->getParent();
178 std::vector<unsigned> annot;
Justin Lebare6867082016-12-14 22:32:44 +0000179 if (findAllNVVMAnnotation(func, "rdoimage", annot)) {
David Majnemer0d955d02016-08-11 22:21:41 +0000180 if (is_contained(annot, arg->getArgNo()))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000181 return true;
182 }
183 }
184 return false;
185}
186
Justin Lebare6867082016-12-14 22:32:44 +0000187bool isImageWriteOnly(const Value &val) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000188 if (const Argument *arg = dyn_cast<Argument>(&val)) {
189 const Function *func = arg->getParent();
190 std::vector<unsigned> annot;
Justin Lebare6867082016-12-14 22:32:44 +0000191 if (findAllNVVMAnnotation(func, "wroimage", annot)) {
David Majnemer0d955d02016-08-11 22:21:41 +0000192 if (is_contained(annot, arg->getArgNo()))
Justin Holewinskiae556d32012-05-04 20:18:50 +0000193 return true;
194 }
195 }
196 return false;
197}
198
Justin Lebare6867082016-12-14 22:32:44 +0000199bool isImageReadWrite(const Value &val) {
Justin Holewinski59596952014-04-09 15:38:52 +0000200 if (const Argument *arg = dyn_cast<Argument>(&val)) {
201 const Function *func = arg->getParent();
202 std::vector<unsigned> annot;
Justin Lebare6867082016-12-14 22:32:44 +0000203 if (findAllNVVMAnnotation(func, "rdwrimage", annot)) {
David Majnemer0d955d02016-08-11 22:21:41 +0000204 if (is_contained(annot, arg->getArgNo()))
Justin Holewinski59596952014-04-09 15:38:52 +0000205 return true;
206 }
207 }
208 return false;
209}
210
Justin Lebare6867082016-12-14 22:32:44 +0000211bool isImage(const Value &val) {
212 return isImageReadOnly(val) || isImageWriteOnly(val) || isImageReadWrite(val);
Justin Holewinski59596952014-04-09 15:38:52 +0000213}
214
Justin Lebare6867082016-12-14 22:32:44 +0000215bool isManaged(const Value &val) {
Justin Holewinski59596952014-04-09 15:38:52 +0000216 if(const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
217 unsigned annot;
Justin Lebare6867082016-12-14 22:32:44 +0000218 if (findOneNVVMAnnotation(gv, "managed", annot)) {
Justin Holewinski59596952014-04-09 15:38:52 +0000219 assert((annot == 1) && "Unexpected annotation on a managed symbol");
220 return true;
221 }
222 }
223 return false;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000224}
225
Justin Lebare6867082016-12-14 22:32:44 +0000226std::string getTextureName(const Value &val) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000227 assert(val.hasName() && "Found texture variable with no name");
228 return val.getName();
229}
230
Justin Lebare6867082016-12-14 22:32:44 +0000231std::string getSurfaceName(const Value &val) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000232 assert(val.hasName() && "Found surface variable with no name");
233 return val.getName();
234}
235
Justin Lebare6867082016-12-14 22:32:44 +0000236std::string getSamplerName(const Value &val) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000237 assert(val.hasName() && "Found sampler variable with no name");
238 return val.getName();
239}
240
Justin Lebare6867082016-12-14 22:32:44 +0000241bool getMaxNTIDx(const Function &F, unsigned &x) {
242 return findOneNVVMAnnotation(&F, "maxntidx", x);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000243}
244
Justin Lebare6867082016-12-14 22:32:44 +0000245bool getMaxNTIDy(const Function &F, unsigned &y) {
246 return findOneNVVMAnnotation(&F, "maxntidy", y);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000247}
248
Justin Lebare6867082016-12-14 22:32:44 +0000249bool getMaxNTIDz(const Function &F, unsigned &z) {
250 return findOneNVVMAnnotation(&F, "maxntidz", z);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000251}
252
Justin Lebare6867082016-12-14 22:32:44 +0000253bool getReqNTIDx(const Function &F, unsigned &x) {
254 return findOneNVVMAnnotation(&F, "reqntidx", x);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000255}
256
Justin Lebare6867082016-12-14 22:32:44 +0000257bool getReqNTIDy(const Function &F, unsigned &y) {
258 return findOneNVVMAnnotation(&F, "reqntidy", y);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000259}
260
Justin Lebare6867082016-12-14 22:32:44 +0000261bool getReqNTIDz(const Function &F, unsigned &z) {
262 return findOneNVVMAnnotation(&F, "reqntidz", z);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000263}
264
Justin Lebare6867082016-12-14 22:32:44 +0000265bool getMinCTASm(const Function &F, unsigned &x) {
266 return findOneNVVMAnnotation(&F, "minctasm", x);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000267}
268
Justin Lebare6867082016-12-14 22:32:44 +0000269bool isKernelFunction(const Function &F) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000270 unsigned x = 0;
Justin Lebare6867082016-12-14 22:32:44 +0000271 bool retval = findOneNVVMAnnotation(&F, "kernel", x);
Eli Bendersky3e840192015-03-23 16:26:23 +0000272 if (!retval) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000273 // There is no NVVM metadata, check the calling convention
Justin Lebare6867082016-12-14 22:32:44 +0000274 return F.getCallingConv() == CallingConv::PTX_Kernel;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000275 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000276 return (x == 1);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000277}
278
Justin Lebare6867082016-12-14 22:32:44 +0000279bool getAlign(const Function &F, unsigned index, unsigned &align) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000280 std::vector<unsigned> Vs;
Justin Lebare6867082016-12-14 22:32:44 +0000281 bool retval = findAllNVVMAnnotation(&F, "align", Vs);
Eli Bendersky3e840192015-03-23 16:26:23 +0000282 if (!retval)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000283 return false;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000284 for (int i = 0, e = Vs.size(); i < e; i++) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000285 unsigned v = Vs[i];
Justin Holewinski0497ab12013-03-30 14:29:21 +0000286 if ((v >> 16) == index) {
287 align = v & 0xFFFF;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000288 return true;
289 }
290 }
291 return false;
292}
293
Justin Lebare6867082016-12-14 22:32:44 +0000294bool getAlign(const CallInst &I, unsigned index, unsigned &align) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000295 if (MDNode *alignNode = I.getMetadata("callalign")) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000296 for (int i = 0, n = alignNode->getNumOperands(); i < n; i++) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000297 if (const ConstantInt *CI =
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000298 mdconst::dyn_extract<ConstantInt>(alignNode->getOperand(i))) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000299 unsigned v = CI->getZExtValue();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000300 if ((v >> 16) == index) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000301 align = v & 0xFFFF;
302 return true;
303 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000304 if ((v >> 16) > index) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000305 return false;
306 }
307 }
308 }
309 }
310 return false;
311}
312
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000313// The following are some useful utilities for debugging
Justin Holewinskiae556d32012-05-04 20:18:50 +0000314
Justin Lebare6867082016-12-14 22:32:44 +0000315BasicBlock *getParentBlock(Value *v) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000316 if (BasicBlock *B = dyn_cast<BasicBlock>(v))
317 return B;
318
319 if (Instruction *I = dyn_cast<Instruction>(v))
320 return I->getParent();
321
Craig Topper062a2ba2014-04-25 05:30:21 +0000322 return nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000323}
324
Justin Lebare6867082016-12-14 22:32:44 +0000325Function *getParentFunction(Value *v) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000326 if (Function *F = dyn_cast<Function>(v))
327 return F;
328
329 if (Instruction *I = dyn_cast<Instruction>(v))
330 return I->getParent()->getParent();
331
332 if (BasicBlock *B = dyn_cast<BasicBlock>(v))
333 return B->getParent();
334
Craig Topper062a2ba2014-04-25 05:30:21 +0000335 return nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000336}
337
338// Dump a block by name
Justin Lebare6867082016-12-14 22:32:44 +0000339void dumpBlock(Value *v, char *blockName) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000340 Function *F = getParentFunction(v);
Craig Topper062a2ba2014-04-25 05:30:21 +0000341 if (!F)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000342 return;
343
344 for (Function::iterator it = F->begin(), ie = F->end(); it != ie; ++it) {
Duncan P. N. Exon Smith61149b82015-10-20 00:54:09 +0000345 BasicBlock *B = &*it;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000346 if (strcmp(B->getName().data(), blockName) == 0) {
347 B->dump();
348 return;
349 }
350 }
351}
352
353// Find an instruction by name
Justin Lebare6867082016-12-14 22:32:44 +0000354Instruction *getInst(Value *base, char *instName) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000355 Function *F = getParentFunction(base);
Craig Topper062a2ba2014-04-25 05:30:21 +0000356 if (!F)
357 return nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000358
359 for (inst_iterator it = inst_begin(F), ie = inst_end(F); it != ie; ++it) {
360 Instruction *I = &*it;
361 if (strcmp(I->getName().data(), instName) == 0) {
362 return I;
363 }
364 }
365
Craig Topper062a2ba2014-04-25 05:30:21 +0000366 return nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000367}
368
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000369// Dump an instruction by name
Justin Lebare6867082016-12-14 22:32:44 +0000370void dumpInst(Value *base, char *instName) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000371 Instruction *I = getInst(base, instName);
372 if (I)
373 I->dump();
374}
375
376// Dump an instruction and all dependent instructions
Justin Lebare6867082016-12-14 22:32:44 +0000377void dumpInstRec(Value *v, std::set<Instruction *> *visited) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000378 if (Instruction *I = dyn_cast<Instruction>(v)) {
379
380 if (visited->find(I) != visited->end())
381 return;
382
383 visited->insert(I);
384
385 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
386 dumpInstRec(I->getOperand(i), visited);
387
388 I->dump();
389 }
390}
391
392// Dump an instruction and all dependent instructions
Justin Lebare6867082016-12-14 22:32:44 +0000393void dumpInstRec(Value *v) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000394 std::set<Instruction *> visited;
395
396 //BasicBlock *B = getParentBlock(v);
397
398 dumpInstRec(v, &visited);
399}
400
401// Dump the parent for Instruction, block or function
Justin Lebare6867082016-12-14 22:32:44 +0000402void dumpParent(Value *v) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000403 if (Instruction *I = dyn_cast<Instruction>(v)) {
404 I->getParent()->dump();
405 return;
406 }
407
408 if (BasicBlock *B = dyn_cast<BasicBlock>(v)) {
409 B->getParent()->dump();
410 return;
411 }
412
413 if (Function *F = dyn_cast<Function>(v)) {
414 F->getParent()->dump();
415 return;
416 }
417}
Justin Lebare6867082016-12-14 22:32:44 +0000418
419} // namespace llvm