[llvm] [MC] Make ELFEntrySizeMap a DenseMap (PR #91728)
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 10 04:16:55 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mc
Author: None (aengelke)
<details>
<summary>Changes</summary>
There is no need for an ordered std::map and also no need to duplicate the section name, which is owned by the ELFSectionKey. Therefore, use a DenseMap instead and don't copy the string. As a further, minor performance optimization, avoid the hash table lookup in isELFGenericMergeableSection when the section name was just added.
This slightly improves compilation performance in our application, where we occasionally compile many small object files.
---
Full diff: https://github.com/llvm/llvm-project/pull/91728.diff
2 Files Affected:
- (modified) llvm/include/llvm/MC/MCContext.h (+3-19)
- (modified) llvm/lib/MC/MCContext.cpp (+8-4)
``````````diff
diff --git a/llvm/include/llvm/MC/MCContext.h b/llvm/include/llvm/MC/MCContext.h
index ef9833cdf2b07..3de41b6a6ea7c 100644
--- a/llvm/include/llvm/MC/MCContext.h
+++ b/llvm/include/llvm/MC/MCContext.h
@@ -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
diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index 3aee96fdf57fc..f027eb65d700e 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -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));
}
}
@@ -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;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/91728
More information about the llvm-commits
mailing list