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

Revert "[MC] Make ELFUniquingMap a StringMap" #95023

Merged
merged 1 commit into from
Jun 10, 2024

Conversation

aengelke
Copy link
Contributor

Reverts #95006

Seems like there's some bug where the section name is empty in the if (!Section.isSingleStringRef()). Revert for now to get builds back to green.

@llvmbot llvmbot added the mc Machine (object) code label Jun 10, 2024
@aengelke aengelke merged commit 66f9192 into main Jun 10, 2024
6 of 7 checks passed
@aengelke aengelke deleted the revert-95006-perf/elf-uniqueing-stringmap branch June 10, 2024 19:00
@llvmbot
Copy link
Collaborator

llvmbot commented Jun 10, 2024

@llvm/pr-subscribers-mc

Author: None (aengelke)

Changes

Reverts llvm/llvm-project#95006

Seems like there's some bug where the section name is empty in the if (!Section.isSingleStringRef()). Revert for now to get builds back to green.


Full diff: https://github.com/llvm/llvm-project/pull/95023.diff

2 Files Affected:

  • (modified) llvm/include/llvm/MC/MCContext.h (+26-1)
  • (modified) llvm/lib/MC/MCContext.cpp (+10-37)
diff --git a/llvm/include/llvm/MC/MCContext.h b/llvm/include/llvm/MC/MCContext.h
index b2a52b4297e60..b0ac432a065bf 100644
--- a/llvm/include/llvm/MC/MCContext.h
+++ b/llvm/include/llvm/MC/MCContext.h
@@ -252,6 +252,31 @@ class MCContext {
   /// A collection of MCPseudoProbe in the current module
   MCPseudoProbeTable PseudoProbeTable;
 
+  // Sections are differentiated by the quadruple (section_name, group_name,
+  // unique_id, link_to_symbol_name). Sections sharing the same quadruple are
+  // combined into one section.
+  struct ELFSectionKey {
+    std::string SectionName;
+    StringRef GroupName;
+    StringRef LinkedToName;
+    unsigned UniqueID;
+
+    ELFSectionKey(StringRef SectionName, StringRef GroupName,
+                  StringRef LinkedToName, unsigned UniqueID)
+        : SectionName(SectionName), GroupName(GroupName),
+          LinkedToName(LinkedToName), UniqueID(UniqueID) {}
+
+    bool operator<(const ELFSectionKey &Other) const {
+      if (SectionName != Other.SectionName)
+        return SectionName < Other.SectionName;
+      if (GroupName != Other.GroupName)
+        return GroupName < Other.GroupName;
+      if (int O = LinkedToName.compare(Other.LinkedToName))
+        return O < 0;
+      return UniqueID < Other.UniqueID;
+    }
+  };
+
   struct COFFSectionKey {
     std::string SectionName;
     StringRef GroupName;
@@ -325,8 +350,8 @@ class MCContext {
   };
 
   StringMap<MCSectionMachO *> MachOUniquingMap;
+  std::map<ELFSectionKey, MCSectionELF *> ELFUniquingMap;
   std::map<COFFSectionKey, MCSectionCOFF *> COFFUniquingMap;
-  StringMap<MCSectionELF *> ELFUniquingMap;
   std::map<std::string, MCSectionGOFF *> GOFFUniquingMap;
   std::map<WasmSectionKey, MCSectionWasm *> WasmUniquingMap;
   std::map<XCOFFSectionKey, MCSectionXCOFF *> XCOFFUniquingMap;
diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index d5bde2bcb7301..771ca9c6006ca 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -44,7 +44,6 @@
 #include "llvm/MC/SectionKind.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/CommandLine.h"
-#include "llvm/Support/EndianStream.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
@@ -549,42 +548,16 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
   if (GroupSym)
     Group = GroupSym->getName();
   assert(!(LinkedToSym && LinkedToSym->getName().empty()));
+  // Do the lookup, if we have a hit, return it.
+  auto IterBool = ELFUniquingMap.insert(std::make_pair(
+      ELFSectionKey{Section.str(), Group,
+                    LinkedToSym ? LinkedToSym->getName() : "", UniqueID},
+      nullptr));
+  auto &Entry = *IterBool.first;
+  if (!IterBool.second)
+    return Entry.second;
 
-  // Sections are differentiated by the quadruple (section_name, group_name,
-  // unique_id, link_to_symbol_name). Sections sharing the same quadruple are
-  // combined into one section. As an optimization, non-unique sections without
-  // group or linked-to symbol have a shorter unique-ing key.
-  std::pair<StringMap<MCSectionELF *>::iterator, bool> EntryNewPair;
-  // Length of the section name, which are the first SectionLen bytes of the key
-  unsigned SectionLen;
-  if (GroupSym || LinkedToSym || UniqueID != MCSection::NonUniqueID) {
-    SmallString<128> Buffer;
-    Section.toVector(Buffer);
-    SectionLen = Buffer.size();
-    Buffer.push_back(0); // separator which cannot occur in the name
-    if (GroupSym)
-      Buffer.append(GroupSym->getName());
-    Buffer.push_back(0); // separator which cannot occur in the name
-    if (LinkedToSym)
-      Buffer.append(LinkedToSym->getName());
-    support::endian::write(Buffer, UniqueID, endianness::native);
-    StringRef UniqueMapKey = StringRef(Buffer);
-    EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
-  } else if (!Section.isSingleStringRef()) {
-    SmallString<128> Buffer;
-    SectionLen = Buffer.size();
-    StringRef UniqueMapKey = Section.toStringRef(Buffer);
-    EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
-  } else {
-    SectionLen = Section.getSingleStringRef().size();
-    StringRef UniqueMapKey = Section.getSingleStringRef();
-    EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
-  }
-
-  if (!EntryNewPair.second)
-    return EntryNewPair.first->second;
-
-  StringRef CachedName = EntryNewPair.first->getKey().take_front(SectionLen);
+  StringRef CachedName = Entry.first.SectionName;
 
   SectionKind Kind;
   if (Flags & ELF::SHF_ARM_PURECODE)
@@ -628,7 +601,7 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
   MCSectionELF *Result =
       createELFSectionImpl(CachedName, Type, Flags, Kind, EntrySize, GroupSym,
                            IsComdat, UniqueID, LinkedToSym);
-  EntryNewPair.first->second = Result;
+  Entry.second = Result;
 
   recordELFMergeableSectionInfo(Result->getName(), Result->getFlags(),
                                 Result->getUniqueID(), Result->getEntrySize());

Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Jun 12, 2024
Reverts llvm#95006

Seems like there's some bug where the section name is empty in the `if
(!Section.isSingleStringRef())`. Revert for now to get builds back to
green.
@HerrCai0907 HerrCai0907 mentioned this pull request Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants