Index: include/llvm/IR/ModuleSummaryIndex.h =================================================================== --- include/llvm/IR/ModuleSummaryIndex.h +++ include/llvm/IR/ModuleSummaryIndex.h @@ -73,6 +73,9 @@ /// per-module summaries. const GlobalValue *GV = nullptr; + /// Summary string representation. Valid for combined summaries + StringRef Name; + /// List of global value summary structures for a particular value held /// in the GlobalValueMap. Requires a vector in the case of multiple /// COMDAT values of the same name. @@ -104,6 +107,10 @@ ArrayRef> getSummaryList() const { return Ref->second.SummaryList; } + + StringRef name() const { + return Ref->second.GV ? Ref->second.GV->getName() : Ref->second.Name; + } }; template <> struct DenseMapInfo { @@ -660,6 +667,13 @@ return ValueInfo(getOrInsertValuePtr(GUID)); } + /// Return a ValueInfo for \p GUID setting value \p Name. + ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID, StringRef Name) { + auto VP = getOrInsertValuePtr(GUID); + VP->second.Name = Name; + return ValueInfo(VP); + } + /// Return a ValueInfo for \p GV and mark it as belonging to GV. ValueInfo getOrInsertValueInfo(const GlobalValue *GV) { auto VP = getOrInsertValuePtr(GV->getGUID()); @@ -823,6 +837,9 @@ /// Summary). void collectDefinedGVSummariesPerModule( StringMap &ModuleToDefinedGVSummaries) const; + + /// Export summary to dot file for GraphViz. + void exportToDot(raw_ostream& OS) const; }; } // end namespace llvm Index: lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- lib/Bitcode/Reader/BitcodeReader.cpp +++ lib/Bitcode/Reader/BitcodeReader.cpp @@ -4813,8 +4813,12 @@ if (PrintSummaryGUIDs) dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " << ValueName << "\n"; - ValueIdToValueInfoMap[ValueID] = - std::make_pair(TheIndex.getOrInsertValueInfo(ValueGUID), OriginalNameID); + + // UseStrtab is false for legacy summary formats and value names are + // created on stack. We can't use them outside of parseValueSymbolTable. + ValueIdToValueInfoMap[ValueID] = std::make_pair( + TheIndex.getOrInsertValueInfo(ValueGUID, UseStrtab ? ValueName : ""), + OriginalNameID); } // Specialized value symbol table parser used when reading module index Index: lib/IR/ModuleSummaryIndex.cpp =================================================================== --- lib/IR/ModuleSummaryIndex.cpp +++ lib/IR/ModuleSummaryIndex.cpp @@ -14,6 +14,7 @@ #include "llvm/IR/ModuleSummaryIndex.h" #include "llvm/ADT/StringMap.h" +#include "llvm/Support/Path.h" using namespace llvm; // Collect for the given module the list of function it defines @@ -69,3 +70,236 @@ return true; return false; } + +namespace { +struct Attributes { + void add(const Twine &Name, const Twine &Value, + const Twine &Comment = Twine()); + std::string getAsString() const; + + std::vector Attrs; + std::string Comments; +}; + +struct Edge { + uint64_t SrcMod; + int Hotness; + GlobalValue::GUID Src; + GlobalValue::GUID Dst; +}; +} + +void Attributes::add(const Twine &Name, const Twine &Value, + const Twine &Comment) { + std::string A = Name.str(); + A += "=\""; + A += Value.str(); + A += "\""; + Attrs.push_back(A); + if (!Comment.isTriviallyEmpty()) { + if (Comments.empty()) + Comments = " // "; + else + Comments += ", "; + Comments += Comment.str(); + } +} + +std::string Attributes::getAsString() const { + if (Attrs.empty()) + return ""; + + std::string Ret = "["; + for (auto &A : Attrs) + Ret += A + ","; + Ret.pop_back(); + Ret += "];"; + if (!Comments.empty()) + Ret += Comments; + return Ret; +} + +static std::string linkageToString(GlobalValue::LinkageTypes LT) { + switch (LT) { + case GlobalValue::ExternalLinkage: + return "extern"; + case GlobalValue::AvailableExternallyLinkage: + return "av_ext"; + case GlobalValue::LinkOnceAnyLinkage: + return "linkonce"; + case GlobalValue::LinkOnceODRLinkage: + return "linkonce_odr"; + case GlobalValue::WeakAnyLinkage: + return "weak"; + case GlobalValue::WeakODRLinkage: + return "weak_odr"; + case GlobalValue::AppendingLinkage: + return "appending"; + case GlobalValue::InternalLinkage: + return "internal"; + case GlobalValue::PrivateLinkage: + return "private"; + case GlobalValue::ExternalWeakLinkage: + return "extern_weak"; + case GlobalValue::CommonLinkage: + return "common"; + } + + return ""; +} + +static std::string fflagsToString(FunctionSummary::FFlags F) { + auto FlagValue = [](unsigned V) { return V ? '1' : '0'; }; + char FlagRep[] = {FlagValue(F.ReadNone), FlagValue(F.ReadOnly), + FlagValue(F.NoRecurse), FlagValue(F.ReturnDoesNotAlias), 0}; + + return FlagRep; +} + +// Get string representation of function instruction count and flags. +static std::string getSummaryAttributes(GlobalValueSummary* GVS) { + auto *FS = dyn_cast_or_null(GVS); + if (!FS) + return ""; + + return std::string("inst: ") + std::to_string(FS->instCount()) + + ", ffl: " + fflagsToString(FS->fflags()); +} + +static std::string getNodeVisualName(const ValueInfo &VI) { + return VI.name().empty() ? std::string("@") + std::to_string(VI.getGUID()) + : VI.name().str(); +} + +// Get node label in form MXXX_. The MXXX prefix is required, +// because we may have multiple linkonce functions summaries. +static std::string getNodeLabel(const ValueInfo &VI, GlobalValueSummary *GVS) { + if (isa(GVS)) + return getNodeVisualName(VI); + + std::string Attrs = getSummaryAttributes(GVS); + std::string Label = + getNodeVisualName(VI) + "|" + linkageToString(GVS->linkage()); + if (!Attrs.empty()) + Label += std::string(" (") + Attrs + ")"; + Label += "}"; + + return Label; +} + +// Write definition of node without ValueInfo. This is typically +// an external function or variable +static void defineExternalNode(raw_ostream &OS, const char *Pfx, + const ValueInfo &VI) { + auto StrId = std::to_string(VI.getGUID()); + OS << " " << StrId << " [label=\"" << getNodeVisualName(VI) + << "\"]; // defined externally\n"; +} + +void ModuleSummaryIndex::exportToDot(raw_ostream& OS) const { + std::vector CrossModuleEdges; + DenseMap> NodeMap; + StringMap ModuleToDefinedGVS; + collectDefinedGVSummariesPerModule(ModuleToDefinedGVS); + + auto NodeId = [](uint64_t ModId, GlobalValue::GUID Id) { + return ModId == (uint64_t)-1 ? std::to_string(Id) + : std::string("M") + std::to_string(ModId) + + "_" + std::to_string(Id); + }; + + auto DrawEdge = [&](const char *Pfx, int SrcMod, GlobalValue::GUID SrcId, + int DstMod, GlobalValue::GUID DstId, int Hotness) { + ++Hotness; // 0 corresponds to ref edge, 1 to call with unknown hotness, ... + static const char *EdgeAttrs[] = { + " [style=dashed]; // ref", + " // call (Hotness : Unknown)", + " [color=blue]; // call (hotness : Cold)", + " // call (hotness : None)", + " [color=brown]; // call (hotness : Hot)", + " [style=bold,color=red]; // call (hotness : Critical)"}; + + assert(static_cast(Hotness) < + sizeof(EdgeAttrs) / sizeof(EdgeAttrs[0])); + OS << Pfx << NodeId(SrcMod, SrcId) << " -> " << NodeId(DstMod, DstId) + << EdgeAttrs[Hotness] << "\n"; + }; + + OS << "digraph Summary {\n"; + for (auto &ModIt : ModuleToDefinedGVS) { + auto ModId = getModuleId(ModIt.first()); + OS << " // Module: " << ModIt.first() << "\n"; + OS << " subgraph cluster_" << std::to_string(ModId) << " {\n"; + OS << " style = filled;\n"; + OS << " color = lightgrey;\n"; + OS << " label = \"" << sys::path::filename(ModIt.first()) << "\";\n"; + OS << " node [style=filled,fillcolor=lightblue];\n"; + + auto &GVSMap = ModIt.second; + auto Draw = [&](GlobalValue::GUID IdFrom, GlobalValue::GUID IdTo, int Hotness) { + if (!GVSMap.count(IdTo)) { + CrossModuleEdges.push_back({ModId, Hotness, IdFrom, IdTo}); + return; + } + DrawEdge(" ", ModId, IdFrom, ModId, IdTo, Hotness); + }; + + for (auto &SummaryIt : GVSMap) { + NodeMap[SummaryIt.first].push_back(ModId); + auto Flags = SummaryIt.second->flags(); + Attributes A; + if (isa(SummaryIt.second)) { + A.add("shape", "record", "function"); + } else if (isa(SummaryIt.second)) { + A.add("style", "dotted,filled", "alias"); + A.add("shape", "box"); + } else { + A.add("shape", "Mrecord", "variable"); + } + + auto VI = getValueInfo(SummaryIt.first); + A.add("label", getNodeLabel(VI, SummaryIt.second)); + if (!Flags.Live) + A.add("fillcolor", "red", "dead"); + else if (Flags.NotEligibleToImport) + A.add("fillcolor", "yellow", "not eligible to import"); + + OS << " " << NodeId(ModId, SummaryIt.first) << " " << A.getAsString() + << "\n"; + } + OS << " // Edges:\n"; + + for (auto &SummaryIt : GVSMap) { + auto *GVS = SummaryIt.second; + for (auto &R : GVS->refs()) + Draw(SummaryIt.first, R.getGUID(), -1); + + if (auto *AS = dyn_cast_or_null(SummaryIt.second)) { + auto AliaseeOrigId = AS->getAliasee().getOriginalName(); + auto AliaseeId = getGUIDFromOriginalID(AliaseeOrigId); + + Draw(SummaryIt.first, AliaseeId ? AliaseeId : AliaseeOrigId, -1); + continue; + } + + if (auto *FS = dyn_cast_or_null(SummaryIt.second)) + for (auto &CGEdge : FS->calls()) + Draw(SummaryIt.first, CGEdge.first.getGUID(), + static_cast(CGEdge.second.Hotness)); + } + OS << " }\n"; + } + + for (auto &E : CrossModuleEdges) { + auto &ModList = NodeMap[E.Dst]; + if (ModList.empty()) { + defineExternalNode(OS, " ", getValueInfo(E.Dst)); + ModList.push_back(-1); + } + for (auto DstMod : ModList) + if (DstMod != E.SrcMod) + DrawEdge(" ", E.SrcMod, E.Src, DstMod, E.Dst, E.Hotness); + } + + OS << "}"; +} Index: lib/LTO/LTOBackend.cpp =================================================================== --- lib/LTO/LTOBackend.cpp +++ lib/LTO/LTOBackend.cpp @@ -103,6 +103,12 @@ if (EC) reportOpenError(Path, EC.message()); WriteIndexToFile(Index, OS); + + Path = OutputFileName + "index.dot"; + raw_fd_ostream OSDot(Path, EC, sys::fs::OpenFlags::F_None); + if (EC) + reportOpenError(Path, EC.message()); + Index.exportToDot(OSDot); return true; }; Index: test/ThinLTO/X86/Inputs/dot-dumper.ll =================================================================== --- test/ThinLTO/X86/Inputs/dot-dumper.ll +++ test/ThinLTO/X86/Inputs/dot-dumper.ll @@ -0,0 +1,20 @@ +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@A = local_unnamed_addr global i32 10, align 4 +@B = local_unnamed_addr global i32 20, align 4 + +; Function Attrs: norecurse nounwind readonly uwtable +define i32 @foo() local_unnamed_addr #0 { + %1 = load i32, i32* @B, align 4 + %2 = load i32, i32* @A, align 4 + %3 = add nsw i32 %2, %1 + ret i32 %3 +} + +; Function Attrs: norecurse nounwind readnone uwtable +define i32 @bar() local_unnamed_addr { + ret i32 42 +} + +attributes #0 = { noinline } Index: test/ThinLTO/X86/dot-dumper.ll =================================================================== --- test/ThinLTO/X86/dot-dumper.ll +++ test/ThinLTO/X86/dot-dumper.ll @@ -0,0 +1,50 @@ +; RUN: opt -module-summary %s -o %t1.bc +; RUN: opt -module-summary %p/Inputs/dot-dumper.ll -o %t2.bc +; RUN: llvm-lto2 run -save-temps %t1.bc %t2.bc -o %t3 \ +; RUN: -r=%t1.bc,main,px \ +; RUN: -r=%t1.bc,foo, \ +; RUN: -r=%t1.bc,A, \ +; RUN: -r=%t2.bc,foo,p \ +; RUN: -r=%t2.bc,bar,p \ +; RUN: -r=%t2.bc,A,p \ +; RUN: -r=%t2.bc,B,p +; RUN: cat %t3.index.dot | FileCheck %s + +; CHECK: digraph Summary +; CHECK-NEXT: Module: + +; CHECK-LABEL: subgraph cluster_0 +; CHECK: M0_[[MAIN:[0-9]+]] [{{.*}}main|extern{{.*}}]; // function + +; CHECK-LABEL: subgraph cluster_1 { +; CHECK: M1_[[A:[0-9]+]] [{{.*}}A|extern{{.*}}]; // variable + +; Node definitions can appear in any order, but they should go before edge list. +; CHECK-DAG: M1_[[FOO:[0-9]+]] [{{.*}}foo|extern{{.*}}]; // function, not eligible to import +; CHECK-DAG: M1_[[B:[0-9]+]] [{{.*}}B|extern{{.*}}]; // variable +; CHECK-DAG: M1_[[BAR:[0-9]+]] [{{.*}}bar|extern{{.*}}]; // function, dead +; CHECK: Edges: + +; Order of edges in dot file is undefined +; CHECK-DAG: M1_[[FOO]] -> M1_[[B]] [{{.*}}]; // ref +; CHECK-DAG: M1_[[FOO]] -> M1_[[A]] [{{.*}}]; // ref +; CHECK: } + +; Cross-module edges +; CHECK-DAG: M0_[[MAIN]] -> M1_[[FOO]] // call +; CHECK-DAG: M0_[[MAIN]] -> M1_[[A]] [{{.*}}]; // ref + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@A = external local_unnamed_addr global i32, align 4 + +; Function Attrs: nounwind uwtable +define i32 @main() local_unnamed_addr { + %1 = tail call i32 (...) @foo() + %2 = load i32, i32* @A, align 4 + %3 = add nsw i32 %2, %1 + ret i32 %3 +} + +declare i32 @foo(...) local_unnamed_addr