CGRAOmp  0.1
CGRAOmpPass.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/CGRAOmpPass.hpp
25 * Project: CGRAOmp
26 * Author: Takuya Kojima in The University of Tokyo (tkojima@hal.ipc.i.u-tokyo.ac.jp)
27 * Created Date: 27-08-2021 14:19:42
28 * Last Modified: 22-02-2022 03:38:11
29 */
30 #ifndef CGRAOmpPass_H
31 #define CGRAOmpPass_H
32 
33 #include "llvm/IR/PassManager.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/IR/Function.h"
36 #include "llvm/Analysis/LoopInfo.h"
37 #include "llvm/IR/Value.h"
38 #include "llvm/Passes/PassBuilder.h"
39 #include "llvm/Passes/PassPlugin.h"
40 #include "llvm/ADT/StringMap.h"
41 #include "llvm/ADT/SetVector.h"
42 #include "llvm/Support/Error.h"
43 #include "llvm/ADT/SmallVector.h"
44 #include "llvm/ADT/iterator_range.h"
45 #include "llvm/Support/FormatVariadic.h"
46 
47 #include "CGRAModel.hpp"
48 
49 #define OMP_STATIC_INIT_SCHED 2
50 #define OMP_STATIC_INIT_PLASTITER 3
51 #define OMP_STATIC_INIT_PLOWER 4
52 #define OMP_STATIC_INIT_PUPPER 5
53 #define OMP_STATIC_INIT_PSTRIDE 6
54 #define OMP_STATIC_INIT_INCR 7
55 #define OMP_STATIC_INIT_CHUNK 8
56 #define OMP_STATIC_INIT_OPERAND_N (OMP_STATIC_INIT_CHUNK + 1)
57 
58 #include <system_error>
59 
60 using namespace llvm;
61 
62 #define KERNEL_INFO_PREFIX ".omp_offloading.entry"
63 #define OFFLOADINFO_METADATA_NAME "omp_offload.info"
64 #define OUTLINED_FUNC_NAME_FMT "__omp_offloading_{0:x-}_{1:x-}_{2}_l{3:d}"
65 #define ADD_FUNC_PASS(P) (createModuleToFunctionPassAdaptor(P))
66 #define ADD_LOOP_PASS(P) (createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(P)))
67 
68 namespace CGRAOmp {
69 
70 
76  class ModelManager {
77  public:
78  ModelManager() = delete;
84  explicit ModelManager(CGRAModel *CM) : model(CM) {};
86  ModelManager(const ModelManager&) = default;
88  ModelManager(ModelManager&&) = default;
93  CGRAModel* getModel() const { return model; }
94 
96  template <typename IRUnitT, typename InvT>
97  bool invalidate(IRUnitT& IR, const PreservedAnalyses &PA,
98  InvT &Inv);
99  private:
101  };
102 
107  class ModelManagerPass : public AnalysisInfoMixin<ModelManagerPass> {
108  public:
110  Result run(Module &M, ModuleAnalysisManager &AM);
111  private:
112  friend AnalysisInfoMixin<ModelManagerPass>;
113  static AnalysisKey Key;
115  };
116 
122  class ModelManagerFunctionProxy : public AnalysisInfoMixin<ModelManagerFunctionProxy> {
123  public:
125  Result run(Function &F, FunctionAnalysisManager &AM);
126  private:
127  friend AnalysisInfoMixin<ModelManagerFunctionProxy>;
128  static AnalysisKey Key;
129  };
130 
136  class ModelManagerLoopProxy : public AnalysisInfoMixin<ModelManagerLoopProxy> {
137  public:
139  Result run(Loop &L, LoopAnalysisManager &AM,
140  LoopStandardAnalysisResults &AR);
141  private:
142  friend AnalysisInfoMixin<ModelManagerLoopProxy>;
143  static AnalysisKey Key;
144  };
145 
151  public:
155  int file_ID;
156  StringRef func_name;
157  int line;
158  int order;
159  };
160 
162  template <typename IRUnitT, typename InvT>
163  bool invalidate(IRUnitT& IR, const PreservedAnalyses &PA,
164  InvT &Inv);
165 
166  using MetadataList = SmallVector<OffloadMetadata_t>;
167  using md_iterator = MetadataList::iterator;
168 
169  using FunctionList = SmallVector<Function*>;
170  using func_iterator = FunctionList::iterator;
171 
172 
173  // metadata itarators
175  return md_list.begin();
176  }
177 
178  inline md_iterator md_end() {
179  return md_list.end();
180  }
181 
182  inline iterator_range<md_iterator> metadata() {
183  return make_range(md_begin(), md_end());
184  }
185 
186  // kernel iterators
188  return kernel_list.begin();
189  }
190 
192  return kernel_list.end();
193  }
194 
195  inline iterator_range<func_iterator> kernels() {
196  return make_range(kernel_begin(), kernel_end());
197  }
198 
205  void add_kernel(Function *offload, Function *kernel) {
206  kernel_list.emplace_back(kernel);
207  offload_func_list.emplace_back(offload);
208  }
209 
216  Function* getOffloadFunction(Function *kernel) {
217  auto kernel_it = kernel_begin();
218  auto offload_it = offload_func_list.begin();
219  for (;kernel_it != kernel_end(); kernel_it++, offload_it++) {
220  if (kernel == *kernel_it) return *offload_it;
221  }
222  return nullptr;
223  }
224 
231  md_iterator getMetadata(Function *offload);
232 
240  void setOffloadMetadata(Module &M);
241 
248  int getKernelLine(Function *kernel);
249  private:
253 
254  };
255 
261  public AnalysisInfoMixin<OmpKernelAnalysisPass> {
262  public:
264  Result run(Module &M, ModuleAnalysisManager &AM);
265  private:
266  friend AnalysisInfoMixin<OmpKernelAnalysisPass>;
268  static AnalysisKey Key;
269 
270 
271  };
272 
278  using Info_t = SetVector<Value*>;
279  public:
280  OmpScheduleInfo(CallBase *caller,
281  Value *schedule_type,
282  Value *last_iter_flag,
283  Value *lower_bound,
284  Value *upper_bound,
285  Value *stride,
286  Value *increment,
287  Value *chunk) :
288  caller(caller) {
289  info.insert(schedule_type);
290  info.insert(last_iter_flag);
291  info.insert(lower_bound);
292  info.insert(upper_bound);
293  info.insert(stride);
294  info.insert(increment);
295  info.insert(chunk);
296  valid = true;
297  };
299  valid = false;
300  for (int i = 0; i < OMP_STATIC_INIT_OPERAND_N; i++) {
301  info.insert(nullptr);
302  }
303  };
304  bool invalidate(Function &F, const PreservedAnalyses &PA,
305  FunctionAnalysisManager::Invalidator &Inv);
306 
307  explicit operator bool() const noexcept {
308  return valid;
309  }
310  Info_t::iterator begin() {
311  return info.begin();
312  }
313  Info_t::iterator end() {
314  return info.end();
315  }
316  bool contains(Value* V) {
317  return info.contains(V);
318  }
319 
320  Value* get_schedule_type() {
321  return info[0];
322  }
324  return info[1];
325  }
326  Value* get_lower_bound() {
327  return info[2];
328  }
329  Value* get_upper_bound() {
330  return info[3];
331  }
332  Value* get_stride() {
333  return info[4];
334  }
335  Value* get_increment() {
336  return info[5];
337  }
338  Value* get_chunk() {
339  return info[6];
340  }
341 
342  CallBase *get_caller() {
343  return caller;
344  }
345 
346 
347  private:
349  CallBase *caller;
350  bool valid;
351  };
352 
358  public AnalysisInfoMixin<OmpStaticShecudleAnalysis> {
359  public:
360  // using Result = Expected<OmpScheduleInfo>;
362  Result run(Function &F, FunctionAnalysisManager &AM);
363 
364  private:
365  friend AnalysisInfoMixin<OmpStaticShecudleAnalysis>;
367  static AnalysisKey Key;
368  };
369 
371  public PassInfoMixin<RemoveScheduleRuntimePass> {
372  public:
373  PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
374  };
375 
376 }
377 
378 #endif //CGRAOmpPass_H
CGRAOmp::OmpScheduleInfo::get_upper_bound
Value * get_upper_bound()
Definition: CGRAOmpPass.hpp:329
CGRAOmp::ModelManager
An interface for each LLVM pass to get the target CGRAModel. It can be obtained as an analysis result...
Definition: CGRAOmpPass.hpp:76
llvm
Definition: OptionPlugin.cpp:128
CGRAOmp::OmpKernelInfo::OffloadMetadata_t::func_name
StringRef func_name
Definition: CGRAOmpPass.hpp:156
CGRAOmp::OmpScheduleInfo::OmpScheduleInfo
OmpScheduleInfo(CallBase *caller, Value *schedule_type, Value *last_iter_flag, Value *lower_bound, Value *upper_bound, Value *stride, Value *increment, Value *chunk)
Definition: CGRAOmpPass.hpp:280
CGRAOmp::OmpKernelInfo::OffloadMetadata_t::line
int line
Definition: CGRAOmpPass.hpp:157
CGRAOmp::OmpScheduleInfo::end
Info_t::iterator end()
Definition: CGRAOmpPass.hpp:313
CGRAOmp::OmpKernelInfo::offload_func_list
FunctionList offload_func_list
Definition: CGRAOmpPass.hpp:252
CGRAOmp::OmpKernelInfo::add_kernel
void add_kernel(Function *offload, Function *kernel)
add kernel function as an entry
Definition: CGRAOmpPass.hpp:205
CGRAOmp::OmpKernelInfo::OffloadMetadata_t::order
int order
Definition: CGRAOmpPass.hpp:158
CGRAOmp::OmpKernelInfo::kernel_list
FunctionList kernel_list
Definition: CGRAOmpPass.hpp:251
CGRAOmp::OmpScheduleInfo::get_stride
Value * get_stride()
Definition: CGRAOmpPass.hpp:332
CGRAOmp::OmpKernelInfo::md_list
MetadataList md_list
Definition: CGRAOmpPass.hpp:250
CGRAOmp::OmpScheduleInfo::get_lower_bound
Value * get_lower_bound()
Definition: CGRAOmpPass.hpp:326
CGRAOmp::RemoveScheduleRuntimePass
Definition: CGRAOmpPass.hpp:370
CGRAOmp::OmpKernelInfo::getOffloadFunction
Function * getOffloadFunction(Function *kernel)
Get the Offload Function pointer from the kernel function.
Definition: CGRAOmpPass.hpp:216
CGRAModel.hpp
CGRAOmp::OmpKernelAnalysisPass
A module pass to find functions annotated as OpenMP kernels.
Definition: CGRAOmpPass.hpp:260
CGRAOmp::OmpKernelAnalysisPass::Key
static AnalysisKey Key
Definition: CGRAOmpPass.hpp:268
CGRAOmp::OmpKernelInfo::FunctionList
SmallVector< Function * > FunctionList
Definition: CGRAOmpPass.hpp:169
CGRAOmp::OmpStaticShecudleAnalysis::Key
static AnalysisKey Key
Definition: CGRAOmpPass.hpp:367
CGRAOmp::OmpScheduleInfo::valid
bool valid
Definition: CGRAOmpPass.hpp:350
CGRAOmp::OmpKernelInfo::MetadataList
SmallVector< OffloadMetadata_t > MetadataList
Definition: CGRAOmpPass.hpp:166
CGRAOmp::ModelManagerPass
Module Pass to give the ModuleManager.
Definition: CGRAOmpPass.hpp:107
CGRAOmp::ModelManager::getModel
CGRAModel * getModel() const
Get the CGRAModel object.
Definition: CGRAOmpPass.hpp:93
CGRAOmp::OmpScheduleInfo::get_last_iter_flag
Value * get_last_iter_flag()
Definition: CGRAOmpPass.hpp:323
CGRAOmp::OmpKernelInfo::OffloadMetadata_t::metadata_kind
int metadata_kind
Definition: CGRAOmpPass.hpp:153
CGRAOmp::ModelManager::model
CGRAModel * model
Definition: CGRAOmpPass.hpp:100
CGRAOmp
Definition: AGVerifyPass.hpp:50
CGRAOmp::OmpScheduleInfo::get_caller
CallBase * get_caller()
Definition: CGRAOmpPass.hpp:342
CGRAOmp::OmpScheduleInfo::get_increment
Value * get_increment()
Definition: CGRAOmpPass.hpp:335
CGRAOmp::ModelManagerFunctionProxy
A proxy to access model manager from function passes.
Definition: CGRAOmpPass.hpp:122
CGRAOmp::OmpKernelInfo::kernel_end
func_iterator kernel_end()
Definition: CGRAOmpPass.hpp:191
CGRAOmp::OmpKernelInfo::kernels
iterator_range< func_iterator > kernels()
Definition: CGRAOmpPass.hpp:195
CGRAOmp::ModelManagerPass::Key
static AnalysisKey Key
Definition: CGRAOmpPass.hpp:113
CGRAOmp::OmpStaticShecudleAnalysis
A function pass to analyze OpenMP static scheduling.
Definition: CGRAOmpPass.hpp:357
CGRAOmp::OmpKernelInfo::md_iterator
MetadataList::iterator md_iterator
Definition: CGRAOmpPass.hpp:167
CGRAOmp::ModelManagerLoopProxy::Key
static AnalysisKey Key
Definition: CGRAOmpPass.hpp:143
CGRAOmp::ModelManagerPass::model
CGRAModel * model
Definition: CGRAOmpPass.hpp:114
CGRAOmp::OmpKernelInfo::func_iterator
FunctionList::iterator func_iterator
Definition: CGRAOmpPass.hpp:170
CGRAOmp::CGRAModel
A base class of CGRA model for DFG extraction.
Definition: CGRAModel.hpp:71
CGRAOmp::ModelManagerLoopProxy
A proxy to access model manager from loop passes.
Definition: CGRAOmpPass.hpp:136
CGRAOmp::OmpKernelInfo::OffloadMetadata_t::file_ID
int file_ID
Definition: CGRAOmpPass.hpp:155
CGRAOmp::OmpScheduleInfo::get_schedule_type
Value * get_schedule_type()
Definition: CGRAOmpPass.hpp:320
CGRAOmp::OmpScheduleInfo::begin
Info_t::iterator begin()
Definition: CGRAOmpPass.hpp:310
CGRAOmp::OmpScheduleInfo::contains
bool contains(Value *V)
Definition: CGRAOmpPass.hpp:316
OMP_STATIC_INIT_OPERAND_N
#define OMP_STATIC_INIT_OPERAND_N
Definition: CGRAOmpPass.hpp:56
CGRAOmp::OmpKernelInfo::OffloadMetadata_t::file_dev_ID
int file_dev_ID
Definition: CGRAOmpPass.hpp:154
CGRAOmp::OmpKernelInfo::kernel_begin
func_iterator kernel_begin()
Definition: CGRAOmpPass.hpp:187
CGRAOmp::OmpKernelInfo
A set of information regarding OpenMP target kernel.
Definition: CGRAOmpPass.hpp:150
CGRAOmp::OmpKernelInfo::md_end
md_iterator md_end()
Definition: CGRAOmpPass.hpp:178
CGRAOmp::OmpScheduleInfo
Bundled values related to OpenMP statinc scheduling.
Definition: CGRAOmpPass.hpp:277
CGRAOmp::OmpScheduleInfo::get_chunk
Value * get_chunk()
Definition: CGRAOmpPass.hpp:338
CGRAOmp::OmpScheduleInfo::Info_t
SetVector< Value * > Info_t
Definition: CGRAOmpPass.hpp:278
CGRAOmp::OmpScheduleInfo::caller
CallBase * caller
Definition: CGRAOmpPass.hpp:349
CGRAOmp::OmpKernelInfo::md_begin
md_iterator md_begin()
Definition: CGRAOmpPass.hpp:174
CGRAOmp::OmpKernelInfo::metadata
iterator_range< md_iterator > metadata()
Definition: CGRAOmpPass.hpp:182
CGRAOmp::ModelManagerFunctionProxy::Key
static AnalysisKey Key
Definition: CGRAOmpPass.hpp:128
CGRAOmp::OmpKernelInfo::OffloadMetadata_t
Definition: CGRAOmpPass.hpp:152
CGRAOmp::OmpScheduleInfo::OmpScheduleInfo
OmpScheduleInfo()
Definition: CGRAOmpPass.hpp:298
CGRAOmp::ModelManager::ModelManager
ModelManager(CGRAModel *CM)
Construct a new ModelManager object.
Definition: CGRAOmpPass.hpp:84
CGRAOmp::OmpScheduleInfo::info
Info_t info
Definition: CGRAOmpPass.hpp:348