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/Support/CommandLine.h"
18#include "llvm/Support/Path.h"
19#include "llvm/Support/raw_ostream.h"
20using namespace llvm;
21
22#define DEBUG_TYPE "module-summary-index"
23
24STATISTIC(ReadOnlyLiveGVars,
25 "Number of live global variables marked read only");
26STATISTIC(WriteOnlyLiveGVars,
27 "Number of live global variables marked write only");
28
29static cl::opt<bool> PropagateAttrs("propagate-attrs", cl::init(Val: true),
30 cl::Hidden,
31 cl::desc("Propagate attributes in index"));
32
33static cl::opt<bool> ImportConstantsWithRefs(
34 "import-constants-with-refs", cl::init(Val: true), cl::Hidden,
35 cl::desc("Import constant global variables with references"));
36
37FunctionSummary FunctionSummary::ExternalNode =
38 FunctionSummary::makeDummyFunctionSummary(
39 Edges: SmallVector<FunctionSummary::EdgeTy, 0>());
40
41GlobalValue::VisibilityTypes ValueInfo::getELFVisibility() const {
42 bool HasProtected = false;
43 for (const auto &S : make_pointee_range(Range: getSummaryList())) {
44 if (S.getVisibility() == GlobalValue::HiddenVisibility)
45 return GlobalValue::HiddenVisibility;
46 if (S.getVisibility() == GlobalValue::ProtectedVisibility)
47 HasProtected = true;
48 }
49 return HasProtected ? GlobalValue::ProtectedVisibility
50 : GlobalValue::DefaultVisibility;
51}
52
53bool ValueInfo::isDSOLocal(bool WithDSOLocalPropagation) const {
54 // With DSOLocal propagation done, the flag in evey summary is the same.
55 // Check the first one is enough.
56 return WithDSOLocalPropagation
57 ? getSummaryList().size() && getSummaryList()[0]->isDSOLocal()
58 : getSummaryList().size() &&
59 llvm::all_of(
60 Range: getSummaryList(),
61 P: [](const std::unique_ptr<GlobalValueSummary> &Summary) {
62 return Summary->isDSOLocal();
63 });
64}
65
66bool ValueInfo::canAutoHide() const {
67 // Can only auto hide if all copies are eligible to auto hide.
68 return getSummaryList().size() &&
69 llvm::all_of(Range: getSummaryList(),
70 P: [](const std::unique_ptr<GlobalValueSummary> &Summary) {
71 return Summary->canAutoHide();
72 });
73}
74
75// Gets the number of readonly and writeonly refs in RefEdgeList
76std::pair<unsigned, unsigned> FunctionSummary::specialRefCounts() const {
77 // Here we take advantage of having all readonly and writeonly references
78 // located in the end of the RefEdgeList.
79 auto Refs = refs();
80 unsigned RORefCnt = 0, WORefCnt = 0;
81 int I;
82 for (I = Refs.size() - 1; I >= 0 && Refs[I].isWriteOnly(); --I)
83 WORefCnt++;
84 for (; I >= 0 && Refs[I].isReadOnly(); --I)
85 RORefCnt++;
86 return {RORefCnt, WORefCnt};
87}
88
89uint64_t ModuleSummaryIndex::getFlags() const {
90 uint64_t Flags = 0;
91 // Flags & 0x4 is reserved. DO NOT REUSE.
92 if (withGlobalValueDeadStripping())
93 Flags |= 0x1;
94 if (skipModuleByDistributedBackend())
95 Flags |= 0x2;
96 if (enableSplitLTOUnit())
97 Flags |= 0x8;
98 if (partiallySplitLTOUnits())
99 Flags |= 0x10;
100 if (withAttributePropagation())
101 Flags |= 0x20;
102 if (withDSOLocalPropagation())
103 Flags |= 0x40;
104 if (withWholeProgramVisibility())
105 Flags |= 0x80;
106 if (withSupportsHotColdNew())
107 Flags |= 0x100;
108 if (hasUnifiedLTO())
109 Flags |= 0x200;
110 if (withInternalizeAndPromote())
111 Flags |= 0x400;
112 return Flags;
113}
114
115void ModuleSummaryIndex::setFlags(uint64_t Flags) {
116 assert(Flags <= 0x7ff && "Unexpected bits in flag");
117 // 1 bit: WithGlobalValueDeadStripping flag.
118 // Set on combined index only.
119 if (Flags & 0x1)
120 setWithGlobalValueDeadStripping();
121 // 1 bit: SkipModuleByDistributedBackend flag.
122 // Set on combined index only.
123 if (Flags & 0x2)
124 setSkipModuleByDistributedBackend();
125 // Flags & 0x4 is reserved. DO NOT REUSE.
126 // 1 bit: DisableSplitLTOUnit flag.
127 // Set on per module indexes. It is up to the client to validate
128 // the consistency of this flag across modules being linked.
129 if (Flags & 0x8)
130 setEnableSplitLTOUnit();
131 // 1 bit: PartiallySplitLTOUnits flag.
132 // Set on combined index only.
133 if (Flags & 0x10)
134 setPartiallySplitLTOUnits();
135 // 1 bit: WithAttributePropagation flag.
136 // Set on combined index only.
137 if (Flags & 0x20)
138 setWithAttributePropagation();
139 // 1 bit: WithDSOLocalPropagation flag.
140 // Set on combined index only.
141 if (Flags & 0x40)
142 setWithDSOLocalPropagation();
143 // 1 bit: WithWholeProgramVisibility flag.
144 // Set on combined index only.
145 if (Flags & 0x80)
146 setWithWholeProgramVisibility();
147 // 1 bit: WithSupportsHotColdNew flag.
148 // Set on combined index only.
149 if (Flags & 0x100)
150 setWithSupportsHotColdNew();
151 // 1 bit: WithUnifiedLTO flag.
152 // Set on combined index only.
153 if (Flags & 0x200)
154 setUnifiedLTO();
155 // 1 bit: WithInternalizeAndPromote flag.
156 // Set on combined index only.
157 if (Flags & 0x400)
158 setWithInternalizeAndPromote();
159}
160
161// Collect for the given module the list of function it defines
162// (GUID -> Summary).
163void ModuleSummaryIndex::collectDefinedFunctionsForModule(
164 StringRef ModulePath, GVSummaryMapTy &GVSummaryMap) const {
165 for (auto &GlobalList : *this) {
166 auto GUID = GlobalList.first;
167 for (auto &GlobSummary : GlobalList.second.getSummaryList()) {
168 auto *Summary = dyn_cast_or_null<FunctionSummary>(Val: GlobSummary.get());
169 if (!Summary)
170 // Ignore global variable, focus on functions
171 continue;
172 // Ignore summaries from other modules.
173 if (Summary->modulePath() != ModulePath)
174 continue;
175 GVSummaryMap[GUID] = Summary;
176 }
177 }
178}
179
180GlobalValueSummary *
181ModuleSummaryIndex::getGlobalValueSummary(uint64_t ValueGUID,
182 bool PerModuleIndex) const {
183 auto VI = getValueInfo(GUID: ValueGUID);
184 assert(VI && "GlobalValue not found in index");
185 assert((!PerModuleIndex || VI.getSummaryList().size() == 1) &&
186 "Expected a single entry per global value in per-module index");
187 auto &Summary = VI.getSummaryList()[0];
188 return Summary.get();
189}
190
191bool ModuleSummaryIndex::isGUIDLive(GlobalValue::GUID GUID) const {
192 auto VI = getValueInfo(GUID);
193 if (!VI)
194 return true;
195 const auto &SummaryList = VI.getSummaryList();
196 if (SummaryList.empty())
197 return true;
198 for (auto &I : SummaryList)
199 if (isGlobalValueLive(GVS: I.get()))
200 return true;
201 return false;
202}
203
204static void
205propagateAttributesToRefs(GlobalValueSummary *S,
206 DenseSet<ValueInfo> &MarkedNonReadWriteOnly) {
207 // If reference is not readonly or writeonly then referenced summary is not
208 // read/writeonly either. Note that:
209 // - All references from GlobalVarSummary are conservatively considered as
210 // not readonly or writeonly. Tracking them properly requires more complex
211 // analysis then we have now.
212 //
213 // - AliasSummary objects have no refs at all so this function is a no-op
214 // for them.
215 for (auto &VI : S->refs()) {
216 assert(VI.getAccessSpecifier() == 0 || isa<FunctionSummary>(S));
217 if (!VI.getAccessSpecifier()) {
218 if (!MarkedNonReadWriteOnly.insert(V: VI).second)
219 continue;
220 } else if (MarkedNonReadWriteOnly.contains(V: VI))
221 continue;
222 bool HasNonGVar = false;
223 for (auto &Ref : VI.getSummaryList()) {
224 // If references to alias is not read/writeonly then aliasee
225 // is not read/writeonly
226 if (auto *GVS = dyn_cast<GlobalVarSummary>(Val: Ref->getBaseObject())) {
227 if (!VI.isReadOnly())
228 GVS->setReadOnly(false);
229 if (!VI.isWriteOnly())
230 GVS->setWriteOnly(false);
231 } else {
232 // Note that this needs special processing.
233 HasNonGVar = true;
234 break;
235 }
236 }
237 // In the case where we have a reference to a VI that is a function not a
238 // variable, conservatively mark all summaries as non-read or write only.
239 // In most cases that would have happened in the above loop. However,
240 // this will make a difference in a few rare cases where there are same
241 // named locals in modules without enough distinguishing path, which end up
242 // with the same GUID. If these are a mix of variables and functions we want
243 // to handle the variables conservatively.
244 if (HasNonGVar) {
245 for (auto &Ref : VI.getSummaryList()) {
246 auto *GVS = dyn_cast<GlobalVarSummary>(Val: Ref->getBaseObject());
247 if (!GVS)
248 continue;
249 GVS->setReadOnly(false);
250 GVS->setWriteOnly(false);
251 }
252 MarkedNonReadWriteOnly.insert(V: VI);
253 }
254 }
255}
256
257// Do the access attribute and DSOLocal propagation in combined index.
258// The goal of attribute propagation is internalization of readonly (RO)
259// or writeonly (WO) variables. To determine which variables are RO or WO
260// and which are not we take following steps:
261// - During analysis we speculatively assign readonly and writeonly
262// attribute to all variables which can be internalized. When computing
263// function summary we also assign readonly or writeonly attribute to a
264// reference if function doesn't modify referenced variable (readonly)
265// or doesn't read it (writeonly).
266//
267// - After computing dead symbols in combined index we do the attribute
268// and DSOLocal propagation. During this step we:
269// a. clear RO and WO attributes from variables which are preserved or
270// can't be imported
271// b. clear RO and WO attributes from variables referenced by any global
272// variable initializer
273// c. clear RO attribute from variable referenced by a function when
274// reference is not readonly
275// d. clear WO attribute from variable referenced by a function when
276// reference is not writeonly
277// e. clear RO and WO attributes from variables with the same GUID as
278// a non-variable.
279// f. clear IsDSOLocal flag in every summary if any of them is false.
280//
281// Because of (c, d) we don't internalize variables read by function A
282// and modified by function B.
283//
284// Internalization itself happens in the backend after import is finished
285// See internalizeGVsAfterImport.
286void ModuleSummaryIndex::propagateAttributes(
287 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
288 if (!PropagateAttrs)
289 return;
290 DenseSet<ValueInfo> MarkedNonReadWriteOnly;
291 for (auto &P : *this) {
292 bool IsDSOLocal = true;
293 for (auto &S : P.second.getSummaryList()) {
294 if (!isGlobalValueLive(GVS: S.get())) {
295 // computeDeadSymbolsAndUpdateIndirectCalls should have marked all
296 // copies live. Note that it is possible that there is a GUID collision
297 // between internal symbols with the same name in different files of the
298 // same name but not enough distinguishing path. Because
299 // computeDeadSymbolsAndUpdateIndirectCalls should conservatively mark
300 // all copies live we can assert here that all are dead if any copy is
301 // dead.
302 assert(llvm::none_of(
303 P.second.getSummaryList(),
304 [&](const std::unique_ptr<GlobalValueSummary> &Summary) {
305 return isGlobalValueLive(Summary.get());
306 }));
307 // We don't examine references from dead objects
308 break;
309 }
310
311 // Global variable can't be marked read/writeonly if it is not eligible
312 // to import since we need to ensure that all external references get
313 // a local (imported) copy. It also can't be marked read/writeonly if
314 // it or any alias (since alias points to the same memory) are preserved
315 // or notEligibleToImport, since either of those means there could be
316 // writes (or reads in case of writeonly) that are not visible (because
317 // preserved means it could have external to DSO writes or reads, and
318 // notEligibleToImport means it could have writes or reads via inline
319 // assembly leading it to be in the @llvm.*used).
320 if (auto *GVS = dyn_cast<GlobalVarSummary>(Val: S->getBaseObject()))
321 // Here we intentionally pass S.get() not GVS, because S could be
322 // an alias. We don't analyze references here, because we have to
323 // know exactly if GV is readonly to do so.
324 if (!canImportGlobalVar(S: S.get(), /* AnalyzeRefs */ false) ||
325 GUIDPreservedSymbols.count(V: P.first)) {
326 GVS->setReadOnly(false);
327 GVS->setWriteOnly(false);
328 }
329 propagateAttributesToRefs(S: S.get(), MarkedNonReadWriteOnly);
330
331 // If the flag from any summary is false, the GV is not DSOLocal.
332 IsDSOLocal &= S->isDSOLocal();
333 }
334 if (!IsDSOLocal)
335 // Mark the flag in all summaries false so that we can do quick check
336 // without going through the whole list.
337 for (const std::unique_ptr<GlobalValueSummary> &Summary :
338 P.second.getSummaryList())
339 Summary->setDSOLocal(false);
340 }
341 setWithAttributePropagation();
342 setWithDSOLocalPropagation();
343 if (llvm::AreStatisticsEnabled())
344 for (auto &P : *this)
345 if (P.second.getSummaryList().size())
346 if (auto *GVS = dyn_cast<GlobalVarSummary>(
347 Val: P.second.getSummaryList()[0]->getBaseObject()))
348 if (isGlobalValueLive(GVS)) {
349 if (GVS->maybeReadOnly())
350 ReadOnlyLiveGVars++;
351 if (GVS->maybeWriteOnly())
352 WriteOnlyLiveGVars++;
353 }
354}
355
356bool ModuleSummaryIndex::canImportGlobalVar(const GlobalValueSummary *S,
357 bool AnalyzeRefs) const {
358 bool CanImportDecl;
359 return canImportGlobalVar(S, AnalyzeRefs, CanImportDecl);
360}
361
362bool ModuleSummaryIndex::canImportGlobalVar(const GlobalValueSummary *S,
363 bool AnalyzeRefs,
364 bool &CanImportDecl) const {
365 auto HasRefsPreventingImport = [this](const GlobalVarSummary *GVS) {
366 // We don't analyze GV references during attribute propagation, so
367 // GV with non-trivial initializer can be marked either read or
368 // write-only.
369 // Importing definiton of readonly GV with non-trivial initializer
370 // allows us doing some extra optimizations (like converting indirect
371 // calls to direct).
372 // Definition of writeonly GV with non-trivial initializer should also
373 // be imported. Not doing so will result in:
374 // a) GV internalization in source module (because it's writeonly)
375 // b) Importing of GV declaration to destination module as a result
376 // of promotion.
377 // c) Link error (external declaration with internal definition).
378 // However we do not promote objects referenced by writeonly GV
379 // initializer by means of converting it to 'zeroinitializer'
380 return !(ImportConstantsWithRefs && GVS->isConstant()) &&
381 !isReadOnly(GVS) && !isWriteOnly(GVS) && GVS->refs().size();
382 };
383 auto *GVS = cast<GlobalVarSummary>(Val: S->getBaseObject());
384
385 const bool nonInterposable =
386 !GlobalValue::isInterposableLinkage(Linkage: S->linkage());
387 const bool eligibleToImport = !S->notEligibleToImport();
388
389 // It's correct to import a global variable only when it is not interposable
390 // and eligible to import.
391 CanImportDecl = (nonInterposable && eligibleToImport);
392
393 // Global variable with non-trivial initializer can be imported
394 // if it's readonly. This gives us extra opportunities for constant
395 // folding and converting indirect calls to direct calls. We don't
396 // analyze GV references during attribute propagation, because we
397 // don't know yet if it is readonly or not.
398 return nonInterposable && eligibleToImport &&
399 (!AnalyzeRefs || !HasRefsPreventingImport(GVS));
400}
401
402// TODO: write a graphviz dumper for SCCs (see ModuleSummaryIndex::exportToDot)
403// then delete this function and update its tests
404LLVM_DUMP_METHOD
405void ModuleSummaryIndex::dumpSCCs(raw_ostream &O) {
406 for (scc_iterator<ModuleSummaryIndex *> I =
407 scc_begin<ModuleSummaryIndex *>(G: this);
408 !I.isAtEnd(); ++I) {
409 O << "SCC (" << utostr(X: I->size()) << " node" << (I->size() == 1 ? "" : "s")
410 << ") {\n";
411 for (const ValueInfo &V : *I) {
412 FunctionSummary *F = nullptr;
413 if (V.getSummaryList().size())
414 F = cast<FunctionSummary>(Val: V.getSummaryList().front().get());
415 O << " " << (F == nullptr ? "External" : "") << " " << utostr(X: V.getGUID())
416 << (I.hasCycle() ? " (has cycle)" : "") << "\n";
417 }
418 O << "}\n";
419 }
420}
421
422namespace {
423struct Attributes {
424 void add(const Twine &Name, const Twine &Value,
425 const Twine &Comment = Twine());
426 void addComment(const Twine &Comment);
427 std::string getAsString() const;
428
429 std::vector<std::string> Attrs;
430 std::string Comments;
431};
432
433struct Edge {
434 uint64_t SrcMod;
435 int Hotness;
436 GlobalValue::GUID Src;
437 GlobalValue::GUID Dst;
438};
439} // namespace
440
441void Attributes::add(const Twine &Name, const Twine &Value,
442 const Twine &Comment) {
443 std::string A = Name.str();
444 A += "=\"";
445 A += Value.str();
446 A += "\"";
447 Attrs.push_back(x: A);
448 addComment(Comment);
449}
450
451void Attributes::addComment(const Twine &Comment) {
452 if (!Comment.isTriviallyEmpty()) {
453 if (Comments.empty())
454 Comments = " // ";
455 else
456 Comments += ", ";
457 Comments += Comment.str();
458 }
459}
460
461std::string Attributes::getAsString() const {
462 if (Attrs.empty())
463 return "";
464
465 std::string Ret = "[";
466 for (auto &A : Attrs)
467 Ret += A + ",";
468 Ret.pop_back();
469 Ret += "];";
470 Ret += Comments;
471 return Ret;
472}
473
474static std::string linkageToString(GlobalValue::LinkageTypes LT) {
475 switch (LT) {
476 case GlobalValue::ExternalLinkage:
477 return "extern";
478 case GlobalValue::AvailableExternallyLinkage:
479 return "av_ext";
480 case GlobalValue::LinkOnceAnyLinkage:
481 return "linkonce";
482 case GlobalValue::LinkOnceODRLinkage:
483 return "linkonce_odr";
484 case GlobalValue::WeakAnyLinkage:
485 return "weak";
486 case GlobalValue::WeakODRLinkage:
487 return "weak_odr";
488 case GlobalValue::AppendingLinkage:
489 return "appending";
490 case GlobalValue::InternalLinkage:
491 return "internal";
492 case GlobalValue::PrivateLinkage:
493 return "private";
494 case GlobalValue::ExternalWeakLinkage:
495 return "extern_weak";
496 case GlobalValue::CommonLinkage:
497 return "common";
498 }
499
500 return "<unknown>";
501}
502
503static std::string fflagsToString(FunctionSummary::FFlags F) {
504 auto FlagValue = [](unsigned V) { return V ? '1' : '0'; };
505 char FlagRep[] = {FlagValue(F.ReadNone),
506 FlagValue(F.ReadOnly),
507 FlagValue(F.NoRecurse),
508 FlagValue(F.ReturnDoesNotAlias),
509 FlagValue(F.NoInline),
510 FlagValue(F.AlwaysInline),
511 FlagValue(F.NoUnwind),
512 FlagValue(F.MayThrow),
513 FlagValue(F.HasUnknownCall),
514 FlagValue(F.MustBeUnreachable),
515 0};
516
517 return FlagRep;
518}
519
520// Get string representation of function instruction count and flags.
521static std::string getSummaryAttributes(GlobalValueSummary* GVS) {
522 auto *FS = dyn_cast_or_null<FunctionSummary>(Val: GVS);
523 if (!FS)
524 return "";
525
526 return std::string("inst: ") + std::to_string(val: FS->instCount()) +
527 ", ffl: " + fflagsToString(F: FS->fflags());
528}
529
530static std::string getNodeVisualName(GlobalValue::GUID Id) {
531 return std::string("@") + std::to_string(val: Id);
532}
533
534static std::string getNodeVisualName(const ValueInfo &VI) {
535 return VI.name().empty() ? getNodeVisualName(Id: VI.getGUID()) : VI.name().str();
536}
537
538static std::string getNodeLabel(const ValueInfo &VI, GlobalValueSummary *GVS) {
539 if (isa<AliasSummary>(Val: GVS))
540 return getNodeVisualName(VI);
541
542 std::string Attrs = getSummaryAttributes(GVS);
543 std::string Label =
544 getNodeVisualName(VI) + "|" + linkageToString(LT: GVS->linkage());
545 if (!Attrs.empty())
546 Label += std::string(" (") + Attrs + ")";
547 Label += "}";
548
549 return Label;
550}
551
552// Write definition of external node, which doesn't have any
553// specific module associated with it. Typically this is function
554// or variable defined in native object or library.
555static void defineExternalNode(raw_ostream &OS, const char *Pfx,
556 const ValueInfo &VI, GlobalValue::GUID Id) {
557 auto StrId = std::to_string(val: Id);
558 OS << " " << StrId << " [label=\"";
559
560 if (VI) {
561 OS << getNodeVisualName(VI);
562 } else {
563 OS << getNodeVisualName(Id);
564 }
565 OS << "\"]; // defined externally\n";
566}
567
568static bool hasReadOnlyFlag(const GlobalValueSummary *S) {
569 if (auto *GVS = dyn_cast<GlobalVarSummary>(Val: S))
570 return GVS->maybeReadOnly();
571 return false;
572}
573
574static bool hasWriteOnlyFlag(const GlobalValueSummary *S) {
575 if (auto *GVS = dyn_cast<GlobalVarSummary>(Val: S))
576 return GVS->maybeWriteOnly();
577 return false;
578}
579
580static bool hasConstantFlag(const GlobalValueSummary *S) {
581 if (auto *GVS = dyn_cast<GlobalVarSummary>(Val: S))
582 return GVS->isConstant();
583 return false;
584}
585
586void ModuleSummaryIndex::exportToDot(
587 raw_ostream &OS,
588 const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) const {
589 std::vector<Edge> CrossModuleEdges;
590 DenseMap<GlobalValue::GUID, std::vector<uint64_t>> NodeMap;
591 using GVSOrderedMapTy = std::map<GlobalValue::GUID, GlobalValueSummary *>;
592 std::map<StringRef, GVSOrderedMapTy> ModuleToDefinedGVS;
593 collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries&: ModuleToDefinedGVS);
594
595 // Assign an id to each module path for use in graph labels. Since the
596 // StringMap iteration order isn't guaranteed, order by path string before
597 // assigning ids.
598 std::vector<StringRef> ModulePaths;
599 for (auto &[ModPath, _] : modulePaths())
600 ModulePaths.push_back(x: ModPath);
601 llvm::sort(C&: ModulePaths);
602 DenseMap<StringRef, uint64_t> ModuleIdMap;
603 for (auto &ModPath : ModulePaths)
604 ModuleIdMap.try_emplace(Key: ModPath, Args: ModuleIdMap.size());
605
606 // Get node identifier in form MXXX_<GUID>. The MXXX prefix is required,
607 // because we may have multiple linkonce functions summaries.
608 auto NodeId = [](uint64_t ModId, GlobalValue::GUID Id) {
609 return ModId == (uint64_t)-1 ? std::to_string(val: Id)
610 : std::string("M") + std::to_string(val: ModId) +
611 "_" + std::to_string(val: Id);
612 };
613
614 auto DrawEdge = [&](const char *Pfx, uint64_t SrcMod, GlobalValue::GUID SrcId,
615 uint64_t DstMod, GlobalValue::GUID DstId,
616 int TypeOrHotness) {
617 // 0 - alias
618 // 1 - reference
619 // 2 - constant reference
620 // 3 - writeonly reference
621 // Other value: (hotness - 4).
622 TypeOrHotness += 4;
623 static const char *EdgeAttrs[] = {
624 " [style=dotted]; // alias",
625 " [style=dashed]; // ref",
626 " [style=dashed,color=forestgreen]; // const-ref",
627 " [style=dashed,color=violetred]; // writeOnly-ref",
628 " // call (hotness : Unknown)",
629 " [color=blue]; // call (hotness : Cold)",
630 " // call (hotness : None)",
631 " [color=brown]; // call (hotness : Hot)",
632 " [style=bold,color=red]; // call (hotness : Critical)"};
633
634 assert(static_cast<size_t>(TypeOrHotness) < std::size(EdgeAttrs));
635 OS << Pfx << NodeId(SrcMod, SrcId) << " -> " << NodeId(DstMod, DstId)
636 << EdgeAttrs[TypeOrHotness] << "\n";
637 };
638
639 OS << "digraph Summary {\n";
640 for (auto &ModIt : ModuleToDefinedGVS) {
641 // Will be empty for a just built per-module index, which doesn't setup a
642 // module paths table. In that case use 0 as the module id.
643 assert(ModuleIdMap.count(ModIt.first) || ModuleIdMap.empty());
644 auto ModId = ModuleIdMap.empty() ? 0 : ModuleIdMap[ModIt.first];
645 OS << " // Module: " << ModIt.first << "\n";
646 OS << " subgraph cluster_" << std::to_string(val: ModId) << " {\n";
647 OS << " style = filled;\n";
648 OS << " color = lightgrey;\n";
649 OS << " label = \"" << sys::path::filename(path: ModIt.first) << "\";\n";
650 OS << " node [style=filled,fillcolor=lightblue];\n";
651
652 auto &GVSMap = ModIt.second;
653 auto Draw = [&](GlobalValue::GUID IdFrom, GlobalValue::GUID IdTo, int Hotness) {
654 if (!GVSMap.count(x: IdTo)) {
655 CrossModuleEdges.push_back(x: {.SrcMod: ModId, .Hotness: Hotness, .Src: IdFrom, .Dst: IdTo});
656 return;
657 }
658 DrawEdge(" ", ModId, IdFrom, ModId, IdTo, Hotness);
659 };
660
661 for (auto &SummaryIt : GVSMap) {
662 NodeMap[SummaryIt.first].push_back(x: ModId);
663 auto Flags = SummaryIt.second->flags();
664 Attributes A;
665 if (isa<FunctionSummary>(Val: SummaryIt.second)) {
666 A.add(Name: "shape", Value: "record", Comment: "function");
667 } else if (isa<AliasSummary>(Val: SummaryIt.second)) {
668 A.add(Name: "style", Value: "dotted,filled", Comment: "alias");
669 A.add(Name: "shape", Value: "box");
670 } else {
671 A.add(Name: "shape", Value: "Mrecord", Comment: "variable");
672 if (Flags.Live && hasReadOnlyFlag(S: SummaryIt.second))
673 A.addComment(Comment: "immutable");
674 if (Flags.Live && hasWriteOnlyFlag(S: SummaryIt.second))
675 A.addComment(Comment: "writeOnly");
676 if (Flags.Live && hasConstantFlag(S: SummaryIt.second))
677 A.addComment(Comment: "constant");
678 }
679 if (Flags.Visibility)
680 A.addComment(Comment: "visibility");
681 if (Flags.DSOLocal)
682 A.addComment(Comment: "dsoLocal");
683 if (Flags.CanAutoHide)
684 A.addComment(Comment: "canAutoHide");
685 if (Flags.ImportType == GlobalValueSummary::ImportKind::Definition)
686 A.addComment(Comment: "definition");
687 else if (Flags.ImportType == GlobalValueSummary::ImportKind::Declaration)
688 A.addComment(Comment: "declaration");
689 if (GUIDPreservedSymbols.count(V: SummaryIt.first))
690 A.addComment(Comment: "preserved");
691
692 auto VI = getValueInfo(GUID: SummaryIt.first);
693 A.add(Name: "label", Value: getNodeLabel(VI, GVS: SummaryIt.second));
694 if (!Flags.Live)
695 A.add(Name: "fillcolor", Value: "red", Comment: "dead");
696 else if (Flags.NotEligibleToImport)
697 A.add(Name: "fillcolor", Value: "yellow", Comment: "not eligible to import");
698
699 OS << " " << NodeId(ModId, SummaryIt.first) << " " << A.getAsString()
700 << "\n";
701 }
702 OS << " // Edges:\n";
703
704 for (auto &SummaryIt : GVSMap) {
705 auto *GVS = SummaryIt.second;
706 for (auto &R : GVS->refs())
707 Draw(SummaryIt.first, R.getGUID(),
708 R.isWriteOnly() ? -1 : (R.isReadOnly() ? -2 : -3));
709
710 if (auto *AS = dyn_cast_or_null<AliasSummary>(Val: SummaryIt.second)) {
711 Draw(SummaryIt.first, AS->getAliaseeGUID(), -4);
712 continue;
713 }
714
715 if (auto *FS = dyn_cast_or_null<FunctionSummary>(Val: SummaryIt.second))
716 for (auto &CGEdge : FS->calls())
717 Draw(SummaryIt.first, CGEdge.first.getGUID(),
718 static_cast<int>(CGEdge.second.Hotness));
719 }
720 OS << " }\n";
721 }
722
723 OS << " // Cross-module edges:\n";
724 for (auto &E : CrossModuleEdges) {
725 auto &ModList = NodeMap[E.Dst];
726 if (ModList.empty()) {
727 defineExternalNode(OS, Pfx: " ", VI: getValueInfo(GUID: E.Dst), Id: E.Dst);
728 // Add fake module to the list to draw an edge to an external node
729 // in the loop below.
730 ModList.push_back(x: -1);
731 }
732 for (auto DstMod : ModList)
733 // The edge representing call or ref is drawn to every module where target
734 // symbol is defined. When target is a linkonce symbol there can be
735 // multiple edges representing a single call or ref, both intra-module and
736 // cross-module. As we've already drawn all intra-module edges before we
737 // skip it here.
738 if (DstMod != E.SrcMod)
739 DrawEdge(" ", E.SrcMod, E.Src, DstMod, E.Dst, E.Hotness);
740 }
741
742 OS << "}";
743}
744