@@ -157,15 +157,17 @@ Instruction *emitCall(Module &M, Type *RetTy, StringRef FunctionName,
157
157
// Insert instrumental annotation calls, that has no arguments (for example
158
158
// work items start/finish/resume and barrier annotation.
159
159
void insertSimpleInstrumentationCall (Module &M, StringRef Name,
160
- Instruction *Position) {
160
+ Instruction *Position,
161
+ const DebugLoc &DL) {
161
162
Type *VoidTy = Type::getVoidTy (M.getContext ());
162
163
ArrayRef<Value *> Args;
163
164
Instruction *InstrumentationCall = emitCall (M, VoidTy, Name, Args, Position);
164
165
assert (InstrumentationCall && " Instrumentation call creation failed" );
166
+ InstrumentationCall->setDebugLoc (DL);
165
167
}
166
168
167
169
// Insert instrumental annotation calls for SPIR-V atomics.
168
- void insertAtomicInstrumentationCall (Module &M, StringRef Name,
170
+ bool insertAtomicInstrumentationCall (Module &M, StringRef Name,
169
171
CallInst *AtomicFun, Instruction *Position,
170
172
StringRef AtomicName) {
171
173
LLVMContext &Ctx = M.getContext ();
@@ -208,7 +210,7 @@ void insertAtomicInstrumentationCall(Module &M, StringRef Name,
208
210
auto *MemFlag = dyn_cast<ConstantInt>(AtomicFun->getArgOperand (2 ));
209
211
// TODO: add non-constant memory order processing
210
212
if (!MemFlag)
211
- return ;
213
+ return false ;
212
214
uint64_t IntMemFlag = MemFlag->getValue ().getZExtValue ();
213
215
uint64_t Order;
214
216
if (IntMemFlag & 0x2 )
@@ -219,10 +221,15 @@ void insertAtomicInstrumentationCall(Module &M, StringRef Name,
219
221
Order = 3 ;
220
222
else
221
223
Order = 0 ;
224
+ PointerType *Int8PtrAS4Ty = PointerType::get (IntegerType::get (Ctx, 8 ), 4 );
225
+ Ptr = CastInst::CreatePointerBitCastOrAddrSpaceCast (Ptr , Int8PtrAS4Ty, " " ,
226
+ Position);
222
227
Value *MemOrder = ConstantInt::get (Int32Ty, Order);
223
228
Value *Args[] = {Ptr , AtomicOp, MemOrder};
224
229
Instruction *InstrumentationCall = emitCall (M, VoidTy, Name, Args, Position);
225
230
assert (InstrumentationCall && " Instrumentation call creation failed" );
231
+ InstrumentationCall->setDebugLoc (AtomicFun->getDebugLoc ());
232
+ return true ;
226
233
}
227
234
228
235
} // namespace
@@ -245,15 +252,24 @@ PreservedAnalyses SPIRITTAnnotationsPass::run(Module &M,
245
252
246
253
// At the beggining of a kernel insert work item start annotation
247
254
// instruction.
248
- if (IsSPIRKernel)
249
- insertSimpleInstrumentationCall (M, ITT_ANNOTATION_WI_START,
250
- &*inst_begin (F));
255
+ if (IsSPIRKernel) {
256
+ Instruction *InsertPt = &*inst_begin (F);
257
+ if (InsertPt->isDebugOrPseudoInst ())
258
+ InsertPt = InsertPt->getNextNonDebugInstruction ();
259
+ assert (InsertPt && " Function does not have any real instructions." );
260
+ insertSimpleInstrumentationCall (M, ITT_ANNOTATION_WI_START, InsertPt,
261
+ InsertPt->getDebugLoc ());
262
+ IRModified = true ;
263
+ }
251
264
252
265
for (BasicBlock &BB : F) {
253
266
// Insert Finish instruction before return instruction
254
267
if (IsSPIRKernel)
255
- if (ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator ()))
256
- insertSimpleInstrumentationCall (M, ITT_ANNOTATION_WI_FINISH, RI);
268
+ if (ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator ())) {
269
+ insertSimpleInstrumentationCall (M, ITT_ANNOTATION_WI_FINISH, RI,
270
+ RI->getDebugLoc ());
271
+ IRModified = true ;
272
+ }
257
273
for (Instruction &I : BB) {
258
274
CallInst *CI = dyn_cast<CallInst>(&I);
259
275
if (!CI)
@@ -275,15 +291,17 @@ PreservedAnalyses SPIRITTAnnotationsPass::run(Module &M,
275
291
return CalleeName.startswith (Name);
276
292
})) {
277
293
Instruction *InstAfterBarrier = CI->getNextNode ();
278
- insertSimpleInstrumentationCall (M, ITT_ANNOTATION_WG_BARRIER, CI);
294
+ const DebugLoc &DL = CI->getDebugLoc ();
295
+ insertSimpleInstrumentationCall (M, ITT_ANNOTATION_WG_BARRIER, CI, DL);
279
296
insertSimpleInstrumentationCall (M, ITT_ANNOTATION_WI_RESUME,
280
- InstAfterBarrier);
297
+ InstAfterBarrier, DL);
298
+ IRModified = true ;
281
299
} else if (CalleeName.startswith (SPIRV_ATOMIC_INST)) {
282
300
Instruction *InstAfterAtomic = CI->getNextNode ();
283
- insertAtomicInstrumentationCall (M, ITT_ANNOTATION_ATOMIC_START, CI,
284
- CI, CalleeName);
285
- insertAtomicInstrumentationCall (M, ITT_ANNOTATION_ATOMIC_FINISH, CI,
286
- InstAfterAtomic, CalleeName);
301
+ IRModified |= insertAtomicInstrumentationCall (
302
+ M, ITT_ANNOTATION_ATOMIC_START, CI, CI, CalleeName);
303
+ IRModified |= insertAtomicInstrumentationCall (
304
+ M, ITT_ANNOTATION_ATOMIC_FINISH, CI, InstAfterAtomic, CalleeName);
287
305
}
288
306
}
289
307
}
0 commit comments