ModuleSummaryIndex.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. //===-- ModuleSummaryIndex.cpp - Module Summary Index ---------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the module index and summary classes for the
  11. // IR library.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/IR/ModuleSummaryIndex.h"
  15. #include "llvm/ADT/SCCIterator.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. FunctionSummary FunctionSummary::ExternalNode =
  21. FunctionSummary::makeDummyFunctionSummary({});
  22. bool ValueInfo::isDSOLocal() const {
  23. // Need to check all summaries are local in case of hash collisions.
  24. return getSummaryList().size() &&
  25. llvm::all_of(getSummaryList(),
  26. [](const std::unique_ptr<GlobalValueSummary> &Summary) {
  27. return Summary->isDSOLocal();
  28. });
  29. }
  30. // Gets the number of immutable refs in RefEdgeList
  31. unsigned FunctionSummary::immutableRefCount() const {
  32. // Here we take advantage of having all readonly references
  33. // located in the end of the RefEdgeList.
  34. auto Refs = refs();
  35. unsigned ImmutableRefCnt = 0;
  36. for (int I = Refs.size() - 1; I >= 0 && Refs[I].isReadOnly(); --I)
  37. ImmutableRefCnt++;
  38. return ImmutableRefCnt;
  39. }
  40. // Collect for the given module the list of function it defines
  41. // (GUID -> Summary).
  42. void ModuleSummaryIndex::collectDefinedFunctionsForModule(
  43. StringRef ModulePath, GVSummaryMapTy &GVSummaryMap) const {
  44. for (auto &GlobalList : *this) {
  45. auto GUID = GlobalList.first;
  46. for (auto &GlobSummary : GlobalList.second.SummaryList) {
  47. auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get());
  48. if (!Summary)
  49. // Ignore global variable, focus on functions
  50. continue;
  51. // Ignore summaries from other modules.
  52. if (Summary->modulePath() != ModulePath)
  53. continue;
  54. GVSummaryMap[GUID] = Summary;
  55. }
  56. }
  57. }
  58. // Collect for each module the list of function it defines (GUID -> Summary).
  59. void ModuleSummaryIndex::collectDefinedGVSummariesPerModule(
  60. StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries) const {
  61. for (auto &GlobalList : *this) {
  62. auto GUID = GlobalList.first;
  63. for (auto &Summary : GlobalList.second.SummaryList) {
  64. ModuleToDefinedGVSummaries[Summary->modulePath()][GUID] = Summary.get();
  65. }
  66. }
  67. }
  68. GlobalValueSummary *
  69. ModuleSummaryIndex::getGlobalValueSummary(uint64_t ValueGUID,
  70. bool PerModuleIndex) const {
  71. auto VI = getValueInfo(ValueGUID);
  72. assert(VI && "GlobalValue not found in index");
  73. assert((!PerModuleIndex || VI.getSummaryList().size() == 1) &&
  74. "Expected a single entry per global value in per-module index");
  75. auto &Summary = VI.getSummaryList()[0];
  76. return Summary.get();
  77. }
  78. bool ModuleSummaryIndex::isGUIDLive(GlobalValue::GUID GUID) const {
  79. auto VI = getValueInfo(GUID);
  80. if (!VI)
  81. return true;
  82. const auto &SummaryList = VI.getSummaryList();
  83. if (SummaryList.empty())
  84. return true;
  85. for (auto &I : SummaryList)
  86. if (isGlobalValueLive(I.get()))
  87. return true;
  88. return false;
  89. }
  90. static void propagateConstantsToRefs(GlobalValueSummary *S) {
  91. // If reference is not readonly then referenced summary is not
  92. // readonly either. Note that:
  93. // - All references from GlobalVarSummary are conservatively considered as
  94. // not readonly. Tracking them properly requires more complex analysis
  95. // then we have now.
  96. //
  97. // - AliasSummary objects have no refs at all so this function is a no-op
  98. // for them.
  99. for (auto &VI : S->refs()) {
  100. if (VI.isReadOnly()) {
  101. // We only mark refs as readonly when computing function summaries on
  102. // analysis phase.
  103. assert(isa<FunctionSummary>(S));
  104. continue;
  105. }
  106. for (auto &Ref : VI.getSummaryList())
  107. // If references to alias is not readonly then aliasee is not readonly
  108. if (auto *GVS = dyn_cast<GlobalVarSummary>(Ref->getBaseObject()))
  109. GVS->setReadOnly(false);
  110. }
  111. }
  112. // Do the constant propagation in combined index.
  113. // The goal of constant propagation is internalization of readonly
  114. // variables. To determine which variables are readonly and which
  115. // are not we take following steps:
  116. // - During analysis we speculatively assign readonly attribute to
  117. // all variables which can be internalized. When computing function
  118. // summary we also assign readonly attribute to a reference if
  119. // function doesn't modify referenced variable.
  120. //
  121. // - After computing dead symbols in combined index we do the constant
  122. // propagation. During this step we clear readonly attribute from
  123. // all variables which:
  124. // a. are dead, preserved or can't be imported
  125. // b. referenced by any global variable initializer
  126. // c. referenced by a function and reference is not readonly
  127. //
  128. // Internalization itself happens in the backend after import is finished
  129. // See internalizeImmutableGVs.
  130. void ModuleSummaryIndex::propagateConstants(
  131. const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
  132. for (auto &P : *this)
  133. for (auto &S : P.second.SummaryList) {
  134. if (!isGlobalValueLive(S.get()))
  135. // We don't examine references from dead objects
  136. continue;
  137. // Global variable can't be marked read only if it is not eligible
  138. // to import since we need to ensure that all external references
  139. // get a local (imported) copy. It also can't be marked read only
  140. // if it or any alias (since alias points to the same memory) are
  141. // preserved or notEligibleToImport, since either of those means
  142. // there could be writes that are not visible (because preserved
  143. // means it could have external to DSO writes, and notEligibleToImport
  144. // means it could have writes via inline assembly leading it to be
  145. // in the @llvm.*used).
  146. if (auto *GVS = dyn_cast<GlobalVarSummary>(S->getBaseObject()))
  147. // Here we intentionally pass S.get() not GVS, because S could be
  148. // an alias.
  149. if (!canImportGlobalVar(S.get()) || GUIDPreservedSymbols.count(P.first))
  150. GVS->setReadOnly(false);
  151. propagateConstantsToRefs(S.get());
  152. }
  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. StringMap<GVSummaryMapTy> ModuleToDefinedGVS;
  300. collectDefinedGVSummariesPerModule(ModuleToDefinedGVS);
  301. // Get node identifier in form MXXX_<GUID>. The MXXX prefix is required,
  302. // because we may have multiple linkonce functions summaries.
  303. auto NodeId = [](uint64_t ModId, GlobalValue::GUID Id) {
  304. return ModId == (uint64_t)-1 ? std::to_string(Id)
  305. : std::string("M") + std::to_string(ModId) +
  306. "_" + std::to_string(Id);
  307. };
  308. auto DrawEdge = [&](const char *Pfx, uint64_t SrcMod, GlobalValue::GUID SrcId,
  309. uint64_t DstMod, GlobalValue::GUID DstId,
  310. int TypeOrHotness) {
  311. // 0 - alias
  312. // 1 - reference
  313. // 2 - constant reference
  314. // Other value: (hotness - 3).
  315. TypeOrHotness += 3;
  316. static const char *EdgeAttrs[] = {
  317. " [style=dotted]; // alias",
  318. " [style=dashed]; // ref",
  319. " [style=dashed,color=forestgreen]; // const-ref",
  320. " // call (hotness : Unknown)",
  321. " [color=blue]; // call (hotness : Cold)",
  322. " // call (hotness : None)",
  323. " [color=brown]; // call (hotness : Hot)",
  324. " [style=bold,color=red]; // call (hotness : Critical)"};
  325. assert(static_cast<size_t>(TypeOrHotness) <
  326. sizeof(EdgeAttrs) / sizeof(EdgeAttrs[0]));
  327. OS << Pfx << NodeId(SrcMod, SrcId) << " -> " << NodeId(DstMod, DstId)
  328. << EdgeAttrs[TypeOrHotness] << "\n";
  329. };
  330. OS << "digraph Summary {\n";
  331. for (auto &ModIt : ModuleToDefinedGVS) {
  332. auto ModId = getModuleId(ModIt.first());
  333. OS << " // Module: " << ModIt.first() << "\n";
  334. OS << " subgraph cluster_" << std::to_string(ModId) << " {\n";
  335. OS << " style = filled;\n";
  336. OS << " color = lightgrey;\n";
  337. OS << " label = \"" << sys::path::filename(ModIt.first()) << "\";\n";
  338. OS << " node [style=filled,fillcolor=lightblue];\n";
  339. auto &GVSMap = ModIt.second;
  340. auto Draw = [&](GlobalValue::GUID IdFrom, GlobalValue::GUID IdTo, int Hotness) {
  341. if (!GVSMap.count(IdTo)) {
  342. CrossModuleEdges.push_back({ModId, Hotness, IdFrom, IdTo});
  343. return;
  344. }
  345. DrawEdge(" ", ModId, IdFrom, ModId, IdTo, Hotness);
  346. };
  347. for (auto &SummaryIt : GVSMap) {
  348. NodeMap[SummaryIt.first].push_back(ModId);
  349. auto Flags = SummaryIt.second->flags();
  350. Attributes A;
  351. if (isa<FunctionSummary>(SummaryIt.second)) {
  352. A.add("shape", "record", "function");
  353. } else if (isa<AliasSummary>(SummaryIt.second)) {
  354. A.add("style", "dotted,filled", "alias");
  355. A.add("shape", "box");
  356. } else {
  357. A.add("shape", "Mrecord", "variable");
  358. if (Flags.Live && hasReadOnlyFlag(SummaryIt.second))
  359. A.addComment("immutable");
  360. }
  361. auto VI = getValueInfo(SummaryIt.first);
  362. A.add("label", getNodeLabel(VI, SummaryIt.second));
  363. if (!Flags.Live)
  364. A.add("fillcolor", "red", "dead");
  365. else if (Flags.NotEligibleToImport)
  366. A.add("fillcolor", "yellow", "not eligible to import");
  367. OS << " " << NodeId(ModId, SummaryIt.first) << " " << A.getAsString()
  368. << "\n";
  369. }
  370. OS << " // Edges:\n";
  371. for (auto &SummaryIt : GVSMap) {
  372. auto *GVS = SummaryIt.second;
  373. for (auto &R : GVS->refs())
  374. Draw(SummaryIt.first, R.getGUID(), R.isReadOnly() ? -1 : -2);
  375. if (auto *AS = dyn_cast_or_null<AliasSummary>(SummaryIt.second)) {
  376. GlobalValue::GUID AliaseeId;
  377. if (AS->hasAliaseeGUID())
  378. AliaseeId = AS->getAliaseeGUID();
  379. else {
  380. auto AliaseeOrigId = AS->getAliasee().getOriginalName();
  381. AliaseeId = getGUIDFromOriginalID(AliaseeOrigId);
  382. if (!AliaseeId)
  383. AliaseeId = AliaseeOrigId;
  384. }
  385. Draw(SummaryIt.first, AliaseeId, -3);
  386. continue;
  387. }
  388. if (auto *FS = dyn_cast_or_null<FunctionSummary>(SummaryIt.second))
  389. for (auto &CGEdge : FS->calls())
  390. Draw(SummaryIt.first, CGEdge.first.getGUID(),
  391. static_cast<int>(CGEdge.second.Hotness));
  392. }
  393. OS << " }\n";
  394. }
  395. OS << " // Cross-module edges:\n";
  396. for (auto &E : CrossModuleEdges) {
  397. auto &ModList = NodeMap[E.Dst];
  398. if (ModList.empty()) {
  399. defineExternalNode(OS, " ", getValueInfo(E.Dst), E.Dst);
  400. // Add fake module to the list to draw an edge to an external node
  401. // in the loop below.
  402. ModList.push_back(-1);
  403. }
  404. for (auto DstMod : ModList)
  405. // The edge representing call or ref is drawn to every module where target
  406. // symbol is defined. When target is a linkonce symbol there can be
  407. // multiple edges representing a single call or ref, both intra-module and
  408. // cross-module. As we've already drawn all intra-module edges before we
  409. // skip it here.
  410. if (DstMod != E.SrcMod)
  411. DrawEdge(" ", E.SrcMod, E.Src, DstMod, E.Dst, E.Hotness);
  412. }
  413. OS << "}";
  414. }