Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nfc]Make InstrProfSymtab non-copyable and non-movable #86882

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class BinaryCoverageReader : public CoverageMappingReader {
private:
std::vector<std::string> Filenames;
std::vector<ProfileMappingRecord> MappingRecords;
InstrProfSymtab ProfileNames;
std::unique_ptr<InstrProfSymtab> ProfileNames;
size_t CurrentRecord = 0;
std::vector<StringRef> FunctionsFilenames;
std::vector<CounterExpression> Expressions;
Expand All @@ -195,8 +195,9 @@ class BinaryCoverageReader : public CoverageMappingReader {
// D69471, which can split up function records into multiple sections on ELF.
FuncRecordsStorage FuncRecords;

BinaryCoverageReader(FuncRecordsStorage &&FuncRecords)
: FuncRecords(std::move(FuncRecords)) {}
BinaryCoverageReader(std::unique_ptr<InstrProfSymtab> Symtab,
FuncRecordsStorage &&FuncRecords)
: ProfileNames(std::move(Symtab)), FuncRecords(std::move(FuncRecords)) {}

public:
BinaryCoverageReader(const BinaryCoverageReader &) = delete;
Expand All @@ -211,7 +212,7 @@ class BinaryCoverageReader : public CoverageMappingReader {
static Expected<std::unique_ptr<BinaryCoverageReader>>
createCoverageReaderFromBuffer(StringRef Coverage,
FuncRecordsStorage &&FuncRecords,
InstrProfSymtab &&ProfileNames,
std::unique_ptr<InstrProfSymtab> ProfileNames,
uint8_t BytesInAddress,
llvm::endianness Endian,
StringRef CompilationDir = "");
Expand Down
9 changes: 9 additions & 0 deletions llvm/include/llvm/ProfileData/InstrProf.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,15 @@ class InstrProfSymtab {
public:
InstrProfSymtab() = default;

// Not copyable or movable.
// Consider std::unique_ptr for move.
// InstrProfSymtab has a few containers as class members, so consider
// std::shared_ptr for read-only copy.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::shared_ptr has (some) overhead and since we aren't using it in this change, lets drop this comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good! I also hesitated about whether to mention shared_ptr and added it mainly for completeness.

InstrProfSymtab(const InstrProfSymtab &) = delete;
InstrProfSymtab &operator=(const InstrProfSymtab &) = delete;
InstrProfSymtab(InstrProfSymtab &&) = delete;
InstrProfSymtab &operator=(InstrProfSymtab &&) = delete;

/// Create InstrProfSymtab from an object file section which
/// contains function PGO names. When section may contain raw
/// string data or string data in compressed form. This method
Expand Down
35 changes: 19 additions & 16 deletions llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,31 +894,34 @@ static Error readCoverageMappingData(
Expected<std::unique_ptr<BinaryCoverageReader>>
BinaryCoverageReader::createCoverageReaderFromBuffer(
StringRef Coverage, FuncRecordsStorage &&FuncRecords,
InstrProfSymtab &&ProfileNames, uint8_t BytesInAddress,
std::unique_ptr<InstrProfSymtab> ProfileNames, uint8_t BytesInAddress,
llvm::endianness Endian, StringRef CompilationDir) {
std::unique_ptr<BinaryCoverageReader> Reader(
new BinaryCoverageReader(std::move(FuncRecords)));
Reader->ProfileNames = std::move(ProfileNames);
if (ProfileNames == nullptr)
return make_error<CoverageMapError>(coveragemap_error::malformed,
"Caller must provide ProfileNames");
std::unique_ptr<BinaryCoverageReader> Reader(new BinaryCoverageReader(
std::move(ProfileNames), std::move(FuncRecords)));
InstrProfSymtab &ProfileNamesRef = *Reader->ProfileNames;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be const too?
nit: Also prefer just ProfileNames, IMO the Ref suffix is unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be const too?

It seems readCoverageMappingData and its callees only callInstrProfSymtab::getFuncName, which could be made const. A couple of places need to be updated for the const reference in this line to compile. I'll just keep it non const as it currently is..

nit: Also prefer just ProfileNames, IMO the Ref suffix is unnecessary.

Sure! I renamed input parameter to ProfileNamesPtr and rename the reference to ProfileNames.

StringRef FuncRecordsRef = Reader->FuncRecords->getBuffer();
if (BytesInAddress == 4 && Endian == llvm::endianness::little) {
if (Error E = readCoverageMappingData<uint32_t, llvm::endianness::little>(
Reader->ProfileNames, Coverage, FuncRecordsRef,
Reader->MappingRecords, CompilationDir, Reader->Filenames))
ProfileNamesRef, Coverage, FuncRecordsRef, Reader->MappingRecords,
CompilationDir, Reader->Filenames))
return std::move(E);
} else if (BytesInAddress == 4 && Endian == llvm::endianness::big) {
if (Error E = readCoverageMappingData<uint32_t, llvm::endianness::big>(
Reader->ProfileNames, Coverage, FuncRecordsRef,
Reader->MappingRecords, CompilationDir, Reader->Filenames))
ProfileNamesRef, Coverage, FuncRecordsRef, Reader->MappingRecords,
CompilationDir, Reader->Filenames))
return std::move(E);
} else if (BytesInAddress == 8 && Endian == llvm::endianness::little) {
if (Error E = readCoverageMappingData<uint64_t, llvm::endianness::little>(
Reader->ProfileNames, Coverage, FuncRecordsRef,
Reader->MappingRecords, CompilationDir, Reader->Filenames))
ProfileNamesRef, Coverage, FuncRecordsRef, Reader->MappingRecords,
CompilationDir, Reader->Filenames))
return std::move(E);
} else if (BytesInAddress == 8 && Endian == llvm::endianness::big) {
if (Error E = readCoverageMappingData<uint64_t, llvm::endianness::big>(
Reader->ProfileNames, Coverage, FuncRecordsRef,
Reader->MappingRecords, CompilationDir, Reader->Filenames))
ProfileNamesRef, Coverage, FuncRecordsRef, Reader->MappingRecords,
CompilationDir, Reader->Filenames))
return std::move(E);
} else
return make_error<CoverageMapError>(
Expand Down Expand Up @@ -963,8 +966,8 @@ loadTestingFormat(StringRef Data, StringRef CompilationDir) {
if (Data.size() < ProfileNamesSize)
return make_error<CoverageMapError>(coveragemap_error::malformed,
"the size of ProfileNames is too big");
InstrProfSymtab ProfileNames;
if (Error E = ProfileNames.create(Data.substr(0, ProfileNamesSize), Address))
auto ProfileNames = std::make_unique<InstrProfSymtab>();
if (Error E = ProfileNames->create(Data.substr(0, ProfileNamesSize), Address))
return std::move(E);
Data = Data.substr(ProfileNamesSize);

Expand Down Expand Up @@ -1099,7 +1102,7 @@ loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch,
OF->isLittleEndian() ? llvm::endianness::little : llvm::endianness::big;

// Look for the sections that we are interested in.
InstrProfSymtab ProfileNames;
auto ProfileNames = std::make_unique<InstrProfSymtab>();
std::vector<SectionRef> NamesSectionRefs;
// If IPSK_name is not found, fallback to search for IPK_covname, which is
// used when binary correlation is enabled.
Expand All @@ -1116,7 +1119,7 @@ loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch,
return make_error<CoverageMapError>(
coveragemap_error::malformed,
"the size of coverage mapping section is not one");
if (Error E = ProfileNames.create(NamesSectionRefs.back()))
if (Error E = ProfileNames->create(NamesSectionRefs.back()))
return std::move(E);

auto CoverageSection = lookupSections(*OF, IPSK_covmap);
Expand Down
Loading