CIRCT 21.0.0git
Loading...
Searching...
No Matches
Services.h
Go to the documentation of this file.
1//===- StdServices.h - ESI standard services C++ API ------------*- 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// The APIs in this backend are all optionally implemented. The lower level
10// ones, however, are strongly recommended. 'Services' here refers to ESI
11// services. These are standard APIs into the standard ESI services.
12//
13// DO NOT EDIT!
14// This file is distributed as part of an ESI package. The source for this file
15// should always be modified within CIRCT.
16//
17//===----------------------------------------------------------------------===//
18
19// NOLINTNEXTLINE(llvm-header-guard)
20#ifndef ESI_RUNTIME_SERVICES_H
21#define ESI_RUNTIME_SERVICES_H
22
23#include "esi/Common.h"
24#include "esi/Context.h"
25#include "esi/Ports.h"
26
27#include <cstdint>
28
29namespace esi {
30class AcceleratorConnection;
31class Engine;
32namespace services {
33
34/// Add a custom interface to a service client at a particular point in the
35/// design hierarchy.
36class ServicePort : public BundlePort {
37public:
39 virtual ~ServicePort() = default;
40 // Get a description of the service port.
41 virtual std::optional<std::string> toString() const { return std::nullopt; }
42};
43
44/// Parent class of all APIs modeled as 'services'. May or may not map to a
45/// hardware side 'service'.
46class Service {
47public:
48 using Type = const std::type_info &;
50 virtual ~Service() = default;
51
52 virtual std::string getServiceSymbol() const = 0;
53
54 /// Create a "child" service of this service. Does not have to be the same
55 /// service type, but typically is. Used when a service already exists in the
56 /// active services table, but a new one wants to replace it. Useful for cases
57 /// where the child service needs to use the parent service. Defaults to
58 /// calling the `getService` method on `AcceleratorConnection` to get the
59 /// global service, implying that the child service does not need to use the
60 /// service it is replacing.
61 virtual Service *getChildService(Service::Type service, AppIDPath id = {},
62 std::string implName = {},
63 ServiceImplDetails details = {},
64 HWClientDetails clients = {});
65
66 /// Get specialized port for this service to attach to the given appid path.
67 /// Null returns mean nothing to attach.
68 virtual BundlePort *getPort(AppIDPath id, const BundleType *type) const {
69 return nullptr;
70 }
71
73
74protected:
76};
77
78/// A service for which there are no standard services registered. Requires
79/// ports be added to the design hierarchy instead of high level interfaces like
80/// the ones in StdServices.h.
81class CustomService : public Service {
82public:
84 const ServiceImplDetails &details,
85 const HWClientDetails &clients);
86 virtual ~CustomService() = default;
87
88 virtual std::string getServiceSymbol() const override {
89 return serviceSymbol;
90 }
91 virtual BundlePort *getPort(AppIDPath id,
92 const BundleType *type) const override;
93
94protected:
95 std::string serviceSymbol;
97};
98
99/// Information about the Accelerator system.
100class SysInfo : public Service {
101public:
102 using Service::Service;
103 virtual ~SysInfo() = default;
104
105 virtual std::string getServiceSymbol() const override;
106
107 /// Get the ESI version number to check version compatibility.
108 virtual uint32_t getEsiVersion() const = 0;
109
110 /// Return the JSON-formatted system manifest.
111 virtual std::string getJsonManifest() const;
112
113 /// Return the zlib compressed JSON system manifest.
114 virtual std::vector<uint8_t> getCompressedManifest() const = 0;
115};
116
117class MMIO : public Service {
118public:
119 static constexpr std::string_view StdName = "esi.service.std.mmio";
120
121 /// Describe a region (slice) of MMIO space.
123 uint32_t base;
124 uint32_t size;
125 };
126
127 MMIO(AcceleratorConnection &, const HWClientDetails &clients);
128 virtual ~MMIO() = default;
129
130 /// Read a 64-bit value from the global MMIO space.
131 virtual uint64_t read(uint32_t addr) const = 0;
132 /// Write a 64-bit value to the global MMIO space.
133 virtual void write(uint32_t addr, uint64_t data) = 0;
134 /// Get the regions of MMIO space that this service manages. Otherwise known
135 /// as the base address table.
136 const std::map<AppIDPath, RegionDescriptor> &getRegions() const {
137 return regions;
138 }
139
140 /// If the service is a MMIO service, return a region of the MMIO space which
141 /// peers into ours.
142 virtual Service *getChildService(Service::Type service, AppIDPath id = {},
143 std::string implName = {},
144 ServiceImplDetails details = {},
145 HWClientDetails clients = {}) override;
146
147 virtual std::string getServiceSymbol() const override;
148
149 /// Get a MMIO region port for a particular region descriptor.
150 virtual BundlePort *getPort(AppIDPath id,
151 const BundleType *type) const override;
152
153private:
154 /// MMIO base address table.
155 std::map<AppIDPath, RegionDescriptor> regions;
156
157public:
158 /// A "slice" of some parent MMIO space.
159 class MMIORegion : public ServicePort {
160 friend class MMIO;
162
163 public:
164 /// Get the offset (and size) of the region in the parent (usually global)
165 /// MMIO address space.
166 virtual RegionDescriptor getDescriptor() const { return desc; };
167 /// Read a 64-bit value from this region, not the global address space.
168 virtual uint64_t read(uint32_t addr) const;
169 /// Write a 64-bit value to this region, not the global address space.
170 virtual void write(uint32_t addr, uint64_t data);
171
172 virtual std::optional<std::string> toString() const override {
173 return "MMIO region " + toHex(desc.base) + " - " +
175 }
176
177 private:
180 };
181};
182
183/// Implement the SysInfo API for a standard MMIO protocol.
184class MMIOSysInfo final : public SysInfo {
185public:
186 MMIOSysInfo(const MMIO *);
187
188 /// Get the ESI version number to check version compatibility.
189 uint32_t getEsiVersion() const override;
190
191 /// Return the zlib compressed JSON system manifest.
192 virtual std::vector<uint8_t> getCompressedManifest() const override;
193
194private:
195 const MMIO *mmio;
196};
197
198class HostMem : public Service {
199public:
200 static constexpr std::string_view StdName = "esi.service.std.hostmem";
201
202 using Service::Service;
203 virtual ~HostMem() = default;
204 virtual std::string getServiceSymbol() const override;
205
206 /// RAII memory region for host memory. Automatically frees the memory when
207 /// deconstructed.
209 virtual ~HostMemRegion() = default;
210 /// Get a pointer to the host memory.
211 virtual void *getPtr() const = 0;
212 /// Sometimes the pointer the device sees is different from the pointer the
213 /// host sees. Call this functon to get the device pointer.
214 virtual void *getDevicePtr() const { return getPtr(); }
215 operator void *() const { return getPtr(); }
216 virtual std::size_t getSize() const = 0;
217 /// Flush the memory region to ensure that the device sees the latest
218 /// contents. Because some platforms require it before DMA transactions, it
219 /// is recommended to call this before any DMA on all platforms. On
220 /// platforms which don't require it, it is a cheap no-op virtual method
221 /// call.
222 virtual void flush() {}
223 };
224
225 /// Options for allocating host memory.
226 struct Options {
227 bool writeable = false;
228 bool useLargePages = false;
229 };
230
231 /// In cases where necessary, enable host memory services.
232 virtual void start() {}
233
234 /// Allocate a region of host memory in accelerator accessible address space.
235 virtual std::unique_ptr<HostMemRegion> allocate(std::size_t size,
236 Options opts) const = 0;
237
238 /// Try to make a region of host memory accessible to the accelerator. Returns
239 /// 'false' on failure. It is optional for an accelerator backend to implement
240 /// this, so client code needs to have a fallback for when this returns
241 /// 'false'. On success, it is the client's responsibility to ensure that the
242 /// memory eventually gets unmapped.
243 virtual bool mapMemory(void *ptr, std::size_t size, Options opts) const {
244 return false;
245 }
246 /// Unmap memory which was previously mapped with 'mapMemory'. Undefined
247 /// behavior when called with a pointer which was not previously mapped.
248 virtual void unmapMemory(void *ptr) const {}
249};
250
251/// Service for calling functions.
252class FuncService : public Service {
253public:
255 HWClientDetails clients);
256
257 virtual std::string getServiceSymbol() const override;
258 virtual BundlePort *getPort(AppIDPath id,
259 const BundleType *type) const override;
260
261 /// A function call which gets attached to a service port.
262 class Function : public ServicePort {
263 friend class FuncService;
264 using ServicePort::ServicePort;
265
266 public:
269
270 void connect();
271 std::future<MessageData> call(const MessageData &arg);
272
273 virtual std::optional<std::string> toString() const override {
274 const esi::Type *argType =
275 dynamic_cast<const ChannelType *>(type->findChannel("arg").first)
276 ->getInner();
277 const esi::Type *resultType =
278 dynamic_cast<const ChannelType *>(type->findChannel("result").first)
279 ->getInner();
280 return "function " + resultType->getID() + "(" + argType->getID() + ")";
281 }
282
283 private:
284 std::mutex callMutex;
287 };
288
289private:
290 std::string symbol;
291};
292
293/// Service for servicing function calls from the accelerator.
294class CallService : public Service {
295public:
297 ServiceImplDetails details);
298
299 virtual std::string getServiceSymbol() const override;
300 virtual BundlePort *getPort(AppIDPath id,
301 const BundleType *type) const override;
302
303 /// A function call which gets attached to a service port.
304 class Callback : public ServicePort {
305 friend class CallService;
308
309 public:
312
313 /// Connect a callback to code which will be executed when the accelerator
314 /// invokes the callback. The 'quick' flag indicates that the callback is
315 /// sufficiently fast that it could be called in the same thread as the
316 /// port callback.
317 void connect(std::function<MessageData(const MessageData &)> callback,
318 bool quick = false);
319
320 virtual std::optional<std::string> toString() const override {
321 const esi::Type *argType =
322 dynamic_cast<const ChannelType *>(type->findChannel("arg").first)
323 ->getInner();
324 const esi::Type *resultType =
325 dynamic_cast<const ChannelType *>(type->findChannel("result").first)
326 ->getInner();
327 return "callback " + resultType->getID() + "(" + argType->getID() + ")";
328 }
329
330 private:
334 };
335
336private:
337 std::string symbol;
338};
339
340/// Registry of services which can be instantiated directly by the Accelerator
341/// class if the backend doesn't do anything special with a service.
343public:
344 /// Create a service instance from the given details. Returns nullptr if
345 /// 'svcType' isn't registered.
347 Service::Type svcType, AppIDPath id,
348 std::string implName,
349 ServiceImplDetails details,
350 HWClientDetails clients);
351
352 /// Resolve a service type from a string. If the string isn't recognized,
353 /// default to CustomService.
354 static Service::Type lookupServiceType(const std::string &);
355};
356
357} // namespace services
358} // namespace esi
359
360#endif // ESI_RUNTIME_SERVICES_H
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:79
Services provide connections to 'bundles' – collections of named, unidirectional communication channe...
Definition Ports.h:226
PortMap channels
Definition Ports.h:270
const BundleType * type
Definition Ports.h:269
BundlePort(AppID id, const BundleType *type, PortMap channels)
Construct a port.
Definition Ports.cpp:22
Bundles represent a collection of channels.
Definition Types.h:44
std::pair< const Type *, Direction > findChannel(std::string name) const
Definition Types.h:57
Channels are the basic communication primitives.
Definition Types.h:70
A logical chunk of data representing serialized data.
Definition Common.h:103
A ChannelPort which reads data from the accelerator.
Definition Ports.h:124
Root class of the ESI type system.
Definition Types.h:27
ID getID() const
Definition Types.h:33
A ChannelPort which sends data to the accelerator.
Definition Ports.h:77
A function call which gets attached to a service port.
Definition Services.h:304
virtual std::optional< std::string > toString() const override
Definition Services.h:320
static Callback * get(AcceleratorConnection &acc, AppID id, BundleType *type, WriteChannelPort &result, ReadChannelPort &arg)
Definition Services.cpp:237
void connect(std::function< MessageData(const MessageData &)> callback, bool quick=false)
Connect a callback to code which will be executed when the accelerator invokes the callback.
Definition Services.cpp:244
AcceleratorConnection & acc
Definition Services.h:333
Service for servicing function calls from the accelerator.
Definition Services.h:294
virtual std::string getServiceSymbol() const override
Definition Services.cpp:226
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const override
Get specialized port for this service to attach to the given appid path.
Definition Services.cpp:228
A service for which there are no standard services registered.
Definition Services.h:81
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const override
Get specialized port for this service to attach to the given appid path.
Definition Services.cpp:173
virtual ~CustomService()=default
virtual std::string getServiceSymbol() const override
Definition Services.h:88
A function call which gets attached to a service port.
Definition Services.h:262
std::future< MessageData > call(const MessageData &arg)
Definition Services.cpp:212
virtual std::optional< std::string > toString() const override
Definition Services.h:273
static Function * get(AppID id, BundleType *type, WriteChannelPort &arg, ReadChannelPort &result)
Definition Services.cpp:194
Service for calling functions.
Definition Services.h:252
virtual std::string getServiceSymbol() const override
Definition Services.cpp:187
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const override
Get specialized port for this service to attach to the given appid path.
Definition Services.cpp:189
virtual ~HostMem()=default
virtual std::string getServiceSymbol() const override
Definition Services.cpp:160
virtual void start()
In cases where necessary, enable host memory services.
Definition Services.h:232
static constexpr std::string_view StdName
Definition Services.h:200
virtual std::unique_ptr< HostMemRegion > allocate(std::size_t size, Options opts) const =0
Allocate a region of host memory in accelerator accessible address space.
virtual void unmapMemory(void *ptr) const
Unmap memory which was previously mapped with 'mapMemory'.
Definition Services.h:248
virtual bool mapMemory(void *ptr, std::size_t size, Options opts) const
Try to make a region of host memory accessible to the accelerator.
Definition Services.h:243
Implement the SysInfo API for a standard MMIO protocol.
Definition Services.h:184
virtual std::vector< uint8_t > getCompressedManifest() const override
Return the zlib compressed JSON system manifest.
Definition Services.cpp:140
uint32_t getEsiVersion() const override
Get the ESI version number to check version compatibility.
Definition Services.cpp:133
A "slice" of some parent MMIO space.
Definition Services.h:159
virtual uint64_t read(uint32_t addr) const
Read a 64-bit value from this region, not the global address space.
Definition Services.cpp:119
virtual void write(uint32_t addr, uint64_t data)
Write a 64-bit value to this region, not the global address space.
Definition Services.cpp:124
virtual RegionDescriptor getDescriptor() const
Get the offset (and size) of the region in the parent (usually global) MMIO address space.
Definition Services.h:166
virtual std::optional< std::string > toString() const override
Definition Services.h:172
virtual uint64_t read(uint32_t addr) const =0
Read a 64-bit value from the global MMIO space.
virtual ~MMIO()=default
virtual void write(uint32_t addr, uint64_t data)=0
Write a 64-bit value to the global MMIO space.
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const override
Get a MMIO region port for a particular region descriptor.
Definition Services.cpp:82
std::map< AppIDPath, RegionDescriptor > regions
MMIO base address table.
Definition Services.h:155
static constexpr std::string_view StdName
Definition Services.h:119
virtual Service * getChildService(Service::Type service, AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={}) override
If the service is a MMIO service, return a region of the MMIO space which peers into ours.
Definition Services.cpp:105
const std::map< AppIDPath, RegionDescriptor > & getRegions() const
Get the regions of MMIO space that this service manages.
Definition Services.h:136
virtual std::string getServiceSymbol() const override
Definition Services.cpp:79
Add a custom interface to a service client at a particular point in the design hierarchy.
Definition Services.h:36
virtual ~ServicePort()=default
virtual std::optional< std::string > toString() const
Definition Services.h:41
Registry of services which can be instantiated directly by the Accelerator class if the backend doesn...
Definition Services.h:342
static Service::Type lookupServiceType(const std::string &)
Resolve a service type from a string.
Definition Services.cpp:284
static Service * createService(AcceleratorConnection *acc, Service::Type svcType, AppIDPath id, std::string implName, ServiceImplDetails details, HWClientDetails clients)
Create a service instance from the given details.
Definition Services.cpp:269
Parent class of all APIs modeled as 'services'.
Definition Services.h:46
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const
Get specialized port for this service to attach to the given appid path.
Definition Services.h:68
virtual std::string getServiceSymbol() const =0
AcceleratorConnection & getConnection() const
Definition Services.h:72
const std::type_info & Type
Definition Services.h:48
virtual Service * getChildService(Service::Type service, AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={})
Create a "child" service of this service.
Definition Services.cpp:28
Service(AcceleratorConnection &conn)
Definition Services.h:49
virtual ~Service()=default
AcceleratorConnection & conn
Definition Services.h:75
Information about the Accelerator system.
Definition Services.h:100
virtual std::string getJsonManifest() const
Return the JSON-formatted system manifest.
Definition Services.cpp:40
virtual uint32_t getEsiVersion() const =0
Get the ESI version number to check version compatibility.
virtual std::vector< uint8_t > getCompressedManifest() const =0
Return the zlib compressed JSON system manifest.
virtual std::string getServiceSymbol() const override
Definition Services.cpp:35
virtual ~SysInfo()=default
Definition esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition Common.h:98
std::string toHex(void *val)
Definition Common.cpp:37
std::map< std::string, ChannelPort & > PortMap
Definition Ports.h:29
std::vector< HWClientDetail > HWClientDetails
Definition Common.h:97
RAII memory region for host memory.
Definition Services.h:208
virtual void * getDevicePtr() const
Sometimes the pointer the device sees is different from the pointer the host sees.
Definition Services.h:214
virtual void * getPtr() const =0
Get a pointer to the host memory.
virtual void flush()
Flush the memory region to ensure that the device sees the latest contents.
Definition Services.h:222
virtual std::size_t getSize() const =0
Options for allocating host memory.
Definition Services.h:226
Describe a region (slice) of MMIO space.
Definition Services.h:122