ModuleSummaryIndex.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. //===-- ModuleSummaryIndex.cpp - Module Summary Index ---------------------===//
  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. //
  9. // This file implements the module index and summary classes for the
  10. // IR library.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/IR/ModuleSummaryIndex.h"
  14. #include "llvm/ADT/SCCIterator.h"
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/ADT/StringMap.h"
  17. #include "llvm/Support/Path.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace llvm;
  20. #define DEBUG_TYPE "module-summary-index"
  21. STATISTIC(ReadOnlyLiveGVars,
  22. "Number of live global variables marked read only");
  23. FunctionSummary FunctionSummary::ExternalNode =
  24. FunctionSummary::makeDummyFunctionSummary({});
  25. bool ValueInfo::isDSOLocal() const {
  26. // Need to check all summaries are local in case of hash collisions.
  27. return getSummaryList().size() &&
  28. llvm::all_of(getSummaryList(),
  29. [](const std::unique_ptr<GlobalValueSummary> &Summary) {
  30. return Summary->isDSOLocal();
  31. });
  32. }
  33. bool ValueInfo::canAutoHide() const {
  34. // Can only auto hide if all copies are eligible to auto hide.
  35. return getSummaryList().size() &&
  36. llvm::all_of(getSummaryList(),
  37. [](const std::unique_ptr<GlobalValueSummary> &Summary) {
  38. return Summary->canAutoHide();
  39. });
  40. }
  41. // Gets the number of immutable refs in RefEdgeList
  42. unsigned FunctionSummary::immutableRefCount() const {
  43. // Here we take advantage of having all readonly references
  44. // located in the end of the RefEdgeList.
  45. auto Refs = refs();
  46. unsigned ImmutableRefCnt = 0;
  47. for (int I = Refs.size() - 1; I >= 0 && Refs[I].isReadOnly(); --I)
  48. ImmutableRefCnt++;
  49. return ImmutableRefCnt;
  50. }
  51. // Collect for the given module the list of function it defines
  52. // (GUID -> Summary).
  53. void ModuleSummaryIndex::collectDefinedFunctionsForModule(
  54. StringRef ModulePath, GVSummaryMapTy &GVSummaryMap) const {
  55. for (auto &GlobalList : *this) {
  56. auto GUID = GlobalList.first;
  57. for (auto &GlobSummary : GlobalList.second.SummaryList) {
  58. auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get());
  59. if (!Summary)
  60. // Ignore global variable, focus on functions
  61. continue;
  62. // Ignore summaries from other modules.
  63. if (Summary->modulePath() != ModulePath)
  64. continue;
  65. GVSummaryMap[GUID] = Summary;
  66. }
  67. }
  68. }
  69. GlobalValueSummary *
  70. ModuleSummaryIndex::getGlobalValueSummary(uint64_t ValueGUID,
  71. bool PerModuleIndex) const {
  72. auto VI = getValueInfo(ValueGUID);
  73. assert(VI && "GlobalValue not found in index");
  74. assert((!PerModuleIndex || VI.getSummaryList().size() == 1) &&
  75. "Expected a single entry per global value in per-module index");
  76. auto &Summary = VI.getSummaryList()[0];
  77. return Summary.get();
  78. }
  79. bool ModuleSummaryIndex::isGUIDLive(GlobalValue::GUID GUID) const {
  80. auto VI = getValueInfo(GUID);
  81. if (!VI)
  82. return true;
  83. const auto &SummaryList = VI.getSummaryList();
  84. if (SummaryList.empty())
  85. return true;
  86. for (auto &I : SummaryList)
  87. if (isGlobalValueLive(I.get()))
  88. return true;
  89. return false;
  90. }
  91. static void propagateConstantsToRefs(GlobalValueSummary *S) {
  92. // If reference is not readonly then referenced summary is not
  93. // readonly either. Note that:
  94. // - All references from GlobalVarSummary are conservatively considered as
  95. // not readonly. Tracking them properly requires more complex analysis
  96. // then we have now.
  97. //
  98. // - AliasSummary objects have no refs at all so this function is a no-op
  99. // for them.
  100. for (auto &VI : S->refs()) {
  101. if (VI.isReadOnly()) {
  102. // We only mark refs as readonly when computing function summaries on
  103. // analysis phase.
  104. assert(isa<FunctionSummary>(S));
  105. continue;
  106. }
  107. for (auto &Ref : VI.getSummaryList())
  108. // If references to alias is not readonly then aliasee is not readonly
  109. if (auto *GVS = dyn_cast<GlobalVarSummary>(Ref->getBaseObject()))
  110. GVS->setReadOnly(false);
  111. }
  112. }
  113. // Do the constant propagation in combined index.
  114. // The goal of constant propagation is internalization of readonly
  115. // variables. To determine which variables are readonly and which
  116. // are not we take following steps:
  117. // - During analysis we speculatively assign readonly attribute to
  118. // all variables which can be internalized. When computing function
  119. // summary we also assign readonly attribute to a reference if
  120. // function doesn't modify referenced variable.
  121. //
  122. // - After computing dead symbols in combined index we do the constant
  123. // propagation. During this step we clear readonly attribute from
  124. // all variables which:
  125. // a. are preserved or can't be imported
  126. // b. referenced by any global variable initializer
  127. // c. referenced by a function and reference is not readonly
  128. //
  129. // Internalization itself happens in the backend after import is finished
  130. // See internalizeImmutableGVs.
  131. void ModuleSummaryIndex::propagateConstants(
  132. const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
  133. for (auto &P : *this)
  134. for (auto &S : P.second.SummaryList) {
  135. if (!isGlobalValueLive(S.get()))
  136. // We don't examine references from dead objects
  137. continue;
  138. // Global variable can't be marked read only if it is not eligible
  139. // to import since we need to ensure that all external references
  140. // get a local (imported) copy. It also can't be marked read only
  141. // if it or any alias (since alias points to the same memory) are
  142. // preserved or notEligibleToImport, since either of those means
  143. // there could be writes that are not visible (because preserved
  144. // means it could have external to DSO writes, and notEligibleToImport
  145. // means it could have writes via inline assembly leading it to be
  146. // in the @llvm.*used).
  147. if (auto *GVS = dyn_cast<GlobalVarSummary>(S->getBaseObject()))
  148. // Here we intentionally pass S.get() not GVS, because S could be
  149. // an alias.
  150. if (!canImportGlobalVar(S.get()) || GUIDPreservedSymbols.count(P.first))
  151. GVS->setReadOnly(false);
  152. propagateConstantsToRefs(S.get());
  153. }
  154. if (llvm::AreStatisticsEnabled())
  155. for (auto &P : *this)
  156. if (P.second.SummaryList.size())
  157. if (auto *GVS = dyn_cast<GlobalVarSummary>(
  158. P.second.SummaryList[0]->getBaseObject()))
  159. if (isGlobalValueLive(GVS) && GVS->isReadOnly())
  160. ReadOnlyLiveGVars++;
  161. }
  162. // TODO: write a graphviz dumper for SCCs (see ModuleSummaryIndex::exportToDot)
  163. // then delete this function and update its tests
  164. LLVM_DUMP_METHOD
  165. void ModuleSummaryIndex::dumpSCCs(raw_ostream &O) {
  166. for (scc_iterator<ModuleSummaryIndex *> I =
  167. scc_begin<ModuleSummaryIndex *>(this);
  168. !I.isAtEnd(); ++I) {
  169. O << "SCC (" << utostr(I->size()) << " node" << (I->size() == 1 ? "" : "s")
  170. << ") {\n";
  171. for (const ValueInfo V : *I) {
  172. FunctionSummary *F = nullptr;
  173. if (V.getSummaryList().size())
  174. F = cast<FunctionSummary>(V.getSummaryList().front().get());
  175. O << " " << (F == nullptr ? "External" : "") << " " << utostr(V.getGUID())
  176. << (I.hasLoop() ? " (has loop)" : "") << "\n";
  177. }
  178. O << "}\n";
  179. }
  180. }
  181. namespace {
  182. struct Attributes {
  183. void add(const Twine &Name, const Twine &Value,
  184. const Twine &Comment = Twine());
  185. void addComment(const Twine &Comment);
  186. std::string getAsString() const;
  187. std::vector<std::string> Attrs;
  188. std::string Comments;
  189. };
  190. struct Edge {
  191. uint64_t SrcMod;
  192. int Hotness;
  193. GlobalValue::GUID Src;
  194. GlobalValue::GUID Dst;
  195. };
  196. }
  197. void Attributes::add(const Twine &Name, const Twine &Value,
  198. const Twine &Comment) {
  199. std::string A = Name.str();
  200. A += "=\"";
  201. A += Value.str();
  202. A += "\"";
  203. Attrs.push_back(A);
  204. addComment(Comment);
  205. }
  206. void Attributes::addComment(const Twine &Comment) {
  207. if (!Comment.isTriviallyEmpty()) {
  208. if (Comments.empty())
  209. Comments = " // ";
  210. else
  211. Comments += ", ";
  212. Comments += Comment.str();
  213. }
  214. }
  215. std::string Attributes::getAsString() const {
  216. if (Attrs.empty())
  217. return "";
  218. std::string Ret = "[";
  219. for (auto &A : Attrs)
  220. Ret += A + ",";
  221. Ret.pop_back();
  222. Ret += "];";
  223. Ret += Comments;
  224. return Ret;
  225. }
  226. static std::string linkageToString(GlobalValue::LinkageTypes LT) {
  227. switch (LT) {
  228. case GlobalValue::ExternalLinkage:
  229. return "extern";
  230. case GlobalValue::AvailableExternallyLinkage:
  231. return "av_ext";
  232. case GlobalValue::LinkOnceAnyLinkage:
  233. return "linkonce";
  234. case GlobalValue::LinkOnceODRLinkage:
  235. return "linkonce_odr";
  236. case GlobalValue::WeakAnyLinkage:
  237. return "weak";
  238. case GlobalValue::WeakODRLinkage:
  239. return "weak_odr";
  240. case GlobalValue::AppendingLinkage:
  241. return "appending";
  242. case GlobalValue::InternalLinkage:
  243. return "internal";
  244. case GlobalValue::PrivateLinkage:
  245. return "private";
  246. case GlobalValue::ExternalWeakLinkage:
  247. return "extern_weak";
  248. case GlobalValue::CommonLinkage:
  249. return "common";
  250. }
  251. return "<unknown>";
  252. }
  253. static std::string fflagsToString(FunctionSummary::FFlags F) {
  254. auto FlagValue = [](unsigned V) { return V ? '1' : '0'; };
  255. char FlagRep[] = {FlagValue(F.ReadNone), FlagValue(F.ReadOnly),
  256. FlagValue(F.NoRecurse), FlagValue(F.ReturnDoesNotAlias),
  257. FlagValue(F.NoInline), 0};
  258. return FlagRep;
  259. }
  260. // Get string representation of function instruction count and flags.
  261. static std::string getSummaryAttributes(GlobalValueSummary* GVS) {
  262. auto *FS = dyn_cast_or_null<FunctionSummary>(GVS);
  263. if (!FS)
  264. return "";
  265. return std::string("inst: ") + std::to_string(FS->instCount()) +
  266. ", ffl: " + fflagsToString(FS->fflags());
  267. }
  268. static std::string getNodeVisualName(GlobalValue::GUID Id) {
  269. return std::string("@") + std::to_string(Id);
  270. }
  271. static std::string getNodeVisualName(const ValueInfo &VI) {
  272. return VI.name().empty() ? getNodeVisualName(VI.getGUID()) : VI.name().str();
  273. }
  274. static std::string getNodeLabel(const ValueInfo &VI, GlobalValueSummary *GVS) {
  275. if (isa<AliasSummary>(GVS))
  276. return getNodeVisualName(VI);
  277. std::string Attrs = getSummaryAttributes(GVS);
  278. std::string Label =
  279. getNodeVisualName(VI) + "|" + linkageToString(GVS->linkage());
  280. if (!Attrs.empty())
  281. Label += std::string(" (") + Attrs + ")";
  282. Label += "}";
  283. return Label;
  284. }
  285. // Write definition of external node, which doesn't have any
  286. // specific module associated with it. Typically this is function
  287. // or variable defined in native object or library.
  288. static void defineExternalNode(raw_ostream &OS, const char *Pfx,
  289. const ValueInfo &VI, GlobalValue::GUID Id) {
  290. auto StrId = std::to_string(Id);
  291. OS << " " << StrId << " [label=\"";
  292. if (VI) {
  293. OS << getNodeVisualName(VI);
  294. } else {
  295. OS << getNodeVisualName(Id);
  296. }
  297. OS << "\"]; // defined externally\n";
  298. }
  299. static bool hasReadOnlyFlag(const GlobalValueSummary *S) {
  300. if (auto *GVS = dyn_cast<GlobalVarSummary>(S))
  301. return GVS->isReadOnly();
  302. return false;
  303. }
  304. void ModuleSummaryIndex::exportToDot(raw_ostream &OS) const {
  305. std::vector<Edge> CrossModuleEdges;
  306. DenseMap<GlobalValue::GUID, std::vector<uint64_t>> NodeMap;
  307. using GVSOrderedMapTy = std::map<GlobalValue::GUID, GlobalValueSummary *>;
  308. std::map<StringRef, GVSOrderedMapTy> ModuleToDefinedGVS;
  309. collectDefinedGVSummariesPerModule(ModuleToDefinedGVS);
  310. // Get node identifier in form MXXX_<GUID>. The MXXX prefix is required,
  311. // because we may have multiple linkonce functions summaries.
  312. auto NodeId = [](uint64_t ModId, GlobalValue::GUID Id) {
  313. return ModId == (uint64_t)-1 ? std::to_string(Id)
  314. : std::string("M") + std::to_string(ModId) +
  315. "_" + std::to_string(Id);
  316. };
  317. auto DrawEdge = [&](const char *Pfx, uint64_t SrcMod, GlobalValue::GUID SrcId,
  318. uint64_t DstMod, GlobalValue::GUID DstId,
  319. int TypeOrHotness) {
  320. // 0 - alias
  321. // 1 - reference
  322. // 2 - constant reference
  323. // Other value: (hotness - 3).
  324. TypeOrHotness += 3;
  325. static const char *EdgeAttrs[] = {
  326. " [style=dotted]; // alias",
  327. " [style=dashed]; // ref",
  328. " [style=dashed,color=forestgreen]; // const-ref",
  329. " // call (hotness : Unknown)",
  330. " [color=blue]; // call (hotness : Cold)",
  331. " // call (hotness : None)",
  332. " [color=brown]; // call (hotness : Hot)",
  333. " [style=bold,color=red]; // call (hotness : Critical)"};
  334. assert(static_cast<size_t>(TypeOrHotness) <
  335. sizeof(EdgeAttrs) / sizeof(EdgeAttrs[0]));
  336. OS << Pfx << NodeId(SrcMod, SrcId) << " -> " << NodeId(DstMod, DstId)
  337. << EdgeAttrs[TypeOrHotness] << "\n";
  338. };
  339. OS << "digraph Summary {\n";
  340. for (auto &ModIt : ModuleToDefinedGVS) {
  341. auto ModId = getModuleId(ModIt.first);
  342. OS << " // Module: " << ModIt.first << "\n";
  343. OS << " subgraph cluster_" << std::to_string(ModId) << " {\n";
  344. OS << " style = filled;\n";
  345. OS << " color = lightgrey;\n";
  346. OS << " label = \"" << sys::path::filename(ModIt.first) << "\";\n";
  347. OS << " node [style=filled,fillcolor=lightblue];\n";
  348. auto &GVSMap = ModIt.second;
  349. auto Draw = [&](GlobalValue::GUID IdFrom, GlobalValue::GUID IdTo, int Hotness) {
  350. if (!GVSMap.count(IdTo)) {
  351. CrossModuleEdges.push_back({ModId, Hotness, IdFrom, IdTo});
  352. return;
  353. }
  354. DrawEdge(" ", ModId, IdFrom, ModId, IdTo, Hotness);
  355. };
  356. for (auto &SummaryIt : GVSMap) {
  357. NodeMap[SummaryIt.first].push_back(ModId);
  358. auto Flags = SummaryIt.second->flags();
  359. Attributes A;
  360. if (isa<FunctionSummary>(SummaryIt.second)) {
  361. A.add("shape", "record", "function");
  362. } else if (isa<AliasSummary>(SummaryIt.second)) {
  363. A.add("style", "dotted,filled", "alias");
  364. A.add("shape", "box");
  365. } else {
  366. A.add("shape", "Mrecord", "variable");
  367. if (Flags.Live && hasReadOnlyFlag(SummaryIt.second))
  368. A.addComment("immutable");
  369. }
  370. if (Flags.DSOLocal)
  371. A.addComment("dsoLocal");
  372. if (Flags.CanAutoHide)
  373. A.addComment("canAutoHide");
  374. auto VI = getValueInfo(SummaryIt.first);
  375. A.add("label", getNodeLabel(VI, SummaryIt.second));
  376. if (!Flags.Live)
  377. A.add("fillcolor", "red", "dead");
  378. else if (Flags.NotEligibleToImport)
  379. A.add("fillcolor", "yellow", "not eligible to import");
  380. OS << " " << NodeId(ModId, SummaryIt.first) << " " << A.getAsString()
  381. << "\n";
  382. }
  383. OS << " // Edges:\n";
  384. for (auto &SummaryIt : GVSMap) {
  385. auto *GVS = SummaryIt.second;
  386. for (auto &R : GVS->refs())
  387. Draw(SummaryIt.first, R.getGUID(), R.isReadOnly() ? -1 : -2);
  388. if (auto *AS = dyn_cast_or_null<AliasSummary>(SummaryIt.second)) {
  389. Draw(SummaryIt.first, AS->getAliaseeGUID(), -3);
  390. continue;
  391. }
  392. if (auto *FS = dyn_cast_or_null<FunctionSummary>(SummaryIt.second))
  393. for (auto &CGEdge : FS->calls())
  394. Draw(SummaryIt.first, CGEdge.first.getGUID(),
  395. static_cast<int>(CGEdge.second.Hotness));
  396. }
  397. OS << " }\n";
  398. }
  399. OS << " // Cross-module edges:\n";
  400. for (auto &E : CrossModuleEdges) {
  401. auto &ModList = NodeMap[E.Dst];
  402. if (ModList.empty()) {
  403. defineExternalNode(OS, " ", getValueInfo(E.Dst), E.Dst);
  404. // Add fake module to the list to draw an edge to an external node
  405. // in the loop below.
  406. ModList.push_back(-1);
  407. }
  408. for (auto DstMod : ModList)
  409. // The edge representing call or ref is drawn to every module where target
  410. // symbol is defined. When target is a linkonce symbol there can be
  411. // multiple edges representing a single call or ref, both intra-module and
  412. // cross-module. As we've already drawn all intra-module edges before we
  413. // skip it here.
  414. if (DstMod != E.SrcMod)
  415. DrawEdge(" ", E.SrcMod, E.Src, DstMod, E.Dst, E.Hotness);
  416. }
  417. OS << "}";
  418. }