Skip to content

Commit ebde38a

Browse files
committed
[CodeLayout] cache-directed sort: limit max chain size
When linking an executable with a slightly larger executable, ld.lld --call-graph-profile-sort=cdsort can be very slow (see llvm#68638). ``` 4.6% 20.7Mi .text.hot 3.5% 15.9Mi .text 3.4% 15.2Mi .text.unknown ``` Add cl option `cds-max-chain-size`, which is similar to `ext-tsp-max-chain-size`, and set it to 128, to improve performance. In `ld.lld @response.txt --threads=4 --call-graph-profile-sort=cdsort --time-trace" builds, the "Total Sort sections" time is measured as follows: * -mllvm -cds-max-chain-size=64: 1.321813 * -mllvm -cds-max-chain-size=128: 2.030425 * -mllvm -cds-max-chain-size=256: 2.927684 * -mllvm -cds-max-chain-size=512: 5.493106 * unlimited: 9 minutes The rest part takes 6.8s.
1 parent a91a664 commit ebde38a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

llvm/lib/Transforms/Utils/CodeLayout.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ cl::opt<bool> ApplyExtTspWithoutProfile(
6262
"ext-tsp-apply-without-profile",
6363
cl::desc("Whether to apply ext-tsp placement for instances w/o profile"),
6464
cl::init(true), cl::Hidden);
65+
66+
namespace codelayout {
67+
cl::opt<unsigned>
68+
CDMaxChainSize("cdsort-max-chain-size", cl::Hidden, cl::init(128),
69+
cl::desc("The maximum size of a chain to create"));
70+
}
6571
} // namespace llvm
6672

6773
// Algorithm-specific params for Ext-TSP. The values are tuned for the best
@@ -1156,6 +1162,9 @@ class CDSortImpl {
11561162
// Ignore loop edges.
11571163
if (Edge->isSelfEdge())
11581164
continue;
1165+
if (Edge->srcChain()->numBlocks() + Edge->dstChain()->numBlocks() >
1166+
CDMaxChainSize)
1167+
continue;
11591168

11601169
// Compute the gain of merging the two chains.
11611170
MergeGainT Gain = getBestMergeGain(Edge);

llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "llvm/Transforms/Utils/CodeLayout.h"
2+
#include "llvm/Support/CommandLine.h"
23
#include "gmock/gmock.h"
34
#include "gtest/gtest.h"
45
#include <vector>
@@ -7,6 +8,10 @@ using namespace llvm;
78
using namespace llvm::codelayout;
89
using testing::ElementsAreArray;
910

11+
namespace llvm::codelayout {
12+
extern cl::opt<unsigned> CDMaxChainSize;
13+
}
14+
1015
namespace {
1116
TEST(CodeLayout, ThreeFunctions) {
1217
// Place the most likely successor (2) first.
@@ -40,6 +45,14 @@ TEST(CodeLayout, HotChain) {
4045
const std::vector<uint64_t> CallOffsets(std::size(Edges), 5);
4146
auto Order = computeCacheDirectedLayout(Sizes, Counts, Edges, CallOffsets);
4247
EXPECT_THAT(Order, ElementsAreArray({0, 3, 4, 2, 1}));
48+
49+
// -cdsort-max-chain-size disables forming a larger chain and therefore may
50+
// change the result.
51+
unsigned Saved = CDMaxChainSize;
52+
CDMaxChainSize.setValue(3);
53+
Order = computeCacheDirectedLayout(Sizes, Counts, Edges, CallOffsets);
54+
EXPECT_THAT(Order, ElementsAreArray({0, 3, 4, 1, 2}));
55+
CDMaxChainSize.setValue(Saved);
4356
}
4457
}
4558

0 commit comments

Comments
 (0)