LLVM  9.0.0svn
NVPTXUtilities.cpp
Go to the documentation of this file.
1 //===- NVPTXUtilities.cpp - Utility Functions -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains miscellaneous utility functions
10 //===----------------------------------------------------------------------===//
11 
12 #include "NVPTXUtilities.h"
13 #include "NVPTX.h"
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/GlobalVariable.h"
17 #include "llvm/IR/InstIterator.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/Operator.h"
22 #include <algorithm>
23 #include <cstring>
24 #include <map>
25 #include <string>
26 #include <vector>
27 
28 namespace llvm {
29 
30 namespace {
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;
34 } // anonymous namespace
35 
38 
39 void clearAnnotationCache(const Module *Mod) {
40  MutexGuard Guard(Lock);
41  annotationCache->erase(Mod);
42 }
43 
44 static void cacheAnnotationFromMD(const MDNode *md, key_val_pair_t &retval) {
45  MutexGuard Guard(Lock);
46  assert(md && "Invalid mdnode for annotation");
47  assert((md->getNumOperands() % 2) == 1 && "Invalid number of operands");
48  // start index = 1, to skip the global variable key
49  // increment = 2, to skip the value for each property-value pairs
50  for (unsigned i = 1, e = md->getNumOperands(); i != e; i += 2) {
51  // property
52  const MDString *prop = dyn_cast<MDString>(md->getOperand(i));
53  assert(prop && "Annotation property not a string");
54 
55  // value
56  ConstantInt *Val = mdconst::dyn_extract<ConstantInt>(md->getOperand(i + 1));
57  assert(Val && "Value operand not a constant int");
58 
59  std::string keyname = prop->getString().str();
60  if (retval.find(keyname) != retval.end())
61  retval[keyname].push_back(Val->getZExtValue());
62  else {
63  std::vector<unsigned> tmp;
64  tmp.push_back(Val->getZExtValue());
65  retval[keyname] = tmp;
66  }
67  }
68 }
69 
70 static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) {
71  MutexGuard Guard(Lock);
72  NamedMDNode *NMD = m->getNamedMetadata("nvvm.annotations");
73  if (!NMD)
74  return;
75  key_val_pair_t tmp;
76  for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
77  const MDNode *elem = NMD->getOperand(i);
78 
79  GlobalValue *entity =
80  mdconst::dyn_extract_or_null<GlobalValue>(elem->getOperand(0));
81  // entity may be null due to DCE
82  if (!entity)
83  continue;
84  if (entity != gv)
85  continue;
86 
87  // accumulate annotations for entity in tmp
88  cacheAnnotationFromMD(elem, tmp);
89  }
90 
91  if (tmp.empty()) // no annotations for this gv
92  return;
93 
94  if ((*annotationCache).find(m) != (*annotationCache).end())
95  (*annotationCache)[m][gv] = std::move(tmp);
96  else {
97  global_val_annot_t tmp1;
98  tmp1[gv] = std::move(tmp);
99  (*annotationCache)[m] = std::move(tmp1);
100  }
101 }
102 
103 bool findOneNVVMAnnotation(const GlobalValue *gv, const std::string &prop,
104  unsigned &retval) {
105  MutexGuard Guard(Lock);
106  const Module *m = gv->getParent();
107  if ((*annotationCache).find(m) == (*annotationCache).end())
108  cacheAnnotationFromMD(m, gv);
109  else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
110  cacheAnnotationFromMD(m, gv);
111  if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
112  return false;
113  retval = (*annotationCache)[m][gv][prop][0];
114  return true;
115 }
116 
117 bool findAllNVVMAnnotation(const GlobalValue *gv, const std::string &prop,
118  std::vector<unsigned> &retval) {
119  MutexGuard Guard(Lock);
120  const Module *m = gv->getParent();
121  if ((*annotationCache).find(m) == (*annotationCache).end())
122  cacheAnnotationFromMD(m, gv);
123  else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
124  cacheAnnotationFromMD(m, gv);
125  if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
126  return false;
127  retval = (*annotationCache)[m][gv][prop];
128  return true;
129 }
130 
131 bool isTexture(const Value &val) {
132  if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
133  unsigned annot;
134  if (findOneNVVMAnnotation(gv, "texture", annot)) {
135  assert((annot == 1) && "Unexpected annotation on a texture symbol");
136  return true;
137  }
138  }
139  return false;
140 }
141 
142 bool isSurface(const Value &val) {
143  if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
144  unsigned annot;
145  if (findOneNVVMAnnotation(gv, "surface", annot)) {
146  assert((annot == 1) && "Unexpected annotation on a surface symbol");
147  return true;
148  }
149  }
150  return false;
151 }
152 
153 bool isSampler(const Value &val) {
154  const char *AnnotationName = "sampler";
155 
156  if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
157  unsigned annot;
158  if (findOneNVVMAnnotation(gv, AnnotationName, annot)) {
159  assert((annot == 1) && "Unexpected annotation on a sampler symbol");
160  return true;
161  }
162  }
163  if (const Argument *arg = dyn_cast<Argument>(&val)) {
164  const Function *func = arg->getParent();
165  std::vector<unsigned> annot;
166  if (findAllNVVMAnnotation(func, AnnotationName, annot)) {
167  if (is_contained(annot, arg->getArgNo()))
168  return true;
169  }
170  }
171  return false;
172 }
173 
174 bool isImageReadOnly(const Value &val) {
175  if (const Argument *arg = dyn_cast<Argument>(&val)) {
176  const Function *func = arg->getParent();
177  std::vector<unsigned> annot;
178  if (findAllNVVMAnnotation(func, "rdoimage", annot)) {
179  if (is_contained(annot, arg->getArgNo()))
180  return true;
181  }
182  }
183  return false;
184 }
185 
186 bool isImageWriteOnly(const Value &val) {
187  if (const Argument *arg = dyn_cast<Argument>(&val)) {
188  const Function *func = arg->getParent();
189  std::vector<unsigned> annot;
190  if (findAllNVVMAnnotation(func, "wroimage", annot)) {
191  if (is_contained(annot, arg->getArgNo()))
192  return true;
193  }
194  }
195  return false;
196 }
197 
198 bool isImageReadWrite(const Value &val) {
199  if (const Argument *arg = dyn_cast<Argument>(&val)) {
200  const Function *func = arg->getParent();
201  std::vector<unsigned> annot;
202  if (findAllNVVMAnnotation(func, "rdwrimage", annot)) {
203  if (is_contained(annot, arg->getArgNo()))
204  return true;
205  }
206  }
207  return false;
208 }
209 
210 bool isImage(const Value &val) {
211  return isImageReadOnly(val) || isImageWriteOnly(val) || isImageReadWrite(val);
212 }
213 
214 bool isManaged(const Value &val) {
215  if(const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
216  unsigned annot;
217  if (findOneNVVMAnnotation(gv, "managed", annot)) {
218  assert((annot == 1) && "Unexpected annotation on a managed symbol");
219  return true;
220  }
221  }
222  return false;
223 }
224 
225 std::string getTextureName(const Value &val) {
226  assert(val.hasName() && "Found texture variable with no name");
227  return val.getName();
228 }
229 
230 std::string getSurfaceName(const Value &val) {
231  assert(val.hasName() && "Found surface variable with no name");
232  return val.getName();
233 }
234 
235 std::string getSamplerName(const Value &val) {
236  assert(val.hasName() && "Found sampler variable with no name");
237  return val.getName();
238 }
239 
240 bool getMaxNTIDx(const Function &F, unsigned &x) {
241  return findOneNVVMAnnotation(&F, "maxntidx", x);
242 }
243 
244 bool getMaxNTIDy(const Function &F, unsigned &y) {
245  return findOneNVVMAnnotation(&F, "maxntidy", y);
246 }
247 
248 bool getMaxNTIDz(const Function &F, unsigned &z) {
249  return findOneNVVMAnnotation(&F, "maxntidz", z);
250 }
251 
252 bool getReqNTIDx(const Function &F, unsigned &x) {
253  return findOneNVVMAnnotation(&F, "reqntidx", x);
254 }
255 
256 bool getReqNTIDy(const Function &F, unsigned &y) {
257  return findOneNVVMAnnotation(&F, "reqntidy", y);
258 }
259 
260 bool getReqNTIDz(const Function &F, unsigned &z) {
261  return findOneNVVMAnnotation(&F, "reqntidz", z);
262 }
263 
264 bool getMinCTASm(const Function &F, unsigned &x) {
265  return findOneNVVMAnnotation(&F, "minctasm", x);
266 }
267 
268 bool getMaxNReg(const Function &F, unsigned &x) {
269  return findOneNVVMAnnotation(&F, "maxnreg", x);
270 }
271 
273  unsigned x = 0;
274  bool retval = findOneNVVMAnnotation(&F, "kernel", x);
275  if (!retval) {
276  // There is no NVVM metadata, check the calling convention
278  }
279  return (x == 1);
280 }
281 
282 bool getAlign(const Function &F, unsigned index, unsigned &align) {
283  std::vector<unsigned> Vs;
284  bool retval = findAllNVVMAnnotation(&F, "align", Vs);
285  if (!retval)
286  return false;
287  for (int i = 0, e = Vs.size(); i < e; i++) {
288  unsigned v = Vs[i];
289  if ((v >> 16) == index) {
290  align = v & 0xFFFF;
291  return true;
292  }
293  }
294  return false;
295 }
296 
297 bool getAlign(const CallInst &I, unsigned index, unsigned &align) {
298  if (MDNode *alignNode = I.getMetadata("callalign")) {
299  for (int i = 0, n = alignNode->getNumOperands(); i < n; i++) {
300  if (const ConstantInt *CI =
301  mdconst::dyn_extract<ConstantInt>(alignNode->getOperand(i))) {
302  unsigned v = CI->getZExtValue();
303  if ((v >> 16) == index) {
304  align = v & 0xFFFF;
305  return true;
306  }
307  if ((v >> 16) > index) {
308  return false;
309  }
310  }
311  }
312  }
313  return false;
314 }
315 
316 } // namespace llvm
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:218
bool getAlign(const Function &F, unsigned index, unsigned &align)
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1080
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool getMaxNReg(const Function &F, unsigned &x)
bool findOneNVVMAnnotation(const GlobalValue *gv, const std::string &prop, unsigned &retval)
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
bool findAllNVVMAnnotation(const GlobalValue *gv, const std::string &prop, std::vector< unsigned > &retval)
bool isTexture(const Value &val)
This class represents a function call, abstracting a target machine&#39;s calling convention.
static sys::Mutex Lock
Metadata node.
Definition: Metadata.h:863
F(f)
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1068
A tuple of MDNodes.
Definition: Metadata.h:1325
void clearAnnotationCache(const Module *Mod)
unsigned getNumOperands() const
Definition: Metadata.cpp:1076
bool getMaxNTIDz(const Function &F, unsigned &z)
bool isKernelFunction(const Function &F)
bool isSurface(const Value &val)
bool getMaxNTIDx(const Function &F, unsigned &x)
std::string getTextureName(const Value &val)
NamedMDNode * getNamedMetadata(const Twine &Name) const
Return the first NamedMDNode in the module with the specified name.
Definition: Module.cpp:250
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:234
bool isSampler(const Value &val)
StringRef getString() const
Definition: Metadata.cpp:463
Instances of this class acquire a given Mutex Lock when constructed and hold that lock until destruct...
Definition: MutexGuard.h:26
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:148
bool hasName() const
Definition: Value.h:250
This file contains the declarations for the subclasses of Constant, which represent the different fla...
PTX_Kernel - Call to a PTX kernel.
Definition: CallingConv.h:114
bool getMaxNTIDy(const Function &F, unsigned &y)
bool getReqNTIDx(const Function &F, unsigned &x)
bool isImage(const Value &val)
bool getReqNTIDz(const Function &F, unsigned &z)
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:212
Module.h This file contains the declarations for the Module class.
bool isImageReadWrite(const Value &val)
std::string getSurfaceName(const Value &val)
std::string getSamplerName(const Value &val)
bool isManaged(const Value &val)
bool getReqNTIDy(const Function &F, unsigned &y)
bool isImageReadOnly(const Value &val)
static void cacheAnnotationFromMD(const MDNode *md, key_val_pair_t &retval)
static ManagedStatic< per_module_annot_t > annotationCache
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:322
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:565
LLVM Value Representation.
Definition: Value.h:72
A single uniqued string.
Definition: Metadata.h:603
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:60
bool getMinCTASm(const Function &F, unsigned &x)
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1074
bool isImageWriteOnly(const Value &val)
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1244