#include "SectionPriorities.h"
#include "Config.h"
#include "InputFiles.h"
#include "Symbols.h"
#include "Target.h"
#include "lld/Common/Args.h"
#include "lld/Common/CommonLinkerContext.h"
#include "lld/Common/ErrorHandler.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/raw_ostream.h"
#include <numeric>
using namespace llvm;
using namespace llvm::MachO;
using namespace llvm::sys;
using namespace lld;
using namespace lld::macho;
PriorityBuilder macho::priorityBuilder;
namespace {
size_t highestAvailablePriority = std::numeric_limits<size_t>::max();
struct Edge {
int from;
uint64_t weight;
};
struct Cluster {
Cluster(int sec, size_t s) : next(sec), prev(sec), size(s) {}
double getDensity() const {
if (size == 0)
return 0;
return double(weight) / double(size);
}
int next;
int prev;
uint64_t size;
uint64_t weight = 0;
uint64_t initialWeight = 0;
Edge bestPred = {-1, 0};
};
class CallGraphSort {
public:
CallGraphSort(const MapVector<SectionPair, uint64_t> &profile);
DenseMap<const InputSection *, size_t> run();
private:
std::vector<Cluster> clusters;
std::vector<const InputSection *> sections;
};
constexpr int MAX_DENSITY_DEGRADATION = 8;
}
CallGraphSort::CallGraphSort(const MapVector<SectionPair, uint64_t> &profile) {
DenseMap<const InputSection *, int> secToCluster;
auto getOrCreateCluster = [&](const InputSection *isec) -> int {
auto res = secToCluster.try_emplace(isec, clusters.size());
if (res.second) {
sections.push_back(isec);
clusters.emplace_back(clusters.size(), isec->getSize());
}
return res.first->second;
};
for (const std::pair<SectionPair, uint64_t> &c : profile) {
const auto fromSec = c.first.first->canonical();
const auto toSec = c.first.second->canonical();
uint64_t weight = c.second;
if (fromSec->parent != toSec->parent)
continue;
int from = getOrCreateCluster(fromSec);
int to = getOrCreateCluster(toSec);
clusters[to].weight += weight;
if (from == to)
continue;
Cluster &toC = clusters[to];
if (toC.bestPred.from == -1 || toC.bestPred.weight < weight) {
toC.bestPred.from = from;
toC.bestPred.weight = weight;
}
}
for (Cluster &c : clusters)
c.initialWeight = c.weight;
}
static bool isNewDensityBad(Cluster &a, Cluster &b) {
double newDensity = double(a.weight + b.weight) / double(a.size + b.size);
return newDensity < a.getDensity() / MAX_DENSITY_DEGRADATION;
}
static int getLeader(std::vector<int> &leaders, int v) {
while (leaders[v] != v) {
leaders[v] = leaders[leaders[v]];
v = leaders[v];
}
return v;
}
static void mergeClusters(std::vector<Cluster> &cs, Cluster &into, int intoIdx,
Cluster &from, int fromIdx) {
int tail1 = into.prev, tail2 = from.prev;
into.prev = tail2;
cs[tail2].next = intoIdx;
from.prev = tail1;
cs[tail1].next = fromIdx;
into.size += from.size;
into.weight += from.weight;
from.size = 0;
from.weight = 0;
}
DenseMap<const InputSection *, size_t> CallGraphSort::run() {
const uint64_t maxClusterSize = target->getPageSize();
std::vector<int> sorted(clusters.size());
std::vector<int> leaders(clusters.size());
std::iota(leaders.begin(), leaders.end(), 0);
std::iota(sorted.begin(), sorted.end(), 0);
llvm::stable_sort(sorted, [&](int a, int b) {
return clusters[a].getDensity() > clusters[b].getDensity();
});
for (int l : sorted) {
Cluster &c = clusters[l];
if (c.bestPred.from == -1 || c.bestPred.weight * 10 <= c.initialWeight)
continue;
int predL = getLeader(leaders, c.bestPred.from);
if (l == predL)
continue;
Cluster *predC = &clusters[predL];
if (c.size + predC->size > maxClusterSize)
continue;
if (isNewDensityBad(*predC, c))
continue;
leaders[l] = predL;
mergeClusters(clusters, *predC, predL, c, l);
}
sorted.clear();
for (int i = 0, e = (int)clusters.size(); i != e; ++i)
if (clusters[i].size > 0)
sorted.push_back(i);
llvm::stable_sort(sorted, [&](int a, int b) {
return clusters[a].getDensity() > clusters[b].getDensity();
});
DenseMap<const InputSection *, size_t> orderMap;
int curOrder = highestAvailablePriority;
for (int leader : sorted) {
for (int i = leader;;) {
orderMap[sections[i]] = curOrder--;
i = clusters[i].next;
if (i == leader)
break;
}
}
if (!config->printSymbolOrder.empty()) {
std::error_code ec;
raw_fd_ostream os(config->printSymbolOrder, ec, sys::fs::OF_None);
if (ec) {
error("cannot open " + config->printSymbolOrder + ": " + ec.message());
return orderMap;
}
for (int leader : sorted)
for (int i = leader;;) {
const InputSection *isec = sections[i];
for (Symbol *sym : isec->getFile()->symbols) {
if (auto *d = dyn_cast_or_null<Defined>(sym)) {
if (d->isec() == isec)
os << sym->getName() << "\n";
}
}
i = clusters[i].next;
if (i == leader)
break;
}
}
return orderMap;
}
std::optional<size_t>
macho::PriorityBuilder::getSymbolPriority(const Defined *sym) {
if (sym->isAbsolute())
return std::nullopt;
auto it = priorities.find(sym->getName());
if (it == priorities.end())
return std::nullopt;
const SymbolPriorityEntry &entry = it->second;
const InputFile *f = sym->isec()->getFile();
if (!f)
return entry.anyObjectFile;
StringRef filename;
if (f->archiveName.empty())
filename = path::filename(f->getName());
else
filename = saver().save(path::filename(f->archiveName) + "(" +
path::filename(f->getName()) + ")");
return std::max(entry.objectFiles.lookup(filename), entry.anyObjectFile);
}
void macho::PriorityBuilder::extractCallGraphProfile() {
TimeTraceScope timeScope("Extract call graph profile");
bool hasOrderFile = !priorities.empty();
for (const InputFile *file : inputFiles) {
auto *obj = dyn_cast_or_null<ObjFile>(file);
if (!obj)
continue;
for (const CallGraphEntry &entry : obj->callGraph) {
assert(entry.fromIndex < obj->symbols.size() &&
entry.toIndex < obj->symbols.size());
auto *fromSym = dyn_cast_or_null<Defined>(obj->symbols[entry.fromIndex]);
auto *toSym = dyn_cast_or_null<Defined>(obj->symbols[entry.toIndex]);
if (fromSym && toSym &&
(!hasOrderFile ||
(!getSymbolPriority(fromSym) && !getSymbolPriority(toSym))))
callGraphProfile[{fromSym->isec(), toSym->isec()}] += entry.count;
}
}
}
void macho::PriorityBuilder::parseOrderFile(StringRef path) {
assert(callGraphProfile.empty() &&
"Order file must be parsed before call graph profile is processed");
std::optional<MemoryBufferRef> buffer = readFile(path);
if (!buffer) {
error("Could not read order file at " + path);
return;
}
MemoryBufferRef mbref = *buffer;
for (StringRef line : args::getLines(mbref)) {
StringRef objectFile, symbol;
line = line.take_until([](char c) { return c == '#'; });
line = line.ltrim();
CPUType cpuType = StringSwitch<CPUType>(line)
.StartsWith("i386:", CPU_TYPE_I386)
.StartsWith("x86_64:", CPU_TYPE_X86_64)
.StartsWith("arm:", CPU_TYPE_ARM)
.StartsWith("arm64:", CPU_TYPE_ARM64)
.StartsWith("ppc:", CPU_TYPE_POWERPC)
.StartsWith("ppc64:", CPU_TYPE_POWERPC64)
.Default(CPU_TYPE_ANY);
if (cpuType != CPU_TYPE_ANY && cpuType != target->cpuType)
continue;
if (cpuType != CPU_TYPE_ANY)
line = line.drop_until([](char c) { return c == ':'; }).drop_front();
constexpr std::array<StringRef, 2> fileEnds = {".o:", ".o):"};
for (StringRef fileEnd : fileEnds) {
size_t pos = line.find(fileEnd);
if (pos != StringRef::npos) {
objectFile = line.take_front(pos + fileEnd.size() - 1);
line = line.drop_front(pos + fileEnd.size());
break;
}
}
symbol = line.trim();
if (!symbol.empty()) {
SymbolPriorityEntry &entry = priorities[symbol];
if (!objectFile.empty())
entry.objectFiles.insert(
std::make_pair(objectFile, highestAvailablePriority));
else
entry.anyObjectFile =
std::max(entry.anyObjectFile, highestAvailablePriority);
}
--highestAvailablePriority;
}
}
DenseMap<const InputSection *, size_t>
macho::PriorityBuilder::buildInputSectionPriorities() {
DenseMap<const InputSection *, size_t> sectionPriorities;
if (config->callGraphProfileSort) {
TimeTraceScope timeScope("Call graph profile sort");
sectionPriorities = CallGraphSort(callGraphProfile).run();
}
if (priorities.empty())
return sectionPriorities;
auto addSym = [&](const Defined *sym) {
std::optional<size_t> symbolPriority = getSymbolPriority(sym);
if (!symbolPriority)
return;
size_t &priority = sectionPriorities[sym->isec()];
priority = std::max(priority, *symbolPriority);
};
for (const InputFile *file : inputFiles) {
if (isa<ObjFile>(file))
for (Symbol *sym : file->symbols)
if (auto *d = dyn_cast_or_null<Defined>(sym))
addSym(d);
}
return sectionPriorities;
}