CIRCT 22.0.0git
Loading...
Searching...
No Matches
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 <span>
24#include <stdexcept>
25#include <string>
26#include <vector>
27
28namespace esi {
29class Type;
30
31//===----------------------------------------------------------------------===//
32// Common accelerator description types.
33//===----------------------------------------------------------------------===//
34
35struct AppID {
36 std::string name;
37 std::optional<uint32_t> idx;
38
39 AppID(const std::string &name, std::optional<uint32_t> idx = std::nullopt)
40 : name(name), idx(idx) {}
41
42 bool operator==(const AppID &other) const {
43 return name == other.name && idx == other.idx;
44 }
45 bool operator!=(const AppID &other) const { return !(*this == other); }
46};
47bool operator<(const AppID &a, const AppID &b);
48
49class AppIDPath : public std::vector<AppID> {
50public:
51 using std::vector<AppID>::vector;
52
53 AppIDPath operator+(const AppIDPath &b) const;
54 AppIDPath parent() const;
55 std::string toStr() const;
56};
57bool operator<(const AppIDPath &a, const AppIDPath &b);
58
59struct Constant {
60 std::any value;
61 std::optional<const Type *> type;
62};
63
64struct ModuleInfo {
65 std::optional<std::string> name;
66 std::optional<std::string> summary;
67 std::optional<std::string> version;
68 std::optional<std::string> repo;
69 std::optional<std::string> commitHash;
70 std::map<std::string, Constant> constants;
71 std::map<std::string, std::any> extra;
72};
73
74/// A description of a service port. Used pretty exclusively in setting up the
75/// design.
77 std::string name;
78 std::string portName;
79};
80
81/// Details about how to connect to a particular channel.
83 /// The name of the type of connection. Typically, the name of the DMA engine
84 /// or "cosim" if a cosimulation channel is being used.
85 std::string type;
86 /// Implementation-specific options.
87 std::map<std::string, std::any> implOptions;
88};
89using ChannelAssignments = std::map<std::string, ChannelAssignment>;
90
91/// A description of a hardware client. Used pretty exclusively in setting up
92/// the design.
99using HWClientDetails = std::vector<HWClientDetail>;
100using ServiceImplDetails = std::map<std::string, std::any>;
101
102/// A logical chunk of data representing serialized data. Currently, just a
103/// wrapper for a vector of bytes, which is not efficient in terms of memory
104/// copying. This will change in the future as will the API.
106public:
107 /// Adopts the data vector buffer.
108 MessageData() = default;
109 MessageData(std::vector<uint8_t> &data) : data(std::move(data)) {}
110 MessageData(std::vector<uint8_t> &&data) : data(std::move(data)) {}
111 MessageData(const uint8_t *data, size_t size) : data(data, data + size) {}
112 ~MessageData() = default;
113
114 const uint8_t *getBytes() const { return data.data(); }
115
116 /// Get the data as a vector of bytes.
117 const std::vector<uint8_t> &getData() const { return data; }
118
119 /// Implicit conversion to a vector/span of bytes, to play nice with other
120 /// APIs that accept bytearray-like things.
121 operator const std::vector<uint8_t> &() const { return data; }
122 operator std::span<const uint8_t>() const { return data; }
123
124 /// Move the data out of this object.
125 std::vector<uint8_t> takeData() { return std::move(data); }
126
127 /// Get the size of the data in bytes.
128 size_t getSize() const { return data.size(); }
129
130 /// Returns true if this message contains no data.
131 bool empty() const { return data.empty(); }
132
133 /// Cast to a type. Throws if the size of the data does not match the size of
134 /// the message. The lifetime of the resulting pointer is tied to the lifetime
135 /// of this object.
136 template <typename T>
137 const T *as() const {
138 if (data.size() != sizeof(T))
139 throw std::runtime_error("Data size does not match type size. Size is " +
140 std::to_string(data.size()) + ", expected " +
141 std::to_string(sizeof(T)) + ".");
142 return reinterpret_cast<const T *>(data.data());
143 }
144
145 /// Cast from a type to its raw bytes.
146 template <typename T>
147 static MessageData from(T &t) {
148 return MessageData(reinterpret_cast<const uint8_t *>(&t), sizeof(T));
149 }
150
151 /// Convert the data to a hex string.
152 std::string toHex() const;
153
154private:
155 std::vector<uint8_t> data;
156};
157
158} // namespace esi
159
160std::ostream &operator<<(std::ostream &, const esi::ModuleInfo &);
161std::ostream &operator<<(std::ostream &, const esi::AppID &);
162
163//===----------------------------------------------------------------------===//
164// Functions which should be in the standard library.
165//===----------------------------------------------------------------------===//
166
167namespace esi {
168std::string toHex(void *val);
169std::string toHex(uint64_t val);
170} // namespace esi
171
172#endif // ESI_COMMON_H
std::ostream & operator<<(std::ostream &, const esi::ModuleInfo &)
std::string toStr() const
Definition Manifest.cpp:740
AppIDPath operator+(const AppIDPath &b) const
Definition Manifest.cpp:727
AppIDPath parent() const
Definition Manifest.cpp:733
A logical chunk of data representing serialized data.
Definition Common.h:105
const std::vector< uint8_t > & getData() const
Get the data as a vector of bytes.
Definition Common.h:117
const uint8_t * getBytes() const
Definition Common.h:114
std::string toHex() const
Convert the data to a hex string.
Definition Common.cpp:22
~MessageData()=default
const T * as() const
Cast to a type.
Definition Common.h:137
std::vector< uint8_t > takeData()
Move the data out of this object.
Definition Common.h:125
size_t getSize() const
Get the size of the data in bytes.
Definition Common.h:128
MessageData()=default
Adopts the data vector buffer.
MessageData(const uint8_t *data, size_t size)
Definition Common.h:111
MessageData(std::vector< uint8_t > &data)
Definition Common.h:109
bool empty() const
Returns true if this message contains no data.
Definition Common.h:131
static MessageData from(T &t)
Cast from a type to its raw bytes.
Definition Common.h:147
std::vector< uint8_t > data
Definition Common.h:155
MessageData(std::vector< uint8_t > &&data)
Definition Common.h:110
Definition esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition Common.h:100
std::string toHex(void *val)
Definition Common.cpp:37
std::map< std::string, ChannelAssignment > ChannelAssignments
Definition Common.h:89
bool operator<(const AppID &a, const AppID &b)
Definition Manifest.cpp:746
std::vector< HWClientDetail > HWClientDetails
Definition Common.h:99
std::string name
Definition Common.h:36
bool operator!=(const AppID &other) const
Definition Common.h:45
bool operator==(const AppID &other) const
Definition Common.h:42
std::optional< uint32_t > idx
Definition Common.h:37
AppID(const std::string &name, std::optional< uint32_t > idx=std::nullopt)
Definition Common.h:39
Details about how to connect to a particular channel.
Definition Common.h:82
std::map< std::string, std::any > implOptions
Implementation-specific options.
Definition Common.h:87
std::string type
The name of the type of connection.
Definition Common.h:85
std::any value
Definition Common.h:60
std::optional< const Type * > type
Definition Common.h:61
A description of a hardware client.
Definition Common.h:93
std::map< std::string, std::any > implOptions
Definition Common.h:97
AppIDPath relPath
Definition Common.h:94
ChannelAssignments channelAssignments
Definition Common.h:96
ServicePortDesc port
Definition Common.h:95
std::optional< std::string > version
Definition Common.h:67
std::optional< std::string > name
Definition Common.h:65
std::map< std::string, Constant > constants
Definition Common.h:70
std::optional< std::string > commitHash
Definition Common.h:69
std::optional< std::string > repo
Definition Common.h:68
std::map< std::string, std::any > extra
Definition Common.h:71
std::optional< std::string > summary
Definition Common.h:66
A description of a service port.
Definition Common.h:76
std::string name
Definition Common.h:77
std::string portName
Definition Common.h:78