[PATCH] D152941: [BOLT] Fixing relative ordering of cold sections under multi-way function splitting

Shatian Wang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 14 11:33:54 PDT 2023


shatianw_mt created this revision.
Herald added subscribers: treapster, ayermolo, kristof.beyls.
Herald added a reviewer: rafauler.
Herald added a reviewer: Amir.
Herald added a reviewer: maksfb.
Herald added a project: All.
shatianw_mt requested review of this revision.
Herald added subscribers: llvm-commits, yota9.
Herald added a project: LLVM.

Order code sections with names in the form of ".text.cold.i" based on the value of i

[Context] SplitFunctions.cpp implements splitting strategies that can potentially split each function into maximum N>2 fragments. 
When such N-way splitting happens, new code sections with names ".text.cold.1", ..., ".text.cold.i", ... "text.cold.N-2" will be created 
A section with name ".text.cold.i" contains the the (i+2)th fragment of each function. 
As an example, if each function is splitted into N=3 fragments: hot, warm, cold, then code sections will now include

- a section with name ".text" containing hot fragments
- a section with name ".text.cold" containing warm fragments
- a section with name ".text.cold.1" containing cold fragments

The order of these new sections in the output binary currently depends on the order in which they are encountered by the emitter.
For example, under N=3-way splitting, if the first function is 2-way splitted into hot and cold and the second function is 3-way splitted into hot, warm, and cold
then the cold fragment is encountered first, resulting in the final section to be in the following order
.text (hot), .text.cold.1 (cold), .text.cold (warm)

The above is suboptimal because the distance of jumps/calls between the hot and the warm sections will be much bigger than when ordering the sections as follows
.text (hot), .text.cold (warm), .text.cold.1 (cold)

This diff orders the sections with names in the form of ".text.cold" or ".text.cold.i" based on the value of i (assuming the i-value of ".text.cold" is 0).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152941

Files:
  bolt/lib/Rewrite/RewriteInstance.cpp


Index: bolt/lib/Rewrite/RewriteInstance.cpp
===================================================================
--- bolt/lib/Rewrite/RewriteInstance.cpp
+++ bolt/lib/Rewrite/RewriteInstance.cpp
@@ -3906,6 +3906,21 @@
       CodeSections.emplace_back(&Section);
 
   auto compareSections = [&](const BinarySection *A, const BinarySection *B) {
+
+    // If both A and B have names starting with ".text.cold", then
+    // - if opts::HotFunctionsAtEnd is true, we want order
+    //   ".text.cold.T", ".text.cold.T-1", ... ".text.cold.1", ".text.cold"
+    // - if opts::HotFunctionsAtEnd is false, we want order
+    //   ".text.cold", ".text.cold.1", ... ".text.cold.T-1", ".text.cold.T"
+    if (A->getName().startswith(BC->getColdCodeSectionName()) &&
+        B->getName().startswith(BC->getColdCodeSectionName()))
+      if (A->getName().size() != B->getName().size())
+        return (opts::HotFunctionsAtEnd)
+                   ? (A->getName().size() > B->getName().size())
+                   : (A->getName().size() < B->getName().size());
+    return (opts::HotFunctionsAtEnd) ? (A->getName() > B->getName())
+                                     : (A->getName() < B->getName());
+
     // Place movers before anything else.
     if (A->getName() == BC->getHotTextMoverSectionName())
       return true;
@@ -3921,6 +3936,9 @@
 
   // Determine the order of sections.
   llvm::stable_sort(CodeSections, compareSections);
+  for (BinarySection *Section : CodeSections) {
+    outs() << "Shatian DDebug: " << Section->getName() << '\n';
+  }
 
   return CodeSections;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152941.531440.patch
Type: text/x-patch
Size: 1577 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230614/ea3f6e0c/attachment.bin>


More information about the llvm-commits mailing list