Caffe2 - C++ API
A deep learning, cross platform ML framework
ControlFlow.h
1 #ifndef NOM_REPRESENTATIONS_CONTROLFLOW_H
2 #define NOM_REPRESENTATIONS_CONTROLFLOW_H
3 
4 #include "nomnigraph/Graph/Graph.h"
5 #include "nomnigraph/Representations/Compiler.h"
6 
7 #include <unordered_map>
8 
9 namespace nom {
10 namespace repr {
11 
15 template <typename T, typename U> class BasicBlock {
16 public:
17  using NodeRef = typename Subgraph<T, U>::NodeRef;
18  BasicBlock() {}
19  ~BasicBlock() {
20  for (auto pair : callbacks) {
21  pair.first->deleteDestructorCallback(pair.second);
22  }
23  }
24 
25  void trackNode(NodeRef node) {
26  callbacks[node] = node->registerDestructorCallback([&](NodeRef n){
27  assert(hasInstruction(n) && "Destructor callback invoked on untracked node in BasicBlock.");
28  deleteInstruction(n);
29  });
30  Nodes.addNode(node);
31  }
32 
33  void untrackNode(NodeRef node) {
34  callbacks.erase(node);
35  Nodes.removeNode(node);
36  }
37 
38  void pushInstructionNode(NodeRef node) {
39  assert(isa<Instruction>(node->data()) &&
40  "Cannot push non-instruction node to basic block.");
41  Instructions.emplace_back(node);
42  trackNode(node);
43  }
44  const std::vector<NodeRef> &getInstructions() { return Instructions; }
45 
46  const bool hasInstruction(NodeRef instr) const {
47  return Nodes.hasNode(instr);
48  }
49 
50  void insertInstructionBefore(NodeRef newInstr, NodeRef instr) {
51  auto it = std::find(std::begin(Instructions), std::end(Instructions), instr);
52  Instructions.insert(it, newInstr);
53  trackNode(newInstr);
54  }
55 
56  void deleteInstruction(NodeRef instr) {
57  assert(hasInstruction(instr) && "Instruction not in basic block.");
58  Instructions.erase(std::remove(Instructions.begin(), Instructions.end(), instr),
59  Instructions.end());
60  untrackNode(instr);
61  }
62 
63 private:
64  Subgraph<T, U> Nodes;
65  std::vector<NodeRef> Instructions;
66  // Because we reference a dataflow graph, we need to register callbacks
67  // for when the dataflow graph is modified.
68  std::unordered_map<NodeRef, typename Notifier<Node<T, U>>::Callback *> callbacks;
69 };
70 
71 using Program = Graph<Value>;
72 
73 template <typename G> struct ControlFlowGraphImpl {
74  // Hack to help debugging in case this class is misused.
75  static_assert(sizeof(ControlFlowGraphImpl), "Template parameter G in "
76  "ControlFlowGraph<G> must be of "
77  "type Graph<T, U>.");
78 };
79 
80 template <typename T, typename U> struct ControlFlowGraphImpl<Graph<T, U>> {
82  using bbType = BasicBlock<T, U>;
83 };
84 
89 template<typename G>
90 class ControlFlowGraph : public ControlFlowGraphImpl<G>::type {
91 public:
92  // This is for C++11 compatibility, otherwise we could use "using"
93  ControlFlowGraph() {}
94  ControlFlowGraph(const ControlFlowGraph &) = delete;
95  ControlFlowGraph(ControlFlowGraph &&) = default;
96  ControlFlowGraph& operator=(ControlFlowGraph &&) = default;
97  ~ControlFlowGraph() {
98  }
99 };
100 
104 template <typename G>
105 using BasicBlockType = typename ControlFlowGraphImpl<G>::bbType;
106 
109 template <typename Phi, typename G> void addSSA(G* dfg, ControlFlowGraph<G> *cfg) {
110  static_assert(std::is_base_of<Instruction, Phi>::value, "Phi type must be derived from Instruction.");
111  auto dfMap = dominanceFrontierMap(cfg);
112  for (auto pair : dfMap) {
113  for (auto n : pair.second) {
114  printf("%llu -> %llu\n", (unsigned long long)pair.first, (unsigned long long)n);
115  }
116  }
117 }
118 
120 template <typename G>
121 void deleteNode(ControlFlowGraph<G>* cfg, typename G::NodeRef node) {
122  for (auto bbNode : cfg->getMutableNodes()) {
123  auto bb = bbNode->data().get();
124  if (bb->hasInstruction(node)) {
125  bb->deleteInstruction(node);
126  }
127  }
128 }
129 
130 } // namespace repr
131 } // namespace nom
132 
133 #endif // NOM_REPRESENTATIONS_CONTROLFLOW_H
Effectively a constant reference to a graph.
Definition: Graph.h:110
Definition: Caffe2.cc:16
Control flow graph is a graph of basic blocks that can be used as an analysis tool.
Definition: ControlFlow.h:90
A simple graph implementation.
Definition: Graph.h:30
A basic block holds a reference to a subgraph of the data flow graph as well as an ordering on instru...
Definition: ControlFlow.h:15