Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BasicBlockSections] Split cold parts of custom-section functions. #66731

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1038,21 +1038,32 @@ MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock(
// under the .text.eh prefix. For regular sections, we either use a unique
// name, or a unique ID for the section.
SmallString<128> Name;
if (MBB.getSectionID() == MBBSectionID::ColdSectionID) {
Name += BBSectionsColdTextPrefix;
Name += MBB.getParent()->getName();
} else if (MBB.getSectionID() == MBBSectionID::ExceptionSectionID) {
Name += ".text.eh.";
Name += MBB.getParent()->getName();
} else {
Name += MBB.getParent()->getSection()->getName();
if (TM.getUniqueBasicBlockSectionNames()) {
if (!Name.endswith("."))
Name += ".";
Name += MBB.getSymbol()->getName();
StringRef FunctionSectionName = MBB.getParent()->getSection()->getName();
if (FunctionSectionName.equals(".text") ||
FunctionSectionName.startswith(".text.")) {
// Function is in a regular .text section.
StringRef FunctionName = MBB.getParent()->getName();
if (MBB.getSectionID() == MBBSectionID::ColdSectionID) {
Name += BBSectionsColdTextPrefix;
Name += FunctionName;
} else if (MBB.getSectionID() == MBBSectionID::ExceptionSectionID) {
Name += ".text.eh.";
Name += FunctionName;
} else {
UniqueID = NextUniqueID++;
Name += FunctionSectionName;
if (TM.getUniqueBasicBlockSectionNames()) {
if (!Name.endswith("."))
Name += ".";
Name += MBB.getSymbol()->getName();
} else {
UniqueID = NextUniqueID++;
}
}
} else {
// If the original function has a custom non-dot-text section, then emit
// all basic block sections into that section too, each with a unique id.
Name = FunctionSectionName;
UniqueID = NextUniqueID++;
}

unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
Expand Down
30 changes: 21 additions & 9 deletions llvm/test/CodeGen/X86/basic-block-sections-pragma-sections.ll
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
;; Tests for basic block sections applied on a function in a custom section.
; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=all | FileCheck %s
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=all | FileCheck %s
; RUN: echo "!_Z3fooi" > %t.list.txt
; RUN: echo "!!2" >> %t.list.txt
; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=%t.list.txt | FileCheck %s --check-prefix=LIST
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t.list.txt | FileCheck %s --check-prefix=LIST
; RUN: echo "!_Z3fooi" > %t1.list.txt
; RUN: echo "!!2" >> %t1.list.txt
; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=%t1.list.txt | FileCheck %s --check-prefix=LIST1
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t1.list.txt | FileCheck %s --check-prefix=LIST1
; RUN: echo "!_Z3fooi" > %t2.list.txt
; RUN: echo "!!0" >> %t2.list.txt
; RUN: llc < %s -mtriple=x86_64-pc-linux -basic-block-sections=%t2.list.txt | FileCheck %s --check-prefix=LIST2
; RUN: llc < %s -mtriple=x86_64-pc-linux -function-sections -basic-block-sections=%t2.list.txt | FileCheck %s --check-prefix=LIST2


; CHECK: .section foo_section,"ax",@progbits,unique,1
; CHECK-LABEL: _Z3fooi:
Expand All @@ -12,11 +18,17 @@
; CHECK: .section foo_section,"ax",@progbits,unique,3
; CHECK-NEXT: _Z3fooi.__part.2:

; LIST: .section foo_section,"ax",@progbits,unique,1
; LIST-LABEL: _Z3fooi:
; LIST: .section foo_section,"ax",@progbits,unique,2
; LIST-NEXT: _Z3fooi.__part.0:
; LIST-NOT: .section foo_section,"ax",@progbits,unique,3
; LIST1: .section foo_section,"ax",@progbits,unique,1
; LIST1-LABEL: _Z3fooi:
; LIST1: .section foo_section,"ax",@progbits,unique,2
; LIST1-NEXT: _Z3fooi.__part.0:
; LIST1-NOT: .section foo_section,"ax",@progbits,unique,3

; LIST2: .section foo_section,"ax",@progbits,unique,1
; LIST2-LABEL: _Z3fooi:
; LIST2: .section foo_section,"ax",@progbits,unique,2
; LIST2-NEXT: _Z3fooi.cold:
; LIST2-NOT: .section foo_section,"ax",@progbits,unique,3

;; Source to generate the IR:
;; #pragma clang section text = "foo_section"
Expand Down