[llvm] [AsmPrinter][ELF] Support profile-guided section prefix for jump tables' (read-only) data sections (PR #122215)

Snehasish Kumar via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 24 10:24:56 PST 2025


================
@@ -2876,42 +2875,111 @@ void AsmPrinter::emitJumpTableInfo() {
       MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference32 ||
           MJTI->getEntryKind() == MachineJumpTableInfo::EK_LabelDifference64,
       F);
+
+  std::vector<unsigned> JumpTableIndices;
+  if (!TM.Options.EnableStaticDataPartitioning) {
+    for (unsigned JTI = 0, JTSize = JT.size(); JTI < JTSize; ++JTI)
+      JumpTableIndices.push_back(JTI);
+    emitJumpTableImpl(
+        *MJTI,
+        llvm::make_range(JumpTableIndices.begin(), JumpTableIndices.end()),
+        JTInDiffSection);
+    return;
+  }
+
+  // When static data partitioning is enabled, collect jump table entries that
+  // go into the same section together to reduce the amount of section switch
+  // statements.
+  //
+  // Iterate all jump tables, put hot jump table indices towards the beginning
+  // of the vector, and cold jump table indices towards the end. Meanwhile
+  // retain the relative orders of original jump tables.
+  int NumHotJumpTables = 0, NextColdJumpTableIndex = JT.size() - 1;
+  JumpTableIndices.resize(JT.size());
+  for (unsigned JTI = 0, JTSize = JT.size(); JTI < JTSize; ++JTI) {
+    if (JT[JTI].Hotness == MachineFunctionDataHotness::Cold) {
+      JumpTableIndices[NextColdJumpTableIndex--] = JTI;
+    } else {
+      JumpTableIndices[NumHotJumpTables++] = JTI;
+    }
+  }
+
+  emitJumpTableImpl(
+      *MJTI,
+      llvm::make_range(JumpTableIndices.begin(),
+                       JumpTableIndices.begin() + NumHotJumpTables),
+
+      JTInDiffSection);
+
+  const int NumColdJumpTables = JT.size() - NumHotJumpTables;
+  assert(NumColdJumpTables >= 0 && "Invalid number of cold jump tables.");
+
+  // Reverse iterating cold jump table indices to emit in the original order.
+  emitJumpTableImpl(
+      *MJTI,
+      llvm::make_range(JumpTableIndices.rbegin(),
+                       JumpTableIndices.rbegin() + NumColdJumpTables),
+      JTInDiffSection);
+
+  return;
----------------
snehasish wrote:

Don't need this return?

https://github.com/llvm/llvm-project/pull/122215


More information about the llvm-commits mailing list