CGRAOmp  0.1
DFGPass.hpp
Go to the documentation of this file.
1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021 Amano laboratory, Keio University & Processor Research Team, RIKEN Center for Computational Science
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * File: /include/DFGPass.hpp
25 * Project: CGRAOmp
26 * Author: Takuya Kojima in The University of Tokyo (tkojima@hal.ipc.i.u-tokyo.ac.jp)
27 * Created Date: 15-12-2021 09:59:52
28 * Last Modified: 12-07-2022 18:22:30
29 */
30 #ifndef DFGPASS_H
31 #define DFGPASS_H
32 
33 #include "llvm/IR/PassManager.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/IR/Instruction.h"
36 #include "llvm/Passes/PassBuilder.h"
37 #include "llvm/Passes/PassPlugin.h"
38 #include "llvm/ADT/SmallVector.h"
39 #include "llvm/ADT/iterator_range.h"
40 
41 #include "CGRAModel.hpp"
42 #include "CGRADataFlowGraph.hpp"
43 
44 using namespace llvm;
45 
46 
47 namespace CGRAOmp {
48 
53  struct DFGPassConcept {
54  virtual ~DFGPassConcept() = default;
55 
60  virtual StringRef name() const = 0;
61 
73  virtual bool run(CGRADFG &G, Loop &L, FunctionAnalysisManager &FAM,
74  LoopAnalysisManager &LAM,
75  LoopStandardAnalysisResults &AR) = 0;
76 
77  };
78 
84  template <typename PassT>
85  struct DFGPassModel : public DFGPassConcept {
87  explicit DFGPassModel(PassT Pass) : Pass(std::move(Pass)) {}
89  DFGPassModel(const DFGPassModel &Arg) : Pass(Arg.Pass) {}
91  DFGPassModel(DFGPassModel &&Arg) : Pass(std::move(Arg.Pass)) {}
92 
94  bool run(CGRADFG &G, Loop &L, FunctionAnalysisManager &FAM,
95  LoopAnalysisManager &LAM,
96  LoopStandardAnalysisResults &AR) override {
97  return Pass.run(G, L, FAM, LAM, AR);
98  };
99 
101  StringRef name() const override { return PassT::name(); }
102 
103  private:
104  PassT Pass;
105  };
106 
112  public:
118  template <typename PassT>
119  void addPass(PassT Pass) {
120  pipeline.emplace_back(new DFGPassModel(std::move(Pass)));
121  }
122 
134  bool run(CGRADFG &G, Loop &L, FunctionAnalysisManager &FAM,
135  LoopAnalysisManager &LAM,
136  LoopStandardAnalysisResults &AR);
137  private:
138  SmallVector<DFGPassConcept*> pipeline;
139  };
140 
146  public:
147  using CallBackT = std::function<bool(StringRef, DFGPassManager &)>;
148  DFGPassBuilder();
153  void registerPipelineParsingCallback(const CallBackT &C);
154 
161  Error parsePassPipeline(DFGPassManager &DPM, ArrayRef<std::string> PipelineText);
162 
163  private:
164  SmallVector<CallBackT> callback_list;
165 
170  Error search_callback();
171  };
172 
179  const char *PluginName;
181  void (*RegisterPassBuilderCallbacks)(DFGPassBuilder &);
182  };
183 
188  class DFGPassHandler : public PassInfoMixin<DFGPassHandler> {
189  public:
190  using GraphList = SmallVector<CGRADFG*>;
191  using graph_iterator = GraphList::iterator;
192 
194  DFGPassHandler();
196  delete DPB;
197  delete DPM;
198  }
200  DFGPassHandler(DFGPassHandler &&P) : PassInfoMixin<DFGPassHandler>(std::move(P)),
201  DPB(P.DPB), DPM(P.DPM) {
202  P.DPB = nullptr;
203  P.DPM = nullptr;
204  };
205 
206  PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
207 
208  void addGraph(CGRADFG *G) {
209  graph_list.emplace_back(G);
210  }
211 
213  graph_iterator it;
214  //find graph
215  for (it = graph_begin(); it != graph_end(); it++) {
216  if (*it == G) {
217  break;
218  }
219  }
220  if (it != graph_end()) {
221  return graph_list.erase(it);
222  }
223  return it;
224  }
225 
227  return graph_list.begin();
228  }
229 
231  return graph_list.end();
232  }
233 
234 
235  iterator_range<graph_iterator> graphs() {
236  return make_range(graph_begin(), graph_end());
237  }
238 
239  private:
246  template<typename VerifyPassT>
247  void createDataFlowGraphsForAllKernels(Function &F, FunctionAnalysisManager &AM);
248 
258  template<typename VerifyPassT>
259  void createDataFlowGraph(Function &F, Loop &L, FunctionAnalysisManager &FAM,
260  LoopAnalysisManager &LAM,
261  LoopStandardAnalysisResults &AR);
262 
270  inline bool isMemAccess(Instruction &I) {
271  return isa<LoadInst>(I) || isa<StoreInst>(I);
272  }
273 
280  inline DFGNode* make_mem_node(Instruction &I) {
281  if (auto load = dyn_cast<LoadInst>(&I)) {
282  return new MemAccessNode(load);
283  } else if (auto store = dyn_cast<StoreInst>(&I)) {
284  return new MemAccessNode(store);
285  } else {
286  assert(false && "Non-memory access instruction");
287  }
288  }
295  inline DFGNode* make_comp_node(Instruction *inst, std::string opcode) {
296  return new ComputeNode(inst, opcode);
297  }
298 
305  inline DFGNode* make_const_node(Value *V) {
306  return new ConstantNode(V);
307  }
308 
316  inline DFGNode* make_const_node(Value *V, SmallVector<Value*>* seq) {
317  return new ConstantNode(V, seq);
318  }
319 
326  inline DFGNode* make_global_node(Value *V) {
327  return new GlobalDataNode(V);
328  }
329 
337  inline DFGNode* make_global_node(Value *V, SmallVector<Value*>* seq) {
338  return new GlobalDataNode(V, seq);
339  }
340 
343  SmallVector<CGRADFG*> graph_list;
344 
345 
346  };
347 
348 }
349 
350 #endif //DFGPASS_H
CGRAOmp::DFGPassModel
A template wrapper used to implement the polymorphic API.
Definition: DFGPass.hpp:85
CGRAOmp::DFGPassHandler::DPB
DFGPassBuilder * DPB
Definition: DFGPass.hpp:341
CGRAOmp::DFGPassHandler::graph_end
graph_iterator graph_end()
Definition: DFGPass.hpp:230
llvm
Definition: OptionPlugin.cpp:128
CGRAOmp::DFGPassHandler::graphs
iterator_range< graph_iterator > graphs()
Definition: DFGPass.hpp:235
CGRAOmp::DFGPassHandler::make_const_node
DFGNode * make_const_node(Value *V, SmallVector< Value * > *seq)
create constant node with different data source
Definition: DFGPass.hpp:316
CGRAOmp::DFGPassHandler::GraphList
SmallVector< CGRADFG * > GraphList
Definition: DFGPass.hpp:190
CGRAModel.hpp
CGRAOmp::DFGPassModel::name
StringRef name() const override
Wrapper to get the name.
Definition: DFGPass.hpp:101
CGRAOmp::DFGPassHandler::make_global_node
DFGNode * make_global_node(Value *V, SmallVector< Value * > *seq)
create global data node with different data source
Definition: DFGPass.hpp:337
CGRAOmp::DFGPassHandler::make_mem_node
DFGNode * make_mem_node(Instruction &I)
create memory access node
Definition: DFGPass.hpp:280
CGRAOmp::DFGPassHandler::make_global_node
DFGNode * make_global_node(Value *V)
create global data node
Definition: DFGPass.hpp:326
CGRAOmp::DFGPassHandler::graph_iterator
GraphList::iterator graph_iterator
Definition: DFGPass.hpp:191
CGRAOmp::DFGPassModel::DFGPassModel
DFGPassModel(DFGPassModel &&Arg)
move constructor
Definition: DFGPass.hpp:91
CGRAOmp::DFGPassModel::DFGPassModel
DFGPassModel(PassT Pass)
default constructor
Definition: DFGPass.hpp:87
llvm::GlobalDataNode
Definition: CGRADataFlowGraph.hpp:357
CGRAOmp::DFGPassHandler::DPM
DFGPassManager * DPM
Definition: DFGPass.hpp:342
CGRAOmp::DFGPassModel::run
bool run(CGRADFG &G, Loop &L, FunctionAnalysisManager &FAM, LoopAnalysisManager &LAM, LoopStandardAnalysisResults &AR) override
Wrapper to run the implemented optimization function.
Definition: DFGPass.hpp:94
CGRAOmp::DFGPassHandler::graph_list
SmallVector< CGRADFG * > graph_list
Definition: DFGPass.hpp:343
CGRAOmp::DFGPassModel::Pass
PassT Pass
Definition: DFGPass.hpp:104
CGRAOmp::DFGPassHandler::DFGPassHandler
DFGPassHandler(DFGPassHandler &&P)
Move constractor.
Definition: DFGPass.hpp:200
CGRAOmp::DFGPassConcept
The abstract class of the DFG pass manager interfaces.
Definition: DFGPass.hpp:53
CGRAOmp::DFGPassHandler::removeGraph
graph_iterator removeGraph(CGRADFG *G)
Definition: DFGPass.hpp:212
CGRAOmp
Definition: AGVerifyPass.hpp:50
CGRADataFlowGraph.hpp
CGRAOmp::DFGPassManager::addPass
void addPass(PassT Pass)
Adding a pass to the DFG pass manager.
Definition: DFGPass.hpp:119
llvm::ConstantNode
Definition: CGRADataFlowGraph.hpp:325
CGRAOmp::DFGPassHandler::make_const_node
DFGNode * make_const_node(Value *V)
create constant node
Definition: DFGPass.hpp:305
CGRAOmp::DFGPassHandler::make_comp_node
DFGNode * make_comp_node(Instruction *inst, std::string opcode)
create computational node
Definition: DFGPass.hpp:295
CGRAOmp::DFGPassHandler::~DFGPassHandler
~DFGPassHandler()
Definition: DFGPass.hpp:195
llvm::CGRADFG
A graph class for CGRA kernel DFG derived from llvm::DirectedGraph.
Definition: CGRADataFlowGraph.hpp:505
CGRAOmp::DFGPassHandler::graph_begin
graph_iterator graph_begin()
Definition: DFGPass.hpp:226
CGRAOmp::DFGPassHandler
Module Pass to create data flow graph for all kernels while aplying DFG Optimization Passes.
Definition: DFGPass.hpp:188
CGRAOmp::DFGPassBuilder::callback_list
SmallVector< CallBackT > callback_list
Definition: DFGPass.hpp:164
CGRAOmp::DFGPassManager::pipeline
SmallVector< DFGPassConcept * > pipeline
Definition: DFGPass.hpp:138
llvm::ComputeNode
A concrete class for computational nodes.
Definition: CGRADataFlowGraph.hpp:171
CGRAOmp::DFGPassModel::DFGPassModel
DFGPassModel(const DFGPassModel &Arg)
copy constructor
Definition: DFGPass.hpp:89
CGRAOmp::DFGPassHandler::addGraph
void addGraph(CGRADFG *G)
Definition: DFGPass.hpp:208
CGRAOmp::DFGPassHandler::isMemAccess
bool isMemAccess(Instruction &I)
check if the instruction is memory access or not
Definition: DFGPass.hpp:270
llvm::DFGNode
An abstract class for DFG node derived from DGNode.
Definition: CGRADataFlowGraph.hpp:69
llvm::MemAccessNode
Definition: CGRADataFlowGraph.hpp:192
CGRAOmp::DFGPassPluginLibraryInfo::PluginName
const char * PluginName
A meaningful name of the plugin.
Definition: DFGPass.hpp:179
CGRAOmp::DFGPassBuilder::CallBackT
std::function< bool(StringRef, DFGPassManager &)> CallBackT
Definition: DFGPass.hpp:147
CGRAOmp::DFGPassBuilder
A class to access the DFG Passes.
Definition: DFGPass.hpp:145
CGRAOmp::DFGPassPluginLibraryInfo
A bundled information about the DFG Pass.
Definition: DFGPass.hpp:177
CGRAOmp::DFGPassManager
Manages a sequence of passes over a DFG.
Definition: DFGPass.hpp:111