diff --git a/llvm/include/llvm/Transforms/Utils/CodeLayout.h b/llvm/include/llvm/Transforms/Utils/CodeLayout.h index f5127cff24af0..9d550fae6dd06 100644 --- a/llvm/include/llvm/Transforms/Utils/CodeLayout.h +++ b/llvm/include/llvm/Transforms/Utils/CodeLayout.h @@ -65,6 +65,8 @@ struct CDSortConfig { unsigned CacheEntries = 16; /// The size of a line in the cache. unsigned CacheSize = 2048; + /// The maximum size of a chain to create. + unsigned MaxChainSize = 128; /// The power exponent for the distance-based locality. double DistancePower = 0.25; /// The scale factor for the frequency-based locality. diff --git a/llvm/lib/Transforms/Utils/CodeLayout.cpp b/llvm/lib/Transforms/Utils/CodeLayout.cpp index 6252c429205ab..a6c9d2ac6cf2f 100644 --- a/llvm/lib/Transforms/Utils/CodeLayout.cpp +++ b/llvm/lib/Transforms/Utils/CodeLayout.cpp @@ -123,6 +123,10 @@ static cl::opt CacheEntries("cds-cache-entries", cl::ReallyHidden, static cl::opt CacheSize("cds-cache-size", cl::ReallyHidden, cl::desc("The size of a line in the cache")); +static cl::opt + CDMaxChainSize("cdsort-max-chain-size", cl::ReallyHidden, + cl::desc("The maximum size of a chain to create")); + static cl::opt DistancePower( "cds-distance-power", cl::ReallyHidden, cl::desc("The power exponent for the distance-based locality")); @@ -1156,6 +1160,9 @@ class CDSortImpl { // Ignore loop edges. if (Edge->isSelfEdge()) continue; + if (Edge->srcChain()->numBlocks() + Edge->dstChain()->numBlocks() > + Config.MaxChainSize) + continue; // Compute the gain of merging the two chains. MergeGainT Gain = getBestMergeGain(Edge); @@ -1452,6 +1459,8 @@ std::vector codelayout::computeCacheDirectedLayout( Config.CacheEntries = CacheEntries; if (CacheSize.getNumOccurrences() > 0) Config.CacheSize = CacheSize; + if (CDMaxChainSize.getNumOccurrences() > 0) + Config.MaxChainSize = CDMaxChainSize; if (DistancePower.getNumOccurrences() > 0) Config.DistancePower = DistancePower; if (FrequencyScale.getNumOccurrences() > 0) diff --git a/llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp b/llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp index ce42f703229bd..ef9aa9a76342f 100644 --- a/llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp +++ b/llvm/unittests/Transforms/Utils/CodeLayoutTest.cpp @@ -40,6 +40,14 @@ TEST(CodeLayout, HotChain) { const std::vector CallOffsets(std::size(Edges), 5); auto Order = computeCacheDirectedLayout(Sizes, Counts, Edges, CallOffsets); EXPECT_THAT(Order, ElementsAreArray({0, 3, 4, 2, 1})); + + // -cdsort-max-chain-size disables forming a larger chain and therefore may + // change the result. + CDSortConfig Config; + Config.MaxChainSize = 3; + Order = + computeCacheDirectedLayout(Config, Sizes, Counts, Edges, CallOffsets); + EXPECT_THAT(Order, ElementsAreArray({0, 3, 4, 1, 2})); } }