Skip to content

Commit a7b8daf

Browse files
authored
[NFC][SYCL] Apply post-commit comments to ITT annotation support (#3691)
Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
1 parent bc5b2fa commit a7b8daf

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
994994
if (!llvm::Triple(TheModule->getTargetTriple()).isSPIR())
995995
llvm::report_fatal_error(
996996
"ITT annotations can only by added to a module with spir target");
997-
PerModulePasses.add(createSPIRITTAnnotationsPass());
997+
PerModulePasses.add(createSPIRITTAnnotationsLegacyPass());
998998
}
999999

10001000
// Allocate static local memory in SYCL kernel scope for each allocation call.

llvm/include/llvm/LinkAllPasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ namespace {
206206
(void)llvm::createSYCLLowerESIMDPass();
207207
(void)llvm::createESIMDLowerLoadStorePass();
208208
(void)llvm::createESIMDLowerVecArgPass();
209-
(void)llvm::createSPIRITTAnnotationsPass();
209+
(void)llvm::createSPIRITTAnnotationsLegacyPass();
210210
(void)llvm::createSYCLLowerWGLocalMemoryLegacyPass();
211211
std::string buf;
212212
llvm::raw_string_ostream os(buf);

llvm/include/llvm/Transforms/Instrumentation/SPIRITTAnnotations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ class SPIRITTAnnotationsPass : public PassInfoMixin<SPIRITTAnnotationsPass> {
2222
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
2323
};
2424

25-
ModulePass *createSPIRITTAnnotationsPass();
25+
ModulePass *createSPIRITTAnnotationsLegacyPass();
2626

2727
} // namespace llvm

llvm/lib/Transforms/Instrumentation/SPIRITTAnnotations.cpp

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ INITIALIZE_PASS(SPIRITTAnnotationsLegacyPass, "SPIRITTAnnotations",
132132
"Insert ITT annotations in SPIR code", false, false)
133133

134134
// Public interface to the SPIRITTAnnotationsPass.
135-
ModulePass *llvm::createSPIRITTAnnotationsPass() {
135+
ModulePass *llvm::createSPIRITTAnnotationsLegacyPass() {
136136
return new SPIRITTAnnotationsLegacyPass();
137137
}
138138

@@ -151,35 +151,32 @@ Instruction *emitCall(Module &M, Type *RetTy, StringRef FunctionName,
151151
auto *FT = FunctionType::get(RetTy, ArgTys, false /*isVarArg*/);
152152
FunctionCallee FC = M.getOrInsertFunction(FunctionName, FT);
153153
assert(FC.getCallee() && "Instruction creation failed");
154-
auto *Call = CallInst::Create(FT, FC.getCallee(), Args, "", InsertBefore);
155-
return Call;
154+
return CallInst::Create(FT, FC.getCallee(), Args, "", InsertBefore);
156155
}
157156

158157
// Insert instrumental annotation calls, that has no arguments (for example
159158
// work items start/finish/resume and barrier annotation.
160-
bool insertSimpleInstrumentationCall(Module &M, StringRef Name,
159+
void insertSimpleInstrumentationCall(Module &M, StringRef Name,
161160
Instruction *Position) {
162161
Type *VoidTy = Type::getVoidTy(M.getContext());
163162
ArrayRef<Value *> Args;
164163
Instruction *InstrumentationCall = emitCall(M, VoidTy, Name, Args, Position);
165164
assert(InstrumentationCall && "Instrumentation call creation failed");
166-
return true;
167165
}
168166

169167
// Insert instrumental annotation calls for SPIR-V atomics.
170-
bool insertAtomicInstrumentationCall(Module &M, StringRef Name,
171-
CallInst *AtomicFun,
172-
Instruction *Position) {
168+
void insertAtomicInstrumentationCall(Module &M, StringRef Name,
169+
CallInst *AtomicFun, Instruction *Position,
170+
StringRef AtomicName) {
173171
LLVMContext &Ctx = M.getContext();
174172
Type *VoidTy = Type::getVoidTy(Ctx);
175-
Type *Int32Ty = Type::getInt32Ty(Ctx);
173+
IntegerType *Int32Ty = IntegerType::get(Ctx, 32);
176174
// __spirv_Atomic... instructions have following arguments:
177175
// Pointer, Memory Scope, Memory Semantics and others. To construct Atomic
178176
// annotation instructions we need Pointer and Memory Semantic arguments
179177
// taken from the original Atomic instruction.
180178
Value *Ptr = dyn_cast<Value>(AtomicFun->getArgOperand(0));
181-
StringRef AtomicName = AtomicFun->getCalledFunction()->getName();
182-
Value *AtomicOp;
179+
assert(Ptr && "Failed to get a pointer argument of atomic instruction");
183180
// Second parameter of Atomic Start/Finish annotation is an Op code of
184181
// the instruction, encoded into a value of enum, defined like this on user's/
185182
// profiler's side:
@@ -189,12 +186,11 @@ bool insertAtomicInstrumentationCall(Module &M, StringRef Name,
189186
// __itt_mem_store = 1,
190187
// __itt_mem_update = 2
191188
// }
192-
if (AtomicName.contains(SPIRV_ATOMIC_LOAD))
193-
AtomicOp = ConstantInt::get(Int32Ty, 0);
194-
else if (AtomicName.contains(SPIRV_ATOMIC_STORE))
195-
AtomicOp = ConstantInt::get(Int32Ty, 1);
196-
else
197-
AtomicOp = ConstantInt::get(Int32Ty, 2);
189+
ConstantInt *AtomicOp =
190+
StringSwitch<ConstantInt *>(AtomicName)
191+
.StartsWith(SPIRV_ATOMIC_LOAD, ConstantInt::get(Int32Ty, 0))
192+
.StartsWith(SPIRV_ATOMIC_STORE, ConstantInt::get(Int32Ty, 1))
193+
.Default(ConstantInt::get(Int32Ty, 2));
198194
// Third parameter of Atomic Start/Finish annotation is an ordering
199195
// semantic of the instruction, encoded into a value of enum, defined like
200196
// this on user's/profiler's side:
@@ -209,23 +205,24 @@ bool insertAtomicInstrumentationCall(Module &M, StringRef Name,
209205
// differencies in values between SYCL mem order and SPIR-V mem order, SYCL RT
210206
// also applies Memory Semantic mask, like WorkgroupMemory (0x100)), need to
211207
// align it.
212-
uint64_t MemFlag = dyn_cast<ConstantInt>(AtomicFun->getArgOperand(2))
213-
->getValue()
214-
.getZExtValue();
208+
auto *MemFlag = dyn_cast<ConstantInt>(AtomicFun->getArgOperand(2));
209+
// TODO: add non-constant memory order processing
210+
if (!MemFlag)
211+
return;
212+
uint64_t IntMemFlag = MemFlag->getValue().getZExtValue();
215213
uint64_t Order;
216-
if (MemFlag & 0x2)
214+
if (IntMemFlag & 0x2)
217215
Order = 1;
218-
else if (MemFlag & 0x4)
216+
else if (IntMemFlag & 0x4)
219217
Order = 2;
220-
else if (MemFlag & 0x8)
218+
else if (IntMemFlag & 0x8)
221219
Order = 3;
222220
else
223221
Order = 0;
224222
Value *MemOrder = ConstantInt::get(Int32Ty, Order);
225223
Value *Args[] = {Ptr, AtomicOp, MemOrder};
226224
Instruction *InstrumentationCall = emitCall(M, VoidTy, Name, Args, Position);
227225
assert(InstrumentationCall && "Instrumentation call creation failed");
228-
return true;
229226
}
230227

231228
} // namespace
@@ -249,15 +246,14 @@ PreservedAnalyses SPIRITTAnnotationsPass::run(Module &M,
249246
// At the beggining of a kernel insert work item start annotation
250247
// instruction.
251248
if (IsSPIRKernel)
252-
IRModified |= insertSimpleInstrumentationCall(M, ITT_ANNOTATION_WI_START,
253-
&*inst_begin(F));
249+
insertSimpleInstrumentationCall(M, ITT_ANNOTATION_WI_START,
250+
&*inst_begin(F));
254251

255252
for (BasicBlock &BB : F) {
256253
// Insert Finish instruction before return instruction
257254
if (IsSPIRKernel)
258255
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator()))
259-
IRModified |=
260-
insertSimpleInstrumentationCall(M, ITT_ANNOTATION_WI_FINISH, RI);
256+
insertSimpleInstrumentationCall(M, ITT_ANNOTATION_WI_FINISH, RI);
261257
for (Instruction &I : BB) {
262258
CallInst *CI = dyn_cast<CallInst>(&I);
263259
if (!CI)
@@ -279,16 +275,15 @@ PreservedAnalyses SPIRITTAnnotationsPass::run(Module &M,
279275
return CalleeName.startswith(Name);
280276
})) {
281277
Instruction *InstAfterBarrier = CI->getNextNode();
282-
IRModified |=
283-
insertSimpleInstrumentationCall(M, ITT_ANNOTATION_WG_BARRIER, CI);
284-
IRModified |= insertSimpleInstrumentationCall(
285-
M, ITT_ANNOTATION_WI_RESUME, InstAfterBarrier);
278+
insertSimpleInstrumentationCall(M, ITT_ANNOTATION_WG_BARRIER, CI);
279+
insertSimpleInstrumentationCall(M, ITT_ANNOTATION_WI_RESUME,
280+
InstAfterBarrier);
286281
} else if (CalleeName.startswith(SPIRV_ATOMIC_INST)) {
287282
Instruction *InstAfterAtomic = CI->getNextNode();
288-
IRModified |= insertAtomicInstrumentationCall(
289-
M, ITT_ANNOTATION_ATOMIC_START, CI, CI);
290-
IRModified |= insertAtomicInstrumentationCall(
291-
M, ITT_ANNOTATION_ATOMIC_FINISH, CI, InstAfterAtomic);
283+
insertAtomicInstrumentationCall(M, ITT_ANNOTATION_ATOMIC_START, CI,
284+
CI, CalleeName);
285+
insertAtomicInstrumentationCall(M, ITT_ANNOTATION_ATOMIC_FINISH, CI,
286+
InstAfterAtomic, CalleeName);
292287
}
293288
}
294289
}

0 commit comments

Comments
 (0)