[llvm] 7b23f41 - MCAsmStreamer: Omit initial ".text"

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 22 22:03:50 PST 2024


Author: Fangrui Song
Date: 2024-12-22T22:03:44-08:00
New Revision: 7b23f413d1f76532825e470b523e971818d453ca

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

LOG: MCAsmStreamer: Omit initial ".text"

llvm-mc --assemble prints an initial `.text` from `initSections`.
This is weird for quick assembly tasks that do not specify `.text`.

Omit the .text by moving section directive printing from `changeSection`
to `switchSection`. switchSectionNoPrint now correctly calls the
`changeSection` hook (needed by MachO).

The initial directives of clang -S are now reordered. On ELF targets, we
get `.file "a.c"; .text` instead of `.text; .file "a.c"`.
If there is no function, `.text` will be omitted.

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCStreamer.h
    llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
    llvm/lib/MC/MCAsmStreamer.cpp
    llvm/lib/MC/MCStreamer.cpp
    llvm/test/CodeGen/PowerPC/check-cpu.ll
    llvm/test/CodeGen/X86/coff-alias-type.ll
    llvm/test/DebugInfo/ARM/header.ll
    llvm/test/DebugInfo/X86/header.ll
    llvm/test/MC/AArch64/armv8.3a-signed-pointer.s
    llvm/test/MC/ELF/section.s
    llvm/test/MC/Hexagon/hexagon_attributes.s

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCStreamer.h b/llvm/include/llvm/MC/MCStreamer.h
index cfb31a76218425..9115dcd2cb7167 100644
--- a/llvm/include/llvm/MC/MCStreamer.h
+++ b/llvm/include/llvm/MC/MCStreamer.h
@@ -424,7 +424,7 @@ class MCStreamer {
   /// Calls changeSection as needed.
   ///
   /// Returns false if the stack was empty.
-  bool popSection();
+  virtual bool popSection();
 
   /// Set the current section where code is being emitted to \p Section.  This
   /// is required to update CurSection.

diff  --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index d2e60bb7f6318c..d34fe0e86c7495 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -531,7 +531,8 @@ bool AsmPrinter::doInitialization(Module &M) {
   if (TM.getTargetTriple().isOSBinFormatXCOFF()) {
     emitModuleCommandLines(M);
     // Now we can generate section information.
-    OutStreamer->initSections(false, *TM.getMCSubtargetInfo());
+    OutStreamer->switchSection(
+        OutContext.getObjectFileInfo()->getTextSection());
 
     // To work around an AIX assembler and/or linker bug, generate
     // a rename for the default text-section symbol name.  This call has

diff  --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp
index 5084fc1d919969..78fed7792ad8ad 100644
--- a/llvm/lib/MC/MCAsmStreamer.cpp
+++ b/llvm/lib/MC/MCAsmStreamer.cpp
@@ -55,6 +55,8 @@ class MCAsmStreamer final : public MCStreamer {
   raw_svector_ostream CommentStream;
   raw_null_ostream NullStream;
 
+  bool EmittedSectionDirective = false;
+
   bool IsVerboseAsm = false;
   bool ShowInst = false;
   bool UseDwarfDirectory = false;
@@ -160,7 +162,8 @@ class MCAsmStreamer final : public MCStreamer {
   /// @name MCStreamer Interface
   /// @{
 
-  void changeSection(MCSection *Section, uint32_t Subsection) override;
+  void switchSection(MCSection *Section, uint32_t Subsection) override;
+  bool popSection() override;
 
   void emitELFSymverDirective(const MCSymbol *OriginalSym, StringRef Name,
                               bool KeepOriginalSym) override;
@@ -532,14 +535,27 @@ void MCAsmStreamer::emitExplicitComments() {
   ExplicitCommentToEmit.clear();
 }
 
-void MCAsmStreamer::changeSection(MCSection *Section, uint32_t Subsection) {
-  if (MCTargetStreamer *TS = getTargetStreamer()) {
-    TS->changeSection(getCurrentSection().first, Section, Subsection, OS);
-  } else {
-    Section->printSwitchToSection(*MAI, getContext().getTargetTriple(), OS,
-                                  Subsection);
+void MCAsmStreamer::switchSection(MCSection *Section, uint32_t Subsection) {
+  MCSectionSubPair Cur = getCurrentSection();
+  if (!EmittedSectionDirective ||
+      MCSectionSubPair(Section, Subsection) != Cur) {
+    EmittedSectionDirective = true;
+    if (MCTargetStreamer *TS = getTargetStreamer()) {
+      TS->changeSection(Cur.first, Section, Subsection, OS);
+    } else {
+      Section->printSwitchToSection(*MAI, getContext().getTargetTriple(), OS,
+                                    Subsection);
+    }
   }
-  MCStreamer::changeSection(Section, Subsection);
+  MCStreamer::switchSection(Section, Subsection);
+}
+
+bool MCAsmStreamer::popSection() {
+  if (!MCStreamer::popSection())
+    return false;
+  auto [Sec, Subsec] = getCurrentSection();
+  Sec->printSwitchToSection(*MAI, getContext().getTargetTriple(), OS, Subsec);
+  return true;
 }
 
 void MCAsmStreamer::emitELFSymverDirective(const MCSymbol *OriginalSym,
@@ -2543,7 +2559,7 @@ void MCAsmStreamer::finishImpl() {
   if (!Tables.empty()) {
     assert(Tables.size() == 1 && "asm output only supports one line table");
     if (auto *Label = Tables.begin()->second.getLabel()) {
-      switchSection(getContext().getObjectFileInfo()->getDwarfLineSection());
+      switchSection(getContext().getObjectFileInfo()->getDwarfLineSection(), 0);
       emitLabel(Label);
     }
   }

diff  --git a/llvm/lib/MC/MCStreamer.cpp b/llvm/lib/MC/MCStreamer.cpp
index 5703cfd03fcebc..ccf65df150e786 100644
--- a/llvm/lib/MC/MCStreamer.cpp
+++ b/llvm/lib/MC/MCStreamer.cpp
@@ -414,7 +414,7 @@ void MCStreamer::emitEHSymAttributes(const MCSymbol *Symbol,
 }
 
 void MCStreamer::initSections(bool NoExecStack, const MCSubtargetInfo &STI) {
-  switchSection(getContext().getObjectFileInfo()->getTextSection());
+  switchSectionNoPrint(getContext().getObjectFileInfo()->getTextSection());
 }
 
 void MCStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
@@ -1332,7 +1332,10 @@ bool MCStreamer::switchSection(MCSection *Section, const MCExpr *SubsecExpr) {
 void MCStreamer::switchSectionNoPrint(MCSection *Section) {
   SectionStack.back().second = SectionStack.back().first;
   SectionStack.back().first = MCSectionSubPair(Section, 0);
-  CurFrag = &Section->getDummyFragment();
+  changeSection(Section, 0);
+  MCSymbol *Sym = Section->getBeginSymbol();
+  if (Sym && !Sym->isInSection())
+    emitLabel(Sym);
 }
 
 MCSymbol *MCStreamer::endSection(MCSection *Section) {

diff  --git a/llvm/test/CodeGen/PowerPC/check-cpu.ll b/llvm/test/CodeGen/PowerPC/check-cpu.ll
index 1dc532cb428f4f..5eba85a3acf38b 100644
--- a/llvm/test/CodeGen/PowerPC/check-cpu.ll
+++ b/llvm/test/CodeGen/PowerPC/check-cpu.ll
@@ -20,5 +20,3 @@
 ; Test -mcpu=[pwr9|pwr10|pwr11|future] is recognized on PowerPC.
 
 ; CHECK-NOT: is not a recognized processor for this target
-; CHECK:     .text
-

diff  --git a/llvm/test/CodeGen/X86/coff-alias-type.ll b/llvm/test/CodeGen/X86/coff-alias-type.ll
index 0e041fc032ff5a..a242cd2d77d7c9 100644
--- a/llvm/test/CodeGen/X86/coff-alias-type.ll
+++ b/llvm/test/CodeGen/X86/coff-alias-type.ll
@@ -13,6 +13,7 @@ entry:
 ; CHECK-NEXT: .scl     2
 ; CHECK-NEXT: .type    32
 ; CHECK-NEXT: .endef
+; CHECK-NEXT: .text
 ; CHECK-NEXT: .globl   _ZN8MyStructC2Ev
 ; CHECK:      {{^}}_ZN8MyStructC2Ev:
 

diff  --git a/llvm/test/DebugInfo/ARM/header.ll b/llvm/test/DebugInfo/ARM/header.ll
index c41e603e15f529..7582f340331593 100644
--- a/llvm/test/DebugInfo/ARM/header.ll
+++ b/llvm/test/DebugInfo/ARM/header.ll
@@ -4,8 +4,8 @@
 ; This is particularly important on ARM MachO as a change in section order can
 ; cause a change the relaxation of the instructions used.
 
-; CHECK:      .section        __TEXT,__text,regular,pure_instructions
-; CHECK-NEXT: .syntax unified
+; CHECK:      .syntax unified
+; CHECK-NEXT: .section        __TEXT,__text,regular,pure_instructions
 ; CHECK-NEXT: .globl  _f
 ; CHECK-NEXT: .p2align  2
 ; CHECK-NEXT: .code 32                    @ @f

diff  --git a/llvm/test/DebugInfo/X86/header.ll b/llvm/test/DebugInfo/X86/header.ll
index f90aa3b50f4671..0c730252701c5a 100644
--- a/llvm/test/DebugInfo/X86/header.ll
+++ b/llvm/test/DebugInfo/X86/header.ll
@@ -2,8 +2,8 @@
 
 ; Test that we don't pollute the start of the file with debug sections
 
-; CHECK:       .text
-; CHECK-NEXT: .file	"<stdin>"
+; CHECK:      .file	"<stdin>"
+; CHECK-NEXT:  .text
 ; CHECK-NEXT: .globl	f
 ; CHECK-NEXT: .p2align	4
 ; CHECK-NEXT: .type	f, at function

diff  --git a/llvm/test/MC/AArch64/armv8.3a-signed-pointer.s b/llvm/test/MC/AArch64/armv8.3a-signed-pointer.s
index accba62517808b..222df6dfa346cd 100644
--- a/llvm/test/MC/AArch64/armv8.3a-signed-pointer.s
+++ b/llvm/test/MC/AArch64/armv8.3a-signed-pointer.s
@@ -5,7 +5,6 @@
 // RUN: FileCheck --check-prefixes=NO83,ALL %s < %t.1
 // RUN: FileCheck --check-prefix=CHECK-REQ %s < %t.2
 
-// ALL: .text
 .text
   mrs x0, apiakeylo_el1
   mrs x0, apiakeyhi_el1
@@ -17,8 +16,7 @@
   mrs x0, apdbkeyhi_el1
   mrs x0, apgakeylo_el1
   mrs x0, apgakeyhi_el1
-// ALL-EMPTY:
-// ALL-EMPTY:
+// ALL: .text
 // CHECK-NEXT: mrs x0, APIAKeyLo_EL1     // encoding: [0x00,0x21,0x38,0xd5]
 // CHECK-NEXT: mrs x0, APIAKeyHi_EL1     // encoding: [0x20,0x21,0x38,0xd5]
 // CHECK-NEXT: mrs x0, APIBKeyLo_EL1     // encoding: [0x40,0x21,0x38,0xd5]

diff  --git a/llvm/test/MC/ELF/section.s b/llvm/test/MC/ELF/section.s
index e4e6f4bccb8a41..f0623114273647 100644
--- a/llvm/test/MC/ELF/section.s
+++ b/llvm/test/MC/ELF/section.s
@@ -1,6 +1,12 @@
 // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | llvm-readobj -S - | FileCheck %s
 // RUN: llvm-mc -filetype=asm -triple x86_64-pc-linux-gnu %s -o - |  FileCheck %s --check-prefix=ASM
 
+/// The second .text has no effect, therefore it is not printed.
+// ASM:       .text
+// ASM-EMPTY:
+.text
+.text
+
 // Test that these names are accepted.
 
 .section	.note.GNU-stack,"", at progbits

diff  --git a/llvm/test/MC/Hexagon/hexagon_attributes.s b/llvm/test/MC/Hexagon/hexagon_attributes.s
index e90536030d977f..4cd5223cd22069 100644
--- a/llvm/test/MC/Hexagon/hexagon_attributes.s
+++ b/llvm/test/MC/Hexagon/hexagon_attributes.s
@@ -78,7 +78,6 @@ v1:0.sf=vadd(v0.bf,v0.bf)    // hvxv73, hvx-ieee-fp
 // ASM-NEXT: .attribute      8, 1    // Tag_zreg
 // ASM-NEXT: .attribute      9, 1    // Tag_audio
 // ASM-NEXT: .attribute      10, 1   // Tag_cabac
-// ASM-NEXT:        .text
 // ASM-EMPTY:
 // ASM-NEXT:        {
 // ASM-NEXT:                q0 &= vcmp.gt(v0.bf,v0.bf)


        


More information about the llvm-commits mailing list