30
30
31
31
using namespace llvm ;
32
32
33
+ enum ProfileFormat { PF_None = 0 , PF_Text, PF_Binary, PF_GCC };
34
+
33
35
static void exitWithError (const Twine &Message,
34
36
StringRef Whence = " " ,
35
37
StringRef Hint = " " ) {
@@ -92,10 +94,14 @@ static void handleMergeWriterError(std::error_code &Error,
92
94
}
93
95
94
96
static void mergeInstrProfile (const cl::list<std::string> &Inputs,
95
- StringRef OutputFilename) {
97
+ StringRef OutputFilename,
98
+ ProfileFormat OutputFormat) {
96
99
if (OutputFilename.compare (" -" ) == 0 )
97
100
exitWithError (" Cannot write indexed profdata format to stdout." );
98
101
102
+ if (OutputFormat != PF_Binary && OutputFormat != PF_Text)
103
+ exitWithError (" Unknown format is specified." );
104
+
99
105
std::error_code EC;
100
106
raw_fd_ostream Output (OutputFilename.data (), EC, sys::fs::F_None);
101
107
if (EC)
@@ -119,14 +125,22 @@ static void mergeInstrProfile(const cl::list<std::string> &Inputs,
119
125
if (Reader->hasError ())
120
126
exitWithErrorCode (Reader->getError (), Filename);
121
127
}
122
- Writer.write (Output);
128
+ if (OutputFormat == PF_Text)
129
+ Writer.writeText (Output);
130
+ else
131
+ Writer.write (Output);
123
132
}
124
133
134
+ static sampleprof::SampleProfileFormat FormatMap[] = {
135
+ sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Binary,
136
+ sampleprof::SPF_GCC};
137
+
125
138
static void mergeSampleProfile (const cl::list<std::string> &Inputs,
126
139
StringRef OutputFilename,
127
- sampleprof::SampleProfileFormat OutputFormat) {
140
+ ProfileFormat OutputFormat) {
128
141
using namespace sampleprof ;
129
- auto WriterOrErr = SampleProfileWriter::create (OutputFilename, OutputFormat);
142
+ auto WriterOrErr =
143
+ SampleProfileWriter::create (OutputFilename, FormatMap[OutputFormat]);
130
144
if (std::error_code EC = WriterOrErr.getError ())
131
145
exitWithErrorCode (EC, OutputFilename);
132
146
@@ -174,19 +188,18 @@ static int merge_main(int argc, const char *argv[]) {
174
188
cl::values (clEnumVal (instr, " Instrumentation profile (default)" ),
175
189
clEnumVal (sample, " Sample profile" ), clEnumValEnd));
176
190
177
- cl::opt<sampleprof::SampleProfileFormat> OutputFormat (
178
- cl::desc (" Format of output profile (only meaningful with --sample)" ),
179
- cl::init (sampleprof::SPF_Binary),
180
- cl::values (clEnumValN (sampleprof::SPF_Binary, " binary" ,
181
- " Binary encoding (default)" ),
182
- clEnumValN (sampleprof::SPF_Text, " text" , " Text encoding" ),
183
- clEnumValN (sampleprof::SPF_GCC, " gcc" , " GCC encoding" ),
191
+ cl::opt<ProfileFormat> OutputFormat (
192
+ cl::desc (" Format of output profile" ), cl::init (PF_Binary),
193
+ cl::values (clEnumValN (PF_Binary, " binary" , " Binary encoding (default)" ),
194
+ clEnumValN (PF_Text, " text" , " Text encoding" ),
195
+ clEnumValN (PF_GCC, " gcc" ,
196
+ " GCC encoding (only meaningful for -sample)" ),
184
197
clEnumValEnd));
185
198
186
199
cl::ParseCommandLineOptions (argc, argv, " LLVM profile data merger\n " );
187
200
188
201
if (ProfileKind == instr)
189
- mergeInstrProfile (Inputs, OutputFilename);
202
+ mergeInstrProfile (Inputs, OutputFilename, OutputFormat );
190
203
else
191
204
mergeSampleProfile (Inputs, OutputFilename, OutputFormat);
192
205
@@ -195,7 +208,8 @@ static int merge_main(int argc, const char *argv[]) {
195
208
196
209
static int showInstrProfile (std::string Filename, bool ShowCounts,
197
210
bool ShowIndirectCallTargets, bool ShowAllFunctions,
198
- std::string ShowFunction, raw_fd_ostream &OS) {
211
+ std::string ShowFunction, bool TextFormat,
212
+ raw_fd_ostream &OS) {
199
213
auto ReaderOrErr = InstrProfReader::create (Filename);
200
214
if (std::error_code EC = ReaderOrErr.getError ())
201
215
exitWithErrorCode (EC, Filename);
@@ -208,53 +222,69 @@ static int showInstrProfile(std::string Filename, bool ShowCounts,
208
222
ShowAllFunctions || (!ShowFunction.empty () &&
209
223
Func.Name .find (ShowFunction) != Func.Name .npos );
210
224
225
+ bool doTextFormatDump = (Show && ShowCounts && TextFormat);
226
+
227
+ if (doTextFormatDump) {
228
+ InstrProfWriter::writeRecordInText (Func, OS);
229
+ continue ;
230
+ }
231
+
211
232
++TotalFunctions;
212
233
assert (Func.Counts .size () > 0 && " function missing entry counter" );
213
234
if (Func.Counts [0 ] > MaxFunctionCount)
214
235
MaxFunctionCount = Func.Counts [0 ];
215
236
237
+ for (size_t I = 1 , E = Func.Counts .size (); I < E; ++I) {
238
+ if (Func.Counts [I] > MaxBlockCount)
239
+ MaxBlockCount = Func.Counts [I];
240
+ }
241
+
216
242
if (Show) {
243
+
217
244
if (!ShownFunctions)
218
245
OS << " Counters:\n " ;
246
+
219
247
++ShownFunctions;
220
248
221
249
OS << " " << Func.Name << " :\n "
222
250
<< " Hash: " << format (" 0x%016" PRIx64, Func.Hash ) << " \n "
223
251
<< " Counters: " << Func.Counts .size () << " \n "
224
252
<< " Function count: " << Func.Counts [0 ] << " \n " ;
253
+
225
254
if (ShowIndirectCallTargets)
226
255
OS << " Indirect Call Site Count: "
227
256
<< Func.getNumValueSites (IPVK_IndirectCallTarget) << " \n " ;
228
- }
229
257
230
- if (Show && ShowCounts)
231
- OS << " Block counts: [" ;
232
- for (size_t I = 1 , E = Func.Counts .size (); I < E; ++I) {
233
- if (Func.Counts [I] > MaxBlockCount)
234
- MaxBlockCount = Func.Counts [I];
235
- if (Show && ShowCounts)
236
- OS << (I == 1 ? " " : " , " ) << Func.Counts [I];
237
- }
238
- if (Show && ShowCounts)
239
- OS << " ]\n " ;
240
-
241
- if (Show && ShowIndirectCallTargets) {
242
- uint32_t NS = Func.getNumValueSites (IPVK_IndirectCallTarget);
243
- OS << " Indirect Target Results: \n " ;
244
- for (size_t I = 0 ; I < NS; ++I) {
245
- uint32_t NV = Func.getNumValueDataForSite (IPVK_IndirectCallTarget, I);
246
- std::unique_ptr<InstrProfValueData[]> VD =
247
- Func.getValueForSite (IPVK_IndirectCallTarget, I);
248
- for (uint32_t V = 0 ; V < NV; V++) {
249
- OS << " \t [ " << I << " , " ;
250
- OS << (const char *)VD[V].Value << " , " << VD[V].Count << " ]\n " ;
258
+ if (ShowCounts) {
259
+ OS << " Block counts: [" ;
260
+ for (size_t I = 1 , E = Func.Counts .size (); I < E; ++I) {
261
+ OS << (I == 1 ? " " : " , " ) << Func.Counts [I];
262
+ }
263
+ OS << " ]\n " ;
264
+ }
265
+
266
+ if (ShowIndirectCallTargets) {
267
+ uint32_t NS = Func.getNumValueSites (IPVK_IndirectCallTarget);
268
+ OS << " Indirect Target Results: \n " ;
269
+ for (size_t I = 0 ; I < NS; ++I) {
270
+ uint32_t NV = Func.getNumValueDataForSite (IPVK_IndirectCallTarget, I);
271
+ std::unique_ptr<InstrProfValueData[]> VD =
272
+ Func.getValueForSite (IPVK_IndirectCallTarget, I);
273
+ for (uint32_t V = 0 ; V < NV; V++) {
274
+ OS << " \t [ " << I << " , " ;
275
+ OS << (const char *)VD[V].Value << " , " << VD[V].Count << " ]\n " ;
276
+ }
251
277
}
252
278
}
253
279
}
254
280
}
281
+
255
282
if (Reader->hasError ())
256
283
exitWithErrorCode (Reader->getError (), Filename);
257
284
285
+ if (ShowCounts && TextFormat)
286
+ return 0 ;
287
+
258
288
if (ShowAllFunctions || !ShowFunction.empty ())
259
289
OS << " Functions shown: " << ShownFunctions << " \n " ;
260
290
OS << " Total functions: " << TotalFunctions << " \n " ;
@@ -289,6 +319,9 @@ static int show_main(int argc, const char *argv[]) {
289
319
290
320
cl::opt<bool > ShowCounts (" counts" , cl::init (false ),
291
321
cl::desc (" Show counter values for shown functions" ));
322
+ cl::opt<bool > TextFormat (
323
+ " text" , cl::init (false ),
324
+ cl::desc (" Show instr profile data in text dump format" ));
292
325
cl::opt<bool > ShowIndirectCallTargets (
293
326
" ic-targets" , cl::init (false ),
294
327
cl::desc (" Show indirect call site target values for shown functions" ));
@@ -314,14 +347,14 @@ static int show_main(int argc, const char *argv[]) {
314
347
std::error_code EC;
315
348
raw_fd_ostream OS (OutputFilename.data (), EC, sys::fs::F_Text);
316
349
if (EC)
317
- exitWithErrorCode (EC, OutputFilename);
350
+ exitWithErrorCode (EC, OutputFilename);
318
351
319
352
if (ShowAllFunctions && !ShowFunction.empty ())
320
353
errs () << " warning: -function argument ignored: showing all functions\n " ;
321
354
322
355
if (ProfileKind == instr)
323
356
return showInstrProfile (Filename, ShowCounts, ShowIndirectCallTargets,
324
- ShowAllFunctions, ShowFunction, OS);
357
+ ShowAllFunctions, ShowFunction, TextFormat, OS);
325
358
else
326
359
return showSampleProfile (Filename, ShowCounts, ShowAllFunctions,
327
360
ShowFunction, OS);
0 commit comments