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"));
44 bool HasProtected =
false;
58 return WithDSOLocalPropagation
63 [](
const std::unique_ptr<GlobalValueSummary> &Summary) {
64 return Summary->isDSOLocal();
72 [](
const std::unique_ptr<GlobalValueSummary> &Summary) {
73 return Summary->canAutoHide();
82 unsigned RORefCnt = 0, WORefCnt = 0;
84 for (
I = Refs.size() - 1;
I >= 0 && Refs[
I].isWriteOnly(); --
I)
86 for (;
I >= 0 && Refs[
I].isReadOnly(); --
I)
88 return {RORefCnt, WORefCnt};
118 assert(Flags <= 0x2ff &&
"Unexpected bits in flag");
163 for (
auto &GlobalList : *
this) {
164 auto GUID = GlobalList.first;
165 for (
auto &GlobSummary : GlobalList.second.SummaryList) {
166 auto *Summary = dyn_cast_or_null<FunctionSummary>(GlobSummary.get());
171 if (Summary->modulePath() != ModulePath)
173 GVSummaryMap[GUID] = Summary;
180 bool PerModuleIndex)
const {
182 assert(VI &&
"GlobalValue not found in index");
183 assert((!PerModuleIndex || VI.getSummaryList().size() == 1) &&
184 "Expected a single entry per global value in per-module index");
185 auto &Summary = VI.getSummaryList()[0];
186 return Summary.get();
193 const auto &SummaryList = VI.getSummaryList();
194 if (SummaryList.empty())
196 for (
auto &
I : SummaryList)
213 for (
auto &VI : S->
refs()) {
214 assert(VI.getAccessSpecifier() == 0 || isa<FunctionSummary>(S));
215 if (!VI.getAccessSpecifier()) {
216 if (!MarkedNonReadWriteOnly.
insert(VI).second)
218 }
else if (MarkedNonReadWriteOnly.
contains(VI))
220 for (
auto &
Ref : VI.getSummaryList())
223 if (
auto *GVS = dyn_cast<GlobalVarSummary>(
Ref->getBaseObject())) {
224 if (!VI.isReadOnly())
225 GVS->setReadOnly(
false);
226 if (!VI.isWriteOnly())
227 GVS->setWriteOnly(
false);
264 for (
auto &
P : *
this) {
265 bool IsDSOLocal =
true;
266 for (
auto &S :
P.second.SummaryList) {
276 P.second.SummaryList,
277 [&](
const std::unique_ptr<GlobalValueSummary> &Summary) {
278 return isGlobalValueLive(Summary.get());
293 if (
auto *GVS = dyn_cast<GlobalVarSummary>(S->getBaseObject()))
298 GUIDPreservedSymbols.
count(
P.first)) {
299 GVS->setReadOnly(
false);
300 GVS->setWriteOnly(
false);
305 IsDSOLocal &= S->isDSOLocal();
310 for (
const std::unique_ptr<GlobalValueSummary> &Summary :
311 P.second.SummaryList)
312 Summary->setDSOLocal(
false);
317 for (
auto &
P : *
this)
318 if (
P.second.SummaryList.size())
319 if (
auto *GVS = dyn_cast<GlobalVarSummary>(
320 P.second.SummaryList[0]->getBaseObject()))
322 if (GVS->maybeReadOnly())
324 if (GVS->maybeWriteOnly())
325 WriteOnlyLiveGVars++;
330 bool AnalyzeRefs)
const {
337 bool &CanImportDecl)
const {
358 const bool nonInterposable =
364 CanImportDecl = (nonInterposable && eligibleToImport);
371 return nonInterposable && eligibleToImport &&
372 (!AnalyzeRefs || !HasRefsPreventingImport(GVS));
380 scc_begin<ModuleSummaryIndex *>(
this);
382 O <<
"SCC (" << utostr(
I->size()) <<
" node" << (
I->size() == 1 ?
"" :
"s")
386 if (V.getSummaryList().size())
387 F = cast<FunctionSummary>(V.getSummaryList().front().get());
388 O <<
" " << (
F ==
nullptr ?
"External" :
"") <<
" " << utostr(V.getGUID())
389 << (
I.hasCycle() ?
" (has cycle)" :
"") <<
"\n";
399 void addComment(
const Twine &Comment);
400 std::string getAsString()
const;
402 std::vector<std::string> Attrs;
403 std::string Comments;
415 const Twine &Comment) {
416 std::string
A =
Name.str();
424void Attributes::addComment(
const Twine &Comment) {
425 if (!
Comment.isTriviallyEmpty()) {
426 if (Comments.empty())
434std::string Attributes::getAsString()
const {
438 std::string
Ret =
"[";
439 for (
auto &
A : Attrs)
456 return "linkonce_odr";
468 return "extern_weak";
477 auto FlagValue = [](
unsigned V) {
return V ?
'1' :
'0'; };
478 char FlagRep[] = {FlagValue(
F.ReadNone),
479 FlagValue(
F.ReadOnly),
480 FlagValue(
F.NoRecurse),
481 FlagValue(
F.ReturnDoesNotAlias),
482 FlagValue(
F.NoInline),
483 FlagValue(
F.AlwaysInline),
484 FlagValue(
F.NoUnwind),
485 FlagValue(
F.MayThrow),
486 FlagValue(
F.HasUnknownCall),
487 FlagValue(
F.MustBeUnreachable),
495 auto *FS = dyn_cast_or_null<FunctionSummary>(GVS);
499 return std::string(
"inst: ") + std::to_string(FS->instCount()) +
504 return std::string(
"@") + std::to_string(Id);
512 if (isa<AliasSummary>(GVS))
519 Label += std::string(
" (") + Attrs +
")";
530 auto StrId = std::to_string(Id);
531 OS <<
" " << StrId <<
" [label=\"";
538 OS <<
"\"]; // defined externally\n";
542 if (
auto *GVS = dyn_cast<GlobalVarSummary>(S))
543 return GVS->maybeReadOnly();
548 if (
auto *GVS = dyn_cast<GlobalVarSummary>(S))
549 return GVS->maybeWriteOnly();
554 if (
auto *GVS = dyn_cast<GlobalVarSummary>(S))
555 return GVS->isConstant();
562 std::vector<Edge> CrossModuleEdges;
564 using GVSOrderedMapTy = std::map<GlobalValue::GUID, GlobalValueSummary *>;
565 std::map<StringRef, GVSOrderedMapTy> ModuleToDefinedGVS;
571 std::vector<StringRef> ModulePaths;
573 ModulePaths.push_back(ModPath);
576 for (
auto &ModPath : ModulePaths)
582 return ModId == (
uint64_t)-1 ? std::to_string(Id)
583 : std::string(
"M") + std::to_string(ModId) +
584 "_" + std::to_string(Id);
596 static const char *EdgeAttrs[] = {
597 " [style=dotted]; // alias",
598 " [style=dashed]; // ref",
599 " [style=dashed,color=forestgreen]; // const-ref",
600 " [style=dashed,color=violetred]; // writeOnly-ref",
601 " // call (hotness : Unknown)",
602 " [color=blue]; // call (hotness : Cold)",
603 " // call (hotness : None)",
604 " [color=brown]; // call (hotness : Hot)",
605 " [style=bold,color=red]; // call (hotness : Critical)"};
607 assert(
static_cast<size_t>(TypeOrHotness) < std::size(EdgeAttrs));
608 OS << Pfx << NodeId(SrcMod, SrcId) <<
" -> " << NodeId(DstMod, DstId)
609 << EdgeAttrs[TypeOrHotness] <<
"\n";
612 OS <<
"digraph Summary {\n";
613 for (
auto &ModIt : ModuleToDefinedGVS) {
617 auto ModId = ModuleIdMap.
empty() ? 0 : ModuleIdMap[ModIt.first];
618 OS <<
" // Module: " << ModIt.first <<
"\n";
619 OS <<
" subgraph cluster_" << std::to_string(ModId) <<
" {\n";
620 OS <<
" style = filled;\n";
621 OS <<
" color = lightgrey;\n";
623 OS <<
" node [style=filled,fillcolor=lightblue];\n";
625 auto &GVSMap = ModIt.second;
627 if (!GVSMap.count(IdTo)) {
628 CrossModuleEdges.push_back({ModId, Hotness, IdFrom, IdTo});
631 DrawEdge(
" ", ModId, IdFrom, ModId, IdTo, Hotness);
634 for (
auto &SummaryIt : GVSMap) {
635 NodeMap[SummaryIt.first].push_back(ModId);
636 auto Flags = SummaryIt.second->flags();
638 if (isa<FunctionSummary>(SummaryIt.second)) {
639 A.add(
"shape",
"record",
"function");
640 }
else if (isa<AliasSummary>(SummaryIt.second)) {
641 A.add(
"style",
"dotted,filled",
"alias");
642 A.add(
"shape",
"box");
644 A.add(
"shape",
"Mrecord",
"variable");
646 A.addComment(
"immutable");
648 A.addComment(
"writeOnly");
650 A.addComment(
"constant");
652 if (Flags.Visibility)
653 A.addComment(
"visibility");
655 A.addComment(
"dsoLocal");
656 if (Flags.CanAutoHide)
657 A.addComment(
"canAutoHide");
659 A.addComment(
"definition");
661 A.addComment(
"declaration");
662 if (GUIDPreservedSymbols.
count(SummaryIt.first))
663 A.addComment(
"preserved");
668 A.add(
"fillcolor",
"red",
"dead");
669 else if (Flags.NotEligibleToImport)
670 A.add(
"fillcolor",
"yellow",
"not eligible to import");
672 OS <<
" " << NodeId(ModId, SummaryIt.first) <<
" " <<
A.getAsString()
675 OS <<
" // Edges:\n";
677 for (
auto &SummaryIt : GVSMap) {
678 auto *GVS = SummaryIt.second;
679 for (
auto &R : GVS->refs())
680 Draw(SummaryIt.first, R.getGUID(),
681 R.isWriteOnly() ? -1 : (R.isReadOnly() ? -2 : -3));
683 if (
auto *AS = dyn_cast_or_null<AliasSummary>(SummaryIt.second)) {
684 Draw(SummaryIt.first, AS->getAliaseeGUID(), -4);
688 if (
auto *FS = dyn_cast_or_null<FunctionSummary>(SummaryIt.second))
689 for (
auto &CGEdge : FS->calls())
690 Draw(SummaryIt.first, CGEdge.first.getGUID(),
691 static_cast<int>(CGEdge.second.Hotness));
696 OS <<
" // Cross-module edges:\n";
697 for (
auto &E : CrossModuleEdges) {
698 auto &ModList = NodeMap[E.Dst];
699 if (ModList.empty()) {
703 ModList.push_back(-1);
705 for (
auto DstMod : ModList)
711 if (DstMod != E.SrcMod)
712 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(SmallVectorImpl< 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()
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.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
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).