[llvm] e48c401 - [MC] Cache current fragment in MCStreamer

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 27 19:44:43 PDT 2024


Author: Fangrui Song
Date: 2024-06-27T19:44:37-07:00
New Revision: e48c4011ca80385573f1b92793c75dc98abb228f

URL: https://github.com/llvm/llvm-project/commit/e48c4011ca80385573f1b92793c75dc98abb228f
DIFF: https://github.com/llvm/llvm-project/commit/e48c4011ca80385573f1b92793c75dc98abb228f.diff

LOG: [MC] Cache current fragment in MCStreamer

This eliminates indirection through `getCurrentSectionOnly()->curFragList()->Tail`.

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCFragment.h
    llvm/include/llvm/MC/MCObjectStreamer.h
    llvm/include/llvm/MC/MCStreamer.h
    llvm/lib/MC/MCAsmStreamer.cpp
    llvm/lib/MC/MCObjectStreamer.cpp
    llvm/lib/MC/MCStreamer.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCFragment.h b/llvm/include/llvm/MC/MCFragment.h
index 72db28cb5eb67..c8832e66e1714 100644
--- a/llvm/include/llvm/MC/MCFragment.h
+++ b/llvm/include/llvm/MC/MCFragment.h
@@ -24,6 +24,7 @@
 namespace llvm {
 
 class MCAssembler;
+class MCObjectStreamer;
 class MCSection;
 class MCSubtargetInfo;
 class MCSymbol;
@@ -31,6 +32,7 @@ class MCSymbol;
 class MCFragment {
   friend class MCAsmLayout;
   friend class MCAssembler;
+  friend class MCObjectStreamer;
   friend class MCSection;
 
 public:

diff  --git a/llvm/include/llvm/MC/MCObjectStreamer.h b/llvm/include/llvm/MC/MCObjectStreamer.h
index a8164494d0de4..e6c07d50baedb 100644
--- a/llvm/include/llvm/MC/MCObjectStreamer.h
+++ b/llvm/include/llvm/MC/MCObjectStreamer.h
@@ -88,9 +88,12 @@ class MCObjectStreamer : public MCStreamer {
   MCFragment *getCurrentFragment() const;
 
   void insert(MCFragment *F) {
-    MCSection *CurSection = getCurrentSectionOnly();
-    CurSection->addFragment(*F);
-    F->setParent(CurSection);
+    auto *Sec = CurFrag->getParent();
+    F->setParent(Sec);
+    F->setLayoutOrder(CurFrag->getLayoutOrder() + 1);
+    CurFrag->Next = F;
+    CurFrag = F;
+    Sec->curFragList()->Tail = F;
   }
 
   /// Get a data fragment to write into, creating a new one if the current

diff  --git a/llvm/include/llvm/MC/MCStreamer.h b/llvm/include/llvm/MC/MCStreamer.h
index 490b1a8dd7bc1..a8ca438cb39ba 100644
--- a/llvm/include/llvm/MC/MCStreamer.h
+++ b/llvm/include/llvm/MC/MCStreamer.h
@@ -19,6 +19,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/MC/MCDirectives.h"
 #include "llvm/MC/MCDwarf.h"
+#include "llvm/MC/MCFragment.h"
 #include "llvm/MC/MCLinkerOptimizationHint.h"
 #include "llvm/MC/MCPseudoProbe.h"
 #include "llvm/MC/MCWinEH.h"
@@ -255,6 +256,8 @@ class MCStreamer {
   bool AllowAutoPadding = false;
 
 protected:
+  MCFragment *CurFrag = nullptr;
+
   MCStreamer(MCContext &Ctx);
 
   virtual void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame);

diff  --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp
index 52719d1f8aeb6..ff240d1652db1 100644
--- a/llvm/lib/MC/MCAsmStreamer.cpp
+++ b/llvm/lib/MC/MCAsmStreamer.cpp
@@ -512,6 +512,7 @@ void MCAsmStreamer::emitExplicitComments() {
 
 void MCAsmStreamer::changeSection(MCSection *Section, uint32_t Subsection) {
   assert(Section && "Cannot switch to a null section!");
+  CurFrag = &Section->getDummyFragment();
   if (MCTargetStreamer *TS = getTargetStreamer()) {
     TS->changeSection(getCurrentSectionOnly(), Section, Subsection, OS);
   } else {

diff  --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp
index 2357203bb73fa..99ac7f4643745 100644
--- a/llvm/lib/MC/MCObjectStreamer.cpp
+++ b/llvm/lib/MC/MCObjectStreamer.cpp
@@ -143,7 +143,8 @@ void MCObjectStreamer::emitFrames(MCAsmBackend *MAB) {
 }
 
 MCFragment *MCObjectStreamer::getCurrentFragment() const {
-  return getCurrentSectionOnly()->curFragList()->Tail;
+  assert(CurFrag->getParent() == getCurrentSection().first);
+  return CurFrag;
 }
 
 static bool canReuseDataFragment(const MCDataFragment &F,
@@ -307,6 +308,7 @@ bool MCObjectStreamer::changeSectionImpl(MCSection *Section,
                        {Subsection, MCSection::FragList{F, F}});
   }
   Section->CurFragList = &Subsections[I].second;
+  CurFrag = Section->CurFragList->Tail;
 
   return getAssembler().registerSection(*Section);
 }

diff  --git a/llvm/lib/MC/MCStreamer.cpp b/llvm/lib/MC/MCStreamer.cpp
index 582b3a6c95a94..cfc27be3feec3 100644
--- a/llvm/lib/MC/MCStreamer.cpp
+++ b/llvm/lib/MC/MCStreamer.cpp
@@ -105,6 +105,7 @@ void MCStreamer::reset() {
   SymbolOrdering.clear();
   SectionStack.clear();
   SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>());
+  CurFrag = nullptr;
 }
 
 raw_ostream &MCStreamer::getCommentOS() {


        


More information about the llvm-commits mailing list