[llvm] d2696de - [llvm] Add -bbsections-cold-text-prefix to emit cold clusters to a different section.

Snehasish Kumar via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 24 15:30:22 PDT 2020


Author: Snehasish Kumar
Date: 2020-09-24T15:26:15-07:00
New Revision: d2696dec45cdcff791cd53b8b8288d33a0d8dddb

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

LOG: [llvm] Add -bbsections-cold-text-prefix to emit cold clusters to a different section.

This change adds an option to basic block sections to allow cold
clusters to be assigned a custom text prefix. With a custom prefix such
as ".text.split." (D87840), lld can place them in a separate output section.
The benefits are -

* Empirically shown to improve icache and itlb metrics by 3-5%
(absolute) compared to placing split parts in .text.unlikely.
* Mitigates against poor profiles, eg samplePGO profiles used with the
machine function splitter. Optimizations such as hugepage remapping can
make different decisions at the section granularity.
* Enables section granularity hotness monitoring (checking on the
decisions made during compilation vs sample data from production).

Differential Revision: https://reviews.llvm.org/D87813

Added: 
    

Modified: 
    llvm/include/llvm/CodeGen/BasicBlockSectionUtils.h
    llvm/lib/CodeGen/BasicBlockSections.cpp
    llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
    llvm/test/CodeGen/X86/basic-block-sections-cold.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/BasicBlockSectionUtils.h b/llvm/include/llvm/CodeGen/BasicBlockSectionUtils.h
index 52a8617cd879..d8da3be0cd4c 100644
--- a/llvm/include/llvm/CodeGen/BasicBlockSectionUtils.h
+++ b/llvm/include/llvm/CodeGen/BasicBlockSectionUtils.h
@@ -10,9 +10,12 @@
 #define LLVM_CODEGEN_BASICBLOCKSECTIONUTILS_H
 
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/CommandLine.h"
 
 namespace llvm {
 
+extern cl::opt<std::string> BBSectionsColdTextPrefix;
+
 class MachineFunction;
 class MachineBasicBlock;
 

diff  --git a/llvm/lib/CodeGen/BasicBlockSections.cpp b/llvm/lib/CodeGen/BasicBlockSections.cpp
index 421c1d896a0f..9692e9b9f091 100644
--- a/llvm/lib/CodeGen/BasicBlockSections.cpp
+++ b/llvm/lib/CodeGen/BasicBlockSections.cpp
@@ -79,6 +79,15 @@ using llvm::StringMap;
 using llvm::StringRef;
 using namespace llvm;
 
+// Placing the cold clusters in a separate section mitigates against poor
+// profiles and allows optimizations such as hugepage mapping to be applied at a
+// section granularity. Where necessary, users should set this to ".text.split."
+// which is recognized by lld via the `-z keep-text-section-prefix` flag.
+cl::opt<std::string> llvm::BBSectionsColdTextPrefix(
+    "bbsections-cold-text-prefix",
+    cl::desc("The text prefix to use for cold basic block clusters"),
+    cl::init(".text.unlikely."), cl::Hidden);
+
 namespace {
 
 // This struct represents the cluster information for a machine basic block.

diff  --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 0c43ec842371..84fe9df6aef9 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -21,6 +21,7 @@
 #include "llvm/BinaryFormat/Dwarf.h"
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/BinaryFormat/MachO.h"
+#include "llvm/CodeGen/BasicBlockSectionUtils.h"
 #include "llvm/CodeGen/MachineBasicBlock.h"
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineModuleInfo.h"
@@ -833,7 +834,7 @@ MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock(
   // name, or a unique ID for the section.
   SmallString<128> Name;
   if (MBB.getSectionID() == MBBSectionID::ColdSectionID) {
-    Name += ".text.unlikely.";
+    Name += BBSectionsColdTextPrefix;
     Name += MBB.getParent()->getName();
   } else if (MBB.getSectionID() == MBBSectionID::ExceptionSectionID) {
     Name += ".text.eh.";

diff  --git a/llvm/test/CodeGen/X86/basic-block-sections-cold.ll b/llvm/test/CodeGen/X86/basic-block-sections-cold.ll
index 432c0952a4a0..d9d456ad90dd 100644
--- a/llvm/test/CodeGen/X86/basic-block-sections-cold.ll
+++ b/llvm/test/CodeGen/X86/basic-block-sections-cold.ll
@@ -2,25 +2,21 @@
 ; Basic block with id 1 and 2 must be in the cold section.
 ; RUN: echo '!_Z3bazb' > %t
 ; RUN: echo '!!0' >> %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 -function-sections -basic-block-sections=%t -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS
+; RUN: llc < %s -mtriple=x86_64 -function-sections -basic-block-sections=%t -unique-basic-block-section-names -bbsections-cold-text-prefix=".text.split." | FileCheck %s -check-prefix=LINUX-SPLIT
 
-define void @_Z3bazb(i1 zeroext) nounwind {
-  %2 = alloca i8, align 1
-  %3 = zext i1 %0 to i8
-  store i8 %3, i8* %2, align 1
-  %4 = load i8, i8* %2, align 1
-  %5 = trunc i8 %4 to i1
-  br i1 %5, label %6, label %8
+define void @_Z3bazb(i1 zeroext %0) nounwind {
+  br i1 %0, label %2, label %4
 
-6:                                                ; preds = %1
-  %7 = call i32 @_Z3barv()
-  br label %10
+2:                                                ; preds = %1
+  %3 = call i32 @_Z3barv()
+  br label %6
 
-8:                                                ; preds = %1
-  %9 = call i32 @_Z3foov()
-  br label %10
+4:                                                ; preds = %1
+  %5 = call i32 @_Z3foov()
+  br label %6
 
-10:                                               ; preds = %8, %6
+6:                                                ; preds = %2, %4
   ret void
 }
 
@@ -38,3 +34,9 @@ declare i32 @_Z3foov() #1
 ; LINUX-SECTIONS-NOT: .section        .text._Z3bazb._Z3bazb.2,"ax", at progbits,unique
 ; LINUX-SECTIONS: .LBB0_2:
 ; LINUX-SECTIONS: .size   _Z3bazb, .Lfunc_end{{[0-9]}}-_Z3bazb
+
+; LINUX-SPLIT:      .section	.text.split._Z3bazb,"ax", at progbits
+; LINUX-SPLIT-NEXT: _Z3bazb.cold:
+; LINUX-SPLIT-NEXT:   callq _Z3barv
+; LINUX-SPLIT:      .LBB0_2:
+; LINUX-SPLIT:      .LBB_END0_2:


        


More information about the llvm-commits mailing list