13
13
#include " NVPTXUtilities.h"
14
14
#include " NVPTX.h"
15
15
#include " NVPTXTargetMachine.h"
16
+ #include " llvm/ADT/StringRef.h"
16
17
#include " llvm/IR/Constants.h"
17
18
#include " llvm/IR/Function.h"
18
19
#include " llvm/IR/GlobalVariable.h"
@@ -130,8 +131,8 @@ static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) {
130
131
}
131
132
}
132
133
133
- bool findOneNVVMAnnotation (const GlobalValue *gv, const std::string &prop ,
134
- unsigned &retval ) {
134
+ static std::optional< unsigned > findOneNVVMAnnotation (const GlobalValue *gv,
135
+ const std::string &prop ) {
135
136
auto &AC = getAnnotationCache ();
136
137
std::lock_guard<sys::Mutex> Guard (AC.Lock );
137
138
const Module *m = gv->getParent ();
@@ -140,21 +141,13 @@ bool findOneNVVMAnnotation(const GlobalValue *gv, const std::string &prop,
140
141
else if (AC.Cache [m].find (gv) == AC.Cache [m].end ())
141
142
cacheAnnotationFromMD (m, gv);
142
143
if (AC.Cache [m][gv].find (prop) == AC.Cache [m][gv].end ())
143
- return false ;
144
- retval = AC.Cache [m][gv][prop][0 ];
145
- return true ;
146
- }
147
-
148
- static std::optional<unsigned >
149
- findOneNVVMAnnotation (const GlobalValue &GV, const std::string &PropName) {
150
- unsigned RetVal;
151
- if (findOneNVVMAnnotation (&GV, PropName, RetVal))
152
- return RetVal;
153
- return std::nullopt;
144
+ return std::nullopt;
145
+ return AC.Cache [m][gv][prop][0 ];
154
146
}
155
147
156
- bool findAllNVVMAnnotation (const GlobalValue *gv, const std::string &prop,
157
- std::vector<unsigned > &retval) {
148
+ static bool findAllNVVMAnnotation (const GlobalValue *gv,
149
+ const std::string &prop,
150
+ std::vector<unsigned > &retval) {
158
151
auto &AC = getAnnotationCache ();
159
152
std::lock_guard<sys::Mutex> Guard (AC.Lock );
160
153
const Module *m = gv->getParent ();
@@ -168,25 +161,13 @@ bool findAllNVVMAnnotation(const GlobalValue *gv, const std::string &prop,
168
161
return true ;
169
162
}
170
163
171
- bool isTexture (const Value &val) {
172
- if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
173
- unsigned Annot;
174
- if (findOneNVVMAnnotation (gv, " texture" , Annot)) {
175
- assert ((Annot == 1 ) && " Unexpected annotation on a texture symbol" );
164
+ static bool globalHasNVVMAnnotation (const Value &V, const std::string &Prop) {
165
+ if (const auto *GV = dyn_cast<GlobalValue>(&V))
166
+ if (const auto Annot = findOneNVVMAnnotation (GV, Prop)) {
167
+ assert ((*Annot == 1 ) && " Unexpected annotation on a symbol" );
176
168
return true ;
177
169
}
178
- }
179
- return false ;
180
- }
181
170
182
- bool isSurface (const Value &val) {
183
- if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
184
- unsigned Annot;
185
- if (findOneNVVMAnnotation (gv, " surface" , Annot)) {
186
- assert ((Annot == 1 ) && " Unexpected annotation on a surface symbol" );
187
- return true ;
188
- }
189
- }
190
171
return false ;
191
172
}
192
173
@@ -220,71 +201,60 @@ bool isParamGridConstant(const Value &V) {
220
201
return false ;
221
202
}
222
203
223
- bool isSampler (const Value &val) {
204
+ bool isTexture (const Value &V) { return globalHasNVVMAnnotation (V, " texture" ); }
205
+
206
+ bool isSurface (const Value &V) { return globalHasNVVMAnnotation (V, " surface" ); }
207
+
208
+ bool isSampler (const Value &V) {
224
209
const char *AnnotationName = " sampler" ;
225
210
226
- if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
227
- unsigned Annot;
228
- if (findOneNVVMAnnotation (gv, AnnotationName, Annot)) {
229
- assert ((Annot == 1 ) && " Unexpected annotation on a sampler symbol" );
230
- return true ;
231
- }
232
- }
233
- return argHasNVVMAnnotation (val, AnnotationName);
211
+ return globalHasNVVMAnnotation (V, AnnotationName) ||
212
+ argHasNVVMAnnotation (V, AnnotationName);
234
213
}
235
214
236
- bool isImageReadOnly (const Value &val ) {
237
- return argHasNVVMAnnotation (val , " rdoimage" );
215
+ bool isImageReadOnly (const Value &V ) {
216
+ return argHasNVVMAnnotation (V , " rdoimage" );
238
217
}
239
218
240
- bool isImageWriteOnly (const Value &val ) {
241
- return argHasNVVMAnnotation (val , " wroimage" );
219
+ bool isImageWriteOnly (const Value &V ) {
220
+ return argHasNVVMAnnotation (V , " wroimage" );
242
221
}
243
222
244
- bool isImageReadWrite (const Value &val ) {
245
- return argHasNVVMAnnotation (val , " rdwrimage" );
223
+ bool isImageReadWrite (const Value &V ) {
224
+ return argHasNVVMAnnotation (V , " rdwrimage" );
246
225
}
247
226
248
- bool isImage (const Value &val ) {
249
- return isImageReadOnly (val ) || isImageWriteOnly (val ) || isImageReadWrite (val );
227
+ bool isImage (const Value &V ) {
228
+ return isImageReadOnly (V ) || isImageWriteOnly (V ) || isImageReadWrite (V );
250
229
}
251
230
252
- bool isManaged (const Value &val) {
253
- if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
254
- unsigned Annot;
255
- if (findOneNVVMAnnotation (gv, " managed" , Annot)) {
256
- assert ((Annot == 1 ) && " Unexpected annotation on a managed symbol" );
257
- return true ;
258
- }
259
- }
260
- return false ;
261
- }
231
+ bool isManaged (const Value &V) { return globalHasNVVMAnnotation (V, " managed" ); }
262
232
263
- std::string getTextureName (const Value &val ) {
264
- assert (val .hasName () && " Found texture variable with no name" );
265
- return std::string (val .getName () );
233
+ StringRef getTextureName (const Value &V ) {
234
+ assert (V .hasName () && " Found texture variable with no name" );
235
+ return V .getName ();
266
236
}
267
237
268
- std::string getSurfaceName (const Value &val ) {
269
- assert (val .hasName () && " Found surface variable with no name" );
270
- return std::string (val .getName () );
238
+ StringRef getSurfaceName (const Value &V ) {
239
+ assert (V .hasName () && " Found surface variable with no name" );
240
+ return V .getName ();
271
241
}
272
242
273
- std::string getSamplerName (const Value &val ) {
274
- assert (val .hasName () && " Found sampler variable with no name" );
275
- return std::string (val .getName () );
243
+ StringRef getSamplerName (const Value &V ) {
244
+ assert (V .hasName () && " Found sampler variable with no name" );
245
+ return V .getName ();
276
246
}
277
247
278
248
std::optional<unsigned > getMaxNTIDx (const Function &F) {
279
- return findOneNVVMAnnotation (F, " maxntidx" );
249
+ return findOneNVVMAnnotation (& F, " maxntidx" );
280
250
}
281
251
282
252
std::optional<unsigned > getMaxNTIDy (const Function &F) {
283
- return findOneNVVMAnnotation (F, " maxntidy" );
253
+ return findOneNVVMAnnotation (& F, " maxntidy" );
284
254
}
285
255
286
256
std::optional<unsigned > getMaxNTIDz (const Function &F) {
287
- return findOneNVVMAnnotation (F, " maxntidz" );
257
+ return findOneNVVMAnnotation (& F, " maxntidz" );
288
258
}
289
259
290
260
std::optional<unsigned > getMaxNTID (const Function &F) {
@@ -302,20 +272,20 @@ std::optional<unsigned> getMaxNTID(const Function &F) {
302
272
return std::nullopt;
303
273
}
304
274
305
- bool getMaxClusterRank (const Function &F, unsigned &x ) {
306
- return findOneNVVMAnnotation (&F, " maxclusterrank" , x );
275
+ std::optional< unsigned > getMaxClusterRank (const Function &F) {
276
+ return findOneNVVMAnnotation (&F, " maxclusterrank" );
307
277
}
308
278
309
279
std::optional<unsigned > getReqNTIDx (const Function &F) {
310
- return findOneNVVMAnnotation (F, " reqntidx" );
280
+ return findOneNVVMAnnotation (& F, " reqntidx" );
311
281
}
312
282
313
283
std::optional<unsigned > getReqNTIDy (const Function &F) {
314
- return findOneNVVMAnnotation (F, " reqntidy" );
284
+ return findOneNVVMAnnotation (& F, " reqntidy" );
315
285
}
316
286
317
287
std::optional<unsigned > getReqNTIDz (const Function &F) {
318
- return findOneNVVMAnnotation (F, " reqntidz" );
288
+ return findOneNVVMAnnotation (& F, " reqntidz" );
319
289
}
320
290
321
291
std::optional<unsigned > getReqNTID (const Function &F) {
@@ -328,21 +298,20 @@ std::optional<unsigned> getReqNTID(const Function &F) {
328
298
return std::nullopt;
329
299
}
330
300
331
- bool getMinCTASm (const Function &F, unsigned &x ) {
332
- return findOneNVVMAnnotation (&F, " minctasm" , x );
301
+ std::optional< unsigned > getMinCTASm (const Function &F) {
302
+ return findOneNVVMAnnotation (&F, " minctasm" );
333
303
}
334
304
335
- bool getMaxNReg (const Function &F, unsigned &x ) {
336
- return findOneNVVMAnnotation (&F, " maxnreg" , x );
305
+ std::optional< unsigned > getMaxNReg (const Function &F) {
306
+ return findOneNVVMAnnotation (&F, " maxnreg" );
337
307
}
338
308
339
309
bool isKernelFunction (const Function &F) {
340
- unsigned x = 0 ;
341
- if (!findOneNVVMAnnotation (&F, " kernel" , x)) {
342
- // There is no NVVM metadata, check the calling convention
343
- return F.getCallingConv () == CallingConv::PTX_Kernel;
344
- }
345
- return (x == 1 );
310
+ if (const auto X = findOneNVVMAnnotation (&F, " kernel" ))
311
+ return (*X == 1 );
312
+
313
+ // There is no NVVM metadata, check the calling convention
314
+ return F.getCallingConv () == CallingConv::PTX_Kernel;
346
315
}
347
316
348
317
MaybeAlign getAlign (const Function &F, unsigned Index) {
0 commit comments