[lld] [LLD][ELF] add bp-* options in ELF (PR #120514)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 5 19:09:26 PST 2025


================
@@ -0,0 +1,77 @@
+//===- BPSectionOrderer.cpp------------------------------------------------===//
+//
+// 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 "llvm/ADT/DenseMap.h"
+#include "llvm/Support/xxhash.h"
+
+#include "SymbolTable.h"
+#include "Symbols.h"
+
+using namespace llvm;
+using namespace lld::elf;
+
+void BPSectionELF::getSectionHashes(
+    llvm::SmallVectorImpl<uint64_t> &hashes,
+    const llvm::DenseMap<const void *, uint64_t> &sectionToIdx) const {
+  constexpr unsigned windowSize = 4;
+
+  size_t size = isec->content().size();
+  for (size_t i = 0; i < size; i++) {
+    auto window = isec->content().drop_front(i).take_front(windowSize);
+    hashes.push_back(xxHash64(window));
+  }
+
+  llvm::sort(hashes);
+  hashes.erase(std::unique(hashes.begin(), hashes.end()), hashes.end());
+}
+
+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
+  // BPSectionELF instances for balanced partitioning which follow the way
+  // '--symbol-ordering-file' does.
+  SmallVector<std::unique_ptr<BPSectionBase>> sections;
+
+  for (Symbol *sym : ctx.symtab->getSymbols())
----------------
MaskRay wrote:

If two non-local symbols are defined relative to the same section, the section will occur in `sections` twice. I think we need to refactor reorderSectionsByBalancedPartitioning so that symbols are sections are built by ELF/MachO instead of Common. I'll think about this more.

https://github.com/llvm/llvm-project/pull/120514


More information about the llvm-commits mailing list