[PATCH] D153039: [BOLT] A new code layout algorithm for function reordering [3b/3]

Sergey Pupyrev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 31 10:49:37 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rGb402487b7445: [BOLT] A new code layout algorithm for function reordering [3b/3] (authored by spupyrev).

Changed prior to commit:
  https://reviews.llvm.org/D153039?vs=544952&id=545740#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153039/new/

https://reviews.llvm.org/D153039

Files:
  bolt/include/bolt/Passes/ReorderFunctions.h
  bolt/lib/Passes/ReorderFunctions.cpp


Index: bolt/lib/Passes/ReorderFunctions.cpp
===================================================================
--- bolt/lib/Passes/ReorderFunctions.cpp
+++ bolt/lib/Passes/ReorderFunctions.cpp
@@ -15,6 +15,7 @@
 #include "bolt/Utils/Utils.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Transforms/Utils/CodeLayout.h"
 #include <fstream>
 
 #define DEBUG_TYPE "hfsort"
@@ -41,6 +42,8 @@
                           "use hfsort algorithm"),
                clEnumValN(bolt::ReorderFunctions::RT_HFSORT_PLUS, "hfsort+",
                           "use hfsort+ algorithm"),
+               clEnumValN(bolt::ReorderFunctions::RT_CDS, "cds",
+                          "use cache-directed sort"),
                clEnumValN(bolt::ReorderFunctions::RT_PETTIS_HANSEN,
                           "pettis-hansen", "use Pettis-Hansen algorithm"),
                clEnumValN(bolt::ReorderFunctions::RT_RANDOM, "random",
@@ -309,6 +312,36 @@
   case RT_HFSORT_PLUS:
     Clusters = hfsortPlus(Cg);
     break;
+  case RT_CDS: {
+    // It is required that the sum of incoming arc weights is not greater
+    // than the number of samples for every function. Ensuring the call graph
+    // obeys the property before running the algorithm.
+    Cg.adjustArcWeights();
+
+    // Initialize CFG nodes and their data
+    std::vector<uint64_t> FuncSizes;
+    std::vector<uint64_t> FuncCounts;
+    using JumpT = std::pair<uint64_t, uint64_t>;
+    std::vector<std::pair<JumpT, uint64_t>> CallCounts;
+    std::vector<uint64_t> CallOffsets;
+    for (NodeId F = 0; F < Cg.numNodes(); ++F) {
+      FuncSizes.push_back(Cg.size(F));
+      FuncCounts.push_back(Cg.samples(F));
+      for (NodeId Succ : Cg.successors(F)) {
+        const Arc &Arc = *Cg.findArc(F, Succ);
+        auto It = std::make_pair(F, Succ);
+        CallCounts.push_back(std::make_pair(It, Arc.weight()));
+        CallOffsets.push_back(uint64_t(Arc.avgCallOffset()));
+      }
+    }
+
+    // Run the layout algorithm.
+    std::vector<uint64_t> Result =
+        applyCDSLayout(FuncSizes, FuncCounts, CallCounts, CallOffsets);
+
+    // Create a single cluster from the computed order of hot functions.
+    Clusters.emplace_back(Cluster(Result, Cg));
+  } break;
   case RT_PETTIS_HANSEN:
     Clusters = pettisAndHansen(Cg);
     break;
Index: bolt/include/bolt/Passes/ReorderFunctions.h
===================================================================
--- bolt/include/bolt/Passes/ReorderFunctions.h
+++ bolt/include/bolt/Passes/ReorderFunctions.h
@@ -32,6 +32,7 @@
     RT_EXEC_COUNT,
     RT_HFSORT,
     RT_HFSORT_PLUS,
+    RT_CDS,
     RT_PETTIS_HANSEN,
     RT_RANDOM,
     RT_USER


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153039.545740.patch
Type: text/x-patch
Size: 2696 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230731/d0ba77e9/attachment.bin>


More information about the llvm-commits mailing list