@@ -181,10 +181,20 @@ static cl::opt<std::string> ShowLinkGraphs(
181
181
" matching that regex after fixups have been applied" ),
182
182
cl::Optional, cl::cat(JITLinkCategory));
183
183
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)" )));
188
198
189
199
static cl::opt<bool > ShowTimes (" show-times" ,
190
200
cl::desc (" Show times for llvm-jitlink phases" ),
@@ -388,13 +398,6 @@ static Error applyHarnessPromotions(Session &S, LinkGraph &G) {
388
398
return Error::success ();
389
399
}
390
400
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
-
398
401
static void dumpSectionContents (raw_ostream &OS, LinkGraph &G) {
399
402
constexpr orc::ExecutorAddrDiff DumpWidth = 16 ;
400
403
static_assert (isPowerOf2_64 (DumpWidth), " DumpWidth must be a power of two" );
@@ -1106,17 +1109,6 @@ void Session::modifyPassConfig(const Triple &TT,
1106
1109
PassConfig.PrePrunePasses .push_back (
1107
1110
[this ](LinkGraph &G) { return applyHarnessPromotions (*this , G); });
1108
1111
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
-
1120
1112
if (ShowRelocatedSectionContents)
1121
1113
PassConfig.PostFixupPasses .push_back ([](LinkGraph &G) -> Error {
1122
1114
outs () << " Relocated section contents for " << G.getName () << " :\n " ;
@@ -1934,17 +1926,87 @@ static Error addSelfRelocations(LinkGraph &G) {
1934
1926
return Error::success ();
1935
1927
}
1936
1928
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
+
1940
1975
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 ();
1948
2010
}
1949
2011
1950
2012
static Expected<ExecutorSymbolDef> getMainEntryPoint (Session &S) {
@@ -2038,6 +2100,10 @@ int main(int argc, char *argv[]) {
2038
2100
2039
2101
auto S = ExitOnErr (Session::Create (std::move (TT), std::move (Features)));
2040
2102
2103
+ std::unique_ptr<LLVMJITLinkStatistics> Stats;
2104
+ if (!ShowStats.empty ())
2105
+ Stats = std::make_unique<LLVMJITLinkStatistics>(*S);
2106
+
2041
2107
{
2042
2108
TimeRegion TR (Timers ? &Timers->LoadObjectsTimer : nullptr );
2043
2109
ExitOnErr (addSessionInputs (*S));
@@ -2063,7 +2129,8 @@ int main(int argc, char *argv[]) {
2063
2129
if (ShowAddrs)
2064
2130
S->dumpSessionInfo (outs ());
2065
2131
2066
- dumpSessionStats (*S);
2132
+ if (Stats)
2133
+ Stats->print (outs ());
2067
2134
2068
2135
if (!EntryPoint) {
2069
2136
if (Timers)
0 commit comments