[llvm] [BasicBlockSections] Split cold parts of custom-section functions. (PR #66731)

Rahman Lavaee via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 22 13:47:16 PDT 2023


https://github.com/rlavaee updated https://github.com/llvm/llvm-project/pull/66731

>From d4af4fdc27685ad9093a47b4721c4a4b6a7723ea Mon Sep 17 00:00:00 2001
From: Rahman Lavaee <rahmanl at google.com>
Date: Tue, 19 Sep 2023 00:09:54 +0000
Subject: [PATCH 1/4] [BasicBlockSections] Split cold parts of custom-section
 functions.

This PR makes `-basic-block-sections` handle functions with custom
non-dot-text sections correctly. Cold parts of such functions must
be placed in the same section (not in `.text.split`) but with a
unique id.
---
 .../CodeGen/TargetLoweringObjectFileImpl.cpp  | 18 +++++++----
 .../basic-block-sections-pragma-sections.ll   | 30 +++++++++++++------
 2 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 622c8addc548f4f..05d035dc697936b 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1038,14 +1038,21 @@ 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();
+  StringRef FunctionSectionName = MBB.getParent()->getSection()->getName();
+  if (!FunctionSectionName.equals(".text") && !FunctionSectionName.startswith(".text.")) {
+    // If the original function has a custom non-dot-text section, then split the cold part into that section too, but with a unique id.
+    Name = FunctionSectionName;
+    UniqueID = NextUniqueID++;
+  } else {
+  StringRef FunctionName = MBB.getParent()->getName();
+  if (MBB.getSectionID() == MBBSectionID::ColdSectionID){
+      Name += BBSectionsColdTextPrefix;
+      Name += FunctionName;
   } else if (MBB.getSectionID() == MBBSectionID::ExceptionSectionID) {
     Name += ".text.eh.";
-    Name += MBB.getParent()->getName();
+    Name += FunctionName;
   } else {
-    Name += MBB.getParent()->getSection()->getName();
+    Name += FunctionSectionName;
     if (TM.getUniqueBasicBlockSectionNames()) {
       if (!Name.endswith("."))
         Name += ".";
@@ -1054,6 +1061,7 @@ MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock(
       UniqueID = NextUniqueID++;
     }
   }
+  }
 
   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
   std::string GroupName;
diff --git a/llvm/test/CodeGen/X86/basic-block-sections-pragma-sections.ll b/llvm/test/CodeGen/X86/basic-block-sections-pragma-sections.ll
index d63fbdd7b362e07..13a4607100a057a 100644
--- a/llvm/test/CodeGen/X86/basic-block-sections-pragma-sections.ll
+++ b/llvm/test/CodeGen/X86/basic-block-sections-pragma-sections.ll
@@ -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", at progbits,unique,1
 ; CHECK-LABEL: _Z3fooi:
@@ -12,11 +18,17 @@
 ; CHECK: .section	foo_section,"ax", at progbits,unique,3
 ; CHECK-NEXT: _Z3fooi.__part.2:
 
-; LIST: .section	foo_section,"ax", at progbits,unique,1
-; LIST-LABEL: _Z3fooi:
-; LIST: .section	foo_section,"ax", at progbits,unique,2
-; LIST-NEXT: _Z3fooi.__part.0:
-; LIST-NOT: .section	foo_section,"ax", at progbits,unique,3
+; LIST1: .section	foo_section,"ax", at progbits,unique,1
+; LIST1-LABEL: _Z3fooi:
+; LIST1: .section	foo_section,"ax", at progbits,unique,2
+; LIST1-NEXT: _Z3fooi.__part.0:
+; LIST1-NOT: .section	foo_section,"ax", at progbits,unique,3
+
+; LIST2: .section	foo_section,"ax", at progbits,unique,1
+; LIST2-LABEL: _Z3fooi:
+; LIST2: .section	foo_section,"ax", at progbits,unique,2
+; LIST2-NEXT: _Z3fooi.cold:
+; LIST2-NOT: .section	foo_section,"ax", at progbits,unique,3
 
 ;; Source to generate the IR:
 ;; #pragma clang section text = "foo_section"

>From 88a8f72c1f2b0967155d74f0b9d60dfd61614b38 Mon Sep 17 00:00:00 2001
From: Rahman Lavaee <rahmanl at google.com>
Date: Tue, 19 Sep 2023 02:57:46 +0000
Subject: [PATCH 2/4] clang-format.

---
 .../CodeGen/TargetLoweringObjectFileImpl.cpp  | 32 ++++++++++---------
 1 file changed, 17 insertions(+), 15 deletions(-)

diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 05d035dc697936b..b38ddd075c2fd48 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1039,29 +1039,31 @@ MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock(
   // name, or a unique ID for the section.
   SmallString<128> Name;
   StringRef FunctionSectionName = MBB.getParent()->getSection()->getName();
-  if (!FunctionSectionName.equals(".text") && !FunctionSectionName.startswith(".text.")) {
-    // If the original function has a custom non-dot-text section, then split the cold part into that section too, but with a unique id.
+  if (!FunctionSectionName.equals(".text") &&
+      !FunctionSectionName.startswith(".text.")) {
+    // If the original function has a custom non-dot-text section, then split
+    // the cold part into that section too, but with a unique id.
     Name = FunctionSectionName;
     UniqueID = NextUniqueID++;
   } else {
-  StringRef FunctionName = MBB.getParent()->getName();
-  if (MBB.getSectionID() == MBBSectionID::ColdSectionID){
+    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 {
-    Name += FunctionSectionName;
-    if (TM.getUniqueBasicBlockSectionNames()) {
-      if (!Name.endswith("."))
-        Name += ".";
-      Name += MBB.getSymbol()->getName();
+    } 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++;
+      }
     }
   }
-  }
 
   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
   std::string GroupName;

>From 1430099025b81a9a0f20f8e550e4bb00c45862d7 Mon Sep 17 00:00:00 2001
From: Rahman Lavaee <rahmanl at google.com>
Date: Tue, 19 Sep 2023 03:00:29 +0000
Subject: [PATCH 3/4] clang-format.

---
 llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index b38ddd075c2fd48..ecbeb335d1e02e8 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1041,8 +1041,8 @@ MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock(
   StringRef FunctionSectionName = MBB.getParent()->getSection()->getName();
   if (!FunctionSectionName.equals(".text") &&
       !FunctionSectionName.startswith(".text.")) {
-    // If the original function has a custom non-dot-text section, then split
-    // the cold part into that section too, but with a unique id.
+    // If the original function has a custom non-dot-text section, then emit
+    // all basic block sections into that section too, but with a unique id.
     Name = FunctionSectionName;
     UniqueID = NextUniqueID++;
   } else {

>From 87ffbe92b212b604ecd2a51caf9fdccdc13f99aa Mon Sep 17 00:00:00 2001
From: Rahman Lavaee <rahmanl at google.com>
Date: Tue, 19 Sep 2023 17:56:04 +0000
Subject: [PATCH 4/4] Address comment.

---
 llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index ecbeb335d1e02e8..6210e7fc128a30c 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1039,13 +1039,9 @@ MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock(
   // name, or a unique ID for the section.
   SmallString<128> Name;
   StringRef FunctionSectionName = MBB.getParent()->getSection()->getName();
-  if (!FunctionSectionName.equals(".text") &&
-      !FunctionSectionName.startswith(".text.")) {
-    // If the original function has a custom non-dot-text section, then emit
-    // all basic block sections into that section too, but with a unique id.
-    Name = FunctionSectionName;
-    UniqueID = NextUniqueID++;
-  } else {
+  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;
@@ -1063,6 +1059,11 @@ MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock(
         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;



More information about the llvm-commits mailing list