CIRCT  19.0.0git
Services.cpp
Go to the documentation of this file.
1 //===- StdServices.cpp - implementations of std services ------------------===//
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 // (lib/dialect/ESI/runtime/cpp/lib/backends/Cosim.cpp).
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "esi/Services.h"
17 #include "esi/Accelerator.h"
18 
19 #include "zlib.h"
20 
21 #include <cassert>
22 #include <stdexcept>
23 
24 using namespace std;
25 
26 using namespace esi;
27 using namespace esi::services;
28 
29 string SysInfo::getServiceSymbol() const { return "__builtin_SysInfo"; }
30 
31 // Allocate 10MB for the uncompressed manifest. This should be plenty.
32 constexpr uint32_t MAX_MANIFEST_SIZE = 10 << 20;
33 /// Get the compressed manifest, uncompress, and return it.
34 string SysInfo::getJsonManifest() const {
35  vector<uint8_t> compressed = getCompressedManifest();
36  vector<Bytef> dst(MAX_MANIFEST_SIZE);
37  uLongf dstSize = MAX_MANIFEST_SIZE;
38  int rc =
39  uncompress(dst.data(), &dstSize, compressed.data(), compressed.size());
40  if (rc != Z_OK)
41  throw runtime_error("zlib uncompress failed with rc=" + to_string(rc));
42  return string(reinterpret_cast<char *>(dst.data()), dstSize);
43 }
44 
45 string MMIO::getServiceSymbol() const { return "__builtin_MMIO"; }
46 
47 MMIOSysInfo::MMIOSysInfo(const MMIO *mmio) : mmio(mmio) {}
48 
49 uint32_t MMIOSysInfo::getEsiVersion() const {
50  uint32_t reg;
52  throw runtime_error("Invalid magic number low bits: " + toHex(reg));
53  if ((reg = mmio->read(MetadataOffset + 4)) != MagicNumberHi)
54  throw runtime_error("Invalid magic number high bits: " + toHex(reg));
55  return mmio->read(MetadataOffset + 8);
56 }
57 
58 vector<uint8_t> MMIOSysInfo::getCompressedManifest() const {
59  uint32_t manifestPtr = mmio->read(MetadataOffset + 12);
60  uint32_t size = mmio->read(manifestPtr);
61  uint32_t numWords = (size + 3) / 4;
62  vector<uint32_t> manifestWords(numWords);
63  for (size_t i = 0; i < numWords; ++i)
64  manifestWords[i] = mmio->read(manifestPtr + 4 + (i * 4));
65 
66  vector<uint8_t> manifest;
67  for (size_t i = 0; i < size; ++i) {
68  uint32_t word = manifestWords[i / 4];
69  manifest.push_back(word >> (8 * (i % 4)));
70  }
71  return manifest;
72 }
73 
75  const ServiceImplDetails &details,
76  const HWClientDetails &clients)
77  : id(idPath) {
78  if (auto f = details.find("service"); f != details.end()) {
79  serviceSymbol = any_cast<string>(f->second);
80  // Strip off initial '@'.
81  serviceSymbol = serviceSymbol.substr(1);
82  }
83 }
84 
86  std::string implName, ServiceImplDetails details,
87  HWClientDetails clients) {
88  if (auto f = details.find("service"); f != details.end())
89  // Strip off initial '@'.
90  symbol = any_cast<string>(f->second).substr(1);
91 }
92 
93 std::string FuncService::getServiceSymbol() const { return symbol; }
94 
97  const std::map<std::string, ChannelPort &> &channels,
98  AcceleratorConnection &acc) const {
99  return new Function(id.back(), channels);
100 }
101 
103  AppID id, const std::map<std::string, ChannelPort &> &channels)
104  : ServicePort(id, channels),
105  arg(dynamic_cast<WriteChannelPort &>(channels.at("arg"))),
106  result(dynamic_cast<ReadChannelPort &>(channels.at("result"))) {
107  if (channels.size() != 2)
108  throw runtime_error("FuncService must have exactly two channels");
109 }
110 
112  arg.connect();
113  result.connect();
114 }
115 
116 std::future<MessageData>
118  arg.write(argData);
119  return result.readAsync();
120 }
121 
123  Service::Type svcType, AppIDPath id,
124  std::string implName,
125  ServiceImplDetails details,
126  HWClientDetails clients) {
127  // TODO: Add a proper registration mechanism.
128  if (svcType == typeid(FuncService))
129  return new FuncService(acc, id, implName, details, clients);
130  return nullptr;
131 }
132 
134  // TODO: Add a proper registration mechanism.
135  if (svcName == "esi.service.std.func")
136  return typeid(FuncService);
137  return typeid(CustomService);
138 }
constexpr uint32_t MAX_MANIFEST_SIZE
Definition: Services.cpp:32
Abstract class representing a connection to an accelerator.
Definition: Accelerator.h:75
std::map< std::string, ChannelPort & > channels
Definition: Ports.h:101
Bundles represent a collection of channels.
Definition: Types.h:44
A logical chunk of data representing serialized data.
Definition: Common.h:85
A ChannelPort which reads data from the accelerator.
Definition: Ports.h:55
A ChannelPort which sends data to the accelerator.
Definition: Ports.h:46
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:74
A function call which gets attached to a service port.
Definition: Services.h:128
std::future< MessageData > call(const MessageData &arg)
Definition: Services.cpp:117
Function(AppID id, const std::map< std::string, ChannelPort & > &channels)
Definition: Services.cpp:102
Service for calling functions.
Definition: Services.h:117
virtual std::string getServiceSymbol() const override
Definition: Services.cpp:93
FuncService(AcceleratorConnection *acc, AppIDPath id, std::string implName, ServiceImplDetails details, HWClientDetails clients)
Definition: Services.cpp:85
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:96
virtual std::vector< uint8_t > getCompressedManifest() const override
Return the zlib compressed JSON system manifest.
Definition: Services.cpp:58
uint32_t getEsiVersion() const override
Get the ESI version number to check version compatibility.
Definition: Services.cpp:49
virtual uint32_t read(uint32_t addr) const =0
Add a custom interface to a service client at a particular point in the design hierarchy.
Definition: Services.h:34
static Service::Type lookupServiceType(const std::string &)
Resolve a service type from a string.
Definition: Services.cpp:133
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:122
Parent class of all APIs modeled as 'services'.
Definition: Services.h:42
const std::type_info & Type
Definition: Services.h:44
Definition: esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition: Common.h:80
std::string toHex(uint32_t val)
Definition: Common.cpp:20
constexpr uint32_t MagicNumberLo
Definition: Accelerator.h:43
constexpr uint32_t MetadataOffset
Definition: Accelerator.h:42
constexpr uint32_t MagicNumberHi
Definition: Accelerator.h:44
std::vector< HWClientDetail > HWClientDetails
Definition: Common.h:79
def reg(value, clock, reset=None, reset_value=None, name=None, sym_name=None)
Definition: seq.py:20