Skip to content

Commit d1a838b

Browse files
committed
Basic block sections should enable function sections implicitly.
Basic block sections enables function sections implicitly, this is not needed and is inefficient with "=list" option. We had basic block sections enable function sections implicitly in clang. This is particularly inefficient with "=list" option as it places functions that do not have any basic block sections in separate sections. This causes unnecessary object file overhead for large applications. This patch disables this implicit behavior. It only creates function sections for those functions that require basic block sections. Further, there was an inconistent behavior with llc as llc was not turning on function sections by default. This patch makes llc and clang consistent and tests are added to check the new behavior. This is the first of two patches and this adds functionality in LLVM to create a new section for the entry block if function sections is not enabled. Differential Revision: https://reviews.llvm.org/D93876
1 parent 4c3f1be commit d1a838b

11 files changed

+65
-26
lines changed

llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
7171
const MachineBasicBlock &MBB,
7272
const TargetMachine &TM) const override;
7373

74+
MCSection *
75+
getUniqueSectionForFunction(const Function &F,
76+
const TargetMachine &TM) const override;
77+
7478
bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
7579
const Function &F) const override;
7680

llvm/include/llvm/Target/TargetLoweringObjectFile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {
102102
const MachineBasicBlock &MBB,
103103
const TargetMachine &TM) const;
104104

105+
virtual MCSection *
106+
getUniqueSectionForFunction(const Function &F,
107+
const TargetMachine &TM) const;
108+
105109
/// Classify the specified global variable into a set of target independent
106110
/// categories embodied in SectionKind.
107111
static SectionKind getKindForGlobal(const GlobalObject *GO,

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,12 @@ void AsmPrinter::emitFunctionHeader() {
709709
emitConstantPool();
710710

711711
// Print the 'header' of function.
712-
MF->setSection(getObjFileLowering().SectionForGlobal(&F, TM));
712+
// If basic block sections is desired and function sections is off,
713+
// explicitly request a unique section for this function.
714+
if (MF->front().isBeginSection() && !TM.getFunctionSections())
715+
MF->setSection(getObjFileLowering().getUniqueSectionForFunction(F, TM));
716+
else
717+
MF->setSection(getObjFileLowering().SectionForGlobal(&F, TM));
713718
OutStreamer->SwitchSection(MF->getSection());
714719

715720
if (!MAI->hasVisibilityOnlyWithLinkage())

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,23 @@ static MCSectionELF *selectELFSectionForGlobal(
798798
AssociatedSymbol);
799799
}
800800

801+
static MCSection *selectELFSectionForGlobal(
802+
MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
803+
const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags,
804+
unsigned *NextUniqueID) {
805+
const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM);
806+
if (LinkedToSym) {
807+
EmitUniqueSection = true;
808+
Flags |= ELF::SHF_LINK_ORDER;
809+
}
810+
811+
MCSectionELF *Section = selectELFSectionForGlobal(
812+
Ctx, GO, Kind, Mang, TM, EmitUniqueSection, Flags,
813+
NextUniqueID, LinkedToSym);
814+
assert(Section->getLinkedToSymbol() == LinkedToSym);
815+
return Section;
816+
}
817+
801818
MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
802819
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
803820
unsigned Flags = getELFSectionFlags(Kind);
@@ -812,18 +829,17 @@ MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
812829
EmitUniqueSection = TM.getDataSections();
813830
}
814831
EmitUniqueSection |= GO->hasComdat();
832+
return selectELFSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
833+
EmitUniqueSection, Flags, &NextUniqueID);
834+
}
815835

816-
const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM);
817-
if (LinkedToSym) {
818-
EmitUniqueSection = true;
819-
Flags |= ELF::SHF_LINK_ORDER;
820-
}
821-
822-
MCSectionELF *Section = selectELFSectionForGlobal(
823-
getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags,
824-
&NextUniqueID, LinkedToSym);
825-
assert(Section->getLinkedToSymbol() == LinkedToSym);
826-
return Section;
836+
MCSection *TargetLoweringObjectFileELF::getUniqueSectionForFunction(
837+
const Function &F, const TargetMachine &TM) const {
838+
SectionKind Kind = SectionKind::getText();
839+
unsigned Flags = getELFSectionFlags(Kind);
840+
return selectELFSectionForGlobal(getContext(), &F, Kind, getMangler(), TM,
841+
/* EmitUniqueSection = */ true, Flags,
842+
&NextUniqueID);
827843
}
828844

829845
MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(

llvm/lib/Target/TargetLoweringObjectFile.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ MCSection *TargetLoweringObjectFile::getSectionForMachineBasicBlock(
380380
return nullptr;
381381
}
382382

383+
MCSection *TargetLoweringObjectFile::getUniqueSectionForFunction(
384+
const Function &F, const TargetMachine &TM) const {
385+
return nullptr;
386+
}
387+
383388
/// getTTypeGlobalReference - Return an MCExpr to use for a
384389
/// reference to the specified global variable from exception
385390
/// handling information.

llvm/test/CodeGen/X86/basic-block-sections-blockaddress-taken.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ entry:
77
indirectbr i8* %1, [label %bb1, label %bb2]
88

99
; CHECK: .text
10+
; CHECK: .section .text.foo,"ax",@progbits
1011
; CHECK-LABEL: foo:
1112
; CHECK: movl $.Ltmp0, %eax
1213
; CHECK-NEXT: movl $.Ltmp1, %ecx
@@ -16,7 +17,7 @@ entry:
1617
bb1: ; preds = %entry
1718
%2 = call i32 @bar()
1819
ret void
19-
; CHECK: .section .text,"ax",@progbits,unique,1
20+
; CHECK: .section .text.foo,"ax",@progbits,unique,1
2021
; CHECK-NEXT: .Ltmp0:
2122
; CHECK-NEXT: foo.__part.1
2223
; CHECK-NEXT: callq bar
@@ -25,7 +26,7 @@ bb1: ; preds = %entry
2526
bb2: ; preds = %entry
2627
%3 = call i32 @baz()
2728
ret void
28-
; CHECK: .section .text,"ax",@progbits,unique,2
29+
; CHECK: .section .text.foo,"ax",@progbits,unique,2
2930
; CHECK-NEXT: .Ltmp1:
3031
; CHECK-NEXT: foo.__part.2
3132
; CHECK-NEXT: callq baz

llvm/test/CodeGen/X86/basic-block-sections-list.ll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; Check the basic block sections list option.
22
; RUN: echo '!_Z3foob' > %t
3-
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS
4-
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t -unique-basic-block-section-names -split-machine-functions | FileCheck %s -check-prefix=LINUX-SECTIONS
3+
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS --check-prefix=LINUX-SECTIONS-FUNCTION-SECTION
4+
; llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=%t -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS --check-prefix=LINUX-SECTIONS-NO-FUNCTION-SECTION
55

66
define i32 @_Z3foob(i1 zeroext %0) nounwind {
77
%2 = alloca i32, align 4
@@ -67,7 +67,8 @@ define i32 @_Z3zipb(i1 zeroext %0) nounwind {
6767
; LINUX-SECTIONS: .section .text._Z3foob._Z3foob.__part.3,"ax",@progbits
6868
; LINUX-SECTIONS: _Z3foob.__part.3:
6969

70-
; LINUX-SECTIONS: .section .text._Z3zipb,"ax",@progbits
70+
; LINUX-SECTIONS-FUNCTION-SECTION: .section .text._Z3zipb,"ax",@progbits
71+
; LINUX-SECIONS-NO-FUNCTION-SECTION-NOT: .section .text._Z3zipb,"ax",@progbits
7172
; LINUX-SECTIONS: _Z3zipb:
7273
; LINUX-SECTIONS-NOT: .section .text._Z3zipb._Z3zipb.__part.{{[0-9]+}},"ax",@progbits
7374
; LINUX-SECTIONS-NOT: _Z3zipb.__part.{{[0-9]+}}:

llvm/test/CodeGen/X86/basic-block-sections-mir-parse.mir

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,11 @@ body: |
122122
123123
...
124124

125+
# CHECK: .section .text._Z3foob,"ax",@progbits
125126
# CHECK: _Z3foob:
126-
# CHECK: .section .text,"ax",@progbits,unique
127+
# CHECK: .section .text._Z3foob,"ax",@progbits,unique
127128
# CHECK: _Z3foob.__part.1:
128-
# CHECK: .section .text,"ax",@progbits,unique
129+
# CHECK: .section .text._Z3foob,"ax",@progbits,unique
129130
# CHECK: _Z3foob.__part.2:
130-
# CHECK: .section .text,"ax",@progbits,unique
131+
# CHECK: .section .text._Z3foob,"ax",@progbits,unique
131132
# CHECK: _Z3foob.__part.3:

llvm/test/CodeGen/X86/basic-block-sections-unreachable.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ target:
1313
default:
1414
unreachable
1515
; CHECK-NOSECTIONS: # %bb.2: # %default
16-
; CHECK-SECTIONS: .section .text,"ax",@progbits,unique,2
16+
; CHECK-SECTIONS: .section .text.foo,"ax",@progbits,unique,2
1717
; CHECK-SECTIONS-NEXT: foo.__part.2: # %default
1818
}

llvm/test/CodeGen/X86/basic-block-sections.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=all -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS
2-
; RUN: llc < %s -mtriple=i386-unknown-linux-gnu -function-sections -basic-block-sections=all -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS
2+
; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=all -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS
33
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=all -unique-basic-block-section-names -split-machine-functions | FileCheck %s -check-prefix=LINUX-SECTIONS
4+
; RUN: llc < %s -mtriple=i386-unknown-linux-gnu -function-sections -basic-block-sections=all -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS
5+
; RUN: llc < %s -mtriple=i386-unknown-linux-gnu -basic-block-sections=all -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS
46

57
define void @_Z3bazb(i1 zeroext) nounwind {
68
%2 = alloca i8, align 1

llvm/test/DebugInfo/X86/basic-block-sections_1.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
; NO-SECTIONS: DW_AT_high_pc [DW_FORM_data4] ({{.*}})
1717
; BB-SECTIONS: DW_AT_low_pc [DW_FORM_addr] (0x0000000000000000)
1818
; BB-SECTIONS-NEXT: DW_AT_ranges [DW_FORM_sec_offset]
19-
; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi.__part.1"
20-
; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi.__part.2"
21-
; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi.__part.3"
22-
; BB-SECTIONS-NEXT: [{{.*}}) ".text"
19+
; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi._Z3fooi.__part.1"
20+
; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi._Z3fooi.__part.2"
21+
; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi._Z3fooi.__part.3"
22+
; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi"
2323
; BB-SECTIONS-ASM: _Z3fooi:
2424
; BB-SECTIONS-ASM: .Ltmp{{[0-9]+}}:
2525
; BB-SECTIONS-ASM-NEXT: .loc 1 2 9 prologue_end

0 commit comments

Comments
 (0)