CIRCT 23.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 friend std::ostream &operator<<(std::ostream &os, const AppID &id);
47
48 std::string toString() const {
49 if (idx.has_value())
50 return name + "[" + std::to_string(idx.value()) + "]";
51 return name;
52 }
53};
54bool operator<(const AppID &a, const AppID &b);
55
56class AppIDPath : public std::vector<AppID> {
57public:
58 using std::vector<AppID>::vector;
59
60 AppIDPath operator+(const AppIDPath &b) const;
61 AppIDPath parent() const;
62 std::string toStr() const;
63 friend std::ostream &operator<<(std::ostream &os, const AppIDPath &path);
64};
65bool operator<(const AppIDPath &a, const AppIDPath &b);
66
67struct Constant {
68 std::any value;
69 std::optional<const Type *> type;
70};
71
72struct ModuleInfo {
73 std::optional<std::string> name;
74 std::optional<std::string> summary;
75 std::optional<std::string> version;
76 std::optional<std::string> repo;
77 std::optional<std::string> commitHash;
78 std::map<std::string, Constant> constants;
79 std::map<std::string, std::any> extra;
80};
81
82/// A description of a service port. Used pretty exclusively in setting up the
83/// design.
85 std::string name;
86 std::string portName;
87};
88
89/// Details about how to connect to a particular channel.
91 /// The name of the type of connection. Typically, the name of the DMA engine
92 /// or "cosim" if a cosimulation channel is being used.
93 std::string type;
94 /// Implementation-specific options.
95 std::map<std::string, std::any> implOptions;
96};
97using ChannelAssignments = std::map<std::string, ChannelAssignment>;
98
99/// A description of a hardware client. Used pretty exclusively in setting up
100/// the design.
107using HWClientDetails = std::vector<HWClientDetail>;
108using ServiceImplDetails = std::map<std::string, std::any>;
109
110/// A logical chunk of data representing serialized data. Currently, just a
111/// wrapper for a vector of bytes, which is not efficient in terms of memory
112/// copying. This will change in the future as will the API.
114public:
115 /// Adopts the data vector buffer.
116 MessageData() = default;
117 MessageData(std::span<const uint8_t> data)
118 : data(data.data(), data.data() + data.size()) {}
119 MessageData(std::vector<uint8_t> &data) : data(std::move(data)) {}
120 MessageData(std::vector<uint8_t> &&data) : data(std::move(data)) {}
121 MessageData(const uint8_t *data, size_t size) : data(data, data + size) {}
122 ~MessageData() = default;
123
124 const uint8_t *getBytes() const { return data.data(); }
125
126 /// Get the data as a vector of bytes.
127 const std::vector<uint8_t> &getData() const { return data; }
128
129 /// Implicit conversion to a vector/span of bytes, to play nice with other
130 /// APIs that accept bytearray-like things.
131 operator const std::vector<uint8_t> &() const { return data; }
132 operator std::span<const uint8_t>() const { return data; }
133
134 /// Move the data out of this object.
135 std::vector<uint8_t> takeData() { return std::move(data); }
136
137 /// Get the size of the data in bytes.
138 size_t getSize() const { return data.size(); }
139 size_t size() const { return getSize(); }
140
141 /// Returns true if this message contains no data.
142 bool empty() const { return data.empty(); }
143
144 /// Cast to a type. Throws if the size of the data does not match the size of
145 /// the message. The lifetime of the resulting pointer is tied to the lifetime
146 /// of this object.
147 template <typename T>
148 const T *as() const {
149 if (data.size() != sizeof(T))
150 throw std::runtime_error("Data size does not match type size. Size is " +
151 std::to_string(data.size()) + ", expected " +
152 std::to_string(sizeof(T)) + ".");
153 return reinterpret_cast<const T *>(data.data());
154 }
155
156 /// Cast from a type to its raw bytes.
157 template <typename T>
158 static MessageData from(T &t) {
159 return MessageData(reinterpret_cast<const uint8_t *>(&t), sizeof(T));
160 }
161
162 /// Convert the data to a hex string.
163 std::string toHex() const;
164
165private:
166 std::vector<uint8_t> data;
167};
168
169//===----------------------------------------------------------------------===//
170// SegmentedMessageData — multi-segment message for generated types.
171//===----------------------------------------------------------------------===//
172
173/// A contiguous, non-owning view of bytes within a SegmentedMessageData.
174/// Valid only while the owning SegmentedMessageData is alive.
175struct Segment {
176 const uint8_t *data;
177 size_t size;
178 std::span<const uint8_t> span() const { return {data, size}; }
179 bool empty() const { return size == 0; }
180};
181
182/// Abstract multi-segment message. Generated types subclass this to expose
183/// header + list segments without flattening into a contiguous buffer.
184///
185/// This class does NOT replace MessageData. It lives alongside it.
186///
187/// Subclasses MUST own all data that their segments point to. The write API
188/// takes ownership (unique_ptr<SegmentedMessageData>) and a backend may hold
189/// the message across async boundaries / partial writes.
191public:
192 virtual ~SegmentedMessageData() = default;
193
194 /// Number of segments in the message.
195 virtual size_t numSegments() const = 0;
196 /// Get a segment by index.
197 virtual Segment segment(size_t idx) const = 0;
198
199 /// Total size across all segments.
200 size_t totalSize() const;
201 /// True if totalSize() == 0.
202 bool empty() const;
203
204 /// Flatten all segments into a standard MessageData.
206};
207
208/// Cursor for incremental consumption of a SegmentedMessageData.
209/// Tracks position across segment boundaries. Backends store this alongside
210/// a unique_ptr<SegmentedMessageData> for partial writes.
211///
212/// Deliberately a separate class (not embedded in SegmentedMessageData) so
213/// that generated types have no hidden members — their layout matches the
214/// hardware wire format exactly.
216public:
218
219 /// Contiguous span from current position to end of current segment.
220 std::span<const uint8_t> remaining() const;
221
222 /// Advance by `n` bytes, crossing segment boundaries as needed.
223 void advance(size_t n);
224
225 /// True when all segments have been consumed.
226 bool done() const;
227
228 /// Reset to the beginning.
229 void reset();
230
231private:
233 size_t segIdx = 0;
234 size_t offset = 0;
235};
236
237} // namespace esi
238
239std::ostream &operator<<(std::ostream &, const esi::ModuleInfo &);
240
241//===----------------------------------------------------------------------===//
242// Functions which should be in the standard library.
243//===----------------------------------------------------------------------===//
244
245namespace esi {
246std::string toHex(void *val);
247std::string toHex(uint64_t val);
248} // namespace esi
249
250#endif // ESI_COMMON_H
std::string toStr() const
Definition Manifest.cpp:814
friend std::ostream & operator<<(std::ostream &os, const AppIDPath &path)
Definition Manifest.cpp:840
AppIDPath operator+(const AppIDPath &b) const
Definition Manifest.cpp:801
AppIDPath parent() const
Definition Manifest.cpp:807
A logical chunk of data representing serialized data.
Definition Common.h:113
const std::vector< uint8_t > & getData() const
Get the data as a vector of bytes.
Definition Common.h:127
const uint8_t * getBytes() const
Definition Common.h:124
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:148
std::vector< uint8_t > takeData()
Move the data out of this object.
Definition Common.h:135
size_t getSize() const
Get the size of the data in bytes.
Definition Common.h:138
MessageData()=default
Adopts the data vector buffer.
MessageData(const uint8_t *data, size_t size)
Definition Common.h:121
size_t size() const
Definition Common.h:139
MessageData(std::vector< uint8_t > &data)
Definition Common.h:119
MessageData(std::span< const uint8_t > data)
Definition Common.h:117
bool empty() const
Returns true if this message contains no data.
Definition Common.h:142
static MessageData from(T &t)
Cast from a type to its raw bytes.
Definition Common.h:158
std::vector< uint8_t > data
Definition Common.h:166
MessageData(std::vector< uint8_t > &&data)
Definition Common.h:120
Cursor for incremental consumption of a SegmentedMessageData.
Definition Common.h:215
const SegmentedMessageData & msg
Definition Common.h:232
void reset()
Reset to the beginning.
Definition Common.cpp:117
void advance(size_t n)
Advance by n bytes, crossing segment boundaries as needed.
Definition Common.cpp:93
bool done() const
True when all segments have been consumed.
Definition Common.cpp:113
std::span< const uint8_t > remaining() const
Contiguous span from current position to end of current segment.
Definition Common.cpp:79
SegmentedMessageDataCursor(const SegmentedMessageData &msg)
Definition Common.h:217
Abstract multi-segment message.
Definition Common.h:190
virtual Segment segment(size_t idx) const =0
Get a segment by index.
virtual ~SegmentedMessageData()=default
MessageData toMessageData() const
Flatten all segments into a standard MessageData.
Definition Common.cpp:60
bool empty() const
True if totalSize() == 0.
Definition Common.cpp:58
size_t totalSize() const
Total size across all segments.
Definition Common.cpp:51
virtual size_t numSegments() const =0
Number of segments in the message.
std::ostream & operator<<(std::ostream &, const esi::ModuleInfo &)
Definition esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition Common.h:108
std::string toHex(void *val)
Definition Common.cpp:37
std::map< std::string, ChannelAssignment > ChannelAssignments
Definition Common.h:97
bool operator<(const AppID &a, const AppID &b)
Definition Manifest.cpp:820
std::vector< HWClientDetail > HWClientDetails
Definition Common.h:107
std::string name
Definition Common.h:36
bool operator!=(const AppID &other) const
Definition Common.h:45
std::string toString() const
Definition Common.h:48
bool operator==(const AppID &other) const
Definition Common.h:42
friend std::ostream & operator<<(std::ostream &os, const AppID &id)
Definition Manifest.cpp:834
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:90
std::map< std::string, std::any > implOptions
Implementation-specific options.
Definition Common.h:95
std::string type
The name of the type of connection.
Definition Common.h:93
std::any value
Definition Common.h:68
std::optional< const Type * > type
Definition Common.h:69
A description of a hardware client.
Definition Common.h:101
std::map< std::string, std::any > implOptions
Definition Common.h:105
AppIDPath relPath
Definition Common.h:102
ChannelAssignments channelAssignments
Definition Common.h:104
ServicePortDesc port
Definition Common.h:103
std::optional< std::string > version
Definition Common.h:75
std::optional< std::string > name
Definition Common.h:73
std::map< std::string, Constant > constants
Definition Common.h:78
std::optional< std::string > commitHash
Definition Common.h:77
std::optional< std::string > repo
Definition Common.h:76
std::map< std::string, std::any > extra
Definition Common.h:79
std::optional< std::string > summary
Definition Common.h:74
A contiguous, non-owning view of bytes within a SegmentedMessageData.
Definition Common.h:175
std::span< const uint8_t > span() const
Definition Common.h:178
const uint8_t * data
Definition Common.h:176
bool empty() const
Definition Common.h:179
size_t size
Definition Common.h:177
A description of a service port.
Definition Common.h:84
std::string name
Definition Common.h:85
std::string portName
Definition Common.h:86