[lld] [lld][InstrProf] Profile guided function order (PR #96268)

Scott Todd via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 25 11:03:09 PDT 2024


================
@@ -0,0 +1,413 @@
+//===- 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 "InputSection.h"
+#include "lld/Common/ErrorHandler.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/ProfileData/InstrProfReader.h"
+#include "llvm/Support/BalancedPartitioning.h"
+#include "llvm/Support/TimeProfiler.h"
+#include "llvm/Support/VirtualFileSystem.h"
+#include "llvm/Support/xxhash.h"
+
+#define DEBUG_TYPE "bp-section-orderer"
+using namespace llvm;
+using namespace lld::macho;
+
+/// Symbols can be appended with "(.__uniq.xxxx)?.llvm.yyyy" where "xxxx" and
+/// "yyyy" are numbers that could change between builds. We need to use the root
+/// symbol name before this suffix so these symbols can be matched with profiles
+/// which may have different suffixes.
+static StringRef getRootSymbol(StringRef Name) {
+  auto [P0, S0] = Name.rsplit(".llvm.");
+  auto [P1, S1] = P0.rsplit(".__uniq.");
+  return P1;
+}
+
+static uint64_t getRelocHash(StringRef kind, uint64_t sectionIdx,
+                             uint64_t offset, uint64_t addend) {
+  return xxHash64((kind + ": " + Twine::utohexstr(sectionIdx) + " + " +
+                   Twine::utohexstr(offset) + " + " + Twine::utohexstr(addend))
+                      .str());
+}
+
+static uint64_t
+getRelocHash(const Reloc &reloc,
+             const DenseMap<const InputSection *, uint64_t> &sectionToIdx) {
+  auto *isec = reloc.getReferentInputSection();
+  std::optional<uint64_t> sectionIdx;
+  auto sectionIdxIt = sectionToIdx.find(isec);
+  if (sectionIdxIt != sectionToIdx.end())
+    sectionIdx = sectionIdxIt->getSecond();
+  std::string kind;
+  if (isec)
+    kind = ("Section " + Twine(isec->kind())).str();
+  if (auto *sym = reloc.referent.dyn_cast<Symbol *>()) {
+    kind += (" Symbol " + Twine(sym->kind())).str();
----------------
ScottTodd wrote:

These two lines are producing warnings/errors when compiled at least with clang-9:
```
llvm-project/lld/MachO/BPSectionOrderer.cpp:51:26: error: ambiguous conversion for functional-style cast from 'lld::macho::InputSection::Kind' to 'llvm::Twine'
    kind = ("Section " + Twine(isec->kind())).str();
                         ^~~~~~~~~~~~~~~~~~
/work/third_party/llvm-project/llvm/include/llvm/ADT/Twine.h:344:14: note: candidate constructor
    explicit Twine(unsigned char Val) : LHSKind(CharKind) {
             ^
/work/third_party/llvm-project/llvm/include/llvm/ADT/Twine.h:354:14: note: candidate constructor
    explicit Twine(int Val) : LHSKind(DecIKind) {
             ^
/work/third_party/llvm-project/llvm/include/llvm/ADT/Twine.h:334:14: note: candidate constructor
    explicit Twine(char Val) : LHSKind(CharKind) {
             ^
/work/third_party/llvm-project/llvm/include/llvm/ADT/Twine.h:339:14: note: candidate constructor
    explicit Twine(signed char Val) : LHSKind(CharKind) {
             ^
/work/third_party/llvm-project/llvm/include/llvm/ADT/Twine.h:349:14: note: candidate constructor
    explicit Twine(unsigned Val) : LHSKind(DecUIKind) {
             ^
/work/third_party/llvm-project/llvm/include/llvm/ADT/Twine.h:359:14: note: candidate constructor
    explicit Twine(const unsigned long &Val) : LHSKind(DecULKind) {
             ^
/work/third_party/llvm-project/llvm/include/llvm/ADT/Twine.h:364:14: note: candidate constructor
    explicit Twine(const long &Val) : LHSKind(DecLKind) {
             ^
/work/third_party/llvm-project/llvm/include/llvm/ADT/Twine.h:369:14: note: candidate constructor
    explicit Twine(const unsigned long long &Val) : LHSKind(DecULLKind) {
             ^
/work/third_party/llvm-project/llvm/include/llvm/ADT/Twine.h:374:14: note: candidate constructor
    explicit Twine(const long long &Val) : LHSKind(DecLLKind) {
             ^
1 error generated.
```
(downstream CI logs: https://github.com/iree-org/iree/actions/runs/10097500625/job/27922509801?pr=18004#step:4:6442)

The enum https://github.com/llvm/llvm-project/blob/88fb56ebf25de6288510364a4be3fbd7ae9a7246/lld/MachO/InputSection.h#L32-L38

needs an explicit conversion, e.g. 

```diff
llvm-project$ git diff
diff --git a/lld/MachO/BPSectionOrderer.cpp b/lld/MachO/BPSectionOrderer.cpp
index 26d4e0cb3987..f6a974370836 100644
--- a/lld/MachO/BPSectionOrderer.cpp
+++ b/lld/MachO/BPSectionOrderer.cpp
@@ -48,9 +48,9 @@ getRelocHash(const Reloc &reloc,
     sectionIdx = sectionIdxIt->getSecond();
   std::string kind;
   if (isec)
-    kind = ("Section " + Twine(isec->kind())).str();
+    kind = ("Section " + Twine((uint8_t)isec->kind())).str();
   if (auto *sym = reloc.referent.dyn_cast<Symbol *>()) {
-    kind += (" Symbol " + Twine(sym->kind())).str();
+    kind += (" Symbol " + Twine((uint8_t)sym->kind())).str();
     if (auto *d = dyn_cast<Defined>(sym)) {
       if (isa_and_nonnull<CStringInputSection>(isec))
         return getRelocHash(kind, 0, isec->getOffset(d->value), reloc.addend);
```

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


More information about the llvm-commits mailing list