Skip to content

Commit 0491cea

Browse files
authored
Revert "[MC][NFC] Make ELFUniquingMap a StringMap (#95006)"
This reverts commit 2f9a80b.
1 parent 2cddf72 commit 0491cea

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

llvm/include/llvm/MC/MCContext.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,31 @@ class MCContext {
252252
/// A collection of MCPseudoProbe in the current module
253253
MCPseudoProbeTable PseudoProbeTable;
254254

255+
// Sections are differentiated by the quadruple (section_name, group_name,
256+
// unique_id, link_to_symbol_name). Sections sharing the same quadruple are
257+
// combined into one section.
258+
struct ELFSectionKey {
259+
std::string SectionName;
260+
StringRef GroupName;
261+
StringRef LinkedToName;
262+
unsigned UniqueID;
263+
264+
ELFSectionKey(StringRef SectionName, StringRef GroupName,
265+
StringRef LinkedToName, unsigned UniqueID)
266+
: SectionName(SectionName), GroupName(GroupName),
267+
LinkedToName(LinkedToName), UniqueID(UniqueID) {}
268+
269+
bool operator<(const ELFSectionKey &Other) const {
270+
if (SectionName != Other.SectionName)
271+
return SectionName < Other.SectionName;
272+
if (GroupName != Other.GroupName)
273+
return GroupName < Other.GroupName;
274+
if (int O = LinkedToName.compare(Other.LinkedToName))
275+
return O < 0;
276+
return UniqueID < Other.UniqueID;
277+
}
278+
};
279+
255280
struct COFFSectionKey {
256281
std::string SectionName;
257282
StringRef GroupName;
@@ -325,8 +350,8 @@ class MCContext {
325350
};
326351

327352
StringMap<MCSectionMachO *> MachOUniquingMap;
353+
std::map<ELFSectionKey, MCSectionELF *> ELFUniquingMap;
328354
std::map<COFFSectionKey, MCSectionCOFF *> COFFUniquingMap;
329-
StringMap<MCSectionELF *> ELFUniquingMap;
330355
std::map<std::string, MCSectionGOFF *> GOFFUniquingMap;
331356
std::map<WasmSectionKey, MCSectionWasm *> WasmUniquingMap;
332357
std::map<XCOFFSectionKey, MCSectionXCOFF *> XCOFFUniquingMap;

llvm/lib/MC/MCContext.cpp

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#include "llvm/MC/SectionKind.h"
4545
#include "llvm/Support/Casting.h"
4646
#include "llvm/Support/CommandLine.h"
47-
#include "llvm/Support/EndianStream.h"
4847
#include "llvm/Support/ErrorHandling.h"
4948
#include "llvm/Support/MemoryBuffer.h"
5049
#include "llvm/Support/Path.h"
@@ -549,42 +548,16 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
549548
if (GroupSym)
550549
Group = GroupSym->getName();
551550
assert(!(LinkedToSym && LinkedToSym->getName().empty()));
551+
// Do the lookup, if we have a hit, return it.
552+
auto IterBool = ELFUniquingMap.insert(std::make_pair(
553+
ELFSectionKey{Section.str(), Group,
554+
LinkedToSym ? LinkedToSym->getName() : "", UniqueID},
555+
nullptr));
556+
auto &Entry = *IterBool.first;
557+
if (!IterBool.second)
558+
return Entry.second;
552559

553-
// Sections are differentiated by the quadruple (section_name, group_name,
554-
// unique_id, link_to_symbol_name). Sections sharing the same quadruple are
555-
// combined into one section. As an optimization, non-unique sections without
556-
// group or linked-to symbol have a shorter unique-ing key.
557-
std::pair<StringMap<MCSectionELF *>::iterator, bool> EntryNewPair;
558-
// Length of the section name, which are the first SectionLen bytes of the key
559-
unsigned SectionLen;
560-
if (GroupSym || LinkedToSym || UniqueID != MCSection::NonUniqueID) {
561-
SmallString<128> Buffer;
562-
Section.toVector(Buffer);
563-
SectionLen = Buffer.size();
564-
Buffer.push_back(0); // separator which cannot occur in the name
565-
if (GroupSym)
566-
Buffer.append(GroupSym->getName());
567-
Buffer.push_back(0); // separator which cannot occur in the name
568-
if (LinkedToSym)
569-
Buffer.append(LinkedToSym->getName());
570-
support::endian::write(Buffer, UniqueID, endianness::native);
571-
StringRef UniqueMapKey = StringRef(Buffer);
572-
EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
573-
} else if (!Section.isSingleStringRef()) {
574-
SmallString<128> Buffer;
575-
SectionLen = Buffer.size();
576-
StringRef UniqueMapKey = Section.toStringRef(Buffer);
577-
EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
578-
} else {
579-
SectionLen = Section.getSingleStringRef().size();
580-
StringRef UniqueMapKey = Section.getSingleStringRef();
581-
EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
582-
}
583-
584-
if (!EntryNewPair.second)
585-
return EntryNewPair.first->second;
586-
587-
StringRef CachedName = EntryNewPair.first->getKey().take_front(SectionLen);
560+
StringRef CachedName = Entry.first.SectionName;
588561

589562
SectionKind Kind;
590563
if (Flags & ELF::SHF_ARM_PURECODE)
@@ -628,7 +601,7 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
628601
MCSectionELF *Result =
629602
createELFSectionImpl(CachedName, Type, Flags, Kind, EntrySize, GroupSym,
630603
IsComdat, UniqueID, LinkedToSym);
631-
EntryNewPair.first->second = Result;
604+
Entry.second = Result;
632605

633606
recordELFMergeableSectionInfo(Result->getName(), Result->getFlags(),
634607
Result->getUniqueID(), Result->getEntrySize());

0 commit comments

Comments
 (0)