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
110class MessageData;
111
112//===----------------------------------------------------------------------===//
113// SegmentedMessageData -- multi-segment message support.
114//===----------------------------------------------------------------------===//
115
116/// A contiguous, non-owning view of bytes within a SegmentedMessageData.
117/// Valid only while the owning SegmentedMessageData is alive.
118struct Segment {
119 const uint8_t *data;
120 size_t size;
121 std::span<const uint8_t> span() const { return {data, size}; }
122 bool empty() const { return size == 0; }
123};
124
125/// Abstract multi-segment message. Generated types subclass this to expose
126/// header + list segments without flattening into a contiguous buffer.
127///
128/// MessageData is the canonical flat, one-segment implementation of this
129/// interface. Other subclasses represent naturally segmented layouts.
130///
131/// Subclasses MUST own all data that their segments point to. Read and write
132/// APIs can hold the message across async boundaries / retries.
134public:
135 virtual ~SegmentedMessageData() = default;
136
137 /// Number of segments in the message.
138 virtual size_t numSegments() const = 0;
139 /// Get a segment by index.
140 virtual Segment segment(size_t idx) const = 0;
141
142 /// Total size across all segments.
143 size_t totalSize() const;
144 /// True if totalSize() == 0.
145 bool empty() const;
146
147 /// Flatten all segments into a standard MessageData.
148 virtual MessageData toMessageData() const;
149};
150
151/// A concrete flat message backed by a single vector of bytes.
152///
153/// This is also a one-segment SegmentedMessageData so flat and segmented
154/// messages can flow through the same internal APIs.
156public:
157 /// Adopts the data vector buffer.
158 MessageData() = default;
159 MessageData(std::span<const uint8_t> data)
160 : data(data.data(), data.data() + data.size()) {}
161 MessageData(std::vector<uint8_t> &data) : data(std::move(data)) {}
162 MessageData(std::vector<uint8_t> &&data) : data(std::move(data)) {}
163 MessageData(const uint8_t *data, size_t size) : data(data, data + size) {}
164 ~MessageData() = default;
165
166 const uint8_t *getBytes() const { return data.data(); }
167
168 /// Get the data as a vector of bytes.
169 const std::vector<uint8_t> &getData() const { return data; }
170
171 /// Implicit conversion to a vector/span of bytes, to play nice with other
172 /// APIs that accept bytearray-like things.
173 operator const std::vector<uint8_t> &() const { return data; }
174 operator std::span<const uint8_t>() const { return data; }
175
176 /// Move the data out of this object.
177 std::vector<uint8_t> takeData() { return std::move(data); }
178
179 /// Get the size of the data in bytes.
180 size_t getSize() const { return data.size(); }
181 size_t size() const { return getSize(); }
182
183 /// Returns true if this message contains no data.
184 bool empty() const { return data.empty(); }
185
186 /// Cast to a type. Throws if the size of the data does not match the size of
187 /// the message. The lifetime of the resulting pointer is tied to the lifetime
188 /// of this object.
189 template <typename T>
190 const T *as() const {
191 if (data.size() != sizeof(T))
192 throw std::runtime_error("Data size does not match type size. Size is " +
193 std::to_string(data.size()) + ", expected " +
194 std::to_string(sizeof(T)) + ".");
195 return reinterpret_cast<const T *>(data.data());
196 }
197
198 /// Cast from a type to its raw bytes.
199 template <typename T>
200 static MessageData from(T &t) {
201 return MessageData(reinterpret_cast<const uint8_t *>(&t), sizeof(T));
202 }
203
204 /// Convert the data to a hex string.
205 std::string toHex() const;
206
207 size_t numSegments() const override { return 1; }
208 Segment segment(size_t idx) const override {
209 if (idx != 0)
210 throw std::out_of_range("MessageData only has one segment");
211 return {data.data(), data.size()};
212 }
213 MessageData toMessageData() const override { return *this; }
214
215private:
216 std::vector<uint8_t> data;
217};
218
219/// Cursor for incremental consumption of a SegmentedMessageData.
220/// Tracks position across segment boundaries. Backends store this alongside
221/// a unique_ptr<SegmentedMessageData> for partial writes.
222///
223/// Deliberately a separate class (not embedded in SegmentedMessageData) so
224/// that generated types have no hidden members — their layout matches the
225/// hardware wire format exactly.
227public:
229
230 /// Contiguous span from current position to end of current segment.
231 std::span<const uint8_t> remaining() const;
232
233 /// Advance by `n` bytes, crossing segment boundaries as needed.
234 void advance(size_t n);
235
236 /// True when all segments have been consumed.
237 bool done() const;
238
239 /// Reset to the beginning.
240 void reset();
241
242private:
244 size_t segIdx = 0;
245 size_t offset = 0;
246};
247
248} // namespace esi
249
250std::ostream &operator<<(std::ostream &, const esi::ModuleInfo &);
251
252//===----------------------------------------------------------------------===//
253// Functions which should be in the standard library.
254//===----------------------------------------------------------------------===//
255
256namespace esi {
257std::string toHex(void *val);
258std::string toHex(uint64_t val);
259} // namespace esi
260
261#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 concrete flat message backed by a single vector of bytes.
Definition Common.h:155
const std::vector< uint8_t > & getData() const
Get the data as a vector of bytes.
Definition Common.h:169
const uint8_t * getBytes() const
Definition Common.h:166
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:190
std::vector< uint8_t > takeData()
Move the data out of this object.
Definition Common.h:177
size_t numSegments() const override
Number of segments in the message.
Definition Common.h:207
Segment segment(size_t idx) const override
Get a segment by index.
Definition Common.h:208
size_t getSize() const
Get the size of the data in bytes.
Definition Common.h:180
MessageData()=default
Adopts the data vector buffer.
MessageData(const uint8_t *data, size_t size)
Definition Common.h:163
size_t size() const
Definition Common.h:181
MessageData(std::vector< uint8_t > &data)
Definition Common.h:161
MessageData toMessageData() const override
Flatten all segments into a standard MessageData.
Definition Common.h:213
MessageData(std::span< const uint8_t > data)
Definition Common.h:159
bool empty() const
Returns true if this message contains no data.
Definition Common.h:184
static MessageData from(T &t)
Cast from a type to its raw bytes.
Definition Common.h:200
std::vector< uint8_t > data
Definition Common.h:216
MessageData(std::vector< uint8_t > &&data)
Definition Common.h:162
Cursor for incremental consumption of a SegmentedMessageData.
Definition Common.h:226
const SegmentedMessageData & msg
Definition Common.h:243
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:228
Abstract multi-segment message.
Definition Common.h:133
virtual Segment segment(size_t idx) const =0
Get a segment by index.
virtual ~SegmentedMessageData()=default
virtual 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:118
std::span< const uint8_t > span() const
Definition Common.h:121
const uint8_t * data
Definition Common.h:119
bool empty() const
Definition Common.h:122
size_t size
Definition Common.h:120
A description of a service port.
Definition Common.h:84
std::string name
Definition Common.h:85
std::string portName
Definition Common.h:86