[lld] [lld][ELF] Extend profile guided function ordering to ELF binaries (PR #117514)
Ellis Hoag via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 2 10:59:00 PST 2024
================
@@ -0,0 +1,67 @@
+//===- BPSectionOrderer.cpp--------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BPSectionOrderer.h"
+#include "Config.h"
+#include "InputFiles.h"
+#include "InputSection.h"
+#include "lld/Common/BPSectionOrdererBase.h"
+#include "lld/Common/CommonLinkerContext.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/Support/BalancedPartitioning.h"
+#include "llvm/Support/TimeProfiler.h"
+
+#include "SymbolTable.h"
+#include "Symbols.h"
+
+using namespace llvm;
+using namespace lld::elf;
+
+llvm::DenseMap<const lld::elf::InputSectionBase *, int>
+lld::elf::runBalancedPartitioning(Ctx &ctx, llvm::StringRef profilePath,
+ bool forFunctionCompression,
+ bool forDataCompression,
+ bool compressionSortStartupFunctions,
+ bool verbose) {
+ size_t highestAvailablePriority = std::numeric_limits<int>::max();
+ // Collect all InputSectionBase objects from symbols and wrap them as
+ // BPSectionBase instances for balanced partitioning which follow the way
+ // '--symbol-ordering-file' does.
+ SmallVector<lld::BPSectionBase *> sections;
+
+ for (Symbol *sym : ctx.symtab->getSymbols())
+ if (auto *d = dyn_cast<Defined>(sym))
+ if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section))
+ if (sym->getSize() > 0)
+ sections.push_back(new BPSectionELF(sec, new BPSymbolELF(sym)));
+
+ for (ELFFileBase *file : ctx.objectFiles)
+ for (Symbol *sym : file->getLocalSymbols())
+ if (auto *d = dyn_cast<Defined>(sym))
+ if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section))
+ if (sym->getSize() > 0)
+ sections.push_back(new BPSectionELF(sec, new BPSymbolELF(sym)));
+
+ auto reorderedSections =
+ lld::BPSectionOrdererBase::reorderSectionsByBalancedPartitioning(
+ highestAvailablePriority, profilePath, forFunctionCompression,
+ forDataCompression, compressionSortStartupFunctions, verbose,
+ sections);
+
+ DenseMap<const InputSectionBase *, int> result;
+ for (const auto &[BPSectionBase, priority] : reorderedSections) {
+ if (const BPSectionELF *elfSection =
+ dyn_cast<BPSectionELF>(BPSectionBase)) {
+ result[elfSection->getSymbol()->getInputSection()] =
+ static_cast<int>(priority);
----------------
ellishg wrote:
If you use `try_emplace()` then it won't override sections that you have already set earlier. I think this is what we want.
```suggestion
result.try_emplace(elfSection->getSymbol()->getInputSection(), static_cast<int>(priority));
```
https://github.com/llvm/llvm-project/pull/117514
More information about the llvm-commits
mailing list