#include "llvm/Transforms/Utils/CodeLayout.h"
#include "llvm/Support/CommandLine.h"
using namespace llvm;
#define DEBUG_TYPE "code-layout"
cl::opt<bool> EnableExtTspBlockPlacement(
"enable-ext-tsp-block-placement", cl::Hidden, cl::init(false),
cl::desc("Enable machine block placement based on the ext-tsp model, "
"optimizing I-cache utilization."));
cl::opt<bool> ApplyExtTspWithoutProfile(
"ext-tsp-apply-without-profile",
cl::desc("Whether to apply ext-tsp placement for instances w/o profile"),
cl::init(true), cl::Hidden);
static cl::opt<double>
ForwardWeight("ext-tsp-forward-weight", cl::Hidden, cl::init(0.1),
cl::desc("The weight of forward jumps for ExtTSP value"));
static cl::opt<double>
BackwardWeight("ext-tsp-backward-weight", cl::Hidden, cl::init(0.1),
cl::desc("The weight of backward jumps for ExtTSP value"));
static cl::opt<unsigned> ForwardDistance(
"ext-tsp-forward-distance", cl::Hidden, cl::init(1024),
cl::desc("The maximum distance (in bytes) of a forward jump for ExtTSP"));
static cl::opt<unsigned> BackwardDistance(
"ext-tsp-backward-distance", cl::Hidden, cl::init(640),
cl::desc("The maximum distance (in bytes) of a backward jump for ExtTSP"));
static cl::opt<unsigned>
MaxChainSize("ext-tsp-max-chain-size", cl::Hidden, cl::init(4096),
cl::desc("The maximum size of a chain to create."));
static cl::opt<unsigned> ChainSplitThreshold(
"ext-tsp-chain-split-threshold", cl::Hidden, cl::init(128),
cl::desc("The maximum size of a chain to apply splitting"));
static cl::opt<bool> EnableChainSplitAlongJumps(
"ext-tsp-enable-chain-split-along-jumps", cl::Hidden, cl::init(true),
cl::desc("The maximum size of a chain to apply splitting"));
namespace {
constexpr double EPS = 1e-8;
double extTSPScore(uint64_t SrcAddr, uint64_t SrcSize, uint64_t DstAddr,
uint64_t Count) {
if (SrcAddr + SrcSize == DstAddr) {
return static_cast<double>(Count);
}
if (SrcAddr + SrcSize < DstAddr) {
const auto Dist = DstAddr - (SrcAddr + SrcSize);
if (Dist <= ForwardDistance) {
double Prob = 1.0 - static_cast<double>(Dist) / ForwardDistance;
return ForwardWeight * Prob * Count;
}
return 0;
}
const auto Dist = SrcAddr + SrcSize - DstAddr;
if (Dist <= BackwardDistance) {
double Prob = 1.0 - static_cast<double>(Dist) / BackwardDistance;
return BackwardWeight * Prob * Count;
}
return 0;
}
enum class MergeTypeTy : int { X_Y, X1_Y_X2, Y_X2_X1, X2_X1_Y };
class MergeGainTy {
public:
explicit MergeGainTy() = default;
explicit MergeGainTy(double Score, size_t MergeOffset, MergeTypeTy MergeType)
: Score(Score), MergeOffset(MergeOffset), MergeType(MergeType) {}
double score() const { return Score; }
size_t mergeOffset() const { return MergeOffset; }
MergeTypeTy mergeType() const { return MergeType; }
bool operator<(const MergeGainTy &Other) const {
return (Other.Score > EPS && Other.Score > Score + EPS);
}
void updateIfLessThan(const MergeGainTy &Other) {
if (*this < Other)
*this = Other;
}
private:
double Score{-1.0};
size_t MergeOffset{0};
MergeTypeTy MergeType{MergeTypeTy::X_Y};
};
class Jump;
class Chain;
class ChainEdge;
class Block {
public:
Block(const Block &) = delete;
Block(Block &&) = default;
Block &operator=(const Block &) = delete;
Block &operator=(Block &&) = default;
size_t Index{0};
size_t CurIndex{0};
uint64_t Size{0};
uint64_t ExecutionCount{0};
Chain *CurChain{nullptr};
mutable uint64_t EstimatedAddr{0};
Block *ForcedSucc{nullptr};
Block *ForcedPred{nullptr};
std::vector<Jump *> OutJumps;
std::vector<Jump *> InJumps;
public:
explicit Block(size_t Index, uint64_t Size_, uint64_t EC)
: Index(Index), Size(Size_), ExecutionCount(EC) {}
bool isEntry() const { return Index == 0; }
};
class Jump {
public:
Jump(const Jump &) = delete;
Jump(Jump &&) = default;
Jump &operator=(const Jump &) = delete;
Jump &operator=(Jump &&) = default;
Block *Source;
Block *Target;
uint64_t ExecutionCount{0};
public:
explicit Jump(Block *Source, Block *Target, uint64_t ExecutionCount)
: Source(Source), Target(Target), ExecutionCount(ExecutionCount) {}
};
class Chain {
public:
Chain(const Chain &) = delete;
Chain(Chain &&) = default;
Chain &operator=(const Chain &) = delete;
Chain &operator=(Chain &&) = default;
explicit Chain(uint64_t Id, Block *Block)
: Id(Id), Score(0), Blocks(1, Block) {}
uint64_t id() const { return Id; }
bool isEntry() const { return Blocks[0]->Index == 0; }
double score() const { return Score; }
void setScore(double NewScore) { Score = NewScore; }
const std::vector<Block *> &blocks() const { return Blocks; }
size_t numBlocks() const { return Blocks.size(); }
const std::vector<std::pair<Chain *, ChainEdge *>> &edges() const {
return Edges;
}
ChainEdge *getEdge(Chain *Other) const {
for (auto It : Edges) {
if (It.first == Other)
return It.second;
}
return nullptr;
}
void removeEdge(Chain *Other) {
auto It = Edges.begin();
while (It != Edges.end()) {
if (It->first == Other) {
Edges.erase(It);
return;
}
It++;
}
}
void addEdge(Chain *Other, ChainEdge *Edge) {
Edges.push_back(std::make_pair(Other, Edge));
}
void merge(Chain *Other, const std::vector<Block *> &MergedBlocks) {
Blocks = MergedBlocks;
for (size_t Idx = 0; Idx < Blocks.size(); Idx++) {
Blocks[Idx]->CurChain = this;
Blocks[Idx]->CurIndex = Idx;
}
}
void mergeEdges(Chain *Other);
void clear() {
Blocks.clear();
Blocks.shrink_to_fit();
Edges.clear();
Edges.shrink_to_fit();
}
private:
uint64_t Id;
double Score;
std::vector<Block *> Blocks;
std::vector<std::pair<Chain *, ChainEdge *>> Edges;
};
class ChainEdge {
public:
ChainEdge(const ChainEdge &) = delete;
ChainEdge(ChainEdge &&) = default;
ChainEdge &operator=(const ChainEdge &) = delete;
ChainEdge &operator=(ChainEdge &&) = default;
explicit ChainEdge(Jump *Jump)
: SrcChain(Jump->Source->CurChain), DstChain(Jump->Target->CurChain),
Jumps(1, Jump) {}
const std::vector<Jump *> &jumps() const { return Jumps; }
void changeEndpoint(Chain *From, Chain *To) {
if (From == SrcChain)
SrcChain = To;
if (From == DstChain)
DstChain = To;
}
void appendJump(Jump *Jump) { Jumps.push_back(Jump); }
void moveJumps(ChainEdge *Other) {
Jumps.insert(Jumps.end(), Other->Jumps.begin(), Other->Jumps.end());
Other->Jumps.clear();
Other->Jumps.shrink_to_fit();
}
bool hasCachedMergeGain(Chain *Src, Chain *Dst) const {
return Src == SrcChain ? CacheValidForward : CacheValidBackward;
}
MergeGainTy getCachedMergeGain(Chain *Src, Chain *Dst) const {
return Src == SrcChain ? CachedGainForward : CachedGainBackward;
}
void setCachedMergeGain(Chain *Src, Chain *Dst, MergeGainTy MergeGain) {
if (Src == SrcChain) {
CachedGainForward = MergeGain;
CacheValidForward = true;
} else {
CachedGainBackward = MergeGain;
CacheValidBackward = true;
}
}
void invalidateCache() {
CacheValidForward = false;
CacheValidBackward = false;
}
private:
Chain *SrcChain{nullptr};
Chain *DstChain{nullptr};
std::vector<Jump *> Jumps;
MergeGainTy CachedGainForward;
MergeGainTy CachedGainBackward;
bool CacheValidForward{false};
bool CacheValidBackward{false};
};
void Chain::mergeEdges(Chain *Other) {
assert(this != Other && "cannot merge a chain with itself");
for (auto EdgeIt : Other->Edges) {
const auto DstChain = EdgeIt.first;
const auto DstEdge = EdgeIt.second;
const auto TargetChain = DstChain == Other ? this : DstChain;
auto CurEdge = getEdge(TargetChain);
if (CurEdge == nullptr) {
DstEdge->changeEndpoint(Other, this);
this->addEdge(TargetChain, DstEdge);
if (DstChain != this && DstChain != Other) {
DstChain->addEdge(this, DstEdge);
}
} else {
CurEdge->moveJumps(DstEdge);
}
if (DstChain != Other) {
DstChain->removeEdge(Other);
}
}
}
using BlockIter = std::vector<Block *>::const_iterator;
class MergedChain {
public:
MergedChain(BlockIter Begin1, BlockIter End1, BlockIter Begin2 = BlockIter(),
BlockIter End2 = BlockIter(), BlockIter Begin3 = BlockIter(),
BlockIter End3 = BlockIter())
: Begin1(Begin1), End1(End1), Begin2(Begin2), End2(End2), Begin3(Begin3),
End3(End3) {}
template <typename F> void forEach(const F &Func) const {
for (auto It = Begin1; It != End1; It++)
Func(*It);
for (auto It = Begin2; It != End2; It++)
Func(*It);
for (auto It = Begin3; It != End3; It++)
Func(*It);
}
std::vector<Block *> getBlocks() const {
std::vector<Block *> Result;
Result.reserve(std::distance(Begin1, End1) + std::distance(Begin2, End2) +
std::distance(Begin3, End3));
Result.insert(Result.end(), Begin1, End1);
Result.insert(Result.end(), Begin2, End2);
Result.insert(Result.end(), Begin3, End3);
return Result;
}
const Block *getFirstBlock() const { return *Begin1; }
private:
BlockIter Begin1;
BlockIter End1;
BlockIter Begin2;
BlockIter End2;
BlockIter Begin3;
BlockIter End3;
};
class ExtTSPImpl {
using EdgeT = std::pair<uint64_t, uint64_t>;
using EdgeCountMap = DenseMap<EdgeT, uint64_t>;
public:
ExtTSPImpl(size_t NumNodes, const std::vector<uint64_t> &NodeSizes,
const std::vector<uint64_t> &NodeCounts,
const EdgeCountMap &EdgeCounts)
: NumNodes(NumNodes) {
initialize(NodeSizes, NodeCounts, EdgeCounts);
}
void run(std::vector<uint64_t> &Result) {
mergeForcedPairs();
mergeChainPairs();
mergeColdChains();
concatChains(Result);
}
private:
void initialize(const std::vector<uint64_t> &NodeSizes,
const std::vector<uint64_t> &NodeCounts,
const EdgeCountMap &EdgeCounts) {
AllBlocks.reserve(NumNodes);
for (uint64_t Node = 0; Node < NumNodes; Node++) {
uint64_t Size = std::max<uint64_t>(NodeSizes[Node], 1ULL);
uint64_t ExecutionCount = NodeCounts[Node];
if (Node == 0 && ExecutionCount == 0)
ExecutionCount = 1;
AllBlocks.emplace_back(Node, Size, ExecutionCount);
}
SuccNodes = std::vector<std::vector<uint64_t>>(NumNodes);
PredNodes = std::vector<std::vector<uint64_t>>(NumNodes);
AllJumps.reserve(EdgeCounts.size());
for (auto It : EdgeCounts) {
auto Pred = It.first.first;
auto Succ = It.first.second;
if (Pred == Succ)
continue;
SuccNodes[Pred].push_back(Succ);
PredNodes[Succ].push_back(Pred);
auto ExecutionCount = It.second;
if (ExecutionCount > 0) {
auto &Block = AllBlocks[Pred];
auto &SuccBlock = AllBlocks[Succ];
AllJumps.emplace_back(&Block, &SuccBlock, ExecutionCount);
SuccBlock.InJumps.push_back(&AllJumps.back());
Block.OutJumps.push_back(&AllJumps.back());
}
}
AllChains.reserve(NumNodes);
HotChains.reserve(NumNodes);
for (auto &Block : AllBlocks) {
AllChains.emplace_back(Block.Index, &Block);
Block.CurChain = &AllChains.back();
if (Block.ExecutionCount > 0) {
HotChains.push_back(&AllChains.back());
}
}
AllEdges.reserve(AllJumps.size());
for (auto &Block : AllBlocks) {
for (auto &Jump : Block.OutJumps) {
auto SuccBlock = Jump->Target;
auto CurEdge = Block.CurChain->getEdge(SuccBlock->CurChain);
if (CurEdge != nullptr) {
assert(SuccBlock->CurChain->getEdge(Block.CurChain) != nullptr);
CurEdge->appendJump(Jump);
continue;
}
AllEdges.emplace_back(Jump);
Block.CurChain->addEdge(SuccBlock->CurChain, &AllEdges.back());
SuccBlock->CurChain->addEdge(Block.CurChain, &AllEdges.back());
}
}
}
void mergeForcedPairs() {
for (auto &Block : AllBlocks) {
if (SuccNodes[Block.Index].size() == 1 &&
PredNodes[SuccNodes[Block.Index][0]].size() == 1 &&
SuccNodes[Block.Index][0] != 0) {
size_t SuccIndex = SuccNodes[Block.Index][0];
Block.ForcedSucc = &AllBlocks[SuccIndex];
AllBlocks[SuccIndex].ForcedPred = &Block;
}
}
for (auto &Block : AllBlocks) {
if (Block.ForcedSucc == nullptr || Block.ForcedPred == nullptr)
continue;
auto SuccBlock = Block.ForcedSucc;
while (SuccBlock != nullptr && SuccBlock != &Block) {
SuccBlock = SuccBlock->ForcedSucc;
}
if (SuccBlock == nullptr)
continue;
AllBlocks[Block.ForcedPred->Index].ForcedSucc = nullptr;
Block.ForcedPred = nullptr;
}
for (auto &Block : AllBlocks) {
if (Block.ForcedPred == nullptr && Block.ForcedSucc != nullptr) {
auto CurBlock = &Block;
while (CurBlock->ForcedSucc != nullptr) {
const auto NextBlock = CurBlock->ForcedSucc;
mergeChains(Block.CurChain, NextBlock->CurChain, 0, MergeTypeTy::X_Y);
CurBlock = NextBlock;
}
}
}
}
void mergeChainPairs() {
auto compareChainPairs = [](const Chain *A1, const Chain *B1,
const Chain *A2, const Chain *B2) {
if (A1 != A2)
return A1->id() < A2->id();
return B1->id() < B2->id();
};
while (HotChains.size() > 1) {
Chain *BestChainPred = nullptr;
Chain *BestChainSucc = nullptr;
auto BestGain = MergeGainTy();
for (auto ChainPred : HotChains) {
for (auto EdgeIter : ChainPred->edges()) {
auto ChainSucc = EdgeIter.first;
auto ChainEdge = EdgeIter.second;
if (ChainPred == ChainSucc)
continue;
if (ChainPred->numBlocks() + ChainSucc->numBlocks() >= MaxChainSize)
continue;
auto CurGain = getBestMergeGain(ChainPred, ChainSucc, ChainEdge);
if (CurGain.score() <= EPS)
continue;
if (BestGain < CurGain ||
(std::abs(CurGain.score() - BestGain.score()) < EPS &&
compareChainPairs(ChainPred, ChainSucc, BestChainPred,
BestChainSucc))) {
BestGain = CurGain;
BestChainPred = ChainPred;
BestChainSucc = ChainSucc;
}
}
}
if (BestGain.score() <= EPS)
break;
mergeChains(BestChainPred, BestChainSucc, BestGain.mergeOffset(),
BestGain.mergeType());
}
}
void mergeColdChains() {
for (size_t SrcBB = 0; SrcBB < NumNodes; SrcBB++) {
size_t NumSuccs = SuccNodes[SrcBB].size();
for (size_t Idx = 0; Idx < NumSuccs; Idx++) {
auto DstBB = SuccNodes[SrcBB][NumSuccs - Idx - 1];
auto SrcChain = AllBlocks[SrcBB].CurChain;
auto DstChain = AllBlocks[DstBB].CurChain;
if (SrcChain != DstChain && !DstChain->isEntry() &&
SrcChain->blocks().back()->Index == SrcBB &&
DstChain->blocks().front()->Index == DstBB) {
mergeChains(SrcChain, DstChain, 0, MergeTypeTy::X_Y);
}
}
}
}
double extTSPScore(const MergedChain &MergedBlocks,
const std::vector<Jump *> &Jumps) const {
if (Jumps.empty())
return 0.0;
uint64_t CurAddr = 0;
MergedBlocks.forEach([&](const Block *BB) {
BB->EstimatedAddr = CurAddr;
CurAddr += BB->Size;
});
double Score = 0;
for (auto &Jump : Jumps) {
const auto SrcBlock = Jump->Source;
const auto DstBlock = Jump->Target;
Score += ::extTSPScore(SrcBlock->EstimatedAddr, SrcBlock->Size,
DstBlock->EstimatedAddr, Jump->ExecutionCount);
}
return Score;
}
MergeGainTy getBestMergeGain(Chain *ChainPred, Chain *ChainSucc,
ChainEdge *Edge) const {
if (Edge->hasCachedMergeGain(ChainPred, ChainSucc)) {
return Edge->getCachedMergeGain(ChainPred, ChainSucc);
}
auto Jumps = Edge->jumps();
auto EdgePP = ChainPred->getEdge(ChainPred);
if (EdgePP != nullptr) {
Jumps.insert(Jumps.end(), EdgePP->jumps().begin(), EdgePP->jumps().end());
}
assert(!Jumps.empty() && "trying to merge chains w/o jumps");
MergeGainTy Gain = MergeGainTy();
auto tryChainMerging = [&](size_t Offset,
const std::vector<MergeTypeTy> &MergeTypes) {
if (Offset == 0 || Offset == ChainPred->blocks().size())
return;
auto BB = ChainPred->blocks()[Offset - 1];
if (BB->ForcedSucc != nullptr)
return;
for (auto &MergeType : MergeTypes) {
Gain.updateIfLessThan(
computeMergeGain(ChainPred, ChainSucc, Jumps, Offset, MergeType));
}
};
Gain.updateIfLessThan(
computeMergeGain(ChainPred, ChainSucc, Jumps, 0, MergeTypeTy::X_Y));
if (EnableChainSplitAlongJumps) {
for (auto &Jump : ChainSucc->blocks().front()->InJumps) {
const auto SrcBlock = Jump->Source;
if (SrcBlock->CurChain != ChainPred)
continue;
size_t Offset = SrcBlock->CurIndex + 1;
tryChainMerging(Offset, {MergeTypeTy::X1_Y_X2, MergeTypeTy::X2_X1_Y});
}
for (auto &Jump : ChainSucc->blocks().back()->OutJumps) {
const auto DstBlock = Jump->Source;
if (DstBlock->CurChain != ChainPred)
continue;
size_t Offset = DstBlock->CurIndex;
tryChainMerging(Offset, {MergeTypeTy::X1_Y_X2, MergeTypeTy::Y_X2_X1});
}
}
if (ChainPred->blocks().size() <= ChainSplitThreshold) {
for (size_t Offset = 1; Offset < ChainPred->blocks().size(); Offset++) {
tryChainMerging(Offset, {MergeTypeTy::X1_Y_X2, MergeTypeTy::Y_X2_X1,
MergeTypeTy::X2_X1_Y});
}
}
Edge->setCachedMergeGain(ChainPred, ChainSucc, Gain);
return Gain;
}
MergeGainTy computeMergeGain(const Chain *ChainPred, const Chain *ChainSucc,
const std::vector<Jump *> &Jumps,
size_t MergeOffset,
MergeTypeTy MergeType) const {
auto MergedBlocks = mergeBlocks(ChainPred->blocks(), ChainSucc->blocks(),
MergeOffset, MergeType);
if ((ChainPred->isEntry() || ChainSucc->isEntry()) &&
!MergedBlocks.getFirstBlock()->isEntry())
return MergeGainTy();
auto NewGainScore = extTSPScore(MergedBlocks, Jumps) - ChainPred->score();
return MergeGainTy(NewGainScore, MergeOffset, MergeType);
}
MergedChain mergeBlocks(const std::vector<Block *> &X,
const std::vector<Block *> &Y, size_t MergeOffset,
MergeTypeTy MergeType) const {
BlockIter BeginX1 = X.begin();
BlockIter EndX1 = X.begin() + MergeOffset;
BlockIter BeginX2 = X.begin() + MergeOffset;
BlockIter EndX2 = X.end();
BlockIter BeginY = Y.begin();
BlockIter EndY = Y.end();
switch (MergeType) {
case MergeTypeTy::X_Y:
return MergedChain(BeginX1, EndX2, BeginY, EndY);
case MergeTypeTy::X1_Y_X2:
return MergedChain(BeginX1, EndX1, BeginY, EndY, BeginX2, EndX2);
case MergeTypeTy::Y_X2_X1:
return MergedChain(BeginY, EndY, BeginX2, EndX2, BeginX1, EndX1);
case MergeTypeTy::X2_X1_Y:
return MergedChain(BeginX2, EndX2, BeginX1, EndX1, BeginY, EndY);
}
llvm_unreachable("unexpected chain merge type");
}
void mergeChains(Chain *Into, Chain *From, size_t MergeOffset,
MergeTypeTy MergeType) {
assert(Into != From && "a chain cannot be merged with itself");
auto MergedBlocks =
mergeBlocks(Into->blocks(), From->blocks(), MergeOffset, MergeType);
Into->merge(From, MergedBlocks.getBlocks());
Into->mergeEdges(From);
From->clear();
auto SelfEdge = Into->getEdge(Into);
if (SelfEdge != nullptr) {
MergedBlocks = MergedChain(Into->blocks().begin(), Into->blocks().end());
Into->setScore(extTSPScore(MergedBlocks, SelfEdge->jumps()));
}
auto Iter = std::remove(HotChains.begin(), HotChains.end(), From);
HotChains.erase(Iter, HotChains.end());
for (auto EdgeIter : Into->edges()) {
EdgeIter.second->invalidateCache();
}
}
void concatChains(std::vector<uint64_t> &Order) {
std::vector<Chain *> SortedChains;
DenseMap<const Chain *, double> ChainDensity;
for (auto &Chain : AllChains) {
if (!Chain.blocks().empty()) {
SortedChains.push_back(&Chain);
double Size = 0;
double ExecutionCount = 0;
for (auto Block : Chain.blocks()) {
Size += static_cast<double>(Block->Size);
ExecutionCount += static_cast<double>(Block->ExecutionCount);
}
assert(Size > 0 && "a chain of zero size");
ChainDensity[&Chain] = ExecutionCount / Size;
}
}
std::stable_sort(SortedChains.begin(), SortedChains.end(),
[&](const Chain *C1, const Chain *C2) {
if (C1->isEntry() != C2->isEntry()) {
return C1->isEntry();
}
const double D1 = ChainDensity[C1];
const double D2 = ChainDensity[C2];
return (D1 != D2) ? (D1 > D2) : (C1->id() < C2->id());
});
Order.reserve(NumNodes);
for (auto Chain : SortedChains) {
for (auto Block : Chain->blocks()) {
Order.push_back(Block->Index);
}
}
}
private:
const size_t NumNodes;
std::vector<std::vector<uint64_t>> SuccNodes;
std::vector<std::vector<uint64_t>> PredNodes;
std::vector<Block> AllBlocks;
std::vector<Jump> AllJumps;
std::vector<Chain> AllChains;
std::vector<ChainEdge> AllEdges;
std::vector<Chain *> HotChains;
};
}
std::vector<uint64_t> llvm::applyExtTspLayout(
const std::vector<uint64_t> &NodeSizes,
const std::vector<uint64_t> &NodeCounts,
const DenseMap<std::pair<uint64_t, uint64_t>, uint64_t> &EdgeCounts) {
size_t NumNodes = NodeSizes.size();
assert(NodeCounts.size() == NodeSizes.size() && "Incorrect input");
assert(NumNodes > 2 && "Incorrect input");
auto Alg = ExtTSPImpl(NumNodes, NodeSizes, NodeCounts, EdgeCounts);
std::vector<uint64_t> Result;
Alg.run(Result);
assert(Result.front() == 0 && "Original entry point is not preserved");
assert(Result.size() == NumNodes && "Incorrect size of reordered layout");
return Result;
}
double llvm::calcExtTspScore(
const std::vector<uint64_t> &Order, const std::vector<uint64_t> &NodeSizes,
const std::vector<uint64_t> &NodeCounts,
const DenseMap<std::pair<uint64_t, uint64_t>, uint64_t> &EdgeCounts) {
auto Addr = std::vector<uint64_t>(NodeSizes.size(), 0);
for (size_t Idx = 1; Idx < Order.size(); Idx++) {
Addr[Order[Idx]] = Addr[Order[Idx - 1]] + NodeSizes[Order[Idx - 1]];
}
double Score = 0;
for (auto It : EdgeCounts) {
auto Pred = It.first.first;
auto Succ = It.first.second;
uint64_t Count = It.second;
Score += extTSPScore(Addr[Pred], NodeSizes[Pred], Addr[Succ], Count);
}
return Score;
}
double llvm::calcExtTspScore(
const std::vector<uint64_t> &NodeSizes,
const std::vector<uint64_t> &NodeCounts,
const DenseMap<std::pair<uint64_t, uint64_t>, uint64_t> &EdgeCounts) {
auto Order = std::vector<uint64_t>(NodeSizes.size());
for (size_t Idx = 0; Idx < NodeSizes.size(); Idx++) {
Order[Idx] = Idx;
}
return calcExtTspScore(Order, NodeSizes, NodeCounts, EdgeCounts);
}