CIRCT  19.0.0git
Common.h
Go to the documentation of this file.
1 //===- Common.h - Commonly used classes w/o dependencies --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // DO NOT EDIT!
10 // This file is distributed as part of an ESI package. The source for this file
11 // should always be modified within CIRCT.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 // NOLINTNEXTLINE(llvm-header-guard)
16 #ifndef ESI_COMMON_H
17 #define ESI_COMMON_H
18 
19 #include <any>
20 #include <cstdint>
21 #include <map>
22 #include <optional>
23 #include <stdexcept>
24 #include <string>
25 #include <vector>
26 
27 namespace esi {
28 
29 //===----------------------------------------------------------------------===//
30 // Common accelerator description types.
31 //===----------------------------------------------------------------------===//
32 
33 struct AppID {
34  std::string name;
35  std::optional<uint32_t> idx;
36 
37  AppID(const AppID &) = default;
38  AppID(const std::string &name, std::optional<uint32_t> idx = std::nullopt)
39  : name(name), idx(idx) {}
40 
41  bool operator==(const AppID &other) const {
42  return name == other.name && idx == other.idx;
43  }
44  bool operator!=(const AppID &other) const { return !(*this == other); }
45 };
46 bool operator<(const AppID &a, const AppID &b);
47 
48 class AppIDPath : public std::vector<AppID> {
49 public:
50  using std::vector<AppID>::vector;
51 
52  AppIDPath operator+(const AppIDPath &b);
53  std::string toStr() const;
54 };
55 bool operator<(const AppIDPath &a, const AppIDPath &b);
56 
57 struct ModuleInfo {
58  const std::optional<std::string> name;
59  const std::optional<std::string> summary;
60  const std::optional<std::string> version;
61  const std::optional<std::string> repo;
62  const std::optional<std::string> commitHash;
63  const std::map<std::string, std::any> extra;
64 };
65 
66 /// A description of a service port. Used pretty exclusively in setting up the
67 /// design.
69  std::string name;
70  std::string portName;
71 };
72 
73 /// A description of a hardware client. Used pretty exclusively in setting up
74 /// the design.
78  std::map<std::string, std::any> implOptions;
79 };
80 using HWClientDetails = std::vector<HWClientDetail>;
81 using ServiceImplDetails = std::map<std::string, std::any>;
82 
83 /// A logical chunk of data representing serialized data. Currently, just a
84 /// wrapper for a vector of bytes, which is not efficient in terms of memory
85 /// copying. This will change in the future as will the API.
86 class MessageData {
87 public:
88  /// Adopts the data vector buffer.
89  MessageData() = default;
90  MessageData(std::vector<uint8_t> &data) : data(std::move(data)) {}
91  MessageData(const uint8_t *data, size_t size) : data(data, data + size) {}
92  ~MessageData() = default;
93 
94  const uint8_t *getBytes() const { return data.data(); }
95  /// Get the size of the data in bytes.
96  size_t getSize() const { return data.size(); }
97 
98  /// Cast to a type. Throws if the size of the data does not match the size of
99  /// the message. The lifetime of the resulting pointer is tied to the lifetime
100  /// of this object.
101  template <typename T>
102  const T *as() const {
103  if (data.size() != sizeof(T))
104  throw std::runtime_error("Data size does not match type size. Size is " +
105  std::to_string(data.size()) + ", expected " +
106  std::to_string(sizeof(T)) + ".");
107  return reinterpret_cast<const T *>(data.data());
108  }
109 
110  /// Cast from a type to its raw bytes.
111  template <typename T>
112  static MessageData from(T &t) {
113  return MessageData(reinterpret_cast<const uint8_t *>(&t), sizeof(T));
114  }
115 
116 private:
117  std::vector<uint8_t> data;
118 };
119 
120 } // namespace esi
121 
122 std::ostream &operator<<(std::ostream &, const esi::ModuleInfo &);
123 std::ostream &operator<<(std::ostream &, const esi::AppID &);
124 
125 //===----------------------------------------------------------------------===//
126 // Functions which should be in the standard library.
127 //===----------------------------------------------------------------------===//
128 
129 namespace esi {
130 std::string toHex(uint32_t val);
131 } // namespace esi
132 
133 #endif // ESI_COMMON_H
std::ostream & operator<<(std::ostream &, const esi::ModuleInfo &)
Definition: Manifest.cpp:551
std::string toStr() const
Definition: Manifest.cpp:604
AppIDPath operator+(const AppIDPath &b)
Definition: Manifest.cpp:598
A logical chunk of data representing serialized data.
Definition: Common.h:86
~MessageData()=default
size_t getSize() const
Get the size of the data in bytes.
Definition: Common.h:96
MessageData()=default
Adopts the data vector buffer.
MessageData(const uint8_t *data, size_t size)
Definition: Common.h:91
const T * as() const
Cast to a type.
Definition: Common.h:102
MessageData(std::vector< uint8_t > &data)
Definition: Common.h:90
static MessageData from(T &t)
Cast from a type to its raw bytes.
Definition: Common.h:112
std::vector< uint8_t > data
Definition: Common.h:117
const uint8_t * getBytes() const
Definition: Common.h:94
Definition: esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition: Common.h:81
std::string toHex(uint32_t val)
Definition: Common.cpp:20
bool operator<(const AppID &a, const AppID &b)
Definition: Manifest.cpp:610
std::vector< HWClientDetail > HWClientDetails
Definition: Common.h:80
std::string name
Definition: Common.h:34
bool operator!=(const AppID &other) const
Definition: Common.h:44
bool operator==(const AppID &other) const
Definition: Common.h:41
AppID(const AppID &)=default
std::optional< uint32_t > idx
Definition: Common.h:35
AppID(const std::string &name, std::optional< uint32_t > idx=std::nullopt)
Definition: Common.h:38
A description of a hardware client.
Definition: Common.h:75
std::map< std::string, std::any > implOptions
Definition: Common.h:78
AppIDPath relPath
Definition: Common.h:76
ServicePortDesc port
Definition: Common.h:77
const std::optional< std::string > commitHash
Definition: Common.h:62
const std::optional< std::string > repo
Definition: Common.h:61
const std::optional< std::string > version
Definition: Common.h:60
const std::optional< std::string > summary
Definition: Common.h:59
const std::map< std::string, std::any > extra
Definition: Common.h:63
const std::optional< std::string > name
Definition: Common.h:58
A description of a service port.
Definition: Common.h:68
std::string name
Definition: Common.h:69
std::string portName
Definition: Common.h:70