CGRAOmp  0.1
CGRAModel.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/CGRAModel.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 15:00:30
28 * Last Modified: 19-02-2022 06:45:05
29 */
30 #ifndef CGRAModel_H
31 #define CGRAModel_H
32 
33 #include "llvm/ADT/SmallVector.h"
34 #include "llvm/ADT/StringMap.h"
35 #include "llvm/ADT/StringRef.h"
36 #include "llvm/ADT/StringSet.h"
37 #include "llvm/ADT/SmallSet.h"
38 #include "llvm/Support/Error.h"
39 #include "llvm/Support/FormatVariadic.h"
40 #include "llvm/Support/JSON.h"
41 
42 #include "CGRAInstMap.hpp"
43 
44 #include <string>
45 #include <climits>
46 
47 // Key setting to parse the JSON object
48 #define CATEGORY_KEY "category"
49 #define AG_CONF_KEY "address_generator"
50 #define AG_STYLE_KEY "control"
51 #define AG_MAX_NEST_KEY "max_nested_level"
52 #define COND_STYLE_KEY "conditional"
53 #define IDP_STYLE_KEY "inter-loop-dependency"
54 #define OPT_ENABLE_KEY "allowed"
55 #define OPT_TYPE_KEY "type"
56 #define CUSTOM_INST_KEY "custom_instructions"
57 #define GEN_INST_KEY "generic_instructions"
58 #define INST_MAP_KEY "instruction_map"
59 
60 
61 
62 using namespace llvm;
63 
64 namespace CGRAOmp
65 {
66 
71  class CGRAModel {
72  public:
77  enum class CGRACategory {
79  TimeMultiplexed,
81  Decoupled,
82  };
83 
88  enum class ConditionalStyle {
90  No,
92  MuxInst,
94  TriState
95  };
96 
101  enum class InterLoopDep {
103  No,
105  Generic,
107  BackwardInst,
108  };
110  static StringMap<CGRACategory> CategoryMap;
112  static StringMap<ConditionalStyle> CondStyleMap;
114  static StringMap<InterLoopDep> InterLoopDepMap;
115 
116  // constructors
125  explicit CGRAModel(StringRef filename, CGRACategory category,
126  ConditionalStyle cond,
127  InterLoopDep inter_loop_dep) :
128  filename(filename), category(category),
129  cond(cond), inter_loop_dep(inter_loop_dep) {};
130  CGRAModel() = delete;
132  CGRAModel(const CGRAModel &) = default;
134  CGRAModel(CGRAModel &&) = default;
135 
143  Error addSupportedInst(StringRef opcode);
144 
145 
153  void addCustomInst(StringRef opcode, ModuleAnalysisManager &MAM);
154 
162  Error add_map_entry(StringRef opcode, MapCondition *map_cond);
163 
171  InstMapEntry* isSupported(Instruction *I);
172 
179  template <typename DerivedT>
180  DerivedT* asDerived() {
181  return dyn_cast<DerivedT>(this);
182  }
183 
190  return category;
191  }
192 
199  return inter_loop_dep;
200  }
201 
202  protected:
203  StringRef filename;
208 
209  };
210 
216  public:
217  enum class Kind {
218  Affine,
219  FullState,
220  };
221 
222  AddressGenerator() = delete;
223  explicit AddressGenerator(Kind kind) : kind(kind) {};
224  AddressGenerator(const AddressGenerator &AG) = default;
225  AddressGenerator(AddressGenerator &&) = default;
226 
227 
228  Kind getKind() const {
229  return kind;
230  }
231 
232  template <typename DerivedT>
233  DerivedT* asDerived() {
234  return dyn_cast<DerivedT>(this);
235  }
236 
237  private:
239  };
240 
245  class DecoupledCGRA : public CGRAModel {
246  public:
254  DecoupledCGRA(StringRef filename, AddressGenerator *AG,
255  ConditionalStyle cond, InterLoopDep inter_loop_dep) :
256  CGRAModel(filename, CGRACategory::Decoupled,
257  cond, inter_loop_dep), AG(AG) {};
258 
261 
264  delete AG;
265  }
266 
273  return AG;
274  }
275 
277  static bool classof(const CGRAModel *M) {
278  return M->getKind() == CGRACategory::Decoupled;
279  }
280  private:
282 
283  };
284 
294  class AffineAG : public AddressGenerator {
296  public:
300  AffineAG() : AG(AG::Kind::Affine) {
301  max_nests = INT_MAX;
302  }
308  explicit AffineAG(int max_nests) : AG(AG::Kind::Affine),
309  max_nests(max_nests) {};
310 
312  AffineAG(const AffineAG&);
313 
317  int getMaxNests() const {
318  return max_nests;
319  }
320 
322  static bool classof(const AG *ag) {
323  return ag->getKind() == Kind::Affine;
324  }
325 
326  private:
328  };
329 
334  class FullStateAG : public AddressGenerator {
336  public:
338  FullStateAG(const FullStateAG&);
340  static bool classof(const AG *ag) {
341  return ag->getKind() == Kind::FullState;
342  }
343  };
344 
349  class TMCGRA : public CGRAModel {
350  public:
356  explicit TMCGRA(StringRef filename) :
357  CGRAModel(filename, CGRACategory::TimeMultiplexed,
358  ConditionalStyle::No,
359  InterLoopDep::No) {}
360  TMCGRA(const TMCGRA &);
361 
363  static bool classof(const CGRAModel *M) {
364  return M->getKind() == CGRAModel::CGRACategory::TimeMultiplexed;
365  }
366 
367  private:
368  };
369 
375  class ModelError : public ErrorInfo<ModelError> {
376  public:
383  ModelError(StringRef filename, StringRef missed_key) :
384  filename(filename),
385  errtype(ErrorType::MissingKey),
386  error_key(missed_key) {}
387 
396  ModelError(StringRef filename, StringRef key,
397  StringRef exptected_type,
398  json::Value *v = nullptr) :
399  filename(filename),
400  errtype(ErrorType::InvalidDataType),
401  error_key(key),
402  exptected_type(exptected_type) {
403  if (v != nullptr) {
404  raw_string_ostream OS(json_val);
405  OS << *v;
406  }
407  }
408 
415  ModelError(StringRef filename, std::pair<StringRef,StringRef> config) :
416  filename(filename), errtype(ErrorType::NoImplemented),
417  error_key(config.first), error_val(config.second.str()) {};
418 
427  ModelError(StringRef filename, StringRef key,
428  StringRef val, ArrayRef<StringRef> list);
429 
435  void setRegion(StringRef region_str) {
436  region = region_str;
437  }
438 
439  static char ID;
440  std::error_code convertToErrorCode() const override {
441  // Unknown error
442  return std::error_code(41, std::generic_category());
443  }
444  void log(raw_ostream &OS) const override;
445 
446 
453  bool isMissingKey() {
454  return errtype == ErrorType::MissingKey;
455  }
456 
457  private:
462  enum class ErrorType {
464  MissingKey,
466  InvalidDataType,
468  InvalidValue,
470  NoImplemented,
471  };
472 
474  std::string json_val = "";
475  StringRef filename;
476  StringRef error_key;
477  std::string error_val;
478  StringRef exptected_type;
479  StringRef region = "";
480  SmallVector<StringRef> valid_values;
481 
482  };
483 
484 
485  /* ---------- Utility functions ---------- */
493  Expected<CGRAModel*> parseCGRASetting(StringRef filepath,
494  ModuleAnalysisManager &MAM);
495 
503  template <typename SettingT>
504  SmallVector<StringRef> get_setting_values(StringMap<SettingT> settingMap);
505 
515  template <typename SettingT>
516  bool containsValidData(StringMap<SettingT> settingMap, StringRef val);
517 
525  Expected<CGRAModel::CGRACategory> getCategory(json::Object *json_obj,
526  StringRef filename);
527 
538  template <char const *key_str, typename SettingT>
539  Expected<SettingT> getOption(json::Object *json_obj,
540  StringRef filename,
541  StringMap<SettingT> settingMap);
542 
550  Expected<AddressGenerator*> parseAGConfig(json::Object *json_obj,
551  StringRef filename);
552 
560  Expected<AddressGenerator*> createAffineAG(json::Object *json_obj,
561  StringRef filename);
562 
563  using AGGen_t = std::function<Expected<AddressGenerator*>(json::Object*,StringRef)>;
564 
565 } // namespace CGRAOmp
566 
567 
568 #endif //CGRAModel_H
CGRAOmp::CGRAModel::ConditionalStyle
ConditionalStyle
Ability to handle conditional execution (i.e., if-else part)
Definition: CGRAModel.hpp:88
llvm
Definition: OptionPlugin.cpp:128
CGRAOmp::DecoupledCGRA
CGRA model for category "decoupled".
Definition: CGRAModel.hpp:245
CGRAOmp::ModelError::ModelError
ModelError(StringRef filename, StringRef key, StringRef exptected_type, json::Value *v=nullptr)
Construct a new ModelError when unexpected value type is specified.
Definition: CGRAModel.hpp:396
CGRAOmp::AffineAG::max_nests
int max_nests
Definition: CGRAModel.hpp:327
CGRAOmp::AffineAG::getMaxNests
int getMaxNests() const
Get the maximum nested level supported by this AddressGenerator.
Definition: CGRAModel.hpp:317
CGRAOmp::AddressGenerator::AddressGenerator
AddressGenerator(Kind kind)
Definition: CGRAModel.hpp:223
CGRAOmp::ModelError::valid_values
SmallVector< StringRef > valid_values
Definition: CGRAModel.hpp:480
CGRAOmp::TMCGRA::classof
static bool classof(const CGRAModel *M)
for downcasting from CGRAModel by llvm::dyn_cast
Definition: CGRAModel.hpp:363
CGRAOmp::AddressGenerator::getKind
Kind getKind() const
Definition: CGRAModel.hpp:228
CGRAOmp::getOption
Expected< SettingT > getOption(json::Object *json_obj, StringRef filename, StringMap< SettingT > settingMap)
Get common option from JSON config.
Definition: CGRAModel.cpp:164
CGRAOmp::AffineAG::AffineAG
AffineAG()
Default constructor without any limitation about the nested level.
Definition: CGRAModel.hpp:300
CGRAOmp::ModelError::ErrorType
ErrorType
Type of error cause.
Definition: CGRAModel.hpp:462
CGRAOmp::AddressGenerator::asDerived
DerivedT * asDerived()
Definition: CGRAModel.hpp:233
CGRAOmp::CGRAModel::asDerived
DerivedT * asDerived()
an interface to down cast to derived classes
Definition: CGRAModel.hpp:180
CGRAOmp::FullStateAG
a model of address generator without any constraints
Definition: CGRAModel.hpp:334
CGRAOmp::DecoupledCGRA::AG
AddressGenerator * AG
Definition: CGRAModel.hpp:281
CGRAOmp::CGRAModel::CategoryMap
static StringMap< CGRACategory > CategoryMap
Map category string to CGRACategory.
Definition: CGRAModel.hpp:110
CGRAOmp::ModelError::isMissingKey
bool isMissingKey()
check if the error cause if due to missing key
Definition: CGRAModel.hpp:453
CGRAOmp::CGRAModel::category
CGRACategory category
Definition: CGRAModel.hpp:206
CGRAOmp::TMCGRA
CGRA model for category "time-multiplexed".
Definition: CGRAModel.hpp:349
CGRAOmp::AffineAG
A model of address generator compatible to memory access with affine expression the accessed memory a...
Definition: CGRAModel.hpp:294
CGRAOmp::ModelError::error_val
std::string error_val
Definition: CGRAModel.hpp:477
CGRAOmp
Definition: AGVerifyPass.hpp:50
CGRAOmp::CGRAModel::inter_loop_dep
InterLoopDep inter_loop_dep
Definition: CGRAModel.hpp:205
CGRAOmp::ModelError::errtype
ErrorType errtype
Definition: CGRAModel.hpp:473
CGRAOmp::MapCondition
a representation of instruction mapping condition
Definition: CGRAInstMap.hpp:116
CGRAOmp::DecoupledCGRA::DecoupledCGRA
DecoupledCGRA(StringRef filename, AddressGenerator *AG, ConditionalStyle cond, InterLoopDep inter_loop_dep)
Construct a new DecoupledCGRA.
Definition: CGRAModel.hpp:254
CGRAOmp::CGRAModel::inst_map
InstMap inst_map
Definition: CGRAModel.hpp:207
CGRAOmp::CGRAModel::CGRACategory
CGRACategory
a category of the CGRA model
Definition: CGRAModel.hpp:77
CGRAOmp::parseCGRASetting
Expected< CGRAModel * > parseCGRASetting(StringRef filepath, ModuleAnalysisManager &MAM)
a helper function to instantiate CGRAModel based on JSON configfile
Definition: CGRAModel.cpp:299
CGRAOmp::CGRAModel::getInterLoopDepType
InterLoopDep getInterLoopDepType() const
Get the Inter Loop Dependency avility.
Definition: CGRAModel.hpp:198
CGRAOmp::AddressGenerator
An abstract class representing a type of address generator.
Definition: CGRAModel.hpp:215
CGRAOmp::CGRAModel::getKind
CGRACategory getKind() const
Get the category of the CGRA.
Definition: CGRAModel.hpp:189
CGRAOmp::CGRAModel::InterLoopDepMap
static StringMap< InterLoopDep > InterLoopDepMap
Map category string to InterLoopDep.
Definition: CGRAModel.hpp:114
CGRAOmp::CGRAModel
A base class of CGRA model for DFG extraction.
Definition: CGRAModel.hpp:71
CGRAOmp::CGRAModel::CGRAModel
CGRAModel(StringRef filename, CGRACategory category, ConditionalStyle cond, InterLoopDep inter_loop_dep)
Construct a new CGRAModel object.
Definition: CGRAModel.hpp:125
CGRAOmp::CGRAModel::cond
ConditionalStyle cond
Definition: CGRAModel.hpp:204
CGRAOmp::AffineAG::AffineAG
AffineAG(int max_nests)
Construct a new AffineAG object.
Definition: CGRAModel.hpp:308
CGRAOmp::DecoupledCGRA::getAG
AddressGenerator * getAG() const
get the address generator
Definition: CGRAModel.hpp:272
CGRAOmp::CGRAModel::CondStyleMap
static StringMap< ConditionalStyle > CondStyleMap
Map category string to ConditionalStyle.
Definition: CGRAModel.hpp:112
CGRAOmp::getCategory
Expected< CGRAModel::CGRACategory > getCategory(json::Object *json_obj, StringRef filename)
Get CGRACategory from JSON config.
Definition: CGRAModel.cpp:116
CGRAOmp::ModelError::setRegion
void setRegion(StringRef region_str)
set a region where parse error ocurrs
Definition: CGRAModel.hpp:435
CGRAOmp::ModelError::ModelError
ModelError(StringRef filename, std::pair< StringRef, StringRef > config)
Construct a new ModelError when not implemented configuration is specified.
Definition: CGRAModel.hpp:415
CGRAOmp::ModelError
A custom error used to notice the specified configuration file is invalid.
Definition: CGRAModel.hpp:375
CGRAOmp::ModelError::ModelError
ModelError(StringRef filename, StringRef missed_key)
Construct a new ModelError when missing a required item.
Definition: CGRAModel.hpp:383
CGRAOmp::containsValidData
bool containsValidData(StringMap< SettingT > settingMap, StringRef val)
Check if val is a valid setting for SettingT.
Definition: CGRAModel.cpp:100
CGRAOmp::get_setting_values
SmallVector< StringRef > get_setting_values(StringMap< SettingT > settingMap)
a template to extract only values from SettingMap
Definition: CGRAModel.cpp:107
CGRAOmp::TMCGRA::TMCGRA
TMCGRA(StringRef filename)
Construct a new time-multiplexed CGRA object.
Definition: CGRAModel.hpp:356
CGRAOmp::ModelError::exptected_type
StringRef exptected_type
Definition: CGRAModel.hpp:478
CGRAOmp::AffineAG::classof
static bool classof(const AG *ag)
for downcasting from AddressGenerator by llvm::dyn_cast
Definition: CGRAModel.hpp:322
CGRAOmp::InstMap
collective of all the instruction mapping
Definition: CGRAInstMap.hpp:592
CGRAOmp::ModelError::ID
static char ID
Definition: CGRAModel.hpp:439
CGRAOmp::AddressGenerator::kind
Kind kind
Definition: CGRAModel.hpp:238
CGRAInstMap.hpp
CGRAOmp::ModelError::convertToErrorCode
std::error_code convertToErrorCode() const override
Definition: CGRAModel.hpp:440
CGRAOmp::AddressGenerator::Kind
Kind
Definition: CGRAModel.hpp:217
CGRAOmp::DecoupledCGRA::classof
static bool classof(const CGRAModel *M)
for downcasting from CGRAModel by llvm::dyn_cast
Definition: CGRAModel.hpp:277
CGRAOmp::ModelError::filename
StringRef filename
Definition: CGRAModel.hpp:475
CGRAOmp::ModelError::error_key
StringRef error_key
Definition: CGRAModel.hpp:476
CGRAOmp::CGRAModel::filename
StringRef filename
Definition: CGRAModel.hpp:203
CGRAOmp::FullStateAG::classof
static bool classof(const AG *ag)
for downcasting from AddressGenerator by llvm::dyn_cast
Definition: CGRAModel.hpp:340
CGRAOmp::parseAGConfig
Expected< AddressGenerator * > parseAGConfig(json::Object *json_obj, StringRef filename)
parse a JSON config and instantiate AddressGenerator
Definition: CGRAModel.cpp:224
CGRAOmp::DecoupledCGRA::~DecoupledCGRA
~DecoupledCGRA()
destructor
Definition: CGRAModel.hpp:263
CGRAOmp::createAffineAG
Expected< AddressGenerator * > createAffineAG(json::Object *json_obj, StringRef filename)
Create a AffineAG object according to JSON config.
Definition: CGRAModel.cpp:267
CGRAOmp::InstMapEntry
An abstract class for an entry to replace IR instruction to targeting CGRA instruction.
Definition: CGRAInstMap.hpp:258
CGRAOmp::CGRAModel::InterLoopDep
InterLoopDep
How to describe the inter-loop dependecy.
Definition: CGRAModel.hpp:101
CGRAOmp::AGGen_t
std::function< Expected< AddressGenerator * >(json::Object *, StringRef)> AGGen_t
Definition: CGRAModel.hpp:563