Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 762a6a2

Browse files
committed
[SampleFDO] Add profile remapping support for profile on-demand loading used
by ExtBinary format profile Profile on-demand loading was added for ExtBinary format profile in rL374233, but currently profile on-demand loading doesn't work well with profile remapping. The patch adds the support. Suppose a function in the current module has outline instance in the profile. The function name in the module is different from the name of the outline instance, but remapper knows the two names are equal. When loading profile on-demand, the outline instance has to be loaded with remapper's help. At the same time SampleProfileReaderItaniumRemapper is changed from a proxy of SampleProfileReader to a helper member in SampleProfileReader. Differential Revision: https://reviews.llvm.org/D68901 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@375295 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 1e88075 commit 762a6a2

File tree

5 files changed

+233
-132
lines changed

5 files changed

+233
-132
lines changed

include/llvm/ProfileData/SampleProfReader.h

+88-49
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,62 @@ class raw_ostream;
235235

236236
namespace sampleprof {
237237

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+
238294
/// Sample-based profile reader.
239295
///
240296
/// Each profile contains sample counts for all the functions
@@ -273,8 +329,17 @@ class SampleProfileReader {
273329
/// Read and validate the file header.
274330
virtual std::error_code readHeader() = 0;
275331

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;
278343

279344
/// Print the profile for \p FName on stream \p OS.
280345
void dumpFunctionProfile(StringRef FName, raw_ostream &OS = dbgs());
@@ -295,6 +360,10 @@ class SampleProfileReader {
295360

296361
/// Return the samples collected for function \p F.
297362
virtual FunctionSamples *getSamplesFor(StringRef Fname) {
363+
if (Remapper) {
364+
if (auto FS = Remapper->getSamplesFor(Fname))
365+
return FS;
366+
}
298367
std::string FGUID;
299368
Fname = getRepInFormat(Fname, getFormat(), FGUID);
300369
auto It = Profiles.find(Fname);
@@ -313,18 +382,24 @@ class SampleProfileReader {
313382
}
314383

315384
/// Create a sample profile reader appropriate to the file format.
385+
/// Create a remapper underlying if RemapFilename is not empty.
316386
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 = "");
318389

319390
/// Create a sample profile reader from the supplied memory buffer.
391+
/// Create a remapper underlying if RemapFilename is not empty.
320392
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 = "");
322395

323396
/// 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(); }
325400

326401
/// \brief Return the profile format.
327-
SampleProfileFormat getFormat() { return Format; }
402+
SampleProfileFormat getFormat() const { return Format; }
328403

329404
virtual std::unique_ptr<ProfileSymbolList> getProfileSymbolList() {
330405
return nullptr;
@@ -361,6 +436,8 @@ class SampleProfileReader {
361436
/// Compute summary for this profile.
362437
void computeSummary();
363438

439+
std::unique_ptr<SampleProfileReaderItaniumRemapper> Remapper;
440+
364441
/// \brief The format of sample.
365442
SampleProfileFormat Format = SPF_None;
366443
};
@@ -374,7 +451,7 @@ class SampleProfileReaderText : public SampleProfileReader {
374451
std::error_code readHeader() override { return sampleprof_error::success; }
375452

376453
/// Read sample profiles from the associated file.
377-
std::error_code read() override;
454+
std::error_code readImpl() override;
378455

379456
/// Return true if \p Buffer is in the format supported by this class.
380457
static bool hasFormat(const MemoryBuffer &Buffer);
@@ -390,7 +467,7 @@ class SampleProfileReaderBinary : public SampleProfileReader {
390467
virtual std::error_code readHeader() override;
391468

392469
/// Read sample profiles from the associated file.
393-
std::error_code read() override;
470+
std::error_code readImpl() override;
394471

395472
/// It includes all the names that have samples either in outline instance
396473
/// or inline instance.
@@ -512,7 +589,7 @@ class SampleProfileReaderExtBinaryBase : public SampleProfileReaderBinary {
512589
: SampleProfileReaderBinary(std::move(B), C, Format) {}
513590

514591
/// Read sample profiles in extensible format from the associated file.
515-
std::error_code read() override;
592+
std::error_code readImpl() override;
516593

517594
/// Get the total size of all \p Type sections.
518595
uint64_t getSectionSize(SecType Type);
@@ -581,7 +658,7 @@ class SampleProfileReaderCompactBinary : public SampleProfileReaderBinary {
581658
static bool hasFormat(const MemoryBuffer &Buffer);
582659

583660
/// Read samples only for functions to use.
584-
std::error_code read() override;
661+
std::error_code readImpl() override;
585662

586663
/// Collect functions to be used when compiling Module \p M.
587664
void collectFuncsFrom(const Module &M) override;
@@ -612,7 +689,7 @@ class SampleProfileReaderGCC : public SampleProfileReader {
612689
std::error_code readHeader() override;
613690

614691
/// Read sample profiles from the associated file.
615-
std::error_code read() override;
692+
std::error_code readImpl() override;
616693

617694
/// Return true if \p Buffer is in the format supported by this class.
618695
static bool hasFormat(const MemoryBuffer &Buffer);
@@ -640,44 +717,6 @@ class SampleProfileReaderGCC : public SampleProfileReader {
640717
static const uint32_t GCOVTagAFDOFunction = 0xac000000;
641718
};
642719

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-
681720
} // end namespace sampleprof
682721

683722
} // end namespace llvm

0 commit comments

Comments
 (0)