ImportedFunctionsInliningStatistics.cpp 8.2 KB

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