Skip to content
Snippets Groups Projects
Commit 25ca91c9 authored by Prakalp Srivastava's avatar Prakalp Srivastava
Browse files

Partial update on graph traits

parent 9733edac
No related branches found
No related tags found
No related merge requests found
...@@ -24,12 +24,15 @@ ...@@ -24,12 +24,15 @@
#include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Value.h" #include "llvm/IR/Value.h"
#include "llvm/Support/Compiler.h" #include "llvm/Support/Compiler.h"
#include "llvm/ADT/GraphTraits.h"
#include "llvm/Support/raw_ostream.h"
namespace llvm { namespace llvm {
class DFNode; class DFNode;
class DFEdge; class DFEdge;
class DFNodeVisitor; class DFNodeVisitor;
class DFTreeTraversal;
class DFEdgeVisitor; class DFEdgeVisitor;
class DFGraph { class DFGraph {
...@@ -49,6 +52,10 @@ public: ...@@ -49,6 +52,10 @@ public:
ChildrenList.push_back(child); ChildrenList.push_back(child);
} }
void addDFEdge(DFEdge* E) {
DFEdgeList.push_back(E);
}
// Iterators // Iterators
typedef DFNodeListType::iterator children_iterator; typedef DFNodeListType::iterator children_iterator;
typedef DFNodeListType::const_iterator const_children_iterator; typedef DFNodeListType::const_iterator const_children_iterator;
...@@ -71,7 +78,7 @@ public: ...@@ -71,7 +78,7 @@ public:
const DFNode *back() const { return ChildrenList.back(); } const DFNode *back() const { return ChildrenList.back(); }
DFNode *back() { return ChildrenList.back(); } DFNode *back() { return ChildrenList.back(); }
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
// DFEdgeList iterator forwarding functions // DFEdgeList iterator forwarding functions
...@@ -88,7 +95,7 @@ public: ...@@ -88,7 +95,7 @@ public:
const DFEdge *dfedge_back() const { return DFEdgeList.back(); } const DFEdge *dfedge_back() const { return DFEdgeList.back(); }
DFEdge *dfedge_back() { return DFEdgeList.back(); } DFEdge *dfedge_back() { return DFEdgeList.back(); }
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
}; };
...@@ -106,14 +113,39 @@ public: ...@@ -106,14 +113,39 @@ public:
class DFNode { class DFNode {
private: private:
typedef std::vector<DFNode*> DFNodeListType;
// Important things that make up a Dataflow Node // Important things that make up a Dataflow Node
IntrinsicInst* II; ///< Associated IntrinsicInst/Value IntrinsicInst* II; ///< Associated IntrinsicInst/Value
Function* FuncPointer; ///< Associated Function Function* FuncPointer; ///< Associated Function
DFNode* Parent; ///< Pointer to parent dataflow Node DFNode* Parent; ///< Pointer to parent dataflow Node
int NumOfDim; ///< Number of dimensions int NumOfDim; ///< Number of dimensions
std::vector<Value*> DimLimits; ///< Number of instances in each dimension std::vector<Value*> DimLimits; ///< Number of instances in each dimension
DFNodeListType Successors; ///< List of successors i.e.,
///< destination DFNodes to DFEdges
///< originating from this DFNode
public: public:
// Iterators
typedef DFNodeListType::iterator successor_iterator;
typedef DFNodeListType::const_iterator const_successor_iterator;
//===--------------------------------------------------------------------===//
// DFNodeList iterator forwarding functions
//
successor_iterator successors_begin() { return Successors.begin(); }
const_successor_iterator successors_begin() const { return Successors.begin(); }
successor_iterator successors_end () { return Successors.end(); }
const_successor_iterator successors_end () const { return Successors.end(); }
size_t successors_size() const { return Successors.size(); }
bool successors_empty() const { return Successors.empty(); }
const DFNode* successors_front() const { return Successors.front(); }
DFNode* successors_front() { return Successors.front(); }
const DFNode* successors_back() const { return Successors.back(); }
DFNode* successors_back() { return Successors.back(); }
//===--------------------------------------------------------------------===//
// Functions // Functions
DFNode(IntrinsicInst* _II, Function* _FuncPointer, DFNode* _Parent, DFNode(IntrinsicInst* _II, Function* _FuncPointer, DFNode* _Parent,
...@@ -121,29 +153,29 @@ public: ...@@ -121,29 +153,29 @@ public:
FuncPointer(_FuncPointer), Parent(_Parent), NumOfDim(_NumOfDim), FuncPointer(_FuncPointer), Parent(_Parent), NumOfDim(_NumOfDim),
DimLimits(_DimLimits) {} DimLimits(_DimLimits) {}
// FIXME: unused - should remove it probably void addSuccessor(DFNode* N) {
static DFNode *Create(IntrinsicInst* II, Function* FuncPointer, Successors.push_back(N);
DFNode* Parent = NULL, int NumOfDim = 0,
std::vector<Value*> DimLimits = std::vector<Value*>()) {
return new DFNode(II, FuncPointer, Parent, NumOfDim, DimLimits);
} }
// FIXME: When constructor and create method are removed, these should be virtual methods Function* getFuncPointer() {
void applyDFNodeVisitor(DFNodeVisitor &V); return FuncPointer;
void applyDFEdgeVisitor(DFEdgeVisitor &V); }
virtual void applyDFNodeVisitor(DFNodeVisitor &V) = 0;
// virtual void applyDFEdgeVisitor(DFEdgeVisitor &V) = 0;
}; };
class DFInternalNode : public DFNode { class DFInternalNode : public DFNode {
private: private:
DFGraph* Graph; DFGraph* childGraph;
// Constructor // Constructor
DFInternalNode(IntrinsicInst* II, Function* FuncPointer, DFNode* Parent, DFInternalNode(IntrinsicInst* II, Function* FuncPointer, DFNode* Parent,
int NumOfDim, std::vector<Value*> DimLimits) : int NumOfDim, std::vector<Value*> DimLimits) :
DFNode(II, FuncPointer, Parent, NumOfDim, DimLimits) { DFNode(II, FuncPointer, Parent, NumOfDim, DimLimits) {
Graph = new DFGraph(); childGraph = new DFGraph();
} }
public: public:
...@@ -154,12 +186,19 @@ public: ...@@ -154,12 +186,19 @@ public:
} }
void addChildToDFGraph(DFNode* N) { void addChildToDFGraph(DFNode* N) {
Graph->addChildDFNode(N); childGraph->addChildDFNode(N);
}
void addEdgeToDFGraph(DFEdge* E) {
childGraph->addDFEdge(E);
} }
void applyDFNodeVisitor(DFNodeVisitor &V); DFGraph* getChildGraph() {
void applyDFEdgeVisitor(DFEdgeVisitor &V); return childGraph;
}
void applyDFNodeVisitor(DFNodeVisitor &V); /*virtual*/
// void applyDFEdgeVisitor(DFEdgeVisitor &V); /*virtual*/
}; };
class DFLeafNode : public DFNode { class DFLeafNode : public DFNode {
...@@ -178,8 +217,8 @@ public: ...@@ -178,8 +217,8 @@ public:
return new DFLeafNode(II, FuncPointer, Parent, NumOfDim, DimLimits); return new DFLeafNode(II, FuncPointer, Parent, NumOfDim, DimLimits);
} }
void applyDFNodeVisitor(DFNodeVisitor &V); void applyDFNodeVisitor(DFNodeVisitor &V); /*virtual*/
void applyDFEdgeVisitor(DFEdgeVisitor &V); // void applyDFEdgeVisitor(DFEdgeVisitor &V); /*virtual*/
}; };
...@@ -225,16 +264,98 @@ public: ...@@ -225,16 +264,98 @@ public:
// Visitor for DFNode objects // Visitor for DFNode objects
class DFNodeVisitor { class DFNodeVisitor {
public: public:
virtual void visit(DFInternalNode &)=0; virtual void visit(DFInternalNode* N) = 0;
virtual void visit(DFLeafNode &)=0; virtual void visit(DFLeafNode* N) = 0;
}; };
void DFInternalNode::applyDFNodeVisitor(DFNodeVisitor &V) {
V.visit(this);
}
void DFLeafNode::applyDFNodeVisitor(DFNodeVisitor &V) {
V.visit(this);
}
class DFTreeTraversal : public DFNodeVisitor {
public:
virtual void visit(DFInternalNode* N){
errs() << "Visted Node (I) - " << N->getFuncPointer()->getName() << "\n";
for(DFGraph::children_iterator i = N->getChildGraph()->begin(),
e = N->getChildGraph()->end(); i != e; ++i) {
DFNode* child = *i;
child->applyDFNodeVisitor(*this);
}
}
virtual void visit(DFLeafNode* N) {
errs() << "Visted Node (L) - " << N->getFuncPointer()->getName() << "\n";
}
};
class FollowSuccessors : public DFNodeVisitor {
public:
virtual void visit(DFInternalNode* N) {
errs() << "Visted Node (I) - " << N->getFuncPointer()->getName() << "\n";
for(DFInternalNode::successor_iterator i = N->successors_begin(),
e = N->successors_end(); i != e; ++i) {
/* Traverse the graph.
* Choose the kind of traversal we want
* Do we do a DAG kind of traversal?
*/
}
}
virtual void visit(DFLeafNode* N) {
errs() << "Visted Node (L) - " << N->getFuncPointer()->getName() << "\n";
}
};
/*
// Visitor for DFEdge objects // Visitor for DFEdge objects
class DFEdgeVisitor { class DFEdgeVisitor {
public: public:
virtual void visit(DFEdge &)=0; virtual void visit(DFEdge* E) = 0;
};
*/
//===--------------------------------------------------------------------===//
// GraphTraits specializations for DFNode graph (DFG)
//===--------------------------------------------------------------------===//
// Provide specializations of GraphTraits to be able to treat a DFNode as a
// graph of DFNodes...
template <> struct GraphTraits<DFNode*> {
typedef DFNode NodeType;
typedef DFNode::successor_iterator ChildIteratorType;
static NodeType *getEntryNode(DFNode *N) { return N; }
static inline ChildIteratorType child_begin(NodeType *N) {
return N->successors_begin();
}
static inline ChildIteratorType child_end(NodeType *N) {
return N->successors_end();
}
};
template <> struct GraphTraits<const DFNode*> {
typedef const DFNode NodeType;
typedef DFNode::const_successor_iterator ChildIteratorType;
static NodeType *getEntryNode(const DFNode *N) { return N; }
static inline ChildIteratorType child_begin(NodeType *N) {
return N->successors_begin();
}
static inline ChildIteratorType child_end(NodeType *N) {
return N->successors_end();
}
}; };
} // End llvm namespace } // End llvm namespace
#endif #endif
...@@ -20,24 +20,26 @@ let TargetPrefix = "visc" in { ...@@ -20,24 +20,26 @@ let TargetPrefix = "visc" in {
/* Create Node intrinsic - /* Create Node intrinsic -
* i8* llvm.visc.createNode(function*); * i8* llvm.visc.createNode(function*);
*/ */
def int_visc_createNode : Intrinsic<[llvm_ptr_ty], [llvm_anyptr_ty], []>; def int_visc_test : Intrinsic<[llvm_ptr_ty], [llvm_i32_ty], []>;
def int_visc_createNode : Intrinsic<[llvm_ptr_ty], [llvm_ptr_ty], []>;
/* Create Node 1D array intrinsic - /* Create Node 1D array intrinsic -
* i8* llvm.visc.createNode1D(function*, i32); * i8* llvm.visc.createNode1D(function*, i32);
*/ */
def int_visc_createNode1D : Intrinsic<[llvm_ptr_ty], [llvm_anyptr_ty, def int_visc_createNode1D : Intrinsic<[llvm_ptr_ty], [llvm_ptr_ty,
llvm_i32_ty], []>; llvm_i32_ty], []>;
/* Create Node 2D array intrinsic - /* Create Node 2D array intrinsic -
* i8* llvm.visc.createNode2D(function*, i32, i32); * i8* llvm.visc.createNode2D(function*, i32, i32);
*/ */
def int_visc_createNode2D : Intrinsic<[llvm_ptr_ty], [llvm_anyptr_ty, def int_visc_createNode2D : Intrinsic<[llvm_ptr_ty], [llvm_ptr_ty,
llvm_i32_ty, llvm_i32_ty], []>; llvm_i32_ty, llvm_i32_ty], []>;
/* Create Node 3D array intrinsic - /* Create Node 3D array intrinsic -
* i8* llvm.visc.createNode2D(function*, i32, i32, i32); * i8* llvm.visc.createNode2D(function*, i32, i32, i32);
*/ */
def int_visc_createNode3D : Intrinsic<[llvm_ptr_ty], [llvm_anyptr_ty, def int_visc_createNode3D : Intrinsic<[llvm_ptr_ty], [llvm_ptr_ty,
llvm_i32_ty, llvm_i32_ty, llvm_i32_ty], llvm_i32_ty, llvm_i32_ty, llvm_i32_ty],
[]>; []>;
...@@ -45,7 +47,7 @@ let TargetPrefix = "visc" in { ...@@ -45,7 +47,7 @@ let TargetPrefix = "visc" in {
* i8* llvm.visc.createEdge(i8*, i8*, funtion*, function*); * i8* llvm.visc.createEdge(i8*, i8*, funtion*, function*);
*/ */
def int_visc_createEdge : Intrinsic<[llvm_ptr_ty], [llvm_ptr_ty, llvm_ptr_ty, def int_visc_createEdge : Intrinsic<[llvm_ptr_ty], [llvm_ptr_ty, llvm_ptr_ty,
llvm_anyptr_ty, llvm_anyptr_ty], []>; llvm_ptr_ty, llvm_ptr_ty], []>;
/* Find associated dataflow node intrinsic - /* Find associated dataflow node intrinsic -
* i8* llvm.visc.getNode(); * i8* llvm.visc.getNode();
......
...@@ -42,27 +42,31 @@ namespace { ...@@ -42,27 +42,31 @@ namespace {
// Functions // Functions
bool isViscIntrinsic(Instruction * I); bool isViscIntrinsic(Instruction * I);
void handleCreateNode (DFInternalNode* N, IntrinsicInst* II); void handleCreateNode (DFInternalNode* N, IntrinsicInst* II);
void handleCreateEdge (DFNode* N, IntrinsicInst* II); void handleCreateEdge (DFInternalNode* N, IntrinsicInst* II);
void BuildGraph (DFInternalNode* N, Function* F); void BuildGraph (DFInternalNode* N, Function* F);
public: public:
virtual bool runOnModule(Module &M) { virtual bool runOnModule(Module &M) {
// Iterate over all functions in the Module // Loop over all of the global variables
for (Module::iterator f = M.begin(), e = M.end(); f != e; ++f) { for (Module::const_global_iterator i = M.global_begin(),
errs() << "Function: " << f->getName() << "\n"; e = M.global_end(); i != e; ++i) {
ValueSymbolTable& V = f->getValueSymbolTable(); errs() << "Global: " << i->getName() << "\t" << *(i->getOperand(0))<< "\n";
errs() << "----------- Dumping Symbol Table ----------\n";
V.dump();
errs() << "------------------ Done -------------------\n\n";
// Find the function associated with root dataflow graph node // Find the function associated with root dataflow graph node
// Start from root to build the dataflow graph in top-down fashion // Start from root to build the dataflow graph in top-down fashion
// The name of the root node function is llvm.visc.root (for now) // The name of the root node function is llvm.visc.root (for now)
if((f->getName()).equals("llvm.visc.root")) { if((i->getName()).equals("llvm.visc.root")) {
errs() << "----------- Found Root Node -------------\n\n";
Function* f = cast<Function>(i->getOperand(0));
Root = DFInternalNode::Create(NULL, f); Root = DFInternalNode::Create(NULL, f);
BuildGraph(Root, f); BuildGraph(Root, f);
break;
}
else {
errs() << "Root Node not found yet :( \n";
} }
} }
return false; //TODO: What does returning "false" mean? return false; //TODO: What does returning "false" mean?
...@@ -73,7 +77,7 @@ namespace { ...@@ -73,7 +77,7 @@ namespace {
// Returns true if the instruction I is a visc intrinsic, false otherwise // Returns true if the instruction I is a visc intrinsic, false otherwise
bool BuildDFG::isViscIntrinsic(Instruction* I) { bool BuildDFG::isViscIntrinsic(Instruction* I) {
if (IntrinsicInst* II = dyn_cast<IntrinsicInst>(I)) { if (IntrinsicInst* II = dyn_cast<IntrinsicInst>(I)) {
if (((II->getCalledFunction()->getName()).startswith("llvm.visc."))) { if (((II->getCalledFunction()->getName()).startswith("llvm.visc.create"))) {
// It is a visc intrinsic // It is a visc intrinsic
return true; return true;
} }
...@@ -85,7 +89,7 @@ namespace { ...@@ -85,7 +89,7 @@ namespace {
void BuildDFG::handleCreateNode(DFInternalNode* N, IntrinsicInst* II) { void BuildDFG::handleCreateNode(DFInternalNode* N, IntrinsicInst* II) {
bool isInternalNode = false; bool isInternalNode = false;
Function* F = cast<Function>(II->getOperand(0)); Function* F = cast<Function>((II->getOperand(0))->stripPointerCasts());
// Check if the function associated with this intrinsic is a leaf or // Check if the function associated with this intrinsic is a leaf or
// internal node // internal node
...@@ -121,7 +125,7 @@ namespace { ...@@ -121,7 +125,7 @@ namespace {
} }
} }
void BuildDFG::handleCreateEdge (DFNode* N, IntrinsicInst* II) { void BuildDFG::handleCreateEdge (DFInternalNode* N, IntrinsicInst* II) {
// The DFNode structures must be in the map before the edge is processed // The DFNode structures must be in the map before the edge is processed
HandleToDFNode::iterator DFI = HandleToDFNodeMap.find(II->getOperand(0)); HandleToDFNode::iterator DFI = HandleToDFNodeMap.find(II->getOperand(0));
assert(DFI != HandleToDFNodeMap.end()); assert(DFI != HandleToDFNodeMap.end());
...@@ -134,10 +138,8 @@ namespace { ...@@ -134,10 +138,8 @@ namespace {
Function* ArgMapFunc = cast<Function>(II->getOperand(3)); Function* ArgMapFunc = cast<Function>(II->getOperand(3));
DFEdge* NewDFEdge = DFEdge::Create(SrcDF, DestDF, DFMapFunc, ArgMapFunc); DFEdge* NewDFEdge = DFEdge::Create(SrcDF, DestDF, DFMapFunc, ArgMapFunc);
// FIXME Need interface of DFNode for the following: // FIXME Need interface of DFNode for the following:
// N->addOutgoingEdge(NewDFEdge); N->addEdgeToDFGraph(NewDFEdge);
// N->addIncomingEdge(NewDFEdge); SrcDF->addSuccessor(DestDF);
// SrcDF->addSuccesor(probably NewDFEdge, could be DestDF);
// DestDF->addPredeccesor(probably NewDFEdge, could be SrcDF);
} }
void BuildDFG::BuildGraph (DFInternalNode* N, Function *F) { void BuildDFG::BuildGraph (DFInternalNode* N, Function *F) {
...@@ -150,10 +152,13 @@ namespace { ...@@ -150,10 +152,13 @@ namespace {
// intrinsics. // intrinsics.
for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) { for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
Instruction* I = &*i; // Grab pointer to instruction reference Instruction* I = &*i; // Grab pointer to instruction reference
// errs() << *I << "\n"; errs() << *I << "\n";
if(IntrinsicInst* II = dyn_cast<IntrinsicInst>(I)) { if(IntrinsicInst* II = dyn_cast<IntrinsicInst>(I)) {
errs() << "IntrinsicID = " << II->getIntrinsicID() << ": " << II->getCalledFunction()->getName()<<"\n"; errs() << "IntrinsicID = " << II->getIntrinsicID() << ": " << II->getCalledFunction()->getName()<<"\n";
switch(II->getIntrinsicID()) { switch(II->getIntrinsicID()) {
case Intrinsic::visc_test:
errs() << "Found Test Intrinsic";
break;
case Intrinsic::visc_createNode: case Intrinsic::visc_createNode:
case Intrinsic::visc_createNode1D: case Intrinsic::visc_createNode1D:
...@@ -171,10 +176,13 @@ namespace { ...@@ -171,10 +176,13 @@ namespace {
break; break;
} }
} }
else {
errs() << "Non-intrinsic instruction\n";
}
} }
} }
} // End of namespace } // End of namespace
char BuildDFG::ID = 0; char BuildDFG::ID = 0;
static RegisterPass<BuildDFG> X("buildDFG", "Hierarchical Dataflow Graph Builder Pass"); static RegisterPass<BuildDFG> X("buildDFG", "Hierarchical Dataflow Graph Builder Pass", false, false);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment