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