Skip to content

Commit e1be36c

Browse files
committed
[llvm-jitlink] Generalize statistics gathering / reporting.
Moves the llvm-jitlink tool statistics out of the Session struct and into a new LLVMJITLinkStatistics class. Also removes the `-show-sizes` option. Each statistic added will now have its own option. The two previous stats (total size of all blocks before pruning and after fixups) are now available as -pre-prune-total-block-size and -post-fixup-total-block-size. This change should make it easier to add new statistics.
1 parent 1a16072 commit e1be36c

File tree

2 files changed

+114
-37
lines changed

2 files changed

+114
-37
lines changed

llvm/tools/llvm-jitlink/llvm-jitlink.cpp

Lines changed: 100 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,20 @@ static cl::opt<std::string> ShowLinkGraphs(
181181
"matching that regex after fixups have been applied"),
182182
cl::Optional, cl::cat(JITLinkCategory));
183183

184-
static cl::opt<bool> ShowSizes(
185-
"show-sizes",
186-
cl::desc("Show sizes pre- and post-dead stripping, and allocations"),
187-
cl::init(false), cl::cat(JITLinkCategory));
184+
enum class JITLinkStats {
185+
PrePruneTotalBlockSize,
186+
PostFixupTotalBlockSize
187+
};
188+
189+
static cl::list<JITLinkStats> ShowStats(
190+
cl::desc("Statistics:"),
191+
cl::values(
192+
clEnumValN(JITLinkStats::PrePruneTotalBlockSize,
193+
"pre-prune-total-block-size",
194+
"Total size of all blocks in all graphs (pre-pruning)"),
195+
clEnumValN(JITLinkStats::PostFixupTotalBlockSize,
196+
"post-fixup-total-block-size",
197+
"Tatal size of all blocks in all graphs (post-fixup)")));
188198

189199
static cl::opt<bool> ShowTimes("show-times",
190200
cl::desc("Show times for llvm-jitlink phases"),
@@ -388,13 +398,6 @@ static Error applyHarnessPromotions(Session &S, LinkGraph &G) {
388398
return Error::success();
389399
}
390400

391-
static uint64_t computeTotalBlockSizes(LinkGraph &G) {
392-
uint64_t TotalSize = 0;
393-
for (auto *B : G.blocks())
394-
TotalSize += B->getSize();
395-
return TotalSize;
396-
}
397-
398401
static void dumpSectionContents(raw_ostream &OS, LinkGraph &G) {
399402
constexpr orc::ExecutorAddrDiff DumpWidth = 16;
400403
static_assert(isPowerOf2_64(DumpWidth), "DumpWidth must be a power of two");
@@ -1106,17 +1109,6 @@ void Session::modifyPassConfig(const Triple &TT,
11061109
PassConfig.PrePrunePasses.push_back(
11071110
[this](LinkGraph &G) { return applyHarnessPromotions(*this, G); });
11081111

1109-
if (ShowSizes) {
1110-
PassConfig.PrePrunePasses.push_back([this](LinkGraph &G) -> Error {
1111-
SizeBeforePruning += computeTotalBlockSizes(G);
1112-
return Error::success();
1113-
});
1114-
PassConfig.PostFixupPasses.push_back([this](LinkGraph &G) -> Error {
1115-
SizeAfterFixups += computeTotalBlockSizes(G);
1116-
return Error::success();
1117-
});
1118-
}
1119-
11201112
if (ShowRelocatedSectionContents)
11211113
PassConfig.PostFixupPasses.push_back([](LinkGraph &G) -> Error {
11221114
outs() << "Relocated section contents for " << G.getName() << ":\n";
@@ -1934,17 +1926,87 @@ static Error addSelfRelocations(LinkGraph &G) {
19341926
return Error::success();
19351927
}
19361928

1937-
static void dumpSessionStats(Session &S) {
1938-
if (!ShowSizes)
1939-
return;
1929+
LLVMJITLinkStatistics::LLVMJITLinkStatistics(Session &S) {
1930+
1931+
class StatisticsPlugin : public ObjectLinkingLayer::Plugin {
1932+
public:
1933+
StatisticsPlugin(LLVMJITLinkStatistics &Stats) : Stats(Stats) {}
1934+
1935+
void modifyPassConfig(MaterializationResponsibility &MR, LinkGraph &G,
1936+
PassConfiguration &PassConfig) override {
1937+
PassConfig.PrePrunePasses.push_back(
1938+
[this](LinkGraph &G) { return Stats.recordPrePruneStats(G); });
1939+
PassConfig.PostFixupPasses.push_back(
1940+
[this](LinkGraph &G) { return Stats.recordPostFixupStats(G); });
1941+
}
1942+
1943+
Error notifyFailed(MaterializationResponsibility &MR) override {
1944+
return Error::success();
1945+
}
1946+
1947+
Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override {
1948+
return Error::success();
1949+
}
1950+
1951+
void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey,
1952+
ResourceKey SrcKey) override {}
1953+
1954+
private:
1955+
LLVMJITLinkStatistics &Stats;
1956+
};
1957+
1958+
S.ObjLayer.addPlugin(std::make_unique<StatisticsPlugin>(*this));
1959+
1960+
// Walk command line option and enable requested stats.
1961+
for (auto &Stat : ShowStats) {
1962+
switch (Stat) {
1963+
case JITLinkStats::PrePruneTotalBlockSize:
1964+
PrePruneTotalBlockSize = 0;
1965+
break;
1966+
case JITLinkStats::PostFixupTotalBlockSize:
1967+
PostFixupTotalBlockSize = 0;
1968+
break;
1969+
}
1970+
}
1971+
}
1972+
1973+
void LLVMJITLinkStatistics::print(raw_ostream &OS) {
1974+
19401975
if (!OrcRuntime.empty())
1941-
outs() << "Note: Session stats include runtime and entry point lookup, but "
1942-
"not JITDylib initialization/deinitialization.\n";
1943-
if (ShowSizes)
1944-
outs() << " Total size of all blocks before pruning: "
1945-
<< S.SizeBeforePruning
1946-
<< "\n Total size of all blocks after fixups: " << S.SizeAfterFixups
1947-
<< "\n";
1976+
OS << "Note: Session stats include runtime and entry point lookup, but "
1977+
"not JITDylib initialization/deinitialization.\n";
1978+
1979+
OS << "Statistics:\n";
1980+
if (PrePruneTotalBlockSize)
1981+
OS << " Total size of all blocks before pruning: "
1982+
<< *PrePruneTotalBlockSize << "\n";
1983+
1984+
if (PostFixupTotalBlockSize)
1985+
OS << " Total size of all blocks after fixups: "
1986+
<< *PostFixupTotalBlockSize << "\n";
1987+
}
1988+
1989+
static uint64_t computeTotalBlockSizes(LinkGraph &G) {
1990+
uint64_t TotalSize = 0;
1991+
for (auto *B : G.blocks())
1992+
TotalSize += B->getSize();
1993+
return TotalSize;
1994+
}
1995+
1996+
Error LLVMJITLinkStatistics::recordPrePruneStats(LinkGraph &G) {
1997+
std::lock_guard<std::mutex> Lock(M);
1998+
1999+
if (PrePruneTotalBlockSize)
2000+
*PrePruneTotalBlockSize += computeTotalBlockSizes(G);
2001+
return Error::success();
2002+
}
2003+
2004+
Error LLVMJITLinkStatistics::recordPostFixupStats(LinkGraph &G) {
2005+
std::lock_guard<std::mutex> Lock(M);
2006+
2007+
if (PostFixupTotalBlockSize)
2008+
*PostFixupTotalBlockSize += computeTotalBlockSizes(G);
2009+
return Error::success();
19482010
}
19492011

19502012
static Expected<ExecutorSymbolDef> getMainEntryPoint(Session &S) {
@@ -2038,6 +2100,10 @@ int main(int argc, char *argv[]) {
20382100

20392101
auto S = ExitOnErr(Session::Create(std::move(TT), std::move(Features)));
20402102

2103+
std::unique_ptr<LLVMJITLinkStatistics> Stats;
2104+
if (!ShowStats.empty())
2105+
Stats = std::make_unique<LLVMJITLinkStatistics>(*S);
2106+
20412107
{
20422108
TimeRegion TR(Timers ? &Timers->LoadObjectsTimer : nullptr);
20432109
ExitOnErr(addSessionInputs(*S));
@@ -2063,7 +2129,8 @@ int main(int argc, char *argv[]) {
20632129
if (ShowAddrs)
20642130
S->dumpSessionInfo(outs());
20652131

2066-
dumpSessionStats(*S);
2132+
if (Stats)
2133+
Stats->print(outs());
20672134

20682135
if (!EntryPoint) {
20692136
if (Timers)

llvm/tools/llvm-jitlink/llvm-jitlink.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929

3030
namespace llvm {
3131

32-
struct Session;
33-
3432
struct Session {
3533

3634
orc::ExecutionSession ES;
@@ -78,8 +76,6 @@ struct Session {
7876

7977
SymbolInfoMap SymbolInfos;
8078
FileInfoMap FileInfos;
81-
uint64_t SizeBeforePruning = 0;
82-
uint64_t SizeAfterFixups = 0;
8379

8480
StringSet<> HarnessFiles;
8581
StringSet<> HarnessExternals;
@@ -92,6 +88,20 @@ struct Session {
9288
Session(std::unique_ptr<orc::ExecutorProcessControl> EPC, Error &Err);
9389
};
9490

91+
class LLVMJITLinkStatistics {
92+
public:
93+
LLVMJITLinkStatistics(Session &S);
94+
void print(raw_ostream &OS);
95+
96+
private:
97+
Error recordPrePruneStats(jitlink::LinkGraph &G);
98+
Error recordPostFixupStats(jitlink::LinkGraph &G);
99+
100+
std::mutex M;
101+
std::optional<uint64_t> PrePruneTotalBlockSize;
102+
std::optional<uint64_t> PostFixupTotalBlockSize;
103+
};
104+
95105
/// Record symbols, GOT entries, stubs, and sections for ELF file.
96106
Error registerELFGraphInfo(Session &S, jitlink::LinkGraph &G);
97107

0 commit comments

Comments
 (0)