Skip to content

Commit

Permalink
MCAsmStreamer: Omit initial ".text"
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MaskRay committed Dec 23, 2024
1 parent 158a600 commit 7b23f41
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 23 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/MC/MCStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 25 additions & 9 deletions llvm/lib/MC/MCAsmStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/MC/MCStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 0 additions & 2 deletions llvm/test/CodeGen/PowerPC/check-cpu.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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

1 change: 1 addition & 0 deletions llvm/test/CodeGen/X86/coff-alias-type.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 2 additions & 2 deletions llvm/test/DebugInfo/ARM/header.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/DebugInfo/X86/header.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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,@function
Expand Down
4 changes: 1 addition & 3 deletions llvm/test/MC/AArch64/armv8.3a-signed-pointer.s
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
6 changes: 6 additions & 0 deletions llvm/test/MC/ELF/section.s
Original file line number Diff line number Diff line change
@@ -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,"",@progbits
Expand Down
1 change: 0 additions & 1 deletion llvm/test/MC/Hexagon/hexagon_attributes.s
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7b23f41

Please sign in to comment.