22#define DEBUG_TYPE "module-summary-index"
25 "Number of live global variables marked read only");
27 "Number of live global variables marked write only");
31 cl::desc(
"Propagate attributes in index"));
35 cl::desc(
"Import constant global variables with references"));
43 bool HasProtected =
false;
57 return WithDSOLocalPropagation
62 [](
const std::unique_ptr<GlobalValueSummary> &Summary) {
63 return Summary->isDSOLocal();
71 [](
const std::unique_ptr<GlobalValueSummary> &Summary) {
72 return Summary->canAutoHide();
81 unsigned RORefCnt = 0, WORefCnt = 0;
83 for (
I = Refs.size() - 1;
I >= 0 && Refs[
I].isWriteOnly(); --
I)
85 for (;
I >= 0 && Refs[
I].isReadOnly(); --
I)
87 return {RORefCnt, WORefCnt};
118 assert(Flags <= 0x2ff &&
"Unexpected bits in flag");
166 for (
auto &GlobalList : *
this) {
167 auto GUID = GlobalList.first;
168 for (
auto &GlobSummary : GlobalList.second.SummaryList) {
169 auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get());
174 if (Summary->modulePath() != ModulePath)
176 GVSummaryMap[GUID] = Summary;
183 bool PerModuleIndex)
const {
185 assert(VI &&
"GlobalValue not found in index");
186 assert((!PerModuleIndex || VI.getSummaryList().size() == 1) &&
187 "Expected a single entry per global value in per-module index");
188 auto &Summary = VI.getSummaryList()[0];
189 return Summary.get();
196 const auto &SummaryList = VI.getSummaryList();
197 if (SummaryList.empty())
199 for (
auto &
I : SummaryList)
216 for (
auto &VI : S->
refs()) {
217 assert(VI.getAccessSpecifier() == 0 || isa<FunctionSummary>(S));
218 if (!VI.getAccessSpecifier()) {
219 if (!MarkedNonReadWriteOnly.
insert(VI).second)
221 }
else if (MarkedNonReadWriteOnly.
contains(VI))
223 for (
auto &
Ref : VI.getSummaryList())
226 if (
auto *GVS = dyn_cast<GlobalVarSummary>(
Ref->getBaseObject())) {
227 if (!VI.isReadOnly())
228 GVS->setReadOnly(
false);
229 if (!VI.isWriteOnly())
230 GVS->setWriteOnly(
false);
267 for (
auto &
P : *
this) {
268 bool IsDSOLocal =
true;
269 for (
auto &S :
P.second.SummaryList) {
279 P.second.SummaryList,
280 [&](
const std::unique_ptr<GlobalValueSummary> &Summary) {
281 return isGlobalValueLive(Summary.get());
296 if (
auto *GVS = dyn_cast<GlobalVarSummary>(S->getBaseObject()))
301 GUIDPreservedSymbols.
count(
P.first)) {
302 GVS->setReadOnly(
false);
303 GVS->setWriteOnly(
false);
308 IsDSOLocal &= S->isDSOLocal();
313 for (
const std::unique_ptr<GlobalValueSummary> &Summary :
314 P.second.SummaryList)
315 Summary->setDSOLocal(
false);
320 for (
auto &
P : *
this)
321 if (
P.second.SummaryList.size())
322 if (
auto *GVS = dyn_cast<GlobalVarSummary>(
323 P.second.SummaryList[0]->getBaseObject()))
325 if (GVS->maybeReadOnly())
327 if (GVS->maybeWriteOnly())
328 WriteOnlyLiveGVars++;
333 bool AnalyzeRefs)
const {
361 (!AnalyzeRefs || !HasRefsPreventingImport(GVS));
369 scc_begin<ModuleSummaryIndex *>(
this);
371 O <<
"SCC (" << utostr(
I->size()) <<
" node" << (
I->size() == 1 ?
"" :
"s")
375 if (V.getSummaryList().size())
376 F = cast<FunctionSummary>(V.getSummaryList().front().get());
377 O <<
" " << (
F ==
nullptr ?
"External" :
"") <<
" " << utostr(V.getGUID())
378 << (
I.hasCycle() ?
" (has cycle)" :
"") <<
"\n";
388 void addComment(
const Twine &Comment);
389 std::string getAsString()
const;
391 std::vector<std::string> Attrs;
392 std::string Comments;
404 const Twine &Comment) {
405 std::string
A =
Name.str();
413void Attributes::addComment(
const Twine &Comment) {
414 if (!
Comment.isTriviallyEmpty()) {
415 if (Comments.empty())
423std::string Attributes::getAsString()
const {
427 std::string
Ret =
"[";
428 for (
auto &
A : Attrs)
445 return "linkonce_odr";
457 return "extern_weak";
466 auto FlagValue = [](
unsigned V) {
return V ?
'1' :
'0'; };
467 char FlagRep[] = {FlagValue(
F.ReadNone),
468 FlagValue(
F.ReadOnly),
469 FlagValue(
F.NoRecurse),
470 FlagValue(
F.ReturnDoesNotAlias),
471 FlagValue(
F.NoInline),
472 FlagValue(
F.AlwaysInline),
473 FlagValue(
F.NoUnwind),
474 FlagValue(
F.MayThrow),
475 FlagValue(
F.HasUnknownCall),
476 FlagValue(
F.MustBeUnreachable),
484 auto *FS = dyn_cast_or_null<FunctionSummary>(GVS);
488 return std::string(
"inst: ") + std::to_string(FS->instCount()) +
493 return std::string(
"@") + std::to_string(Id);
501 if (isa<AliasSummary>(GVS))
508 Label += std::string(
" (") + Attrs +
")";
519 auto StrId = std::to_string(Id);
520 OS <<
" " << StrId <<
" [label=\"";
527 OS <<
"\"]; // defined externally\n";
531 if (
auto *GVS = dyn_cast<GlobalVarSummary>(S))
532 return GVS->maybeReadOnly();
537 if (
auto *GVS = dyn_cast<GlobalVarSummary>(S))
538 return GVS->maybeWriteOnly();
543 if (
auto *GVS = dyn_cast<GlobalVarSummary>(S))
544 return GVS->isConstant();
551 std::vector<Edge> CrossModuleEdges;
553 using GVSOrderedMapTy = std::map<GlobalValue::GUID, GlobalValueSummary *>;
554 std::map<StringRef, GVSOrderedMapTy> ModuleToDefinedGVS;
560 std::vector<StringRef> ModulePaths;
562 ModulePaths.push_back(ModPath);
565 for (
auto &ModPath : ModulePaths)
571 return ModId == (
uint64_t)-1 ? std::to_string(Id)
572 : std::string(
"M") + std::to_string(ModId) +
573 "_" + std::to_string(Id);
585 static const char *EdgeAttrs[] = {
586 " [style=dotted]; // alias",
587 " [style=dashed]; // ref",
588 " [style=dashed,color=forestgreen]; // const-ref",
589 " [style=dashed,color=violetred]; // writeOnly-ref",
590 " // call (hotness : Unknown)",
591 " [color=blue]; // call (hotness : Cold)",
592 " // call (hotness : None)",
593 " [color=brown]; // call (hotness : Hot)",
594 " [style=bold,color=red]; // call (hotness : Critical)"};
596 assert(
static_cast<size_t>(TypeOrHotness) < std::size(EdgeAttrs));
597 OS << Pfx << NodeId(SrcMod, SrcId) <<
" -> " << NodeId(DstMod, DstId)
598 << EdgeAttrs[TypeOrHotness] <<
"\n";
601 OS <<
"digraph Summary {\n";
602 for (
auto &ModIt : ModuleToDefinedGVS) {
606 auto ModId = ModuleIdMap.
empty() ? 0 : ModuleIdMap[ModIt.first];
607 OS <<
" // Module: " << ModIt.first <<
"\n";
608 OS <<
" subgraph cluster_" << std::to_string(ModId) <<
" {\n";
609 OS <<
" style = filled;\n";
610 OS <<
" color = lightgrey;\n";
612 OS <<
" node [style=filled,fillcolor=lightblue];\n";
614 auto &GVSMap = ModIt.second;
616 if (!GVSMap.count(IdTo)) {
617 CrossModuleEdges.push_back({ModId, Hotness, IdFrom, IdTo});
620 DrawEdge(
" ", ModId, IdFrom, ModId, IdTo, Hotness);
623 for (
auto &SummaryIt : GVSMap) {
624 NodeMap[SummaryIt.first].push_back(ModId);
625 auto Flags = SummaryIt.second->flags();
627 if (isa<FunctionSummary>(SummaryIt.second)) {
628 A.add(
"shape",
"record",
"function");
629 }
else if (isa<AliasSummary>(SummaryIt.second)) {
630 A.add(
"style",
"dotted,filled",
"alias");
631 A.add(
"shape",
"box");
633 A.add(
"shape",
"Mrecord",
"variable");
635 A.addComment(
"immutable");
637 A.addComment(
"writeOnly");
639 A.addComment(
"constant");
641 if (Flags.Visibility)
642 A.addComment(
"visibility");
644 A.addComment(
"dsoLocal");
645 if (Flags.CanAutoHide)
646 A.addComment(
"canAutoHide");
648 A.addComment(
"definition");
650 A.addComment(
"declaration");
651 if (GUIDPreservedSymbols.
count(SummaryIt.first))
652 A.addComment(
"preserved");
657 A.add(
"fillcolor",
"red",
"dead");
658 else if (Flags.NotEligibleToImport)
659 A.add(
"fillcolor",
"yellow",
"not eligible to import");
661 OS <<
" " << NodeId(ModId, SummaryIt.first) <<
" " <<
A.getAsString()
664 OS <<
" // Edges:\n";
666 for (
auto &SummaryIt : GVSMap) {
667 auto *GVS = SummaryIt.second;
668 for (
auto &R : GVS->refs())
669 Draw(SummaryIt.first, R.getGUID(),
670 R.isWriteOnly() ? -1 : (R.isReadOnly() ? -2 : -3));
672 if (
auto *AS = dyn_cast_or_null<AliasSummary>(SummaryIt.second)) {
673 Draw(SummaryIt.first, AS->getAliaseeGUID(), -4);
677 if (
auto *FS = dyn_cast_or_null<FunctionSummary>(SummaryIt.second))
678 for (
auto &CGEdge : FS->calls())
679 Draw(SummaryIt.first, CGEdge.first.getGUID(),
680 static_cast<int>(CGEdge.second.Hotness));
685 OS <<
" // Cross-module edges:\n";
686 for (
auto &E : CrossModuleEdges) {
687 auto &ModList = NodeMap[E.Dst];
688 if (ModList.empty()) {
692 ModList.push_back(-1);
694 for (
auto DstMod : ModList)
700 if (DstMod != E.SrcMod)
701 DrawEdge(
" ", E.SrcMod, E.Src, DstMod, E.Dst, E.Hotness);
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
static std::string getNodeVisualName(GlobalValue::GUID Id)
static std::string getNodeLabel(const ValueInfo &VI, GlobalValueSummary *GVS)
static std::string getSummaryAttributes(GlobalValueSummary *GVS)
static cl::opt< bool > ImportConstantsWithRefs("import-constants-with-refs", cl::init(true), cl::Hidden, cl::desc("Import constant global variables with references"))
static std::string fflagsToString(FunctionSummary::FFlags F)
static bool hasWriteOnlyFlag(const GlobalValueSummary *S)
static void propagateAttributesToRefs(GlobalValueSummary *S, DenseSet< ValueInfo > &MarkedNonReadWriteOnly)
static std::string linkageToString(GlobalValue::LinkageTypes LT)
static void defineExternalNode(raw_ostream &OS, const char *Pfx, const ValueInfo &VI, GlobalValue::GUID Id)
static cl::opt< bool > PropagateAttrs("propagate-attrs", cl::init(true), cl::Hidden, cl::desc("Propagate attributes in index"))
static bool hasReadOnlyFlag(const GlobalValueSummary *S)
static bool hasConstantFlag(const GlobalValueSummary *S)
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Implements a dense probed hash-table based set.
Function summary information to aid decisions and implementation of importing.
static FunctionSummary ExternalNode
A dummy node to reference external functions that aren't in the index.
static FunctionSummary makeDummyFunctionSummary(std::vector< FunctionSummary::EdgeTy > Edges)
Create an empty FunctionSummary (with specified call edges).
std::pair< unsigned, unsigned > specialRefCounts() const
Function and variable summary information to aid decisions and implementation of importing.
GlobalValueSummary * getBaseObject()
If this is an alias summary, returns the summary of the aliased object (a global variable or function...
ArrayRef< ValueInfo > refs() const
Return the list of values referenced by this global value definition.
GlobalValue::LinkageTypes linkage() const
Return linkage type recorded for this global value.
bool notEligibleToImport() const
Return true if this global value can't be imported.
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
VisibilityTypes
An enumeration for the kinds of visibility of global values.
@ DefaultVisibility
The GV is visible.
@ HiddenVisibility
The GV is hidden.
@ ProtectedVisibility
The GV is protected.
static bool isInterposableLinkage(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time.
LinkageTypes
An enumeration for the kinds of linkage for global values.
@ PrivateLinkage
Like Internal, but omit from symbol table.
@ CommonLinkage
Tentative definitions.
@ InternalLinkage
Rename collisions when linking (static functions).
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
@ WeakODRLinkage
Same, but only replaced by something equivalent.
@ ExternalLinkage
Externally visible function.
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
@ AppendingLinkage
Special purpose, only applies to global arrays.
@ AvailableExternallyLinkage
Available for inspection, not emission.
@ ExternalWeakLinkage
ExternalWeak linkage description.
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Global variable summary information to aid decisions and implementation of importing.
bool withAttributePropagation() const
bool withGlobalValueDeadStripping() const
bool withWholeProgramVisibility() const
bool isGUIDLive(GlobalValue::GUID GUID) const
bool isReadOnly(const GlobalVarSummary *GVS) const
void setFlags(uint64_t Flags)
bool isWriteOnly(const GlobalVarSummary *GVS) const
void setPartiallySplitLTOUnits()
ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const
Return a ValueInfo for the index value_type (convenient when iterating index).
void setEnableSplitLTOUnit()
void setHasSyntheticEntryCounts()
bool hasSyntheticEntryCounts() const
bool partiallySplitLTOUnits() const
void collectDefinedFunctionsForModule(StringRef ModulePath, GVSummaryMapTy &GVSummaryMap) const
Collect for the given module the list of functions it defines (GUID -> Summary).
void dumpSCCs(raw_ostream &OS)
Print out strongly connected components for debugging.
bool isGlobalValueLive(const GlobalValueSummary *GVS) const
bool enableSplitLTOUnit() const
void setWithSupportsHotColdNew()
void propagateAttributes(const DenseSet< GlobalValue::GUID > &PreservedSymbols)
Do the access attribute and DSOLocal propagation in combined index.
void setSkipModuleByDistributedBackend()
void setWithAttributePropagation()
const StringMap< ModuleHash > & modulePaths() const
Table of modules, containing module hash and id.
bool withSupportsHotColdNew() const
void collectDefinedGVSummariesPerModule(Map &ModuleToDefinedGVSummaries) const
Collect for each module the list of Summaries it defines (GUID -> Summary).
static constexpr uint64_t BitcodeSummaryVersion
bool withDSOLocalPropagation() const
void exportToDot(raw_ostream &OS, const DenseSet< GlobalValue::GUID > &GUIDPreservedSymbols) const
Export summary to dot file for GraphViz.
bool hasUnifiedLTO() const
void setWithDSOLocalPropagation()
bool skipModuleByDistributedBackend() const
uint64_t getFlags() const
GlobalValueSummary * getGlobalValueSummary(const GlobalValue &GV, bool PerModuleIndex=true) const
Returns the first GlobalValueSummary for GV, asserting that there is only one if PerModuleIndex.
void setWithGlobalValueDeadStripping()
void setWithWholeProgramVisibility()
bool canImportGlobalVar(const GlobalValueSummary *S, bool AnalyzeRefs) const
Checks if we can import global variable from another module.
StringRef - Represent a constant reference to a string, i.e.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM Value Representation.
std::pair< iterator, bool > insert(const ValueT &V)
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
This class implements an extremely fast bulk output stream that can only output to a stream.
Enumerate the SCCs of a directed graph in reverse topological order of the SCC DAG.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
initializer< Ty > init(const Ty &Val)
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
iterator_range< pointee_iterator< WrappedIteratorT > > make_pointee_range(RangeT &&Range)
void sort(IteratorTy Start, IteratorTy End)
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
bool AreStatisticsEnabled()
Check if statistics are enabled.
@ Ref
The access may reference the value stored in memory.
Flags specific to function summaries.
static constexpr uint32_t RangeWidth
Struct that holds a reference to a particular GUID in a global value summary.
GlobalValue::VisibilityTypes getELFVisibility() const
Returns the most constraining visibility among summaries.
ArrayRef< std::unique_ptr< GlobalValueSummary > > getSummaryList() const
bool canAutoHide() const
Checks if all copies are eligible for auto-hiding (have flag set).
bool isDSOLocal(bool WithDSOLocalPropagation=false) const
Checks if all summaries are DSO local (have the flag set).