[llvm] e705b37 - [CodeLayout] Add unittest for cache-directed sort
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 27 10:52:17 PDT 2023
Author: Fangrui Song
Date: 2023-09-27T10:52:12-07:00
New Revision: e705b37a77c635a2cb7c9f5933805a82c6892e8c
URL: https://github.com/llvm/llvm-project/commit/e705b37a77c635a2cb7c9f5933805a82c6892e8c
DIFF: https://github.com/llvm/llvm-project/commit/e705b37a77c635a2cb7c9f5933805a82c6892e8c.diff
LOG: [CodeLayout] Add unittest for cache-directed sort
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.
Depends on D159526
Differential Revision: https://reviews.llvm.org/D159527
Added:
llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp
Modified:
llvm/test/CodeGen/X86/code_placement_ext_tsp.ll
llvm/unittests/Transforms/Utils/CMakeLists.txt
Removed:
################################################################################
diff --git a/llvm/test/CodeGen/X86/code_placement_ext_tsp.ll b/llvm/test/CodeGen/X86/code_placement_ext_tsp.ll
index a5c2da8c6e975b2..4053b8a8e123b1c 100644
--- a/llvm/test/CodeGen/X86/code_placement_ext_tsp.ll
+++ b/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
diff --git a/llvm/unittests/Transforms/Utils/CMakeLists.txt b/llvm/unittests/Transforms/Utils/CMakeLists.txt
index 751de6fc5becc9d..d1714a7532d5ff4 100644
--- a/llvm/unittests/Transforms/Utils/CMakeLists.txt
+++ b/llvm/unittests/Transforms/Utils/CMakeLists.txt
@@ -15,6 +15,7 @@ add_llvm_unittest(UtilsTests
CallPromotionUtilsTest.cpp
CloningTest.cpp
CodeExtractorTest.cpp
+ CodeLayoutTest.cpp
CodeMoverUtilsTest.cpp
DebugifyTest.cpp
FunctionComparatorTest.cpp
diff --git a/llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp b/llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp
new file mode 100644
index 000000000000000..ce42f703229bd01
--- /dev/null
+++ b/llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp
@@ -0,0 +1,63 @@
+#include "llvm/Transforms/Utils/CodeLayout.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include <vector>
+
+using namespace llvm;
+using namespace llvm::codelayout;
+using testing::ElementsAreArray;
+
+namespace {
+TEST(CodeLayout, ThreeFunctions) {
+ // Place the most likely successor (2) first.
+ {
+ const uint64_t Counts[3] = {140, 40, 140};
+ const std::vector<uint64_t> Sizes(std::size(Counts), 9);
+ const EdgeCount 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 uint64_t Counts[3] = {180, 80, 180};
+ const std::vector<uint64_t> Sizes(std::size(Counts), 9);
+ const EdgeCount 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 uint64_t Counts[5] = {22, 7, 22, 15, 46};
+ const std::vector<uint64_t> Sizes(std::size(Counts), 9);
+ const EdgeCount 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.
+ const uint64_t Counts[6] = {177, 371, 196, 124, 70, 177};
+ std::vector<uint64_t> Sizes(std::size(Counts), 9);
+ const EdgeCount 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 move node 4 closer to the
+ // (1,2,3) loop.
+ Sizes[0] = 18;
+ Order = computeCacheDirectedLayout(Sizes, Counts, Edges, CallOffsets);
+ EXPECT_THAT(Order, ElementsAreArray({0, 4, 1, 2, 3, 5}));
+}
+} // namespace
More information about the llvm-commits
mailing list