#include "llvm/Transforms/Utils/CodeLayout.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include <cmath>
#include <set>
usingnamespacellvm;
usingnamespacellvm::codelayout;
#define DEBUG_TYPE …
namespace llvm {
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> ForwardWeightCond(
"ext-tsp-forward-weight-cond", cl::ReallyHidden, cl::init(0.1),
cl::desc("The weight of conditional forward jumps for ExtTSP value"));
static cl::opt<double> ForwardWeightUncond(
"ext-tsp-forward-weight-uncond", cl::ReallyHidden, cl::init(0.1),
cl::desc("The weight of unconditional forward jumps for ExtTSP value"));
static cl::opt<double> BackwardWeightCond(
"ext-tsp-backward-weight-cond", cl::ReallyHidden, cl::init(0.1),
cl::desc("The weight of conditional backward jumps for ExtTSP value"));
static cl::opt<double> BackwardWeightUncond(
"ext-tsp-backward-weight-uncond", cl::ReallyHidden, cl::init(0.1),
cl::desc("The weight of unconditional backward jumps for ExtTSP value"));
static cl::opt<double> FallthroughWeightCond(
"ext-tsp-fallthrough-weight-cond", cl::ReallyHidden, cl::init(1.0),
cl::desc("The weight of conditional fallthrough jumps for ExtTSP value"));
static cl::opt<double> FallthroughWeightUncond(
"ext-tsp-fallthrough-weight-uncond", cl::ReallyHidden, cl::init(1.05),
cl::desc("The weight of unconditional fallthrough jumps for ExtTSP value"));
static cl::opt<unsigned> ForwardDistance(
"ext-tsp-forward-distance", cl::ReallyHidden, 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::ReallyHidden, 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::ReallyHidden, cl::init(512),
cl::desc("The maximum size of a chain to create"));
static cl::opt<unsigned> ChainSplitThreshold(
"ext-tsp-chain-split-threshold", cl::ReallyHidden, cl::init(128),
cl::desc("The maximum size of a chain to apply splitting"));
static cl::opt<double> MaxMergeDensityRatio(
"ext-tsp-max-merge-density-ratio", cl::ReallyHidden, cl::init(100),
cl::desc("The maximum ratio between densities of two chains for merging"));
static cl::opt<unsigned> CacheEntries("cdsort-cache-entries", cl::ReallyHidden,
cl::desc("The size of the cache"));
static cl::opt<unsigned> CacheSize("cdsort-cache-size", cl::ReallyHidden,
cl::desc("The size of a line in the cache"));
static cl::opt<unsigned>
CDMaxChainSize("cdsort-max-chain-size", cl::ReallyHidden,
cl::desc("The maximum size of a chain to create"));
static cl::opt<double> DistancePower(
"cdsort-distance-power", cl::ReallyHidden,
cl::desc("The power exponent for the distance-based locality"));
static cl::opt<double> FrequencyScale(
"cdsort-frequency-scale", cl::ReallyHidden,
cl::desc("The scale factor for the frequency-based locality"));
namespace {
constexpr double EPS = …;
double jumpExtTSPScore(uint64_t JumpDist, uint64_t JumpMaxDist, uint64_t Count,
double Weight) { … }
double extTSPScore(uint64_t SrcAddr, uint64_t SrcSize, uint64_t DstAddr,
uint64_t Count, bool IsConditional) { … }
enum class MergeTypeT : int { … };
struct MergeGainT { … };
struct JumpT;
struct ChainT;
struct ChainEdge;
struct NodeT { … };
struct JumpT { … };
struct ChainT { … };
struct ChainEdge { … };
bool NodeT::isSuccessor(const NodeT *Other) const { … }
uint64_t NodeT::outCount() const { … }
uint64_t NodeT::inCount() const { … }
void ChainT::mergeEdges(ChainT *Other) { … }
NodeIter;
static std::vector<NodeT *> EmptyList;
struct MergedNodesT { … };
struct MergedJumpsT { … };
MergedNodesT mergeNodes(const std::vector<NodeT *> &X,
const std::vector<NodeT *> &Y, size_t MergeOffset,
MergeTypeT MergeType) { … }
class ExtTSPImpl { … };
class CDSortImpl { … };
}
std::vector<uint64_t>
codelayout::computeExtTspLayout(ArrayRef<uint64_t> NodeSizes,
ArrayRef<uint64_t> NodeCounts,
ArrayRef<EdgeCount> EdgeCounts) { … }
double codelayout::calcExtTspScore(ArrayRef<uint64_t> Order,
ArrayRef<uint64_t> NodeSizes,
ArrayRef<EdgeCount> EdgeCounts) { … }
double codelayout::calcExtTspScore(ArrayRef<uint64_t> NodeSizes,
ArrayRef<EdgeCount> EdgeCounts) { … }
std::vector<uint64_t> codelayout::computeCacheDirectedLayout(
const CDSortConfig &Config, ArrayRef<uint64_t> FuncSizes,
ArrayRef<uint64_t> FuncCounts, ArrayRef<EdgeCount> CallCounts,
ArrayRef<uint64_t> CallOffsets) { … }
std::vector<uint64_t> codelayout::computeCacheDirectedLayout(
ArrayRef<uint64_t> FuncSizes, ArrayRef<uint64_t> FuncCounts,
ArrayRef<EdgeCount> CallCounts, ArrayRef<uint64_t> CallOffsets) { … }