Skip to content

[MC] Make ELFEntrySizeMap a DenseMap #91728

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

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 3 additions & 19 deletions llvm/include/llvm/MC/MCContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,29 +391,13 @@ class MCContext {
/// Map of currently defined macros.
StringMap<MCAsmMacro> MacroMap;

struct ELFEntrySizeKey {
std::string SectionName;
unsigned Flags;
unsigned EntrySize;

ELFEntrySizeKey(StringRef SectionName, unsigned Flags, unsigned EntrySize)
: SectionName(SectionName), Flags(Flags), EntrySize(EntrySize) {}

bool operator<(const ELFEntrySizeKey &Other) const {
if (SectionName != Other.SectionName)
return SectionName < Other.SectionName;
if (Flags != Other.Flags)
return Flags < Other.Flags;
return EntrySize < Other.EntrySize;
}
};

// Symbols must be assigned to a section with a compatible entry size and
// flags. This map is used to assign unique IDs to sections to distinguish
// between sections with identical names but incompatible entry sizes and/or
// flags. This can occur when a symbol is explicitly assigned to a section,
// e.g. via __attribute__((section("myname"))).
std::map<ELFEntrySizeKey, unsigned> ELFEntrySizeMap;
// e.g. via __attribute__((section("myname"))). The map key is the tuple
// (section name, flags, entry size).
DenseMap<std::tuple<StringRef, unsigned, unsigned>, unsigned> ELFEntrySizeMap;

// This set is used to record the generic mergeable section names seen.
// These are sections that are created as mergeable e.g. .debug_str. We need
Expand Down
12 changes: 8 additions & 4 deletions llvm/lib/MC/MCContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,15 +620,20 @@ void MCContext::recordELFMergeableSectionInfo(StringRef SectionName,
unsigned Flags, unsigned UniqueID,
unsigned EntrySize) {
bool IsMergeable = Flags & ELF::SHF_MERGE;
if (UniqueID == GenericSectionID)
if (UniqueID == GenericSectionID) {
ELFSeenGenericMergeableSections.insert(SectionName);
// Minor performance optimization: avoid hash map lookup in
// isELFGenericMergeableSection, which will return true for SectionName.
IsMergeable = true;
}

// For mergeable sections or non-mergeable sections with a generic mergeable
// section name we enter their Unique ID into the ELFEntrySizeMap so that
// compatible globals can be assigned to the same section.

if (IsMergeable || isELFGenericMergeableSection(SectionName)) {
ELFEntrySizeMap.insert(std::make_pair(
ELFEntrySizeKey{SectionName, Flags, EntrySize}, UniqueID));
std::make_tuple(SectionName, Flags, EntrySize), UniqueID));
}
}

Expand All @@ -645,8 +650,7 @@ bool MCContext::isELFGenericMergeableSection(StringRef SectionName) {
std::optional<unsigned>
MCContext::getELFUniqueIDForEntsize(StringRef SectionName, unsigned Flags,
unsigned EntrySize) {
auto I = ELFEntrySizeMap.find(
MCContext::ELFEntrySizeKey{SectionName, Flags, EntrySize});
auto I = ELFEntrySizeMap.find(std::make_tuple(SectionName, Flags, EntrySize));
return (I != ELFEntrySizeMap.end()) ? std::optional<unsigned>(I->second)
: std::nullopt;
}
Expand Down