Skip to content

Commit

Permalink
[lld-macho][nfc] Factor out callgraph parsing code
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
int3 committed Feb 16, 2022
1 parent bd1ebe9 commit 94c28d2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
39 changes: 20 additions & 19 deletions lld/MachO/InputFiles.cpp
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lld/MachO/InputFiles.h
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions lld/MachO/InputSection.h
Expand Up @@ -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";
Expand Down

0 comments on commit 94c28d2

Please sign in to comment.