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