Go to the documentation of this file.
30 #ifndef CGRADataFlowGraph_H
31 #define CGRADataFlowGraph_H
33 #include "llvm/Support/DOTGraphTraits.h"
34 #include "llvm/ADT/DirectedGraph.h"
35 #include "llvm/Support/FormatVariadic.h"
36 #include "llvm/ADT/StringRef.h"
37 #include "llvm/ADT/StringMap.h"
38 #include "llvm/IR/Constant.h"
39 #include "llvm/IR/Instruction.h"
40 #include "llvm/ADT/APFloat.h"
41 #include "llvm/Support/JSON.h"
54 #define VROOT_NODE_ID (-1)
86 DFGNode((std::uintptr_t)(val), kind, val) {}
101 DGNode::operator=(std::move(N));
113 virtual string getUniqueName()
const = 0;
114 virtual string getNodeAttr()
const = 0;
118 return this->ID == N.
ID;
122 extra_info[key] =
new json::Value(std::move(V));
126 return !extra_info.empty();
130 if (!hasExtraInfo()) {
131 return json::Object({});
133 json::Object json_obj;
134 for (
auto &item : extra_info) {
135 json_obj[item.getKey()] = *(item.getValue());
137 return json::Value(std::move(json_obj));
177 return opcode +
"_" + to_string(getID());
180 return formatv(
"type=op,{0}={1}",
OptDFGOpKey, opcode);
183 return N->
getKind() == NodeKind::Compute;
186 return dyn_cast<Instruction>(val);
197 addr = load->getOperand(0);
203 addr = store->getOperand(1);
210 return "Load_" + to_string(getID());
212 return "Store_" + to_string(getID());
217 string type = (is_load) ?
"input" :
"output";
218 return formatv(
"type={0},data={1}", type, getSymbol());
227 if (
auto gep = dyn_cast<GetElementPtrInst>(addr)) {
228 auto *ptr = gep->getPointerOperand();
229 if (isa<Argument>(*ptr)) {
230 if (ptr->hasName()) {
231 return string(ptr->getName());
233 }
else if (
auto load = dyn_cast<LoadInst>(ptr)) {
234 auto child_ptr = load->getPointerOperand();
235 if (isa<Argument>(*child_ptr)) {
236 if (child_ptr->hasName()) {
237 return string(child_ptr->getName());
240 }
else if (
auto *alloc_inst = dyn_cast<AllocaInst>(ptr)) {
241 return string(alloc_inst->getName());
250 return N->
getKind() == NodeKind::MemLoad ||
251 N->
getKind() == NodeKind::MemStore;
258 template <DFGNode::NodeKind DrivedKind>
268 DFGNode(DrivedKind, v), skip_seq(seq) {};
271 DFGNode(ID, DrivedKind, v), skip_seq(seq) {};
278 string format_str =
"{0}", type_str;
280 while (ele_ty->isPointerTy()) {
281 ele_ty = ty->getPointerElementType();
283 while (ele_ty->isArrayTy()) {
284 int size = dyn_cast<ArrayType>(ele_ty)->getArrayNumElements();
285 format_str += formatv(
"[{0}]", size);
286 ele_ty = ele_ty->getArrayElementType();
289 if (ele_ty->isFloatingPointTy()) {
291 }
else if (ele_ty->isIntegerTy()) {
294 type_str =
"unknown";
297 return formatv(format_str.c_str(), type_str);
301 #define DEBUG_TYPE "cgraomp"
304 SmallVector<string> opcode_vec;
305 for (
auto it = ++(skip_seq->rbegin()); it != skip_seq->rend(); it++) {
306 if (
auto inst = dyn_cast<Instruction>(*it)) {
307 opcode_vec.emplace_back(inst->getOpcodeName());
310 <<
" Unexpected skip instruction: ";
311 (*it)->print(dbgs());
316 str = formatv(
"skipped=\"({0})\",", make_range(opcode_vec.begin(), opcode_vec.end()));
342 return "Const_" + to_string(getID());
344 virtual string getNodeAttr()
const;
347 return getConstStr();
350 return N->
getKind() == NodeKind::Constant;
353 string getConstStr()
const;
367 return "GlobalData_" + to_string(getID());
369 string getNodeAttr()
const;
375 return N->
getKind() == NodeKind::GlobalData;
378 string getDataStr()
const;
383 template<
char const* OPCODE_STR>
390 return opcode +
"_" + to_string(getID());
393 return formatv(
"type=op,{0}={1}",
OptDFGOpKey, opcode);
396 return N->
getKind() == NodeKind::Compute;
399 return dyn_cast<Instruction>(val);
411 return formatv(
"datatype=int,value={0}", const_value);
414 return formatv(
"type=const,{0}", getExtraAttr());
440 *
this = std::move(E);
444 DFGEdgeBase::operator=(E);
449 DFGEdgeBase::operator=(std::move(E));
454 return formatv(
"operand={0}", operand);
472 return formatv(
"operand={0},dir=back,distance={1},label={1}", operand ,distance);
476 return E->
getKind() == EdgeKind::LoopCarried;
489 return formatv(
"operand={0},type=init,label=init", operand);
493 return E->
getKind() == EdgeKind::Init;
525 virtual_root = G.virtual_root;
526 G.virtual_root =
nullptr;
533 connect(getRoot(), N, *E);
548 return *virtual_root;
560 NodeType* addNode(NodeType &N);
571 bool connect(NodeType &Src, NodeType &Dst, EdgeType &E);
586 SmallVectorImpl<EdgeInfoType> &EL,
587 bool ignore_vroot =
false)
const {
588 assert(EL.empty() &&
"Expected the list of edges to be empty.");
590 for (
auto *Node : Nodes) {
593 if (ignore_vroot && *Node == getRoot())
continue;
594 if (Node->findEdgesTo(N, TempList)) {
595 EL.push_back(std::make_pair(Node,TempList));
604 for (
auto *Node : Nodes) {
605 if (Node->hasExtraInfo()) {
619 string convertToReadableNodeName(
const string dot_string)
const;
627 Error saveAsDotGraph(StringRef filepath);
629 Error saveExtraInfo(StringRef filepath);
649 void makeSequentialNodeID();
663 CGRADFGBase::addNode(*virtual_root);
682 return &P->getTargetNode();
685 using ChildIteratorType =
686 mapped_iterator<DFGNode::iterator, decltype(&DDGGetTargetNode)>;
732 return &P->getTargetNode();
735 using ChildIteratorType =
736 mapped_iterator<DFGNode::iterator, decltype(&DDGGetTargetNode)>;
779 struct DOTGraphTraits<const
CGRADFG *> :
public DefaultDOTGraphTraits {
788 static string getGraphProperties(
const CGRADFG *G);
815 static string getEdgeAttributes(
const DFGNode *Node,
816 GraphTraits<DFGNode *>::ChildIteratorType I,
838 #endif //CGRADataFlowGraph_H
void createVirtualRoot()
Definition: CGRADataFlowGraph.hpp:661
bool hasExtraInfo() const
Definition: CGRADataFlowGraph.hpp:602
StringMap< json::Value * > extra_info
Definition: CGRADataFlowGraph.hpp:146
string getSkipSeq() const
Definition: CGRADataFlowGraph.hpp:300
Definition: OptionPlugin.cpp:128
Definition: CGRADataFlowGraph.hpp:384
string getExtraAttr() const
Definition: CGRADataFlowGraph.hpp:371
string getGraphName(const CGRADFG *G)
Definition: CGRADataFlowGraph.hpp:784
Loop * L
Definition: CGRADataFlowGraph.hpp:670
static NodeRef getEntryNode(CGRADFG *G)
Definition: CGRADataFlowGraph.hpp:712
static string getNodeAttributes(const DFGNode *Node, const CGRADFG *G)
Definition: CGRADataFlowGraph.hpp:810
string getNodeAttr() const
Definition: CGRADataFlowGraph.hpp:179
bool is_load
Definition: CGRADataFlowGraph.hpp:254
A concrete class for virtual root node, connected to all the primary input node.
Definition: CGRADataFlowGraph.hpp:154
VirtualRootNode()
Definition: CGRADataFlowGraph.hpp:156
MemAccessNode(LoadInst *load)
Definition: CGRADataFlowGraph.hpp:194
Loop * getLoop()
Definition: CGRADataFlowGraph.hpp:655
Specilized template of DotGraphTraits for CGRADFG This is needed to save CGRADFG as DOT graph file.
Definition: CGRADataFlowGraph.hpp:779
Definition: CGRADataFlowGraph.hpp:405
string getSymbol() const
Get a symbol name to be accessed.
Definition: CGRADataFlowGraph.hpp:226
static StringMap< StringRef > default_node_prop
a default node propterties for DOT graph
Definition: CGRADataFlowGraph.hpp:826
void setName(const string graph_name)
Set the Name object.
Definition: CGRADataFlowGraph.hpp:636
static bool classof(const DFGEdge *E)
Definition: CGRADataFlowGraph.hpp:492
DFGNode::iterator ChildEdgeIteratorType
Definition: CGRADataFlowGraph.hpp:737
DFGNode(int ID, NodeKind kind, Value *val)
Definition: CGRADataFlowGraph.hpp:82
DOTGraphTraits(bool isSimple)
Definition: CGRADataFlowGraph.hpp:781
string getUniqueName() const
Definition: CGRADataFlowGraph.hpp:176
DFGNode(NodeKind kind, Value *val)
Definition: CGRADataFlowGraph.hpp:85
SkipSeq * skip_seq
Definition: CGRADataFlowGraph.hpp:322
GlobalDataNode(Value *v)
Definition: CGRADataFlowGraph.hpp:359
DFGNode & operator=(DFGNode &&N)
Definition: CGRADataFlowGraph.hpp:100
ComputeNode(Instruction *inst, std::string opcode)
Definition: CGRADataFlowGraph.hpp:173
CGRADFG(NodeType &N)
constructor with an initial node
Definition: CGRADataFlowGraph.hpp:530
CGRADFG(Function *F, Loop *L)
Constructor.
Definition: CGRADataFlowGraph.hpp:518
DataNode(Value *v, SkipSeq *seq)
Definition: CGRADataFlowGraph.hpp:267
Instruction * getInst() const
Definition: CGRADataFlowGraph.hpp:398
virtual string getNodeAttr() const
Definition: CGRADataFlowGraph.hpp:413
EdgeKind Kind
Definition: CGRADataFlowGraph.hpp:461
DFGNode(DFGNode &&N)
Definition: CGRADataFlowGraph.hpp:91
virtual string getExtraAttr() const
Definition: CGRADataFlowGraph.hpp:346
CGRADFG::iterator nodes_iterator
Definition: CGRADataFlowGraph.hpp:711
static bool classof(const DFGNode *N)
Definition: CGRADataFlowGraph.hpp:349
DOTGraphTraits()
Definition: CGRADataFlowGraph.hpp:782
friend CGRADFG
Definition: CGRADataFlowGraph.hpp:71
Class of DFG edge derived from DGEdge.
Definition: CGRADataFlowGraph.hpp:426
string getNodeAttr() const
Definition: CGRADataFlowGraph.hpp:162
NodeType & getRoot() const
Get the virtual route node object.
Definition: CGRADataFlowGraph.hpp:547
static string getNodeLabel(const DFGNode *Node, const CGRADFG *G)
Definition: CGRADataFlowGraph.hpp:795
DFGNode(const DFGNode &N)
Definition: CGRADataFlowGraph.hpp:88
static StringMap< StringRef > default_edge_prop
a default edge propterties for DOT graph
Definition: CGRADataFlowGraph.hpp:830
static string getNodeIdentifierLabel(const DFGNode *Node, const CGRADFG *G)
Definition: CGRADataFlowGraph.hpp:800
virtual string getNodeAttr() const =0
NodeKind
Definition: CGRADataFlowGraph.hpp:72
string getUniqueName() const
Definition: CGRADataFlowGraph.hpp:389
static ChildIteratorType child_end(const NodeRef N)
Definition: CGRADataFlowGraph.hpp:742
Definition: CGRADataFlowGraph.hpp:357
DFGEdge(DFGNode &N, int operand=0, EdgeKind Kind=EdgeKind::Normal)
Definition: CGRADataFlowGraph.hpp:433
static bool classof(const DFGNode *N)
Definition: CGRADataFlowGraph.hpp:249
static DFGNode * DDGGetTargetNode(DFGEdge *P)
Definition: CGRADataFlowGraph.hpp:681
mapped_iterator< DFGNode::iterator, decltype(&DDGGetTargetNode)> ChildIteratorType
Definition: CGRADataFlowGraph.hpp:686
int distance
Definition: CGRADataFlowGraph.hpp:480
EdgeKind getKind() const
Definition: CGRADataFlowGraph.hpp:457
static ChildEdgeIteratorType child_edge_begin(NodeRef N)
Definition: CGRADataFlowGraph.hpp:696
static NodeRef getEntryNode(const CGRADFG *G)
Definition: CGRADataFlowGraph.hpp:762
int getDataWidth(const Type *T)
Definition: Utils.cpp:137
string getUniqueName() const
Definition: CGRADataFlowGraph.hpp:208
NodeKind kind
Definition: CGRADataFlowGraph.hpp:143
string getUniqueName() const
Definition: CGRADataFlowGraph.hpp:159
static bool classof(const DFGEdge *E)
Definition: CGRADataFlowGraph.hpp:475
static nodes_iterator nodes_begin(const CGRADFG *G)
Definition: CGRADataFlowGraph.hpp:765
virtual string getUniqueName() const =0
int const_value
Definition: CGRADataFlowGraph.hpp:418
string getTypeName(Type *ty) const
Definition: CGRADataFlowGraph.hpp:276
Definition: AGVerifyPass.hpp:50
static nodes_iterator nodes_end(const CGRADFG *G)
Definition: CGRADataFlowGraph.hpp:768
DGEdge< DFGNode, DFGEdge > DFGEdgeBase
Definition: CGRADataFlowGraph.hpp:62
string getUniqueName() const
Definition: CGRADataFlowGraph.hpp:366
MemAccessNode(StoreInst *store)
Definition: CGRADataFlowGraph.hpp:200
ConstantNode(Value *v, SkipSeq *seq, int ID)
Definition: CGRADataFlowGraph.hpp:338
static nodes_iterator nodes_begin(CGRADFG *G)
Definition: CGRADataFlowGraph.hpp:715
InitDataEdge(DFGNode &N, int operand)
Definition: CGRADataFlowGraph.hpp:485
virtual string getEdgeAttr() const
Definition: CGRADataFlowGraph.hpp:470
std::string opcode
Definition: CGRADataFlowGraph.hpp:402
NodeKind getKind() const
Definition: CGRADataFlowGraph.hpp:105
LoopDependencyEdge(DFGNode &N, int operand, int distance)
Definition: CGRADataFlowGraph.hpp:466
virtual string getEdgeAttr() const
Definition: CGRADataFlowGraph.hpp:453
#define VROOT_NODE_ID
Definition: CGRADataFlowGraph.hpp:54
std::pair< NodeType *, EdgeListTy > EdgeInfoType
Definition: CGRADataFlowGraph.hpp:509
static bool classof(const DFGNode *N)
Definition: CGRADataFlowGraph.hpp:182
#define ERR_DEBUG_PREFIX
Definition: common.hpp:41
Definition: CGRADataFlowGraph.hpp:325
cl::opt< string > OptDFGOpKey
key string for opcode in DOT graph
ConstantNode(Value *v, int ID)
Definition: CGRADataFlowGraph.hpp:330
CGRADFG::const_iterator nodes_iterator
Definition: CGRADataFlowGraph.hpp:761
static bool isNodeHidden(const DFGNode *Node, const CGRADFG *G)
Definition: CGRADataFlowGraph.hpp:790
static ChildEdgeIteratorType child_edge_end(NodeRef N)
Definition: CGRADataFlowGraph.hpp:700
mapped_iterator< DFGNode::iterator, decltype(&DDGGetTargetNode)> ChildIteratorType
Definition: CGRADataFlowGraph.hpp:736
A graph class for CGRA kernel DFG derived from llvm::DirectedGraph.
Definition: CGRADataFlowGraph.hpp:505
Definition: CGRADataFlowGraph.hpp:464
bool findIncomingEdgesToNode(const NodeType &N, SmallVectorImpl< EdgeInfoType > &EL, bool ignore_vroot=false) const
find in-coming edges and get the list of them Unlike the same name method in llvm::DirectedGraph,...
Definition: CGRADataFlowGraph.hpp:585
Value * addr
Definition: CGRADataFlowGraph.hpp:255
CGRADFG(CGRADFG &&G)
move constructor
Definition: CGRADataFlowGraph.hpp:524
static ChildIteratorType child_begin(const NodeRef N)
Definition: CGRADataFlowGraph.hpp:739
Definition: CGRADataFlowGraph.hpp:259
json::Value getExtraInfoAsJSONObject()
Definition: CGRADataFlowGraph.hpp:129
~CGRADFG()
Destructor.
Definition: CGRADataFlowGraph.hpp:537
virtual string getEdgeAttr() const
Definition: CGRADataFlowGraph.hpp:488
string getName() const
Get the Name object.
Definition: CGRADataFlowGraph.hpp:645
void setExtraInfo(StringRef key, json::Value V)
Definition: CGRADataFlowGraph.hpp:121
GEPNode(GetElementPtrInst *gep, int ID)
Definition: CGRADataFlowGraph.hpp:386
static bool classof(const DFGNode *N)
Definition: CGRADataFlowGraph.hpp:374
static ChildEdgeIteratorType child_edge_begin(const NodeRef N)
Definition: CGRADataFlowGraph.hpp:746
A concrete class for computational nodes.
Definition: CGRADataFlowGraph.hpp:171
DFGNode & operator=(const DFGNode &N)
Definition: CGRADataFlowGraph.hpp:95
SmallVector< Value * > SkipSeq
Definition: CGRADataFlowGraph.hpp:261
ConstantNode(Value *v, SkipSeq *seq)
Definition: CGRADataFlowGraph.hpp:335
Function * getFunction()
Definition: CGRADataFlowGraph.hpp:651
static string getNodeDescription(const DFGNode *Node, const CGRADFG *G)
Definition: CGRADataFlowGraph.hpp:805
EdgeKind
Definition: CGRADataFlowGraph.hpp:428
DataNode(Value *v, SkipSeq *seq, int ID)
Definition: CGRADataFlowGraph.hpp:270
DFGEdge(const DFGEdge &E)
Definition: CGRADataFlowGraph.hpp:436
static nodes_iterator nodes_end(CGRADFG *G)
Definition: CGRADataFlowGraph.hpp:718
string getNodeAttr() const
Definition: CGRADataFlowGraph.hpp:216
std::string opcode
Definition: CGRADataFlowGraph.hpp:189
An abstract class for DFG node derived from DGNode.
Definition: CGRADataFlowGraph.hpp:69
bool isEqualTo(const DFGNode &N) const
Definition: CGRADataFlowGraph.hpp:117
static ChildEdgeIteratorType child_edge_end(const NodeRef N)
Definition: CGRADataFlowGraph.hpp:750
Specilized template of GraphTraits for DFNode.
Definition: CGRADataFlowGraph.hpp:678
DFGEdge & operator=(DFGEdge &&E)
Definition: CGRADataFlowGraph.hpp:448
static ChildIteratorType child_begin(NodeRef N)
Definition: CGRADataFlowGraph.hpp:689
bool isLoad()
Definition: CGRADataFlowGraph.hpp:206
Value * getValue() const
Definition: CGRADataFlowGraph.hpp:111
static const DFGNode * DDGGetTargetNode(const DFGEdge *P)
Definition: CGRADataFlowGraph.hpp:731
DirectedGraph< DFGNode, DFGEdge > CGRADFGBase
Definition: CGRADataFlowGraph.hpp:63
string getUniqueName() const
Definition: CGRADataFlowGraph.hpp:341
Definition: CGRADataFlowGraph.hpp:192
virtual string getExtraAttr() const
Definition: CGRADataFlowGraph.hpp:115
DFGEdge(DFGEdge &&E)
Definition: CGRADataFlowGraph.hpp:439
DGNode< DFGNode, DFGEdge > DFGNodeBase
Definition: CGRADataFlowGraph.hpp:61
GlobalDataNode(Value *v, SkipSeq *seq)
Definition: CGRADataFlowGraph.hpp:363
Instruction * getInst() const
Definition: CGRADataFlowGraph.hpp:185
DFGEdge & operator=(const DFGEdge &E)
Definition: CGRADataFlowGraph.hpp:443
int operand
Definition: CGRADataFlowGraph.hpp:460
GEPConstantNode(Value *v, int ID, int const_value)
Definition: CGRADataFlowGraph.hpp:407
static StringMap< StringRef > default_graph_prop
a default graph properties for DOT graph
Definition: CGRADataFlowGraph.hpp:822
Definition: CGRADataFlowGraph.hpp:483
Function * F
Definition: CGRADataFlowGraph.hpp:669
DFGNode::iterator ChildEdgeIteratorType
Definition: CGRADataFlowGraph.hpp:687
virtual string getExtraAttr() const
Definition: CGRADataFlowGraph.hpp:410
ConstantNode(Value *v)
Definition: CGRADataFlowGraph.hpp:327
int getID() const
Definition: CGRADataFlowGraph.hpp:109
Value * val
Definition: CGRADataFlowGraph.hpp:145
string getNodeAttr() const
Definition: CGRADataFlowGraph.hpp:392
static ChildIteratorType child_end(NodeRef N)
Definition: CGRADataFlowGraph.hpp:692
static bool classof(const DFGNode *N)
Definition: CGRADataFlowGraph.hpp:395
int ID
Definition: CGRADataFlowGraph.hpp:144
bool hasExtraInfo() const
Definition: CGRADataFlowGraph.hpp:125
DataNode(Value *v)
Definition: CGRADataFlowGraph.hpp:263