ImportedFunctionsInliningStatistics.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //===-- ImportedFunctionsInliningStats.cpp ----------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // Generating inliner statistics for imported functions, mostly useful for
  9. // ThinLTO.
  10. //===----------------------------------------------------------------------===//
  11. #include "llvm/Analysis/Utils/ImportedFunctionsInliningStatistics.h"
  12. #include "llvm/ADT/STLExtras.h"
  13. #include "llvm/IR/Function.h"
  14. #include "llvm/IR/Module.h"
  15. #include "llvm/Support/CommandLine.h"
  16. #include "llvm/Support/Debug.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. #include <algorithm>
  19. #include <iomanip>
  20. #include <sstream>
  21. #include <string>
  22. using namespace llvm;
  23. cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats(
  24. "inliner-function-import-stats",
  25. cl::init(InlinerFunctionImportStatsOpts::No),
  26. cl::values(clEnumValN(InlinerFunctionImportStatsOpts::Basic, "basic",
  27. "basic statistics"),
  28. clEnumValN(InlinerFunctionImportStatsOpts::Verbose, "verbose",
  29. "printing of statistics for each inlined function")),
  30. cl::Hidden, cl::desc("Enable inliner stats for imported functions"));
  31. ImportedFunctionsInliningStatistics::InlineGraphNode &
  32. ImportedFunctionsInliningStatistics::createInlineGraphNode(const Function &F) {
  33. auto &ValueLookup = NodesMap[F.getName()];
  34. if (!ValueLookup) {
  35. ValueLookup = std::make_unique<InlineGraphNode>();
  36. ValueLookup->Imported = F.hasMetadata("thinlto_src_module");
  37. }
  38. return *ValueLookup;
  39. }
  40. void ImportedFunctionsInliningStatistics::recordInline(const Function &Caller,
  41. const Function &Callee) {
  42. InlineGraphNode &CallerNode = createInlineGraphNode(Caller);
  43. InlineGraphNode &CalleeNode = createInlineGraphNode(Callee);
  44. CalleeNode.NumberOfInlines++;
  45. if (!CallerNode.Imported && !CalleeNode.Imported) {
  46. // Direct inline from not imported callee to not imported caller, so we
  47. // don't have to add this to graph. It might be very helpful if you wanna
  48. // get the inliner statistics in compile step where there are no imported
  49. // functions. In this case the graph would be empty.
  50. CalleeNode.NumberOfRealInlines++;
  51. return;
  52. }
  53. CallerNode.InlinedCallees.push_back(&CalleeNode);
  54. if (!CallerNode.Imported) {
  55. // We could avoid second lookup, but it would make the code ultra ugly.
  56. auto It = NodesMap.find(Caller.getName());
  57. assert(It != NodesMap.end() && "The node should be already there.");
  58. // Save Caller as a starting node for traversal. The string has to be one
  59. // from map because Caller can disappear (and function name with it).
  60. NonImportedCallers.push_back(It->first());
  61. }
  62. }
  63. void ImportedFunctionsInliningStatistics::setModuleInfo(const Module &M) {
  64. ModuleName = M.getName();
  65. for (const auto &F : M.functions()) {
  66. if (F.isDeclaration())
  67. continue;
  68. AllFunctions++;
  69. ImportedFunctions += int(F.hasMetadata("thinlto_src_module"));
  70. }
  71. }
  72. static std::string getStatString(const char *Msg, int32_t Fraction, int32_t All,
  73. const char *PercentageOfMsg,
  74. bool LineEnd = true) {
  75. double Result = 0;
  76. if (All != 0)
  77. Result = 100 * static_cast<double>(Fraction) / All;
  78. std::stringstream Str;
  79. Str << std::setprecision(4) << Msg << ": " << Fraction << " [" << Result
  80. << "% of " << PercentageOfMsg << "]";
  81. if (LineEnd)
  82. Str << "\n";
  83. return Str.str();
  84. }
  85. void ImportedFunctionsInliningStatistics::dump(const bool Verbose) {
  86. calculateRealInlines();
  87. NonImportedCallers.clear();
  88. int32_t InlinedImportedFunctionsCount = 0;
  89. int32_t InlinedNotImportedFunctionsCount = 0;
  90. int32_t InlinedImportedFunctionsToImportingModuleCount = 0;
  91. int32_t InlinedNotImportedFunctionsToImportingModuleCount = 0;
  92. const auto SortedNodes = getSortedNodes();
  93. std::string Out;
  94. Out.reserve(5000);
  95. raw_string_ostream Ostream(Out);
  96. Ostream << "------- Dumping inliner stats for [" << ModuleName
  97. << "] -------\n";
  98. if (Verbose)
  99. Ostream << "-- List of inlined functions:\n";
  100. for (const auto &Node : SortedNodes) {
  101. assert(Node->second->NumberOfInlines >= Node->second->NumberOfRealInlines);
  102. if (Node->second->NumberOfInlines == 0)
  103. continue;
  104. if (Node->second->Imported) {
  105. InlinedImportedFunctionsCount++;
  106. InlinedImportedFunctionsToImportingModuleCount +=
  107. int(Node->second->NumberOfRealInlines > 0);
  108. } else {
  109. InlinedNotImportedFunctionsCount++;
  110. InlinedNotImportedFunctionsToImportingModuleCount +=
  111. int(Node->second->NumberOfRealInlines > 0);
  112. }
  113. if (Verbose)
  114. Ostream << "Inlined "
  115. << (Node->second->Imported ? "imported " : "not imported ")
  116. << "function [" << Node->first() << "]"
  117. << ": #inlines = " << Node->second->NumberOfInlines
  118. << ", #inlines_to_importing_module = "
  119. << Node->second->NumberOfRealInlines << "\n";
  120. }
  121. auto InlinedFunctionsCount =
  122. InlinedImportedFunctionsCount + InlinedNotImportedFunctionsCount;
  123. auto NotImportedFuncCount = AllFunctions - ImportedFunctions;
  124. auto ImportedNotInlinedIntoModule =
  125. ImportedFunctions - InlinedImportedFunctionsToImportingModuleCount;
  126. Ostream << "-- Summary:\n"
  127. << "All functions: " << AllFunctions
  128. << ", imported functions: " << ImportedFunctions << "\n"
  129. << getStatString("inlined functions", InlinedFunctionsCount,
  130. AllFunctions, "all functions")
  131. << getStatString("imported functions inlined anywhere",
  132. InlinedImportedFunctionsCount, ImportedFunctions,
  133. "imported functions")
  134. << getStatString("imported functions inlined into importing module",
  135. InlinedImportedFunctionsToImportingModuleCount,
  136. ImportedFunctions, "imported functions",
  137. /*LineEnd=*/false)
  138. << getStatString(", remaining", ImportedNotInlinedIntoModule,
  139. ImportedFunctions, "imported functions")
  140. << getStatString("non-imported functions inlined anywhere",
  141. InlinedNotImportedFunctionsCount,
  142. NotImportedFuncCount, "non-imported functions")
  143. << getStatString(
  144. "non-imported functions inlined into importing module",
  145. InlinedNotImportedFunctionsToImportingModuleCount,
  146. NotImportedFuncCount, "non-imported functions");
  147. Ostream.flush();
  148. dbgs() << Out;
  149. }
  150. void ImportedFunctionsInliningStatistics::calculateRealInlines() {
  151. // Removing duplicated Callers.
  152. llvm::sort(NonImportedCallers);
  153. NonImportedCallers.erase(
  154. std::unique(NonImportedCallers.begin(), NonImportedCallers.end()),
  155. NonImportedCallers.end());
  156. for (const auto &Name : NonImportedCallers) {
  157. auto &Node = *NodesMap[Name];
  158. if (!Node.Visited)
  159. dfs(Node);
  160. }
  161. }
  162. void ImportedFunctionsInliningStatistics::dfs(InlineGraphNode &GraphNode) {
  163. assert(!GraphNode.Visited);
  164. GraphNode.Visited = true;
  165. for (auto *const InlinedFunctionNode : GraphNode.InlinedCallees) {
  166. InlinedFunctionNode->NumberOfRealInlines++;
  167. if (!InlinedFunctionNode->Visited)
  168. dfs(*InlinedFunctionNode);
  169. }
  170. }
  171. ImportedFunctionsInliningStatistics::SortedNodesTy
  172. ImportedFunctionsInliningStatistics::getSortedNodes() {
  173. SortedNodesTy SortedNodes;
  174. SortedNodes.reserve(NodesMap.size());
  175. for (const NodesMapTy::value_type &Node : NodesMap)
  176. SortedNodes.push_back(&Node);
  177. llvm::sort(SortedNodes, [&](const SortedNodesTy::value_type &Lhs,
  178. const SortedNodesTy::value_type &Rhs) {
  179. if (Lhs->second->NumberOfInlines != Rhs->second->NumberOfInlines)
  180. return Lhs->second->NumberOfInlines > Rhs->second->NumberOfInlines;
  181. if (Lhs->second->NumberOfRealInlines != Rhs->second->NumberOfRealInlines)
  182. return Lhs->second->NumberOfRealInlines >
  183. Rhs->second->NumberOfRealInlines;
  184. return Lhs->first() < Rhs->first();
  185. });
  186. return SortedNodes;
  187. }