diff --git a/llvm/include/llvm/Analysis/DDG.h b/llvm/include/llvm/Analysis/DDG.h --- a/llvm/include/llvm/Analysis/DDG.h +++ b/llvm/include/llvm/Analysis/DDG.h @@ -290,6 +290,11 @@ bool getDependencies(const NodeType &Src, const NodeType &Dst, DependenceList &Deps) const; + /// Return a string representing the type of dependence that the dependence + /// analysis identified between the two given nodes. This function assumes + /// that there is a memory dependence between the given two nodes. + const std::string getDependenceString(const NodeType &Src, + const NodeType &Dst) const; protected: // Name of the graph. std::string Name; @@ -463,6 +468,27 @@ return !Deps.empty(); } +template +const std::string +DependenceGraphInfo::getDependenceString(const NodeType &Src, + const NodeType &Dst) const { + std::string Str(""); + raw_string_ostream OS(Str); + DependenceList Deps; + if (!getDependencies(Src, Dst, Deps)) + return OS.str(); + unsigned count = 0; + for (auto &D : Deps) { + D->dump(OS); + // Remove the extra new-line character printed by the dump method + if (OS.str().back() == '\n') + OS.str().pop_back(); + if (++count < Deps.size()) + OS << ", "; + } + return OS.str(); +} + //===--------------------------------------------------------------------===// // GraphTraits specializations for the DDG //===--------------------------------------------------------------------===// diff --git a/llvm/include/llvm/Analysis/DDGPrinter.h b/llvm/include/llvm/Analysis/DDGPrinter.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/Analysis/DDGPrinter.h @@ -0,0 +1,99 @@ +//===- llvm/Analysis/DDGPrinter.h -------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +//===----------------------------------------------------------------------===// +// +// This file defines the DOT printer for the Data-Dependence Graph (DDG). +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ANALYSIS_DDGPRINTER_H +#define LLVM_ANALYSIS_DDGPRINTER_H + +#include "llvm/Analysis/DDG.h" +#include "llvm/Pass.h" +#include "llvm/Support/DOTGraphTraits.h" +namespace llvm { + +//===--------------------------------------------------------------------===// +// Implementation of DDG DOT Printer for a loop. +//===--------------------------------------------------------------------===// +class DDGDotPrinterPass : public PassInfoMixin { +public: + PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, + LoopStandardAnalysisResults &AR, LPMUpdater &U); +}; + +//===--------------------------------------------------------------------===// +// Specialization of DOTGraphTraits. +//===--------------------------------------------------------------------===// +template <> +struct DOTGraphTraits + : public DefaultDOTGraphTraits { + + DOTGraphTraits(bool isSimple = false) + : DefaultDOTGraphTraits(isSimple), Graph(nullptr) {} + + /// Generate a title for the graph in DOT format + std::string getGraphName(const DataDependenceGraph *G) { + assert(G && "expected a valid pointer to the graph."); + Graph = G; + return "DDG for '" + std::string(G->getName()) + "'"; + } + + /// Print a DDG node either in concise form (-ddg-dot-only) or + /// verbose mode (-ddg-dot). + std::string getNodeLabel(const DDGNode *Node, + const DataDependenceGraph *Graph); + + /// Print attributes of an edge in the DDG graph. If the edge + /// is a MemoryDependence edge, then detailed dependence info + /// available from DependenceAnalysis is displayed. + std::string + getEdgeAttributes(const DDGNode *Node, + GraphTraits::ChildIteratorType I, + const DataDependenceGraph *G); + + /// Do not print nodes that are part of a pi-block separately. They + /// will be printed when their containing pi-block is being printed. + bool isNodeHidden(const DDGNode *Node); + +private: + /// Print a DDG node in concise form. + static std::string getSimpleNodeLabel(const DDGNode *Node, + const DataDependenceGraph *G); + + /// Print a DDG node with more information including containing instructions + /// and detailed information about the dependence edges. + static std::string getVerboseNodeLabel(const DDGNode *Node, + const DataDependenceGraph *G); + + /// Print a DDG edge in concise form. + static std::string getSimpleEdgeAttributes(const DDGNode *Src, + const DDGEdge *Edge, + const DataDependenceGraph *G); + + /// Print a DDG edge with more information including detailed information + /// about the dependence edges. + static std::string getVerboseEdgeAttributes(const DDGNode *Src, + const DDGEdge *Edge, + const DataDependenceGraph *G); + + /// Cache a pointer to the graph upon being invoked by the GrapthWriter so + /// we can access the graph without passing it in as argument to functions + /// such as isNodeHidden(). + /// FIXME: this can be avoided by changing the isNodeHidden interface to + /// also take in a graph pointer. + const DataDependenceGraph *Graph = nullptr; +}; + +using DDGDotGraphTraits = struct DOTGraphTraits; + +} // namespace llvm + +#endif // LLVM_ANALYSIS_DDGPRINTER_H diff --git a/llvm/lib/Analysis/CMakeLists.txt b/llvm/lib/Analysis/CMakeLists.txt --- a/llvm/lib/Analysis/CMakeLists.txt +++ b/llvm/lib/Analysis/CMakeLists.txt @@ -39,6 +39,7 @@ CodeMetrics.cpp ConstantFolding.cpp DDG.cpp + DDGPrinter.cpp ConstraintSystem.cpp Delinearization.cpp DemandedBits.cpp diff --git a/llvm/lib/Analysis/DDGPrinter.cpp b/llvm/lib/Analysis/DDGPrinter.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/Analysis/DDGPrinter.cpp @@ -0,0 +1,147 @@ +//===- DDGPrinter.cpp - DOT printer for the data dependence graph ----------==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +//===----------------------------------------------------------------------===// +// +// This file defines the `-dot-ddg` analysis pass, which emits DDG in DOT format +// in a file named `ddg..dot` for each loop in a function. +//===----------------------------------------------------------------------===// + +#include "llvm/Analysis/DDGPrinter.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/GraphWriter.h" + +using namespace llvm; + +static cl::opt DotOnly("dot-ddg-only", cl::init(false), cl::Hidden, + cl::ZeroOrMore, cl::desc("simple ddg dot graph")); +static cl::opt DDGDotFilenamePrefix( + "dot-ddg-filename-prefix", cl::init("ddg"), cl::Hidden, + cl::desc("The prefix used for the DDG dot file names.")); + +static void writeDDGToDotFile(DataDependenceGraph &G, bool DOnly = false); + +//===--------------------------------------------------------------------===// +// Implementation of DDG DOT Printer for a loop +//===--------------------------------------------------------------------===// +PreservedAnalyses DDGDotPrinterPass::run(Loop &L, LoopAnalysisManager &AM, + LoopStandardAnalysisResults &AR, + LPMUpdater &U) { + writeDDGToDotFile(*AM.getResult(L, AR), DotOnly); + return PreservedAnalyses::all(); +} + +static void writeDDGToDotFile(DataDependenceGraph &G, bool DOnly) { + std::string Filename = + Twine(DDGDotFilenamePrefix + "." + G.getName() + ".dot").str(); + errs() << "Writing '" << Filename << "'..."; + + std::error_code EC; + raw_fd_ostream File(Filename, EC, sys::fs::F_Text); + + if (!EC) + // We only provide the constant verson of the DOTGraphTrait specialization, + // hence the conversion to const pointer + WriteGraph(File, (const DataDependenceGraph *)&G, DOnly); + else + errs() << " error opening file for writing!"; + errs() << "\n"; +} + +//===--------------------------------------------------------------------===// +// DDG DOT Printer Implementation +//===--------------------------------------------------------------------===// +std::string DDGDotGraphTraits::getNodeLabel(const DDGNode *Node, + const DataDependenceGraph *Graph) { + if (isSimple()) + return getSimpleNodeLabel(Node, Graph); + else + return getVerboseNodeLabel(Node, Graph); +} + +std::string DDGDotGraphTraits::getEdgeAttributes( + const DDGNode *Node, GraphTraits::ChildIteratorType I, + const DataDependenceGraph *G) { + const DDGEdge *E = static_cast(*I.getCurrent()); + if (isSimple()) + return getSimpleEdgeAttributes(Node, E, G); + else + return getVerboseEdgeAttributes(Node, E, G); +} + +bool DDGDotGraphTraits::isNodeHidden(const DDGNode *Node) { + if (isSimple() && isa(Node)) + return true; + assert(Graph && "expected a valid graph pointer"); + return Graph->getPiBlock(*Node) != nullptr; +} + +std::string +DDGDotGraphTraits::getSimpleNodeLabel(const DDGNode *Node, + const DataDependenceGraph *G) { + std::string Str; + raw_string_ostream OS(Str); + if (isa(Node)) + for (auto *II : static_cast(Node)->getInstructions()) + OS << *II << "\n"; + else if (isa(Node)) + OS << "pi-block\nwith\n" + << cast(Node)->getNodes().size() << " nodes\n"; + else if (isa(Node)) + OS << "root\n"; + else + llvm_unreachable("Unimplemented type of node"); + return OS.str(); +} + +std::string +DDGDotGraphTraits::getVerboseNodeLabel(const DDGNode *Node, + const DataDependenceGraph *G) { + std::string Str; + raw_string_ostream OS(Str); + OS << "getKind() << ">\n"; + if (isa(Node)) + for (auto *II : static_cast(Node)->getInstructions()) + OS << *II << "\n"; + else if (isa(Node)) { + OS << "--- start of nodes in pi-block ---\n"; + unsigned Count = 0; + auto &PNodes = cast(Node)->getNodes(); + for (auto *PN : PNodes) + OS << getVerboseNodeLabel(PN, G) + << (++Count == PNodes.size() ? "" : "\n"); + OS << "--- end of nodes in pi-block ---\n"; + } else if (isa(Node)) + OS << "root\n"; + else + llvm_unreachable("Unimplemented type of node"); + return OS.str(); +} + +std::string DDGDotGraphTraits::getSimpleEdgeAttributes( + const DDGNode *Src, const DDGEdge *Edge, const DataDependenceGraph *G) { + std::string Str; + raw_string_ostream OS(Str); + DDGEdge::EdgeKind Kind = Edge->getKind(); + OS << "label=\"[" << Kind << "]\""; + return OS.str(); +} + +std::string DDGDotGraphTraits::getVerboseEdgeAttributes( + const DDGNode *Src, const DDGEdge *Edge, const DataDependenceGraph *G) { + std::string Str; + raw_string_ostream OS(Str); + DDGEdge::EdgeKind Kind = Edge->getKind(); + OS << "label=\"["; + if (Kind == DDGEdge::EdgeKind::MemoryDependence) + OS << G->getDependenceString(*Src, Edge->getTargetNode()); + else + OS << Kind; + OS << "]\""; + return OS.str(); +} diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -29,6 +29,7 @@ #include "llvm/Analysis/CGSCCPassManager.h" #include "llvm/Analysis/CallGraph.h" #include "llvm/Analysis/DDG.h" +#include "llvm/Analysis/DDGPrinter.h" #include "llvm/Analysis/Delinearization.h" #include "llvm/Analysis/DemandedBits.h" #include "llvm/Analysis/DependenceAnalysis.h" diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -362,6 +362,7 @@ #define LOOP_PASS(NAME, CREATE_PASS) #endif LOOP_PASS("canon-freeze", CanonicalizeFreezeInLoopsPass()) +LOOP_PASS("dot-ddg", DDGDotPrinterPass()) LOOP_PASS("invalidate", InvalidateAllAnalysesPass()) LOOP_PASS("licm", LICMPass()) LOOP_PASS("loop-idiom", LoopIdiomRecognizePass())