104eeddc0SDimitry Andric //===- SectionPriorities.cpp ----------------------------------------------===// 204eeddc0SDimitry Andric // 304eeddc0SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 404eeddc0SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 504eeddc0SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 604eeddc0SDimitry Andric // 704eeddc0SDimitry Andric //===----------------------------------------------------------------------===// 804eeddc0SDimitry Andric /// 904eeddc0SDimitry Andric /// This is based on the ELF port, see ELF/CallGraphSort.cpp for the details 1004eeddc0SDimitry Andric /// about the algorithm. 1104eeddc0SDimitry Andric /// 1204eeddc0SDimitry Andric //===----------------------------------------------------------------------===// 1304eeddc0SDimitry Andric 1404eeddc0SDimitry Andric #include "SectionPriorities.h" 1504eeddc0SDimitry Andric #include "Config.h" 1604eeddc0SDimitry Andric #include "InputFiles.h" 1704eeddc0SDimitry Andric #include "Symbols.h" 1804eeddc0SDimitry Andric #include "Target.h" 1981ad6265SDimitry Andric 2004eeddc0SDimitry Andric #include "lld/Common/Args.h" 2104eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h" 2204eeddc0SDimitry Andric #include "lld/Common/ErrorHandler.h" 2304eeddc0SDimitry Andric #include "llvm/ADT/DenseMap.h" 2404eeddc0SDimitry Andric #include "llvm/ADT/MapVector.h" 2504eeddc0SDimitry Andric #include "llvm/Support/Path.h" 2604eeddc0SDimitry Andric #include "llvm/Support/TimeProfiler.h" 2704eeddc0SDimitry Andric #include "llvm/Support/raw_ostream.h" 28bdd1243dSDimitry Andric 2904eeddc0SDimitry Andric #include <numeric> 3004eeddc0SDimitry Andric 3104eeddc0SDimitry Andric using namespace llvm; 3204eeddc0SDimitry Andric using namespace llvm::MachO; 3304eeddc0SDimitry Andric using namespace llvm::sys; 3404eeddc0SDimitry Andric using namespace lld; 3504eeddc0SDimitry Andric using namespace lld::macho; 3604eeddc0SDimitry Andric 3781ad6265SDimitry Andric PriorityBuilder macho::priorityBuilder; 3881ad6265SDimitry Andric 3904eeddc0SDimitry Andric namespace { 4081ad6265SDimitry Andric 4181ad6265SDimitry Andric size_t highestAvailablePriority = std::numeric_limits<size_t>::max(); 4281ad6265SDimitry Andric 4304eeddc0SDimitry Andric struct Edge { 4404eeddc0SDimitry Andric int from; 4504eeddc0SDimitry Andric uint64_t weight; 4604eeddc0SDimitry Andric }; 4704eeddc0SDimitry Andric 4804eeddc0SDimitry Andric struct Cluster { 4904eeddc0SDimitry Andric Cluster(int sec, size_t s) : next(sec), prev(sec), size(s) {} 5004eeddc0SDimitry Andric 5104eeddc0SDimitry Andric double getDensity() const { 5204eeddc0SDimitry Andric if (size == 0) 5304eeddc0SDimitry Andric return 0; 5404eeddc0SDimitry Andric return double(weight) / double(size); 5504eeddc0SDimitry Andric } 5604eeddc0SDimitry Andric 5704eeddc0SDimitry Andric int next; 5804eeddc0SDimitry Andric int prev; 5904eeddc0SDimitry Andric uint64_t size; 6004eeddc0SDimitry Andric uint64_t weight = 0; 6104eeddc0SDimitry Andric uint64_t initialWeight = 0; 6204eeddc0SDimitry Andric Edge bestPred = {-1, 0}; 6304eeddc0SDimitry Andric }; 6404eeddc0SDimitry Andric 6504eeddc0SDimitry Andric class CallGraphSort { 6604eeddc0SDimitry Andric public: 6781ad6265SDimitry Andric CallGraphSort(const MapVector<SectionPair, uint64_t> &profile); 6804eeddc0SDimitry Andric 6904eeddc0SDimitry Andric DenseMap<const InputSection *, size_t> run(); 7004eeddc0SDimitry Andric 7104eeddc0SDimitry Andric private: 7204eeddc0SDimitry Andric std::vector<Cluster> clusters; 7304eeddc0SDimitry Andric std::vector<const InputSection *> sections; 7404eeddc0SDimitry Andric }; 7504eeddc0SDimitry Andric // Maximum amount the combined cluster density can be worse than the original 7604eeddc0SDimitry Andric // cluster to consider merging. 7704eeddc0SDimitry Andric constexpr int MAX_DENSITY_DEGRADATION = 8; 7804eeddc0SDimitry Andric } // end anonymous namespace 7904eeddc0SDimitry Andric 8081ad6265SDimitry Andric // Take the edge list in callGraphProfile, resolve symbol names to Symbols, and 8181ad6265SDimitry Andric // generate a graph between InputSections with the provided weights. 8281ad6265SDimitry Andric CallGraphSort::CallGraphSort(const MapVector<SectionPair, uint64_t> &profile) { 8304eeddc0SDimitry Andric DenseMap<const InputSection *, int> secToCluster; 8404eeddc0SDimitry Andric 8504eeddc0SDimitry Andric auto getOrCreateCluster = [&](const InputSection *isec) -> int { 8604eeddc0SDimitry Andric auto res = secToCluster.try_emplace(isec, clusters.size()); 8704eeddc0SDimitry Andric if (res.second) { 8804eeddc0SDimitry Andric sections.push_back(isec); 8904eeddc0SDimitry Andric clusters.emplace_back(clusters.size(), isec->getSize()); 9004eeddc0SDimitry Andric } 9104eeddc0SDimitry Andric return res.first->second; 9204eeddc0SDimitry Andric }; 9304eeddc0SDimitry Andric 9404eeddc0SDimitry Andric // Create the graph 9581ad6265SDimitry Andric for (const std::pair<SectionPair, uint64_t> &c : profile) { 9604eeddc0SDimitry Andric const auto fromSec = c.first.first->canonical(); 9704eeddc0SDimitry Andric const auto toSec = c.first.second->canonical(); 9804eeddc0SDimitry Andric uint64_t weight = c.second; 9904eeddc0SDimitry Andric // Ignore edges between input sections belonging to different output 10004eeddc0SDimitry Andric // sections. This is done because otherwise we would end up with clusters 10104eeddc0SDimitry Andric // containing input sections that can't actually be placed adjacently in the 10204eeddc0SDimitry Andric // output. This messes with the cluster size and density calculations. We 10304eeddc0SDimitry Andric // would also end up moving input sections in other output sections without 10404eeddc0SDimitry Andric // moving them closer to what calls them. 10504eeddc0SDimitry Andric if (fromSec->parent != toSec->parent) 10604eeddc0SDimitry Andric continue; 10704eeddc0SDimitry Andric 10804eeddc0SDimitry Andric int from = getOrCreateCluster(fromSec); 10904eeddc0SDimitry Andric int to = getOrCreateCluster(toSec); 11004eeddc0SDimitry Andric 11104eeddc0SDimitry Andric clusters[to].weight += weight; 11204eeddc0SDimitry Andric 11304eeddc0SDimitry Andric if (from == to) 11404eeddc0SDimitry Andric continue; 11504eeddc0SDimitry Andric 11604eeddc0SDimitry Andric // Remember the best edge. 11704eeddc0SDimitry Andric Cluster &toC = clusters[to]; 11804eeddc0SDimitry Andric if (toC.bestPred.from == -1 || toC.bestPred.weight < weight) { 11904eeddc0SDimitry Andric toC.bestPred.from = from; 12004eeddc0SDimitry Andric toC.bestPred.weight = weight; 12104eeddc0SDimitry Andric } 12204eeddc0SDimitry Andric } 12304eeddc0SDimitry Andric for (Cluster &c : clusters) 12404eeddc0SDimitry Andric c.initialWeight = c.weight; 12504eeddc0SDimitry Andric } 12604eeddc0SDimitry Andric 12704eeddc0SDimitry Andric // It's bad to merge clusters which would degrade the density too much. 12804eeddc0SDimitry Andric static bool isNewDensityBad(Cluster &a, Cluster &b) { 12904eeddc0SDimitry Andric double newDensity = double(a.weight + b.weight) / double(a.size + b.size); 13004eeddc0SDimitry Andric return newDensity < a.getDensity() / MAX_DENSITY_DEGRADATION; 13104eeddc0SDimitry Andric } 13204eeddc0SDimitry Andric 13304eeddc0SDimitry Andric // Find the leader of V's belonged cluster (represented as an equivalence 13404eeddc0SDimitry Andric // class). We apply union-find path-halving technique (simple to implement) in 13504eeddc0SDimitry Andric // the meantime as it decreases depths and the time complexity. 13604eeddc0SDimitry Andric static int getLeader(std::vector<int> &leaders, int v) { 13704eeddc0SDimitry Andric while (leaders[v] != v) { 13804eeddc0SDimitry Andric leaders[v] = leaders[leaders[v]]; 13904eeddc0SDimitry Andric v = leaders[v]; 14004eeddc0SDimitry Andric } 14104eeddc0SDimitry Andric return v; 14204eeddc0SDimitry Andric } 14304eeddc0SDimitry Andric 14404eeddc0SDimitry Andric static void mergeClusters(std::vector<Cluster> &cs, Cluster &into, int intoIdx, 14504eeddc0SDimitry Andric Cluster &from, int fromIdx) { 14604eeddc0SDimitry Andric int tail1 = into.prev, tail2 = from.prev; 14704eeddc0SDimitry Andric into.prev = tail2; 14804eeddc0SDimitry Andric cs[tail2].next = intoIdx; 14904eeddc0SDimitry Andric from.prev = tail1; 15004eeddc0SDimitry Andric cs[tail1].next = fromIdx; 15104eeddc0SDimitry Andric into.size += from.size; 15204eeddc0SDimitry Andric into.weight += from.weight; 15304eeddc0SDimitry Andric from.size = 0; 15404eeddc0SDimitry Andric from.weight = 0; 15504eeddc0SDimitry Andric } 15604eeddc0SDimitry Andric 15704eeddc0SDimitry Andric // Group InputSections into clusters using the Call-Chain Clustering heuristic 15804eeddc0SDimitry Andric // then sort the clusters by density. 15904eeddc0SDimitry Andric DenseMap<const InputSection *, size_t> CallGraphSort::run() { 16004eeddc0SDimitry Andric const uint64_t maxClusterSize = target->getPageSize(); 16104eeddc0SDimitry Andric 16204eeddc0SDimitry Andric // Cluster indices sorted by density. 16304eeddc0SDimitry Andric std::vector<int> sorted(clusters.size()); 16404eeddc0SDimitry Andric // For union-find. 16504eeddc0SDimitry Andric std::vector<int> leaders(clusters.size()); 16604eeddc0SDimitry Andric 16704eeddc0SDimitry Andric std::iota(leaders.begin(), leaders.end(), 0); 16804eeddc0SDimitry Andric std::iota(sorted.begin(), sorted.end(), 0); 16904eeddc0SDimitry Andric 17004eeddc0SDimitry Andric llvm::stable_sort(sorted, [&](int a, int b) { 17104eeddc0SDimitry Andric return clusters[a].getDensity() > clusters[b].getDensity(); 17204eeddc0SDimitry Andric }); 17304eeddc0SDimitry Andric 17404eeddc0SDimitry Andric for (int l : sorted) { 17504eeddc0SDimitry Andric // The cluster index is the same as the index of its leader here because 17604eeddc0SDimitry Andric // clusters[L] has not been merged into another cluster yet. 17704eeddc0SDimitry Andric Cluster &c = clusters[l]; 17804eeddc0SDimitry Andric 17904eeddc0SDimitry Andric // Don't consider merging if the edge is unlikely. 18004eeddc0SDimitry Andric if (c.bestPred.from == -1 || c.bestPred.weight * 10 <= c.initialWeight) 18104eeddc0SDimitry Andric continue; 18204eeddc0SDimitry Andric 18304eeddc0SDimitry Andric int predL = getLeader(leaders, c.bestPred.from); 18404eeddc0SDimitry Andric // Already in the same cluster. 18504eeddc0SDimitry Andric if (l == predL) 18604eeddc0SDimitry Andric continue; 18704eeddc0SDimitry Andric 18804eeddc0SDimitry Andric Cluster *predC = &clusters[predL]; 18904eeddc0SDimitry Andric if (c.size + predC->size > maxClusterSize) 19004eeddc0SDimitry Andric continue; 19104eeddc0SDimitry Andric 19204eeddc0SDimitry Andric if (isNewDensityBad(*predC, c)) 19304eeddc0SDimitry Andric continue; 19404eeddc0SDimitry Andric 19504eeddc0SDimitry Andric leaders[l] = predL; 19604eeddc0SDimitry Andric mergeClusters(clusters, *predC, predL, c, l); 19704eeddc0SDimitry Andric } 19804eeddc0SDimitry Andric // Sort remaining non-empty clusters by density. 19904eeddc0SDimitry Andric sorted.clear(); 20004eeddc0SDimitry Andric for (int i = 0, e = (int)clusters.size(); i != e; ++i) 20104eeddc0SDimitry Andric if (clusters[i].size > 0) 20204eeddc0SDimitry Andric sorted.push_back(i); 20304eeddc0SDimitry Andric llvm::stable_sort(sorted, [&](int a, int b) { 20404eeddc0SDimitry Andric return clusters[a].getDensity() > clusters[b].getDensity(); 20504eeddc0SDimitry Andric }); 20604eeddc0SDimitry Andric 20704eeddc0SDimitry Andric DenseMap<const InputSection *, size_t> orderMap; 20804eeddc0SDimitry Andric 20904eeddc0SDimitry Andric // Sections will be sorted by decreasing order. Absent sections will have 21004eeddc0SDimitry Andric // priority 0 and be placed at the end of sections. 21104eeddc0SDimitry Andric // NB: This is opposite from COFF/ELF to be compatible with the existing 21204eeddc0SDimitry Andric // order-file code. 21381ad6265SDimitry Andric int curOrder = highestAvailablePriority; 21404eeddc0SDimitry Andric for (int leader : sorted) { 21504eeddc0SDimitry Andric for (int i = leader;;) { 21604eeddc0SDimitry Andric orderMap[sections[i]] = curOrder--; 21704eeddc0SDimitry Andric i = clusters[i].next; 21804eeddc0SDimitry Andric if (i == leader) 21904eeddc0SDimitry Andric break; 22004eeddc0SDimitry Andric } 22104eeddc0SDimitry Andric } 22204eeddc0SDimitry Andric if (!config->printSymbolOrder.empty()) { 22304eeddc0SDimitry Andric std::error_code ec; 22404eeddc0SDimitry Andric raw_fd_ostream os(config->printSymbolOrder, ec, sys::fs::OF_None); 22504eeddc0SDimitry Andric if (ec) { 22604eeddc0SDimitry Andric error("cannot open " + config->printSymbolOrder + ": " + ec.message()); 22704eeddc0SDimitry Andric return orderMap; 22804eeddc0SDimitry Andric } 22904eeddc0SDimitry Andric // Print the symbols ordered by C3, in the order of decreasing curOrder 23004eeddc0SDimitry Andric // Instead of sorting all the orderMap, just repeat the loops above. 23104eeddc0SDimitry Andric for (int leader : sorted) 23204eeddc0SDimitry Andric for (int i = leader;;) { 23304eeddc0SDimitry Andric const InputSection *isec = sections[i]; 23404eeddc0SDimitry Andric // Search all the symbols in the file of the section 23504eeddc0SDimitry Andric // and find out a Defined symbol with name that is within the 23604eeddc0SDimitry Andric // section. 23704eeddc0SDimitry Andric for (Symbol *sym : isec->getFile()->symbols) { 23804eeddc0SDimitry Andric if (auto *d = dyn_cast_or_null<Defined>(sym)) { 239*0fca6ea1SDimitry Andric if (d->isec() == isec) 24004eeddc0SDimitry Andric os << sym->getName() << "\n"; 24104eeddc0SDimitry Andric } 24204eeddc0SDimitry Andric } 24304eeddc0SDimitry Andric i = clusters[i].next; 24404eeddc0SDimitry Andric if (i == leader) 24504eeddc0SDimitry Andric break; 24604eeddc0SDimitry Andric } 24704eeddc0SDimitry Andric } 24804eeddc0SDimitry Andric 24904eeddc0SDimitry Andric return orderMap; 25004eeddc0SDimitry Andric } 25104eeddc0SDimitry Andric 252bdd1243dSDimitry Andric std::optional<size_t> 253bdd1243dSDimitry Andric macho::PriorityBuilder::getSymbolPriority(const Defined *sym) { 25481ad6265SDimitry Andric if (sym->isAbsolute()) 255bdd1243dSDimitry Andric return std::nullopt; 25681ad6265SDimitry Andric 25781ad6265SDimitry Andric auto it = priorities.find(sym->getName()); 25881ad6265SDimitry Andric if (it == priorities.end()) 259bdd1243dSDimitry Andric return std::nullopt; 26081ad6265SDimitry Andric const SymbolPriorityEntry &entry = it->second; 261*0fca6ea1SDimitry Andric const InputFile *f = sym->isec()->getFile(); 26281ad6265SDimitry Andric if (!f) 26381ad6265SDimitry Andric return entry.anyObjectFile; 26404eeddc0SDimitry Andric // We don't use toString(InputFile *) here because it returns the full path 26504eeddc0SDimitry Andric // for object files, and we only want the basename. 26604eeddc0SDimitry Andric StringRef filename; 26704eeddc0SDimitry Andric if (f->archiveName.empty()) 26804eeddc0SDimitry Andric filename = path::filename(f->getName()); 26904eeddc0SDimitry Andric else 27004eeddc0SDimitry Andric filename = saver().save(path::filename(f->archiveName) + "(" + 27104eeddc0SDimitry Andric path::filename(f->getName()) + ")"); 27204eeddc0SDimitry Andric return std::max(entry.objectFiles.lookup(filename), entry.anyObjectFile); 27304eeddc0SDimitry Andric } 27404eeddc0SDimitry Andric 27581ad6265SDimitry Andric void macho::PriorityBuilder::extractCallGraphProfile() { 27604eeddc0SDimitry Andric TimeTraceScope timeScope("Extract call graph profile"); 27781ad6265SDimitry Andric bool hasOrderFile = !priorities.empty(); 27804eeddc0SDimitry Andric for (const InputFile *file : inputFiles) { 27904eeddc0SDimitry Andric auto *obj = dyn_cast_or_null<ObjFile>(file); 28004eeddc0SDimitry Andric if (!obj) 28104eeddc0SDimitry Andric continue; 28204eeddc0SDimitry Andric for (const CallGraphEntry &entry : obj->callGraph) { 28304eeddc0SDimitry Andric assert(entry.fromIndex < obj->symbols.size() && 28404eeddc0SDimitry Andric entry.toIndex < obj->symbols.size()); 28504eeddc0SDimitry Andric auto *fromSym = dyn_cast_or_null<Defined>(obj->symbols[entry.fromIndex]); 28604eeddc0SDimitry Andric auto *toSym = dyn_cast_or_null<Defined>(obj->symbols[entry.toIndex]); 28781ad6265SDimitry Andric if (fromSym && toSym && 28881ad6265SDimitry Andric (!hasOrderFile || 28981ad6265SDimitry Andric (!getSymbolPriority(fromSym) && !getSymbolPriority(toSym)))) 290*0fca6ea1SDimitry Andric callGraphProfile[{fromSym->isec(), toSym->isec()}] += entry.count; 29104eeddc0SDimitry Andric } 29204eeddc0SDimitry Andric } 29304eeddc0SDimitry Andric } 29404eeddc0SDimitry Andric 29581ad6265SDimitry Andric void macho::PriorityBuilder::parseOrderFile(StringRef path) { 29681ad6265SDimitry Andric assert(callGraphProfile.empty() && 29781ad6265SDimitry Andric "Order file must be parsed before call graph profile is processed"); 298bdd1243dSDimitry Andric std::optional<MemoryBufferRef> buffer = readFile(path); 29904eeddc0SDimitry Andric if (!buffer) { 30004eeddc0SDimitry Andric error("Could not read order file at " + path); 30104eeddc0SDimitry Andric return; 30204eeddc0SDimitry Andric } 30304eeddc0SDimitry Andric 30404eeddc0SDimitry Andric MemoryBufferRef mbref = *buffer; 30504eeddc0SDimitry Andric for (StringRef line : args::getLines(mbref)) { 30604eeddc0SDimitry Andric StringRef objectFile, symbol; 30704eeddc0SDimitry Andric line = line.take_until([](char c) { return c == '#'; }); // ignore comments 30804eeddc0SDimitry Andric line = line.ltrim(); 30904eeddc0SDimitry Andric 31004eeddc0SDimitry Andric CPUType cpuType = StringSwitch<CPUType>(line) 31104eeddc0SDimitry Andric .StartsWith("i386:", CPU_TYPE_I386) 31204eeddc0SDimitry Andric .StartsWith("x86_64:", CPU_TYPE_X86_64) 31304eeddc0SDimitry Andric .StartsWith("arm:", CPU_TYPE_ARM) 31404eeddc0SDimitry Andric .StartsWith("arm64:", CPU_TYPE_ARM64) 31504eeddc0SDimitry Andric .StartsWith("ppc:", CPU_TYPE_POWERPC) 31604eeddc0SDimitry Andric .StartsWith("ppc64:", CPU_TYPE_POWERPC64) 31704eeddc0SDimitry Andric .Default(CPU_TYPE_ANY); 31804eeddc0SDimitry Andric 31904eeddc0SDimitry Andric if (cpuType != CPU_TYPE_ANY && cpuType != target->cpuType) 32004eeddc0SDimitry Andric continue; 32104eeddc0SDimitry Andric 32204eeddc0SDimitry Andric // Drop the CPU type as well as the colon 32304eeddc0SDimitry Andric if (cpuType != CPU_TYPE_ANY) 32404eeddc0SDimitry Andric line = line.drop_until([](char c) { return c == ':'; }).drop_front(); 32504eeddc0SDimitry Andric 32604eeddc0SDimitry Andric constexpr std::array<StringRef, 2> fileEnds = {".o:", ".o):"}; 32704eeddc0SDimitry Andric for (StringRef fileEnd : fileEnds) { 32804eeddc0SDimitry Andric size_t pos = line.find(fileEnd); 32904eeddc0SDimitry Andric if (pos != StringRef::npos) { 33004eeddc0SDimitry Andric // Split the string around the colon 33104eeddc0SDimitry Andric objectFile = line.take_front(pos + fileEnd.size() - 1); 33204eeddc0SDimitry Andric line = line.drop_front(pos + fileEnd.size()); 33304eeddc0SDimitry Andric break; 33404eeddc0SDimitry Andric } 33504eeddc0SDimitry Andric } 33604eeddc0SDimitry Andric symbol = line.trim(); 33704eeddc0SDimitry Andric 33804eeddc0SDimitry Andric if (!symbol.empty()) { 33981ad6265SDimitry Andric SymbolPriorityEntry &entry = priorities[symbol]; 34004eeddc0SDimitry Andric if (!objectFile.empty()) 34181ad6265SDimitry Andric entry.objectFiles.insert( 34281ad6265SDimitry Andric std::make_pair(objectFile, highestAvailablePriority)); 34304eeddc0SDimitry Andric else 34481ad6265SDimitry Andric entry.anyObjectFile = 34581ad6265SDimitry Andric std::max(entry.anyObjectFile, highestAvailablePriority); 34604eeddc0SDimitry Andric } 34704eeddc0SDimitry Andric 34881ad6265SDimitry Andric --highestAvailablePriority; 34904eeddc0SDimitry Andric } 35004eeddc0SDimitry Andric } 35104eeddc0SDimitry Andric 35281ad6265SDimitry Andric DenseMap<const InputSection *, size_t> 35381ad6265SDimitry Andric macho::PriorityBuilder::buildInputSectionPriorities() { 35404eeddc0SDimitry Andric DenseMap<const InputSection *, size_t> sectionPriorities; 35581ad6265SDimitry Andric if (config->callGraphProfileSort) { 35681ad6265SDimitry Andric // Sort sections by the profile data provided by __LLVM,__cg_profile 35781ad6265SDimitry Andric // sections. 35881ad6265SDimitry Andric // 35981ad6265SDimitry Andric // This first builds a call graph based on the profile data then merges 36081ad6265SDimitry Andric // sections according to the C³ heuristic. All clusters are then sorted by a 36181ad6265SDimitry Andric // density metric to further improve locality. 36281ad6265SDimitry Andric TimeTraceScope timeScope("Call graph profile sort"); 36381ad6265SDimitry Andric sectionPriorities = CallGraphSort(callGraphProfile).run(); 36481ad6265SDimitry Andric } 36504eeddc0SDimitry Andric 36681ad6265SDimitry Andric if (priorities.empty()) 36704eeddc0SDimitry Andric return sectionPriorities; 36804eeddc0SDimitry Andric 36981ad6265SDimitry Andric auto addSym = [&](const Defined *sym) { 370bdd1243dSDimitry Andric std::optional<size_t> symbolPriority = getSymbolPriority(sym); 37181ad6265SDimitry Andric if (!symbolPriority) 37204eeddc0SDimitry Andric return; 373*0fca6ea1SDimitry Andric size_t &priority = sectionPriorities[sym->isec()]; 374bdd1243dSDimitry Andric priority = std::max(priority, *symbolPriority); 37504eeddc0SDimitry Andric }; 37604eeddc0SDimitry Andric 37704eeddc0SDimitry Andric // TODO: Make sure this handles weak symbols correctly. 37804eeddc0SDimitry Andric for (const InputFile *file : inputFiles) { 37904eeddc0SDimitry Andric if (isa<ObjFile>(file)) 38004eeddc0SDimitry Andric for (Symbol *sym : file->symbols) 38104eeddc0SDimitry Andric if (auto *d = dyn_cast_or_null<Defined>(sym)) 38281ad6265SDimitry Andric addSym(d); 38304eeddc0SDimitry Andric } 38404eeddc0SDimitry Andric 38504eeddc0SDimitry Andric return sectionPriorities; 38604eeddc0SDimitry Andric } 387