@@ -235,6 +235,62 @@ class raw_ostream;
235
235
236
236
namespace sampleprof {
237
237
238
+ class SampleProfileReader ;
239
+
240
+ // / SampleProfileReaderItaniumRemapper remaps the profile data from a
241
+ // / sample profile data reader, by applying a provided set of equivalences
242
+ // / between components of the symbol names in the profile.
243
+ class SampleProfileReaderItaniumRemapper {
244
+ public:
245
+ SampleProfileReaderItaniumRemapper (std::unique_ptr<MemoryBuffer> B,
246
+ std::unique_ptr<SymbolRemappingReader> SRR,
247
+ SampleProfileReader &R)
248
+ : Buffer(std::move(B)), Remappings(std::move(SRR)), Reader(R) {
249
+ assert (Remappings && " Remappings cannot be nullptr" );
250
+ }
251
+
252
+ // / Create a remapper from the given remapping file. The remapper will
253
+ // / be used for profile read in by Reader.
254
+ static ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
255
+ create (const std::string Filename, SampleProfileReader &Reader,
256
+ LLVMContext &C);
257
+
258
+ // / Create a remapper from the given Buffer. The remapper will
259
+ // / be used for profile read in by Reader.
260
+ static ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
261
+ create (std::unique_ptr<MemoryBuffer> &B, SampleProfileReader &Reader,
262
+ LLVMContext &C);
263
+
264
+ // / Apply remappings to the profile read by Reader.
265
+ void applyRemapping (LLVMContext &Ctx);
266
+
267
+ bool hasApplied () { return RemappingApplied; }
268
+
269
+ // / Insert function name into remapper.
270
+ void insert (StringRef FunctionName) { Remappings->insert (FunctionName); }
271
+
272
+ // / Query whether there is equivalent in the remapper which has been
273
+ // / inserted.
274
+ bool exist (StringRef FunctionName) {
275
+ return Remappings->lookup (FunctionName);
276
+ }
277
+
278
+ // / Return the samples collected for function \p F if remapper knows
279
+ // / it is present in SampleMap.
280
+ FunctionSamples *getSamplesFor (StringRef FunctionName);
281
+
282
+ private:
283
+ // The buffer holding the content read from remapping file.
284
+ std::unique_ptr<MemoryBuffer> Buffer;
285
+ std::unique_ptr<SymbolRemappingReader> Remappings;
286
+ DenseMap<SymbolRemappingReader::Key, FunctionSamples *> SampleMap;
287
+ // The Reader the remapper is servicing.
288
+ SampleProfileReader &Reader;
289
+ // Indicate whether remapping has been applied to the profile read
290
+ // by Reader -- by calling applyRemapping.
291
+ bool RemappingApplied = false ;
292
+ };
293
+
238
294
// / Sample-based profile reader.
239
295
// /
240
296
// / Each profile contains sample counts for all the functions
@@ -273,8 +329,17 @@ class SampleProfileReader {
273
329
// / Read and validate the file header.
274
330
virtual std::error_code readHeader () = 0;
275
331
276
- // / Read sample profiles from the associated file.
277
- virtual std::error_code read () = 0;
332
+ // / The interface to read sample profiles from the associated file.
333
+ std::error_code read () {
334
+ if (std::error_code EC = readImpl ())
335
+ return EC;
336
+ if (Remapper)
337
+ Remapper->applyRemapping (Ctx);
338
+ return sampleprof_error::success;
339
+ }
340
+
341
+ // / The implementaion to read sample profiles from the associated file.
342
+ virtual std::error_code readImpl () = 0;
278
343
279
344
// / Print the profile for \p FName on stream \p OS.
280
345
void dumpFunctionProfile (StringRef FName, raw_ostream &OS = dbgs());
@@ -295,6 +360,10 @@ class SampleProfileReader {
295
360
296
361
// / Return the samples collected for function \p F.
297
362
virtual FunctionSamples *getSamplesFor (StringRef Fname) {
363
+ if (Remapper) {
364
+ if (auto FS = Remapper->getSamplesFor (Fname))
365
+ return FS;
366
+ }
298
367
std::string FGUID;
299
368
Fname = getRepInFormat (Fname, getFormat (), FGUID);
300
369
auto It = Profiles.find (Fname);
@@ -313,18 +382,24 @@ class SampleProfileReader {
313
382
}
314
383
315
384
// / Create a sample profile reader appropriate to the file format.
385
+ // / Create a remapper underlying if RemapFilename is not empty.
316
386
static ErrorOr<std::unique_ptr<SampleProfileReader>>
317
- create (const Twine &Filename, LLVMContext &C);
387
+ create (const std::string Filename, LLVMContext &C,
388
+ const std::string RemapFilename = " " );
318
389
319
390
// / Create a sample profile reader from the supplied memory buffer.
391
+ // / Create a remapper underlying if RemapFilename is not empty.
320
392
static ErrorOr<std::unique_ptr<SampleProfileReader>>
321
- create (std::unique_ptr<MemoryBuffer> &B, LLVMContext &C);
393
+ create (std::unique_ptr<MemoryBuffer> &B, LLVMContext &C,
394
+ const std::string RemapFilename = " " );
322
395
323
396
// / Return the profile summary.
324
- ProfileSummary &getSummary () { return *(Summary.get ()); }
397
+ ProfileSummary &getSummary () const { return *(Summary.get ()); }
398
+
399
+ MemoryBuffer *getBuffer () const { return Buffer.get (); }
325
400
326
401
// / \brief Return the profile format.
327
- SampleProfileFormat getFormat () { return Format; }
402
+ SampleProfileFormat getFormat () const { return Format; }
328
403
329
404
virtual std::unique_ptr<ProfileSymbolList> getProfileSymbolList () {
330
405
return nullptr ;
@@ -361,6 +436,8 @@ class SampleProfileReader {
361
436
// / Compute summary for this profile.
362
437
void computeSummary ();
363
438
439
+ std::unique_ptr<SampleProfileReaderItaniumRemapper> Remapper;
440
+
364
441
// / \brief The format of sample.
365
442
SampleProfileFormat Format = SPF_None;
366
443
};
@@ -374,7 +451,7 @@ class SampleProfileReaderText : public SampleProfileReader {
374
451
std::error_code readHeader () override { return sampleprof_error::success; }
375
452
376
453
// / Read sample profiles from the associated file.
377
- std::error_code read () override ;
454
+ std::error_code readImpl () override ;
378
455
379
456
// / Return true if \p Buffer is in the format supported by this class.
380
457
static bool hasFormat (const MemoryBuffer &Buffer);
@@ -390,7 +467,7 @@ class SampleProfileReaderBinary : public SampleProfileReader {
390
467
virtual std::error_code readHeader () override ;
391
468
392
469
// / Read sample profiles from the associated file.
393
- std::error_code read () override ;
470
+ std::error_code readImpl () override ;
394
471
395
472
// / It includes all the names that have samples either in outline instance
396
473
// / or inline instance.
@@ -512,7 +589,7 @@ class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary {
512
589
: SampleProfileReaderBinary(std::move(B), C, Format) {}
513
590
514
591
// / Read sample profiles in extensible format from the associated file.
515
- std::error_code read () override ;
592
+ std::error_code readImpl () override ;
516
593
517
594
// / Get the total size of all \p Type sections.
518
595
uint64_t getSectionSize (SecType Type);
@@ -581,7 +658,7 @@ class SampleProfileReaderCompactBinary : public SampleProfileReaderBinary {
581
658
static bool hasFormat (const MemoryBuffer &Buffer);
582
659
583
660
// / Read samples only for functions to use.
584
- std::error_code read () override ;
661
+ std::error_code readImpl () override ;
585
662
586
663
// / Collect functions to be used when compiling Module \p M.
587
664
void collectFuncsFrom (const Module &M) override ;
@@ -612,7 +689,7 @@ class SampleProfileReaderGCC : public SampleProfileReader {
612
689
std::error_code readHeader () override ;
613
690
614
691
// / Read sample profiles from the associated file.
615
- std::error_code read () override ;
692
+ std::error_code readImpl () override ;
616
693
617
694
// / Return true if \p Buffer is in the format supported by this class.
618
695
static bool hasFormat (const MemoryBuffer &Buffer);
@@ -640,44 +717,6 @@ class SampleProfileReaderGCC : public SampleProfileReader {
640
717
static const uint32_t GCOVTagAFDOFunction = 0xac000000 ;
641
718
};
642
719
643
- // / A profile data reader proxy that remaps the profile data from another
644
- // / sample profile data reader, by applying a provided set of equivalences
645
- // / between components of the symbol names in the profile.
646
- class SampleProfileReaderItaniumRemapper : public SampleProfileReader {
647
- public:
648
- SampleProfileReaderItaniumRemapper (
649
- std::unique_ptr<MemoryBuffer> B, LLVMContext &C,
650
- std::unique_ptr<SampleProfileReader> Underlying)
651
- : SampleProfileReader(std::move(B), C, Underlying->getFormat ()) {
652
- Profiles = std::move (Underlying->getProfiles ());
653
- Summary = takeSummary (*Underlying);
654
- // Keep the underlying reader alive; the profile data may contain
655
- // StringRefs referencing names in its name table.
656
- UnderlyingReader = std::move (Underlying);
657
- }
658
-
659
- // / Create a remapped sample profile from the given remapping file and
660
- // / underlying samples.
661
- static ErrorOr<std::unique_ptr<SampleProfileReader>>
662
- create (const Twine &Filename, LLVMContext &C,
663
- std::unique_ptr<SampleProfileReader> Underlying);
664
-
665
- // / Read and validate the file header.
666
- std::error_code readHeader () override { return sampleprof_error::success; }
667
-
668
- // / Read remapping file and apply it to the sample profile.
669
- std::error_code read () override ;
670
-
671
- // / Return the samples collected for function \p F.
672
- FunctionSamples *getSamplesFor (StringRef FunctionName) override ;
673
- using SampleProfileReader::getSamplesFor;
674
-
675
- private:
676
- SymbolRemappingReader Remappings;
677
- DenseMap<SymbolRemappingReader::Key, FunctionSamples*> SampleMap;
678
- std::unique_ptr<SampleProfileReader> UnderlyingReader;
679
- };
680
-
681
720
} // end namespace sampleprof
682
721
683
722
} // end namespace llvm
0 commit comments