1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
//===- KernelInfo.cpp - Kernel Analysis -----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the KernelInfoPrinter class used to emit remarks about
// function properties from a GPU kernel.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/KernelInfo.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
using namespace llvm;
#define DEBUG_TYPE "kernel-info"
namespace {
/// Data structure holding function info for kernels.
class KernelInfo {
void updateForBB(const BasicBlock &BB, OptimizationRemarkEmitter &ORE);
public:
static void emitKernelInfo(Function &F, FunctionAnalysisManager &FAM,
TargetMachine *TM);
/// Whether the function has external linkage and is not a kernel function.
bool ExternalNotKernel = false;
/// Launch bounds.
SmallVector<std::pair<StringRef, int64_t>> LaunchBounds;
/// The number of alloca instructions inside the function, the number of those
/// with allocation sizes that cannot be determined at compile time, and the
/// sum of the sizes that can be.
///
/// With the current implementation for at least some GPU archs,
/// AllocasDyn > 0 might not be possible, but we report AllocasDyn anyway in
/// case the implementation changes.
int64_t Allocas = 0;
int64_t AllocasDyn = 0;
int64_t AllocasStaticSizeSum = 0;
/// Number of direct/indirect calls (anything derived from CallBase).
int64_t DirectCalls = 0;
int64_t IndirectCalls = 0;
/// Number of direct calls made from this function to other functions
/// defined in this module.
int64_t DirectCallsToDefinedFunctions = 0;
/// Number of direct calls to inline assembly.
int64_t InlineAssemblyCalls = 0;
/// Number of calls of type InvokeInst.
int64_t Invokes = 0;
/// Target-specific flat address space.
unsigned FlatAddrspace;
/// Number of flat address space memory accesses (via load, store, etc.).
int64_t FlatAddrspaceAccesses = 0;
};
} // end anonymous namespace
static void identifyCallee(OptimizationRemark &R, const Module *M,
const Value *V, StringRef Kind = "") {
SmallString<100> Name; // might be function name or asm expression
if (const Function *F = dyn_cast<Function>(V)) {
if (auto *SubProgram = F->getSubprogram()) {
if (SubProgram->isArtificial())
R << "artificial ";
Name = SubProgram->getName();
}
}
if (Name.empty()) {
raw_svector_ostream OS(Name);
V->printAsOperand(OS, /*PrintType=*/false, M);
}
if (!Kind.empty())
R << Kind << " ";
R << "'" << Name << "'";
}
static void identifyFunction(OptimizationRemark &R, const Function &F) {
identifyCallee(R, F.getParent(), &F, "function");
}
static void remarkAlloca(OptimizationRemarkEmitter &ORE, const Function &Caller,
const AllocaInst &Alloca,
TypeSize::ScalarTy StaticSize) {
ORE.emit([&] {
StringRef DbgName;
DebugLoc Loc;
bool Artificial = false;
auto DVRs = findDVRDeclares(&const_cast<AllocaInst &>(Alloca));
if (!DVRs.empty()) {
const DbgVariableRecord &DVR = **DVRs.begin();
DbgName = DVR.getVariable()->getName();
Loc = DVR.getDebugLoc();
Artificial = DVR.Variable->isArtificial();
}
OptimizationRemark R(DEBUG_TYPE, "Alloca", DiagnosticLocation(Loc),
Alloca.getParent());
R << "in ";
identifyFunction(R, Caller);
R << ", ";
if (Artificial)
R << "artificial ";
SmallString<20> ValName;
raw_svector_ostream OS(ValName);
Alloca.printAsOperand(OS, /*PrintType=*/false, Caller.getParent());
R << "alloca ('" << ValName << "') ";
if (!DbgName.empty())
R << "for '" << DbgName << "' ";
else
R << "without debug info ";
R << "with ";
if (StaticSize)
R << "static size of " << itostr(StaticSize) << " bytes";
else
R << "dynamic size";
return R;
});
}
static void remarkCall(OptimizationRemarkEmitter &ORE, const Function &Caller,
const CallBase &Call, StringRef CallKind,
StringRef RemarkKind) {
ORE.emit([&] {
OptimizationRemark R(DEBUG_TYPE, RemarkKind, &Call);
R << "in ";
identifyFunction(R, Caller);
R << ", " << CallKind << ", callee is ";
identifyCallee(R, Caller.getParent(), Call.getCalledOperand());
return R;
});
}
static void remarkFlatAddrspaceAccess(OptimizationRemarkEmitter &ORE,
const Function &Caller,
const Instruction &Inst) {
ORE.emit([&] {
OptimizationRemark R(DEBUG_TYPE, "FlatAddrspaceAccess", &Inst);
R << "in ";
identifyFunction(R, Caller);
if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(&Inst)) {
R << ", '" << II->getCalledFunction()->getName() << "' call";
} else {
R << ", '" << Inst.getOpcodeName() << "' instruction";
}
if (!Inst.getType()->isVoidTy()) {
SmallString<20> Name;
raw_svector_ostream OS(Name);
Inst.printAsOperand(OS, /*PrintType=*/false, Caller.getParent());
R << " ('" << Name << "')";
}
R << " accesses memory in flat address space";
return R;
});
}
void KernelInfo::updateForBB(const BasicBlock &BB,
OptimizationRemarkEmitter &ORE) {
const Function &F = *BB.getParent();
const Module &M = *F.getParent();
const DataLayout &DL = M.getDataLayout();
for (const Instruction &I : BB.instructionsWithoutDebug()) {
if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(&I)) {
++Allocas;
TypeSize::ScalarTy StaticSize = 0;
if (std::optional<TypeSize> Size = Alloca->getAllocationSize(DL)) {
StaticSize = Size->getFixedValue();
assert(StaticSize <=
(TypeSize::ScalarTy)std::numeric_limits<int64_t>::max());
AllocasStaticSizeSum += StaticSize;
} else {
++AllocasDyn;
}
remarkAlloca(ORE, F, *Alloca, StaticSize);
} else if (const CallBase *Call = dyn_cast<CallBase>(&I)) {
SmallString<40> CallKind;
SmallString<40> RemarkKind;
if (Call->isIndirectCall()) {
++IndirectCalls;
CallKind += "indirect";
RemarkKind += "Indirect";
} else {
++DirectCalls;
CallKind += "direct";
RemarkKind += "Direct";
}
if (isa<InvokeInst>(Call)) {
++Invokes;
CallKind += " invoke";
RemarkKind += "Invoke";
} else {
CallKind += " call";
RemarkKind += "Call";
}
if (!Call->isIndirectCall()) {
if (const Function *Callee = Call->getCalledFunction()) {
if (!Callee->isIntrinsic() && !Callee->isDeclaration()) {
++DirectCallsToDefinedFunctions;
CallKind += " to defined function";
RemarkKind += "ToDefinedFunction";
}
} else if (Call->isInlineAsm()) {
++InlineAssemblyCalls;
CallKind += " to inline assembly";
RemarkKind += "ToInlineAssembly";
}
}
remarkCall(ORE, F, *Call, CallKind, RemarkKind);
if (const AnyMemIntrinsic *MI = dyn_cast<AnyMemIntrinsic>(Call)) {
if (MI->getDestAddressSpace() == FlatAddrspace) {
++FlatAddrspaceAccesses;
remarkFlatAddrspaceAccess(ORE, F, I);
} else if (const AnyMemTransferInst *MT =
dyn_cast<AnyMemTransferInst>(MI)) {
if (MT->getSourceAddressSpace() == FlatAddrspace) {
++FlatAddrspaceAccesses;
remarkFlatAddrspaceAccess(ORE, F, I);
}
}
}
} else if (const LoadInst *Load = dyn_cast<LoadInst>(&I)) {
if (Load->getPointerAddressSpace() == FlatAddrspace) {
++FlatAddrspaceAccesses;
remarkFlatAddrspaceAccess(ORE, F, I);
}
} else if (const StoreInst *Store = dyn_cast<StoreInst>(&I)) {
if (Store->getPointerAddressSpace() == FlatAddrspace) {
++FlatAddrspaceAccesses;
remarkFlatAddrspaceAccess(ORE, F, I);
}
} else if (const AtomicRMWInst *At = dyn_cast<AtomicRMWInst>(&I)) {
if (At->getPointerAddressSpace() == FlatAddrspace) {
++FlatAddrspaceAccesses;
remarkFlatAddrspaceAccess(ORE, F, I);
}
} else if (const AtomicCmpXchgInst *At = dyn_cast<AtomicCmpXchgInst>(&I)) {
if (At->getPointerAddressSpace() == FlatAddrspace) {
++FlatAddrspaceAccesses;
remarkFlatAddrspaceAccess(ORE, F, I);
}
}
}
}
static void remarkProperty(OptimizationRemarkEmitter &ORE, const Function &F,
StringRef Name, int64_t Value) {
ORE.emit([&] {
OptimizationRemark R(DEBUG_TYPE, Name, &F);
R << "in ";
identifyFunction(R, F);
R << ", " << Name << " = " << itostr(Value);
return R;
});
}
static std::optional<int64_t> parseFnAttrAsInteger(Function &F,
StringRef Name) {
if (!F.hasFnAttribute(Name))
return std::nullopt;
return F.getFnAttributeAsParsedInteger(Name);
}
void KernelInfo::emitKernelInfo(Function &F, FunctionAnalysisManager &FAM,
TargetMachine *TM) {
KernelInfo KI;
TargetTransformInfo &TheTTI = FAM.getResult<TargetIRAnalysis>(F);
KI.FlatAddrspace = TheTTI.getFlatAddressSpace();
// Record function properties.
KI.ExternalNotKernel = F.hasExternalLinkage() && !F.hasKernelCallingConv();
for (StringRef Name : {"omp_target_num_teams", "omp_target_thread_limit"}) {
if (auto Val = parseFnAttrAsInteger(F, Name))
KI.LaunchBounds.push_back({Name, *Val});
}
TheTTI.collectKernelLaunchBounds(F, KI.LaunchBounds);
auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
for (const auto &BB : F)
KI.updateForBB(BB, ORE);
#define REMARK_PROPERTY(PROP_NAME) \
remarkProperty(ORE, F, #PROP_NAME, KI.PROP_NAME)
REMARK_PROPERTY(ExternalNotKernel);
for (auto LB : KI.LaunchBounds)
remarkProperty(ORE, F, LB.first, LB.second);
REMARK_PROPERTY(Allocas);
REMARK_PROPERTY(AllocasStaticSizeSum);
REMARK_PROPERTY(AllocasDyn);
REMARK_PROPERTY(DirectCalls);
REMARK_PROPERTY(IndirectCalls);
REMARK_PROPERTY(DirectCallsToDefinedFunctions);
REMARK_PROPERTY(InlineAssemblyCalls);
REMARK_PROPERTY(Invokes);
REMARK_PROPERTY(FlatAddrspaceAccesses);
#undef REMARK_PROPERTY
}
PreservedAnalyses KernelInfoPrinter::run(Function &F,
FunctionAnalysisManager &AM) {
// Skip it if remarks are not enabled as it will do nothing useful.
if (F.getContext().getDiagHandlerPtr()->isPassedOptRemarkEnabled(DEBUG_TYPE))
KernelInfo::emitKernelInfo(F, AM, TM);
return PreservedAnalyses::all();
}
|