Skip to content

Commit a244183

Browse files
authored
[CodeLayout] cache-directed sort: limit max chain size (#69039)
When linking an executable with a slightly larger executable, ld.lld --call-graph-profile-sort=cdsort can be very slow (see #68638). ``` 4.6% 20.7Mi .text.hot 3.5% 15.9Mi .text 3.4% 15.2Mi .text.unknown ``` Add cl option `cdsort-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 -cdsort-max-chain-size=64: 1.321813 * -mllvm -cdsort-max-chain-size=128: 2.030425 * -mllvm -cdsort-max-chain-size=256: 2.927684 * -mllvm -cdsort-max-chain-size=512: 5.493106 * unlimited: 9 minutes The rest part takes 6.8s.
1 parent 8511ade commit a244183

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

llvm/include/llvm/Transforms/Utils/CodeLayout.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ struct CDSortConfig {
6565
unsigned CacheEntries = 16;
6666
/// The size of a line in the cache.
6767
unsigned CacheSize = 2048;
68+
/// The maximum size of a chain to create.
69+
unsigned MaxChainSize = 128;
6870
/// The power exponent for the distance-based locality.
6971
double DistancePower = 0.25;
7072
/// The scale factor for the frequency-based locality.

llvm/lib/Transforms/Utils/CodeLayout.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ static cl::opt<unsigned> CacheEntries("cds-cache-entries", cl::ReallyHidden,
123123
static cl::opt<unsigned> CacheSize("cds-cache-size", cl::ReallyHidden,
124124
cl::desc("The size of a line in the cache"));
125125

126+
static cl::opt<unsigned>
127+
CDMaxChainSize("cdsort-max-chain-size", cl::ReallyHidden,
128+
cl::desc("The maximum size of a chain to create"));
129+
126130
static cl::opt<double> DistancePower(
127131
"cds-distance-power", cl::ReallyHidden,
128132
cl::desc("The power exponent for the distance-based locality"));
@@ -1156,6 +1160,9 @@ class CDSortImpl {
11561160
// Ignore loop edges.
11571161
if (Edge->isSelfEdge())
11581162
continue;
1163+
if (Edge->srcChain()->numBlocks() + Edge->dstChain()->numBlocks() >
1164+
Config.MaxChainSize)
1165+
continue;
11591166

11601167
// Compute the gain of merging the two chains.
11611168
MergeGainT Gain = getBestMergeGain(Edge);
@@ -1452,6 +1459,8 @@ std::vector<uint64_t> codelayout::computeCacheDirectedLayout(
14521459
Config.CacheEntries = CacheEntries;
14531460
if (CacheSize.getNumOccurrences() > 0)
14541461
Config.CacheSize = CacheSize;
1462+
if (CDMaxChainSize.getNumOccurrences() > 0)
1463+
Config.MaxChainSize = CDMaxChainSize;
14551464
if (DistancePower.getNumOccurrences() > 0)
14561465
Config.DistancePower = DistancePower;
14571466
if (FrequencyScale.getNumOccurrences() > 0)

llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ TEST(CodeLayout, HotChain) {
4040
const std::vector<uint64_t> CallOffsets(std::size(Edges), 5);
4141
auto Order = computeCacheDirectedLayout(Sizes, Counts, Edges, CallOffsets);
4242
EXPECT_THAT(Order, ElementsAreArray({0, 3, 4, 2, 1}));
43+
44+
// -cdsort-max-chain-size disables forming a larger chain and therefore may
45+
// change the result.
46+
CDSortConfig Config;
47+
Config.MaxChainSize = 3;
48+
Order =
49+
computeCacheDirectedLayout(Config, Sizes, Counts, Edges, CallOffsets);
50+
EXPECT_THAT(Order, ElementsAreArray({0, 3, 4, 1, 2}));
4351
}
4452
}
4553

0 commit comments

Comments
 (0)