ModuleSummaryIndex.cpp 16 KB

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