[llvm] d1a838b - Basic block sections should enable function sections implicitly.

Sriraman Tallam via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 16 16:30:19 PST 2021


Author: Sriraman Tallam
Date: 2021-02-16T16:27:16-08:00
New Revision: d1a838babcc3360eb7e311006a4acd1eee61b8f2

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

LOG: 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

Added: 
    

Modified: 
    llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
    llvm/include/llvm/Target/TargetLoweringObjectFile.h
    llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
    llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
    llvm/lib/Target/TargetLoweringObjectFile.cpp
    llvm/test/CodeGen/X86/basic-block-sections-blockaddress-taken.ll
    llvm/test/CodeGen/X86/basic-block-sections-list.ll
    llvm/test/CodeGen/X86/basic-block-sections-mir-parse.mir
    llvm/test/CodeGen/X86/basic-block-sections-unreachable.ll
    llvm/test/CodeGen/X86/basic-block-sections.ll
    llvm/test/DebugInfo/X86/basic-block-sections_1.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
index 98a020709ee3..73df97a81e70 100644
--- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
+++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
@@ -71,6 +71,10 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
                                  const MachineBasicBlock &MBB,
                                  const TargetMachine &TM) const override;
 
+  MCSection *
+  getUniqueSectionForFunction(const Function &F,
+                              const TargetMachine &TM) const override;
+
   bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
                                            const Function &F) const override;
 

diff  --git a/llvm/include/llvm/Target/TargetLoweringObjectFile.h b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
index 921063bab7a9..93bfdd20e082 100644
--- a/llvm/include/llvm/Target/TargetLoweringObjectFile.h
+++ b/llvm/include/llvm/Target/TargetLoweringObjectFile.h
@@ -102,6 +102,10 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {
                                  const MachineBasicBlock &MBB,
                                  const TargetMachine &TM) const;
 
+  virtual MCSection *
+  getUniqueSectionForFunction(const Function &F,
+                              const TargetMachine &TM) const;
+
   /// Classify the specified global variable into a set of target independent
   /// categories embodied in SectionKind.
   static SectionKind getKindForGlobal(const GlobalObject *GO,

diff  --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 764293e7f536..e292d5b89e05 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -709,7 +709,12 @@ void AsmPrinter::emitFunctionHeader() {
   emitConstantPool();
 
   // Print the 'header' of function.
-  MF->setSection(getObjFileLowering().SectionForGlobal(&F, TM));
+  // If basic block sections is desired and function sections is off,
+  // explicitly request a unique section for this function.
+  if (MF->front().isBeginSection() && !TM.getFunctionSections())
+    MF->setSection(getObjFileLowering().getUniqueSectionForFunction(F, TM));
+  else
+    MF->setSection(getObjFileLowering().SectionForGlobal(&F, TM));
   OutStreamer->SwitchSection(MF->getSection());
 
   if (!MAI->hasVisibilityOnlyWithLinkage())

diff  --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index eef05557a353..f0f4516b7c93 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -798,6 +798,23 @@ static MCSectionELF *selectELFSectionForGlobal(
                            AssociatedSymbol);
 }
 
+static MCSection *selectELFSectionForGlobal(
+    MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
+    const TargetMachine &TM, bool EmitUniqueSection,  unsigned Flags,
+    unsigned *NextUniqueID) {
+  const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM);
+  if (LinkedToSym) {
+    EmitUniqueSection = true;
+    Flags |= ELF::SHF_LINK_ORDER;
+  }
+
+  MCSectionELF *Section = selectELFSectionForGlobal(
+      Ctx, GO, Kind, Mang, TM, EmitUniqueSection, Flags,
+      NextUniqueID, LinkedToSym);
+  assert(Section->getLinkedToSymbol() == LinkedToSym);
+  return Section;
+}
+
 MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
   unsigned Flags = getELFSectionFlags(Kind);
@@ -812,18 +829,17 @@ MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
       EmitUniqueSection = TM.getDataSections();
   }
   EmitUniqueSection |= GO->hasComdat();
+  return selectELFSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
+                                   EmitUniqueSection, Flags, &NextUniqueID);
+}
 
-  const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM);
-  if (LinkedToSym) {
-    EmitUniqueSection = true;
-    Flags |= ELF::SHF_LINK_ORDER;
-  }
-
-  MCSectionELF *Section = selectELFSectionForGlobal(
-      getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags,
-      &NextUniqueID, LinkedToSym);
-  assert(Section->getLinkedToSymbol() == LinkedToSym);
-  return Section;
+MCSection *TargetLoweringObjectFileELF::getUniqueSectionForFunction(
+    const Function &F, const TargetMachine &TM) const {
+  SectionKind Kind = SectionKind::getText();
+  unsigned Flags = getELFSectionFlags(Kind);
+  return selectELFSectionForGlobal(getContext(), &F, Kind, getMangler(), TM,
+                                   /* EmitUniqueSection = */ true, Flags,
+                                   &NextUniqueID);
 }
 
 MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(

diff  --git a/llvm/lib/Target/TargetLoweringObjectFile.cpp b/llvm/lib/Target/TargetLoweringObjectFile.cpp
index 81af4eead6d2..239f12c4d6f7 100644
--- a/llvm/lib/Target/TargetLoweringObjectFile.cpp
+++ b/llvm/lib/Target/TargetLoweringObjectFile.cpp
@@ -380,6 +380,11 @@ MCSection *TargetLoweringObjectFile::getSectionForMachineBasicBlock(
   return nullptr;
 }
 
+MCSection *TargetLoweringObjectFile::getUniqueSectionForFunction(
+    const Function &F, const TargetMachine &TM) const {
+  return nullptr;
+}
+
 /// getTTypeGlobalReference - Return an MCExpr to use for a
 /// reference to the specified global variable from exception
 /// handling information.

diff  --git a/llvm/test/CodeGen/X86/basic-block-sections-blockaddress-taken.ll b/llvm/test/CodeGen/X86/basic-block-sections-blockaddress-taken.ll
index 4957916ddc99..8d3a5ff9d4aa 100644
--- a/llvm/test/CodeGen/X86/basic-block-sections-blockaddress-taken.ll
+++ b/llvm/test/CodeGen/X86/basic-block-sections-blockaddress-taken.ll
@@ -7,6 +7,7 @@ entry:
   indirectbr i8* %1, [label %bb1, label %bb2]
 
 ; CHECK:         .text
+; CHECK:         .section .text.foo,"ax", at progbits
 ; CHECK-LABEL: foo:
 ; CHECK:         movl    $.Ltmp0, %eax
 ; CHECK-NEXT:    movl    $.Ltmp1, %ecx
@@ -16,7 +17,7 @@ entry:
 bb1:                                                ; preds = %entry
   %2 = call i32 @bar()
   ret void
-; CHECK:         .section .text,"ax", at progbits,unique,1
+; CHECK:         .section .text.foo,"ax", at progbits,unique,1
 ; CHECK-NEXT:  .Ltmp0:
 ; CHECK-NEXT:  foo.__part.1
 ; CHECK-NEXT:    callq   bar
@@ -25,7 +26,7 @@ bb1:                                                ; preds = %entry
 bb2:                                                ; preds = %entry
   %3 = call i32 @baz()
   ret void
-; CHECK:         .section .text,"ax", at progbits,unique,2
+; CHECK:         .section .text.foo,"ax", at progbits,unique,2
 ; CHECK-NEXT:  .Ltmp1:
 ; CHECK-NEXT:  foo.__part.2
 ; CHECK-NEXT:    callq   baz

diff  --git a/llvm/test/CodeGen/X86/basic-block-sections-list.ll b/llvm/test/CodeGen/X86/basic-block-sections-list.ll
index ab5abdcd01c1..d2408a0b3e4b 100644
--- a/llvm/test/CodeGen/X86/basic-block-sections-list.ll
+++ b/llvm/test/CodeGen/X86/basic-block-sections-list.ll
@@ -1,7 +1,7 @@
 ; Check the basic block sections list option.
 ; RUN: echo '!_Z3foob' > %t
-; 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
-; 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
+; 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
+;  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
 
 define i32 @_Z3foob(i1 zeroext %0) nounwind {
   %2 = alloca i32, align 4
@@ -67,7 +67,8 @@ define i32 @_Z3zipb(i1 zeroext %0) nounwind {
 ; LINUX-SECTIONS: .section        .text._Z3foob._Z3foob.__part.3,"ax", at progbits
 ; LINUX-SECTIONS: _Z3foob.__part.3:
 
-; LINUX-SECTIONS: .section        .text._Z3zipb,"ax", at progbits
+; LINUX-SECTIONS-FUNCTION-SECTION: .section        .text._Z3zipb,"ax", at progbits
+; LINUX-SECIONS-NO-FUNCTION-SECTION-NOT: .section        .text._Z3zipb,"ax", at progbits
 ; LINUX-SECTIONS: _Z3zipb:
 ; LINUX-SECTIONS-NOT: .section        .text._Z3zipb._Z3zipb.__part.{{[0-9]+}},"ax", at progbits
 ; LINUX-SECTIONS-NOT: _Z3zipb.__part.{{[0-9]+}}:

diff  --git a/llvm/test/CodeGen/X86/basic-block-sections-mir-parse.mir b/llvm/test/CodeGen/X86/basic-block-sections-mir-parse.mir
index 6a2cee05ec88..ae67f16e85f2 100644
--- a/llvm/test/CodeGen/X86/basic-block-sections-mir-parse.mir
+++ b/llvm/test/CodeGen/X86/basic-block-sections-mir-parse.mir
@@ -122,10 +122,11 @@ body:             |
 
 ...
 
+# CHECK: .section	.text._Z3foob,"ax", at progbits
 # CHECK: _Z3foob:
-# CHECK: .section	.text,"ax", at progbits,unique
+# CHECK: .section	.text._Z3foob,"ax", at progbits,unique
 # CHECK: _Z3foob.__part.1:
-# CHECK: .section	.text,"ax", at progbits,unique
+# CHECK: .section	.text._Z3foob,"ax", at progbits,unique
 # CHECK: _Z3foob.__part.2:
-# CHECK: .section	.text,"ax", at progbits,unique
+# CHECK: .section	.text._Z3foob,"ax", at progbits,unique
 # CHECK: _Z3foob.__part.3:

diff  --git a/llvm/test/CodeGen/X86/basic-block-sections-unreachable.ll b/llvm/test/CodeGen/X86/basic-block-sections-unreachable.ll
index 8c77070a0278..838e2d925f78 100644
--- a/llvm/test/CodeGen/X86/basic-block-sections-unreachable.ll
+++ b/llvm/test/CodeGen/X86/basic-block-sections-unreachable.ll
@@ -13,6 +13,6 @@ target:
 default:
   unreachable
 ; CHECK-NOSECTIONS:     # %bb.2:     # %default
-; CHECK-SECTIONS:       .section .text,"ax", at progbits,unique,2
+; CHECK-SECTIONS:       .section .text.foo,"ax", at progbits,unique,2
 ; CHECK-SECTIONS-NEXT:  foo.__part.2:       # %default
 }

diff  --git a/llvm/test/CodeGen/X86/basic-block-sections.ll b/llvm/test/CodeGen/X86/basic-block-sections.ll
index a6dc6e5758ea..0ae7922a78bb 100644
--- a/llvm/test/CodeGen/X86/basic-block-sections.ll
+++ b/llvm/test/CodeGen/X86/basic-block-sections.ll
@@ -1,6 +1,8 @@
 ; 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
-; 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
+; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=all -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS
 ; 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
+; 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
+; RUN: llc < %s -mtriple=i386-unknown-linux-gnu  -basic-block-sections=all -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS
 
 define void @_Z3bazb(i1 zeroext) nounwind {
   %2 = alloca i8, align 1

diff  --git a/llvm/test/DebugInfo/X86/basic-block-sections_1.ll b/llvm/test/DebugInfo/X86/basic-block-sections_1.ll
index 959ba6728f4e..1e5df81bfd39 100644
--- a/llvm/test/DebugInfo/X86/basic-block-sections_1.ll
+++ b/llvm/test/DebugInfo/X86/basic-block-sections_1.ll
@@ -16,10 +16,10 @@
 ; NO-SECTIONS: DW_AT_high_pc [DW_FORM_data4] ({{.*}})
 ; BB-SECTIONS: DW_AT_low_pc [DW_FORM_addr] (0x0000000000000000)
 ; BB-SECTIONS-NEXT: DW_AT_ranges [DW_FORM_sec_offset]
-; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi.__part.1"
-; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi.__part.2"
-; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi.__part.3"
-; BB-SECTIONS-NEXT: [{{.*}}) ".text"
+; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi._Z3fooi.__part.1"
+; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi._Z3fooi.__part.2"
+; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi._Z3fooi.__part.3"
+; BB-SECTIONS-NEXT: [{{.*}}) ".text._Z3fooi"
 ; BB-SECTIONS-ASM: _Z3fooi:
 ; BB-SECTIONS-ASM: .Ltmp{{[0-9]+}}:
 ; BB-SECTIONS-ASM-NEXT: .loc 1 2 9 prologue_end


        


More information about the llvm-commits mailing list