[lld] [lld][ELF] Extend profile guided function ordering to ELF binaries (PR #117514)

Ellis Hoag via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 10 11:12:10 PST 2024


================
@@ -14,13 +14,140 @@
 #ifndef LLD_MACHO_BPSECTION_ORDERER_H
 #define LLD_MACHO_BPSECTION_ORDERER_H
 
+#include "InputSection.h"
+#include "Relocations.h"
+#include "Symbols.h"
+#include "lld/Common/BPSectionOrdererBase.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/TinyPtrVector.h"
 
 namespace lld::macho {
 
 class InputSection;
 
+class BPSymbolMacho : public BPSymbol {
+  const Symbol *sym;
+
+public:
+  explicit BPSymbolMacho(const Symbol *s) : sym(s) {}
+
+  llvm::StringRef getName() const override { return sym->getName(); }
+
+  const Defined *asDefined() const {
+    return llvm::dyn_cast_or_null<Defined>(sym);
+  }
+
+  BPSymbol *asDefinedSymbol() override { return asDefined() ? this : nullptr; }
+
+  std::optional<uint64_t> getValue() const override {
+    if (auto *d = asDefined())
+      return d->value;
+    return {};
+  }
+
+  std::optional<uint64_t> getSize() const override {
+    if (auto *d = asDefined())
+      return d->size;
+    return {};
+  }
+
+  const Symbol *getSymbol() const { return sym; }
+};
+
+class BPSectionMacho : public BPSectionBase {
+  const InputSection *isec;
+  uint64_t sectionIdx;
+  mutable std::vector<std::unique_ptr<BPSymbol>> symbols;
+
+public:
+  explicit BPSectionMacho(const InputSection *sec, uint64_t sectionIdx)
+      : isec(sec), sectionIdx(sectionIdx) {}
+
+  const InputSection *getSection() const { return isec; }
+
+  llvm::StringRef getName() const override { return isec->getName(); }
+
+  uint64_t getSize() const override { return isec->getSize(); }
+
+  uint64_t getSectionIdx() const { return sectionIdx; }
+
+  bool isCodeSection() const override { return macho::isCodeSection(isec); }
+
+  bool hasValidData() const override {
+    return isec && !isec->data.empty() && isec->data.data();
+  }
+
+  llvm::ArrayRef<uint8_t> getSectionData() const override { return isec->data; }
+
+  llvm::ArrayRef<std::unique_ptr<BPSymbol>> getSymbols() const override {
+    for (auto *d : isec->symbols) {
+      symbols.emplace_back(std::make_unique<BPSymbolMacho>(d));
+    }
+    return symbols;
+  }
----------------
ellishg wrote:

This is only safe if called once. We could check if `symbols` is non-empty first, but then it would not be thread-safe and could cause bugs in the future. It might just be safest to return a `SmallVector<BPSymbol>`, which would have the same overhead as this anyway.

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


More information about the llvm-commits mailing list