[PATCH] D159527: [CodeLayout] Add unittest for cache-directed sort

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 17 16:09:37 PDT 2023


MaskRay created this revision.
MaskRay added reviewers: spupyrev, wenlei, hoy, wlei, rahmanl.
Herald added a subscriber: pengfei.
Herald added a project: All.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The function reordering algorithm added by https://reviews.llvm.org/D152834 and
used by BOLT (https://reviews.llvm.org/D153039) is untested.

Add some tests at the appropriate layer.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159527

Files:
  llvm/test/CodeGen/X86/code_placement_ext_tsp.ll
  llvm/unittests/Transforms/Utils/CMakeLists.txt
  llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp


Index: llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp
===================================================================
--- /dev/null
+++ llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp
@@ -0,0 +1,62 @@
+#include "llvm/Transforms/Utils/CodeLayout.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include <vector>
+
+using namespace llvm;
+using testing::ElementsAreArray;
+
+namespace {
+TEST(CodeLayout, ThreeFunctions) {
+  // Place the most likely successor (2) first.
+  {
+    const std::vector<uint64_t> Sizes(3, 9);
+    const uint64_t Counts[3] = {140, 40, 140};
+    const EdgeCountT Edges[] = {{{0, 1}, 40}, {{0, 2}, 100}, {{1, 2}, 40}};
+    const std::vector<uint64_t> CallOffsets(std::size(Edges), 5);
+    auto Order = computeCacheDirectedLayout(Sizes, Counts, Edges, CallOffsets);
+    EXPECT_THAT(Order, ElementsAreArray({0, 2, 1}));
+  }
+
+  // Prefer fallthroughs even in the presence of a heavy successor.
+  {
+    const std::vector<uint64_t> Sizes(3, 9);
+    const uint64_t Counts[3] = {180, 80, 180};
+    const EdgeCountT Edges[] = {{{0, 1}, 80}, {{0, 2}, 100}, {{1, 2}, 80}};
+    const uint64_t CallOffsets[] = {9, 5, 9};
+    auto Order = computeCacheDirectedLayout(Sizes, Counts, Edges, CallOffsets);
+    EXPECT_THAT(Order, ElementsAreArray({0, 1, 2}));
+  }
+}
+
+TEST(CodeLayout, HotChain) {
+  // Place the hot chain (0,3,4,2) continuously.
+  {
+    const std::vector<uint64_t> Sizes(5, 9);
+    const uint64_t Counts[5] = {22, 7, 22, 15, 46};
+    const EdgeCountT Edges[] = {{{0, 1}, 7},  {{1, 2}, 7},  {{0, 3}, 15},
+                                {{3, 4}, 15}, {{4, 4}, 31}, {{4, 2}, 15}};
+    const std::vector<uint64_t> CallOffsets(std::size(Edges), 5);
+    auto Order = computeCacheDirectedLayout(Sizes, Counts, Edges, CallOffsets);
+    EXPECT_THAT(Order, ElementsAreArray({0, 3, 4, 2, 1}));
+  }
+}
+
+TEST(CodeLayout, BreakLoop) {
+  // There are two loops (1,2,3) and (1,2,4). It is beneficial to place 4
+  // elsewhere.
+  std::vector<uint64_t> Sizes(6, 9);
+  const uint64_t Counts[6] = {177, 371, 196, 124, 70, 177};
+  const EdgeCountT Edges[] = {{{0, 1}, 177}, {{1, 2}, 196}, {{2, 3}, 124},
+                              {{3, 1}, 124}, {{1, 5}, 177}, {{2, 4}, 79},
+                              {{4, 1}, 70}};
+  const std::vector<uint64_t> CallOffsets(std::size(Edges), 5);
+  auto Order = computeCacheDirectedLayout(Sizes, Counts, Edges, CallOffsets);
+  EXPECT_THAT(Order, ElementsAreArray({4, 0, 1, 2, 3, 5}));
+
+  // When node 0 is larger, it is beneficial to swap 0 and 4.
+  Sizes[0] = 18;
+  Order = computeCacheDirectedLayout(Sizes, Counts, Edges, CallOffsets);
+  EXPECT_THAT(Order, ElementsAreArray({0, 4, 1, 2, 3, 5}));
+}
+} // namespace
Index: llvm/unittests/Transforms/Utils/CMakeLists.txt
===================================================================
--- llvm/unittests/Transforms/Utils/CMakeLists.txt
+++ llvm/unittests/Transforms/Utils/CMakeLists.txt
@@ -15,6 +15,7 @@
   CallPromotionUtilsTest.cpp
   CloningTest.cpp
   CodeExtractorTest.cpp
+  CodeLayoutTest.cpp
   CodeMoverUtilsTest.cpp
   DebugifyTest.cpp
   FunctionComparatorTest.cpp
Index: llvm/test/CodeGen/X86/code_placement_ext_tsp.ll
===================================================================
--- llvm/test/CodeGen/X86/code_placement_ext_tsp.ll
+++ llvm/test/CodeGen/X86/code_placement_ext_tsp.ll
@@ -1,3 +1,4 @@
+;; See also llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp
 ; RUN: llc -mcpu=corei7 -mtriple=x86_64-linux -enable-ext-tsp-block-placement=1 < %s | FileCheck %s
 ; RUN: llc -mcpu=corei7 -mtriple=x86_64-linux -enable-ext-tsp-block-placement=1 -ext-tsp-chain-split-threshold=0 -ext-tsp-enable-chain-split-along-jumps=0 < %s | FileCheck %s -check-prefix=CHECK2
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159527.556918.patch
Type: text/x-patch
Size: 3746 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230917/116848a1/attachment.bin>


More information about the llvm-commits mailing list