CIRCT  19.0.0git
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/Ports.h"
25 
26 #include <cstdint>
27 
28 namespace esi {
29 class AcceleratorConnection;
30 namespace services {
31 
32 /// Add a custom interface to a service client at a particular point in the
33 /// design hierarchy.
34 class ServicePort : public BundlePort {
35 public:
37  virtual ~ServicePort() = default;
38 };
39 
40 /// Parent class of all APIs modeled as 'services'. May or may not map to a
41 /// hardware side 'service'.
42 class Service {
43 public:
44  using Type = const std::type_info &;
45  virtual ~Service() = default;
46 
47  virtual std::string getServiceSymbol() const = 0;
48 
49  /// Get specialized port for this service to attach to the given appid path.
50  /// Null returns mean nothing to attach.
51  virtual ServicePort *getPort(AppIDPath id, const BundleType *type,
52  const std::map<std::string, ChannelPort &> &,
53  AcceleratorConnection &) const {
54  return nullptr;
55  }
56 };
57 
58 /// A service for which there are no standard services registered. Requires
59 /// ports be added to the design hierarchy instead of high level interfaces like
60 /// the ones in StdServices.h.
61 class CustomService : public Service {
62 public:
63  CustomService(AppIDPath idPath, const ServiceImplDetails &details,
64  const HWClientDetails &clients);
65  virtual ~CustomService() = default;
66 
67  virtual std::string getServiceSymbol() const override {
68  return serviceSymbol;
69  }
70 
71 protected:
72  std::string serviceSymbol;
74 };
75 
76 /// Information about the Accelerator system.
77 class SysInfo : public Service {
78 public:
79  virtual ~SysInfo() = default;
80 
81  virtual std::string getServiceSymbol() const override;
82 
83  /// Get the ESI version number to check version compatibility.
84  virtual uint32_t getEsiVersion() const = 0;
85 
86  /// Return the JSON-formatted system manifest.
87  virtual std::string getJsonManifest() const;
88 
89  /// Return the zlib compressed JSON system manifest.
90  virtual std::vector<uint8_t> getCompressedManifest() const = 0;
91 };
92 
93 class MMIO : public Service {
94 public:
95  virtual ~MMIO() = default;
96  virtual uint64_t read(uint32_t addr) const = 0;
97  virtual void write(uint32_t addr, uint64_t data) = 0;
98  virtual std::string getServiceSymbol() const override;
99 };
100 
101 /// Implement the SysInfo API for a standard MMIO protocol.
102 class MMIOSysInfo final : public SysInfo {
103 public:
104  MMIOSysInfo(const MMIO *);
105 
106  /// Get the ESI version number to check version compatibility.
107  uint32_t getEsiVersion() const override;
108 
109  /// Return the zlib compressed JSON system manifest.
110  virtual std::vector<uint8_t> getCompressedManifest() const override;
111 
112 private:
113  const MMIO *mmio;
114 };
115 
116 class HostMem : public Service {
117 public:
118  virtual ~HostMem() = default;
119  virtual std::string getServiceSymbol() const override;
120 
121  /// RAII memory region for host memory. Automatically frees the memory when
122  /// deconstructed.
123  struct HostMemRegion {
124  virtual ~HostMemRegion() = default;
125  virtual void *getPtr() const = 0;
126  operator void *() const { return getPtr(); }
127  virtual std::size_t getSize() const = 0;
128  };
129 
130  /// Options for allocating host memory.
131  struct Options {
132  bool writeable = false;
133  bool useLargePages = false;
134  };
135 
136  /// Allocate a region of host memory in accelerator accessible address space.
137  virtual std::unique_ptr<HostMemRegion> allocate(std::size_t size,
138  Options opts) const = 0;
139 
140  /// Try to make a region of host memory accessible to the accelerator. Returns
141  /// 'false' on failure. It is optional for an accelerator backend to implement
142  /// this, so client code needs to have a fallback for when this returns
143  /// 'false'. On success, it is the client's responsibility to ensure that the
144  /// memory eventually gets unmapped.
145  virtual bool mapMemory(void *ptr, std::size_t size, Options opts) const {
146  return false;
147  }
148  /// Unmap memory which was previously mapped with 'mapMemory'. Undefined
149  /// behavior when called with a pointer which was not previously mapped.
150  virtual void unmapMemory(void *ptr) const {}
151 };
152 
153 /// Service for calling functions.
154 class FuncService : public Service {
155 public:
157  const std::string &implName, ServiceImplDetails details,
158  HWClientDetails clients);
159 
160  virtual std::string getServiceSymbol() const override;
161  virtual ServicePort *getPort(AppIDPath id, const BundleType *type,
162  const std::map<std::string, ChannelPort &> &,
163  AcceleratorConnection &) const override;
164 
165  /// A function call which gets attached to a service port.
166  class Function : public ServicePort {
167  friend class FuncService;
168  Function(AppID id, const std::map<std::string, ChannelPort &> &channels);
169 
170  public:
171  static Function *get(AppID id, WriteChannelPort &arg,
173 
174  void connect();
175  std::future<MessageData> call(const MessageData &arg);
176 
177  private:
178  std::mutex callMutex;
181  };
182 
183 private:
184  std::string symbol;
185 };
186 
187 /// Service for servicing function calls from the accelerator.
188 class CallService : public Service {
189 public:
190  CallService(AcceleratorConnection *acc, AppIDPath id, std::string implName,
191  ServiceImplDetails details, HWClientDetails clients);
192 
193  virtual std::string getServiceSymbol() const override;
194  virtual ServicePort *getPort(AppIDPath id, const BundleType *type,
195  const std::map<std::string, ChannelPort &> &,
196  AcceleratorConnection &) const override;
197 
198  /// A function call which gets attached to a service port.
199  class Callback : public ServicePort {
200  friend class CallService;
202  const std::map<std::string, ChannelPort &> &channels);
203 
204  public:
205  /// Connect a callback to code which will be executed when the accelerator
206  /// invokes the callback. The 'quick' flag indicates that the callback is
207  /// sufficiently fast that it could be called in the same thread as the
208  /// port callback.
209  void connect(std::function<MessageData(const MessageData &)> callback,
210  bool quick = false);
211 
212  private:
216  };
217 
218 private:
219  std::string symbol;
220 };
221 
222 /// Registry of services which can be instantiated directly by the Accelerator
223 /// class if the backend doesn't do anything special with a service.
225 public:
226  /// Create a service instance from the given details. Returns nullptr if
227  /// 'svcType' isn't registered.
229  Service::Type svcType, AppIDPath id,
230  std::string implName,
231  ServiceImplDetails details,
232  HWClientDetails clients);
233 
234  /// Resolve a service type from a string. If the string isn't recognized,
235  /// default to CustomService.
236  static Service::Type lookupServiceType(const std::string &);
237 };
238 
239 } // namespace services
240 } // namespace esi
241 
242 #endif // ESI_RUNTIME_SERVICES_H
Abstract class representing a connection to an accelerator.
Definition: Accelerator.h:78
Services provide connections to 'bundles' – collections of named, unidirectional communication channe...
Definition: Ports.h:147
std::map< std::string, ChannelPort & > channels
Definition: Ports.h:183
BundlePort(AppID id, std::map< std::string, ChannelPort & > channels)
Construct a port.
Definition: Ports.cpp:22
Bundles represent a collection of channels.
Definition: Types.h:44
A logical chunk of data representing serialized data.
Definition: Common.h:86
A ChannelPort which reads data from the accelerator.
Definition: Ports.h:69
A ChannelPort which sends data to the accelerator.
Definition: Ports.h:57
A function call which gets attached to a service port.
Definition: Services.h:199
Callback(AcceleratorConnection &acc, AppID id, const std::map< std::string, ChannelPort & > &channels)
Definition: Services.cpp:148
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:159
AcceleratorConnection & acc
Definition: Services.h:215
Service for servicing function calls from the accelerator.
Definition: Services.h:188
CallService(AcceleratorConnection *acc, AppIDPath id, std::string implName, ServiceImplDetails details, HWClientDetails clients)
Definition: Services.cpp:131
virtual std::string getServiceSymbol() const override
Definition: Services.cpp:139
virtual ServicePort * getPort(AppIDPath id, const BundleType *type, const std::map< std::string, ChannelPort & > &, AcceleratorConnection &) const override
Get specialized port for this service to attach to the given appid path.
Definition: Services.cpp:142
A service for which there are no standard services registered.
Definition: Services.h:61
CustomService(AppIDPath idPath, const ServiceImplDetails &details, const HWClientDetails &clients)
Definition: Services.cpp:77
virtual ~CustomService()=default
virtual std::string getServiceSymbol() const override
Definition: Services.h:67
A function call which gets attached to a service port.
Definition: Services.h:166
std::future< MessageData > call(const MessageData &arg)
Definition: Services.cpp:125
static Function * get(AppID id, WriteChannelPort &arg, ReadChannelPort &result)
Definition: Services.cpp:113
Function(AppID id, const std::map< std::string, ChannelPort & > &channels)
Definition: Services.cpp:105
Service for calling functions.
Definition: Services.h:154
virtual std::string getServiceSymbol() const override
Definition: Services.cpp:96
virtual ServicePort * getPort(AppIDPath id, const BundleType *type, const std::map< std::string, ChannelPort & > &, AcceleratorConnection &) const override
Get specialized port for this service to attach to the given appid path.
Definition: Services.cpp:99
FuncService(AcceleratorConnection *acc, AppIDPath id, const std::string &implName, ServiceImplDetails details, HWClientDetails clients)
Definition: Services.cpp:88
virtual ~HostMem()=default
virtual std::string getServiceSymbol() const override
Definition: Services.cpp:75
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:150
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:145
Implement the SysInfo API for a standard MMIO protocol.
Definition: Services.h:102
virtual std::vector< uint8_t > getCompressedManifest() const override
Return the zlib compressed JSON system manifest.
Definition: Services.cpp:55
uint32_t getEsiVersion() const override
Get the ESI version number to check version compatibility.
Definition: Services.cpp:48
MMIOSysInfo(const MMIO *)
Definition: Services.cpp:46
virtual uint64_t read(uint32_t addr) const =0
virtual ~MMIO()=default
virtual void write(uint32_t addr, uint64_t data)=0
virtual std::string getServiceSymbol() const override
Definition: Services.cpp:44
Add a custom interface to a service client at a particular point in the design hierarchy.
Definition: Services.h:34
virtual ~ServicePort()=default
Registry of services which can be instantiated directly by the Accelerator class if the backend doesn...
Definition: Services.h:224
static Service::Type lookupServiceType(const std::string &)
Resolve a service type from a string.
Definition: Services.cpp:194
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:181
Parent class of all APIs modeled as 'services'.
Definition: Services.h:42
virtual std::string getServiceSymbol() const =0
const std::type_info & Type
Definition: Services.h:44
virtual ~Service()=default
virtual ServicePort * getPort(AppIDPath id, const BundleType *type, const std::map< std::string, ChannelPort & > &, AcceleratorConnection &) const
Get specialized port for this service to attach to the given appid path.
Definition: Services.h:51
Information about the Accelerator system.
Definition: Services.h:77
virtual std::string getJsonManifest() const
Return the JSON-formatted system manifest.
Definition: Services.cpp:32
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:27
virtual ~SysInfo()=default
Definition: esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition: Common.h:81
std::vector< HWClientDetail > HWClientDetails
Definition: Common.h:80
RAII memory region for host memory.
Definition: Services.h:123
virtual void * getPtr() const =0
virtual std::size_t getSize() const =0
Options for allocating host memory.
Definition: Services.h:131