-
Notifications
You must be signed in to change notification settings - Fork 56
/
instcount.hpp
347 lines (303 loc) · 12.4 KB
/
instcount.hpp
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#ifndef PTI_TOOLS_INSTCOUNT_H
#define PTI_TOOLS_INSTCOUNT_H
#include <memory>
#include "profiler.hpp"
/**
* @file instcount.hpp
* @brief Contains classes and structures related to instruction counting using GTPin.
*/
namespace gtpin_prof {
/**
* @struct InstCountRawRecord
* @brief Structure representing a raw record for instruction counting.
* It extends the base RawRecord structure.
* Contains a count field to store the number of instructions.
*/
struct InstCountRawRecord : public RawRecord {
uint64_t count;
};
/**
* @class InstCountSiteOfInstrument
* @brief Class representing a site of instrument for instruction counting.
* It extends the base SiteOfInstrument class.
* Contains a reference to the instruction and the type of instrument.
*/
class InstCountSiteOfInstrument : public SiteOfInstrument {
public:
using SiteOfInstrument::SiteOfInstrument;
/**
* @enum InstCountSiteOfInstrumentType
* @brief Enumeration representing the type of instrument for instruction counting.
* It includes Count, Simd, and TypeInvalid.
*/
enum class Type { Count, Simd, TypeInvalid };
/**
* @brief Constructs an InstCountSiteOfInstrument object with the given instruction and type.
* @param ins The reference to the instruction.
* @param type The type of instrument.
*/
InstCountSiteOfInstrument(const gtpin::IGtIns& ins, const Type type = Type::TypeInvalid)
: instruction(ins), type(type) {}
const gtpin::IGtIns& instruction; // The instruction reference.
const Type type; // The type of instrument.
};
/**
* @class InstCountGTPinTool
* @brief Class representing the InstCountGTPinTool.
* It extends the GTPinTool class.
* Provides implementation for analyzing, instrumenting, and accumulating kernel data for
* instruction counting.
*/
class InstCountGTPinTool : public GTPinTool {
public:
using GTPinTool::GTPinTool;
~InstCountGTPinTool() override = default;
/**
* @brief Returns the name of the InstCountGTPinTool.
* @return The name of the tool.
*/
const char* Name() const final { return "InstCountProfiler"; };
/**
* @brief Analyzes the kernel data and performs instruction counting.
* @param kernelData The shared pointer to the kernel data.
* @param instrumentor The reference to the kernel instrumentor.
* @return The status of the analysis.
*/
PROF_STATUS AnalyzeKernel(KernelDataSPtr kernelData,
const gtpin::IGtKernelInstrument& instrumentor) final;
/**
* @brief Instruments the kernel data for instruction counting.
* @param kernelData The shared pointer to the kernel data.
* @param instrumentor The reference to the kernel instrumentor.
* @return The status of the instrumentation.
*/
PROF_STATUS Instrument(KernelDataSPtr kernelData, gtpin::IGtKernelInstrument& instrumentor) final;
/**
* @brief Accumulates the profiling result for instruction counting.
* @param kernelData The shared pointer to the kernel data.
* @param profilingResult The shared pointer to the profiling result data.
* @param siteOfInstrument The shared pointer to the site of instrument.
* @param record The raw record for instruction counting.
* @return The status of the accumulation.
*/
PROF_STATUS Accumulate(KernelDataSPtr kernelData, ResultDataSPtr profilingResult,
SiteOfInstrumentSPtr siteOfInstrument, RawRecord* record) final;
};
/**
* Class representing the InstCountApplicationData.
* It extends the ApplicationData class.
* Provides additional data specific to the InstCountGTPinTool.
*/
class InstCountApplicationData : public ApplicationData {
public:
using ApplicationData::ApplicationData;
};
/**
* Class representing the InstCountKernelData.
* It extends the KernelData class.
* Provides additional data specific to the InstCountGTPinTool.
*/
class InstCountKernelData : public KernelData {
public:
using KernelData::KernelData;
};
/**
* Class representing the InstCountInvocationData.
* It extends the InvocationData class.
* Provides additional data specific to the InstCountGTPinTool.
*/
class InstCountInvocationData : public InvocationData {
public:
using InvocationData::InvocationData;
};
/**
* Class representing the InstCountResultDataCommon.
* It extends the ResultDataCommon class.
* Provides additional data specific to the InstCountGTPinTool.
* Includes the instruction offset and basic block ID.
*/
class InstCountResultDataCommon : public ResultDataCommon {
public:
/**
* Constructs an InstCountResultDataCommon object with the given offset and basic block ID.
* @param offset The instruction offset.
* @param bblId The basic block ID.
*/
InstCountResultDataCommon(InstructionOffset offset, gtpin::BblId bblId)
: offset(offset), bblId(bblId) {}
InstructionOffset offset = -1; // The instruction offset.
gtpin::BblId bblId = -1; // The basic block ID.
};
/**
* Class representing the InstCountResultData.
* It extends the ResultData class.
* Provides additional data specific to the InstCountGTPinTool.
* Includes counters for the number of active SIMD lanes and the total number of instructions.
*/
class InstCountResultData : public ResultData {
public:
using ResultData::ResultData;
size_t simdActiveLaneCounter = 0; // Counter for the number of active SIMD lanes.
size_t instructionCounter = 0; // Counter for the total number of instructions.
};
using InstCountApplicationDataSPtr = std::shared_ptr<InstCountApplicationData>;
using InstCountKernelDataSPtr = std::shared_ptr<InstCountKernelData>;
using InstCountInvocationDataSPtr = std::shared_ptr<InstCountInvocationData>;
using InstCountResultDataSPtr = std::shared_ptr<InstCountResultData>;
using InstCountSiteOfInstrumentSPtr = std::shared_ptr<InstCountSiteOfInstrument>;
using InstCountResultDataCommonSPtr = std::shared_ptr<InstCountResultDataCommon>;
/**
* Class representing the InstCountFactory.
* It extends the ToolFactory class.
* Provides implementation for creating instances of the InstCountGTPinTool and related data
* structures.
*/
class InstCountFactory : public ToolFactory {
public:
using ToolFactory::ToolFactory;
~InstCountFactory() override = default;
/**
* Creates an instance of the InstCountGTPinTool.
* @param factory The shared pointer to the tool factory.
* @param control The shared pointer to the control base.
* @return The shared pointer to the InstCountGTPinTool.
*/
GTPinToolSPtr MakeGTPinTool() const final {
return std::make_shared<InstCountGTPinTool>(std::make_shared<InstCountFactory>(*this));
}
/**
* Returns the size of the raw record for instruction counting.
* @return The size of the raw record.
*/
uint32_t GetRecordSize() const final { return sizeof(InstCountRawRecord); }
/**
* Creates an instance of the InstCountApplicationData.
* @return The shared pointer to the InstCountApplicationData.
*/
ApplicationDataSPtr MakeApplicationData() const final {
return std::make_shared<InstCountApplicationData>();
}
/**
* Creates an instance of the InstCountKernelData.
* @param instrumentor The reference to the kernel instrumentor.
* @return The shared pointer to the InstCountKernelData.
*/
KernelDataSPtr MakeKernelData(const gtpin::IGtKernelInstrument& instrumentor) const final {
return std::make_shared<InstCountKernelData>(instrumentor);
}
/**
* Creates an instance of the InstCountInvocationData.
* @param execDescr The kernel execution descriptor.
* @return The shared pointer to the InstCountInvocationData.
*/
InvocationDataSPtr MakeInvocationData(const KernelExecDescriptor& execDescr) const final {
return std::make_shared<InstCountInvocationData>(execDescr);
}
/**
* Creates an instance of the InstCountResultData.
* @param resultDataCommon The shared pointer to the common result data.
* @return The shared pointer to the InstCountResultData.
*/
ResultDataSPtr MakeResultData(ResultDataCommonSPtr resultDataCommon, size_t tileId) const final {
return std::make_shared<InstCountResultData>(resultDataCommon, tileId);
}
};
/**
* @class InstCountWriterBase
* @brief Base class for writers in the InstCountGTPinTool.
* It extends the WriterBase class.
* Provides a common interface for writing data specific to the InstCountGTPinTool.
*/
class InstCountWriterBase : public virtual WriterBase {
public:
using WriterBase::WriterBase;
virtual ~InstCountWriterBase() = default;
protected:
// WriterBase interface implementation. It casts structures to InstCount types and calls InstCount
// specific functions for writing the data
bool WriteApplicationData(const ApplicationDataSPtr res) override;
bool WriteKernelData(const ApplicationDataSPtr res, const KernelDataSPtr kernelData) override;
bool WriteInvocationData(const ApplicationDataSPtr res, const KernelDataSPtr kernelData,
const InvocationDataSPtr invocationData) override;
bool WriteResultData(const ApplicationDataSPtr res, const KernelDataSPtr kernelData,
const InvocationDataSPtr invocationData, const ResultDataSPtr resultData,
const ResultDataCommonSPtr resultDataCommon, size_t tileId) override;
// InstCount specific functions for writing data, which operates InstCount structures
virtual bool WriteInstCountApplicationData(const InstCountApplicationDataSPtr res) {
return false;
}
virtual bool WriteInstCountKernelData(const InstCountApplicationDataSPtr res,
const InstCountKernelDataSPtr kernelData) {
return false;
}
virtual bool WriteInstCountInvocationData(const InstCountApplicationDataSPtr res,
const InstCountKernelDataSPtr kernelData,
const InstCountInvocationDataSPtr invocationData) {
return false;
}
virtual bool WriteInstCountResultData(const InstCountApplicationDataSPtr res,
const InstCountKernelDataSPtr kernelData,
const InstCountInvocationDataSPtr invocationData,
const InstCountResultDataSPtr resultData,
const InstCountResultDataCommonSPtr resultDataCommon,
size_t tileId) {
return false;
}
};
/**
* Class representing the InstCountControl.
* It extends the ControlBase class.
* Provides additional control options specific to the InstCountGTPinTool.
*/
class InstCountControl : public ControlBase {
public:
using ControlBase::ControlBase;
/**
* @brief Checks if the SIMD width should be collected.
* @return True if the SIMD width should be collected, false otherwise.
*/
virtual bool ShouldCollectSimdWidth() const = 0;
};
/**
* @class InstCountControlDefault
* @brief Default implementation of the InstCountControl class.
* It extends the ControlBase class.
* Provides additional control options specific to the InstCountGTPinTool.
*/
class InstCountControlDefault : public InstCountControl {
public:
using InstCountControl::InstCountControl;
bool ShouldCollectSimdWidth() const override { return true; }
bool ShouldInstrument(const KernelBuildDescriptor& buildDescr) const override { return true; }
bool EnablePerTileCollection(const KernelBuildDescriptor& buildDescr) const override {
return false;
}
bool ShouldProfileEnqueue(const KernelExecDescriptor& execDescr) const override { return true; }
};
/**
* Class representing the InstCountGTPinProfiler.
* It extends the GTPinProfiler class.
* Provides implementation for creating the InstCountFactory.
*/
class InstCountGTPinProfiler : public GTPinProfiler {
public:
InstCountGTPinProfiler(
const std::shared_ptr<InstCountWriterBase> writer,
const std::shared_ptr<InstCountControl> control = std::make_shared<InstCountControlDefault>())
: GTPinProfiler(writer, control) {}
private:
/**
* Returns the factory for the InstCountGTPinTool.
* @return The shared pointer to the InstCountFactory.
*/
ToolFactorySPtr GetFactory(const ControlBaseSPtr control) final {
return std::make_shared<InstCountFactory>(control);
};
};
} // namespace gtpin_prof
#endif // PTI_TOOLS_INSTCOUNT_H