[lld] 94c28d2 - [lld-macho][nfc] Factor out callgraph parsing code

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 15 18:16:09 PST 2022


Author: Jez Ng
Date: 2022-02-15T21:13:55-05:00
New Revision: 94c28d289aec0ceb2b774152f405123076918095

URL: https://github.com/llvm/llvm-project/commit/94c28d289aec0ceb2b774152f405123076918095
DIFF: https://github.com/llvm/llvm-project/commit/94c28d289aec0ceb2b774152f405123076918095.diff

LOG: [lld-macho][nfc] Factor out callgraph parsing code

`parseSections()` is a getting a bit large unwieldy, let's factor out
logic where we can.

Other minor changes in this diff:
* `"__cg_profile"` is now a global constexpr
* We now use `checkError()` instead of `fatal()`-ing without handling
  the Error
* Check for `callGraphProfileSort` before checking the section name,
  since the boolean comparison is likely cheaper

Reviewed By: #lld-macho, lgrey, oontvoo

Differential Revision: https://reviews.llvm.org/D119892

Added: 
    

Modified: 
    lld/MachO/InputFiles.cpp
    lld/MachO/InputFiles.h
    lld/MachO/InputSection.h

Removed: 
    


################################################################################
diff  --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index aa3567b778b7..12df200b7347 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -262,6 +262,24 @@ static Optional<size_t> getRecordSize(StringRef segname, StringRef name) {
   return {};
 }
 
+static Error parseCallGraph(ArrayRef<uint8_t> data,
+                            std::vector<CallGraphEntry> &callGraph) {
+  TimeTraceScope timeScope("Parsing call graph section");
+  BinaryStreamReader reader(data, support::little);
+  while (!reader.empty()) {
+    uint32_t fromIndex, toIndex;
+    uint64_t count;
+    if (Error err = reader.readInteger(fromIndex))
+      return err;
+    if (Error err = reader.readInteger(toIndex))
+      return err;
+    if (Error err = reader.readInteger(count))
+      return err;
+    callGraph.emplace_back(fromIndex, toIndex, count);
+  }
+  return Error::success();
+}
+
 // Parse the sequence of sections within a single LC_SEGMENT(_64).
 // Split each section into subsections.
 template <class SectionHeader>
@@ -320,25 +338,8 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
       if (name == section_names::compactUnwind)
         compactUnwindSection = sections.back();
     } else if (segname == segment_names::llvm) {
-      if (name == "__cg_profile" && config->callGraphProfileSort) {
-        TimeTraceScope timeScope("Parsing call graph section");
-        BinaryStreamReader reader(data, support::little);
-        while (!reader.empty()) {
-          uint32_t fromIndex, toIndex;
-          uint64_t count;
-          if (Error err = reader.readInteger(fromIndex))
-            fatal(toString(this) + ": Expected 32-bit integer");
-          if (Error err = reader.readInteger(toIndex))
-            fatal(toString(this) + ": Expected 32-bit integer");
-          if (Error err = reader.readInteger(count))
-            fatal(toString(this) + ": Expected 64-bit integer");
-          callGraph.emplace_back();
-          CallGraphEntry &entry = callGraph.back();
-          entry.fromIndex = fromIndex;
-          entry.toIndex = toIndex;
-          entry.count = count;
-        }
-      }
+      if (config->callGraphProfileSort && name == section_names::cgProfile)
+        checkError(parseCallGraph(data, callGraph));
       // ld64 does not appear to emit contents from sections within the __LLVM
       // segment. Symbols within those sections point to bitcode metadata
       // instead of actual symbols. Global symbols within those sections could

diff  --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h
index 432cddd94a50..66e4e086db47 100644
--- a/lld/MachO/InputFiles.h
+++ b/lld/MachO/InputFiles.h
@@ -86,6 +86,9 @@ struct CallGraphEntry {
   uint32_t toIndex;
   // Number of calls from callee to caller in the profile.
   uint64_t count;
+
+  CallGraphEntry(uint32_t fromIndex, uint32_t toIndex, uint64_t count)
+      : fromIndex(fromIndex), toIndex(toIndex), count(count) {}
 };
 
 class InputFile {

diff  --git a/lld/MachO/InputSection.h b/lld/MachO/InputSection.h
index 4d716e60caa4..49fadc24a2d5 100644
--- a/lld/MachO/InputSection.h
+++ b/lld/MachO/InputSection.h
@@ -283,6 +283,7 @@ constexpr const char binding[] = "__binding";
 constexpr const char bitcodeBundle[] = "__bundle";
 constexpr const char cString[] = "__cstring";
 constexpr const char cfString[] = "__cfstring";
+constexpr const char cgProfile[] = "__cg_profile";
 constexpr const char codeSignature[] = "__code_signature";
 constexpr const char common[] = "__common";
 constexpr const char compactUnwind[] = "__compact_unwind";


        


More information about the llvm-commits mailing list