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