19#include <nlohmann/json.hpp>
32 friend class ::esi::Manifest;
35 Impl(Context &
ctxt,
const std::string &jsonManifest);
40 std::optional<ModuleInfo>
getModInfo(
const nlohmann::json &)
const;
47 void createEngine(AcceleratorConnection &, AppIDPath appID,
48 const nlohmann::json &)
const;
53 services::Service *
getService(AppIDPath idPath, AcceleratorConnection &,
54 const nlohmann::json &,
56 bool isEngine =
false)
const;
60 std::vector<services::Service *>
61 getServices(AppIDPath idPath, AcceleratorConnection &,
const nlohmann::json &,
66 std::vector<std::unique_ptr<BundlePort>>
69 const nlohmann::json &instJson)
const;
73 std::vector<std::unique_ptr<Instance>>
76 const nlohmann::json &instJson)
const;
80 std::unique_ptr<Instance>
83 const nlohmann::json &childJson)
const;
93 std::unique_ptr<Accelerator>
96 const Type *
parseType(
const nlohmann::json &typeJson);
106 std::optional<const Type *>
getType(Type::ID
id)
const {
107 return ctxt.getType(
id);
110 std::any
getAny(
const nlohmann::json &value)
const;
124static std::optional<AppID>
parseID(
const nlohmann::json &jsonID) {
125 if (!jsonID.is_object())
127 std::optional<uint32_t> idx;
128 if (jsonID.contains(
"index"))
129 idx = jsonID.at(
"index").get<uint32_t>();
130 if (jsonID.contains(
"name") && jsonID.size() <= 2)
131 return AppID(jsonID.at(
"name").get<std::string>(), idx);
136 std::optional<AppID>
id =
parseID(jsonID);
138 throw std::runtime_error(
"Malformed manifest: invalid appID");
143 for (
auto &idJson : jsonIDPath)
149 return ServicePortDesc{jsonPort.at(
"serviceName").get<std::string>(),
150 jsonPort.at(
"port").get<std::string>()};
156 auto getObject = [
this](
const nlohmann::json &
json) -> std::any {
157 std::map<std::string, std::any> ret;
158 for (
auto &e :
json.items())
159 ret[e.key()] =
getAny(e.value());
162 if (ret.size() != 2 || !ret.contains(
"type") || !ret.contains(
"value"))
164 std::any value = ret.at(
"value");
165 std::any typeID = ret.at(
"type");
166 if (typeID.type() !=
typeid(std::string))
168 std::optional<const Type *> type =
169 getType(std::any_cast<std::string>(type));
174 return Constant{value, type};
177 auto getArray = [
this](
const nlohmann::json &
json) -> std::any {
178 std::vector<std::any> ret;
184 auto getValue = [&](
const nlohmann::json &innerValue) -> std::any {
185 if (innerValue.is_string())
186 return innerValue.get<std::string>();
187 else if (innerValue.is_number_unsigned())
188 return innerValue.get<uint64_t>();
189 else if (innerValue.is_number_integer())
190 return innerValue.get<int64_t>();
191 else if (innerValue.is_number_float())
192 return innerValue.get<
double>();
193 else if (innerValue.is_boolean())
194 return innerValue.get<
bool>();
195 else if (innerValue.is_null())
196 return innerValue.get<std::nullptr_t>();
197 else if (innerValue.is_object())
198 return getObject(innerValue);
199 else if (innerValue.is_array())
200 return getArray(innerValue);
202 throw std::runtime_error(
"Unknown type in manifest: " +
206 std::optional<AppID> appid =
parseID(value);
209 if (!value.is_object() || !value.contains(
"type") || !value.contains(
"value"))
210 return getValue(value);
211 return Constant{getValue(value.at(
"value")),
getType(value.at(
"type"))};
215 const nlohmann::json &mod)
const {
216 for (
auto &extra : mod.items())
217 if (extra.key() !=
"name" && extra.key() !=
"summary" &&
218 extra.key() !=
"version" && extra.key() !=
"repo" &&
219 extra.key() !=
"commitHash")
220 info.extra[extra.key()] = getAny(extra.value());
222 auto value = [&](
const std::string &key) -> std::optional<std::string> {
223 auto f = mod.find(key);
228 info.name = value(
"name");
229 info.summary = value(
"summary");
230 info.version = value(
"version");
231 info.repo = value(
"repo");
232 info.commitHash = value(
"commitHash");
236 const nlohmann::json &mod)
const {
237 for (
auto &item : mod.items()) {
238 std::any value = getAny(item.value());
239 auto *c = std::any_cast<Constant>(&value);
241 info.constants[item.key()] = *c;
244 info.constants[item.key()] = Constant{value, std::nullopt};
254 manifestJson = nlohmann::ordered_json::parse(manifestStr);
263 if (mod.contains(
"symInfo"))
265 if (mod.contains(
"symConsts"))
269 }
catch (
const std::exception &e) {
270 std::string msg =
"malformed manifest: " + std::string(e.what());
272 msg +=
" (schema version 0 is not considered stable)";
273 throw std::runtime_error(msg);
277std::unique_ptr<Accelerator>
281 auto designJson = manifestJson.at(
"design");
285 auto enginesIter = designJson.find(
"engines");
286 if (enginesIter != designJson.end())
287 for (
auto &engineDesc : enginesIter.value())
288 createEngine(acc, {}, engineDesc);
291 auto svcDecls = manifestJson.at(
"serviceDeclarations");
292 scanServiceDecls(acc, svcDecls, activeSvcs);
295 std::vector<services::Service *> services =
296 getServices({}, acc, designJson, activeSvcs);
299 auto ports = getBundlePorts(acc, {}, activeSvcs, designJson);
301 return std::make_unique<Accelerator>(
302 getModInfo(designJson),
303 getChildInstances({}, acc, activeSvcs, designJson), services, ports);
306std::optional<ModuleInfo>
308 auto instOfIter =
json.find(
"instOf");
309 if (instOfIter ==
json.end())
311 auto f = symbolInfoCache.find(instOfIter.value());
312 if (f != symbolInfoCache.end())
320 const nlohmann::json &eng)
const {
322 getService(idPath, acc, eng, dummy,
true);
326 const nlohmann::json &svcDecls,
328 for (
auto &svcDecl : svcDecls) {
330 ServiceImplDetails svcDetails;
331 for (
auto &
detail : svcDecl.items())
335 auto serviceNameIter = svcDecl.find(
"serviceName");
336 std::string serviceName;
337 if (serviceNameIter != svcDecl.end())
338 serviceName = serviceNameIter.value();
339 services::Service::Type svcId =
340 services::ServiceRegistry::lookupServiceType(serviceName);
341 auto svc = acc.getService(svcId, {},
"",
344 activeServices[svcDecl.at(
"symbol")] = svc;
348std::vector<std::unique_ptr<Instance>>
351 const nlohmann::json &instJson)
const {
352 std::vector<std::unique_ptr<Instance>> ret;
353 auto childrenIter = instJson.find(
"children");
354 if (childrenIter == instJson.end())
356 for (
auto &child : childrenIter.value())
357 ret.emplace_back(getChildInstance(idPath, acc, activeServices, child));
361std::unique_ptr<Instance>
364 const nlohmann::json &child)
const {
366 idPath.push_back(childID);
368 std::vector<services::Service *> services =
369 getServices(idPath, acc, child, activeServices);
371 auto children = getChildInstances(idPath, acc, activeServices, child);
372 auto ports = getBundlePorts(acc, idPath, activeServices, child);
373 return std::make_unique<Instance>(
parseIDChecked(child.at(
"appID")),
374 getModInfo(child), std::move(children),
379 AcceleratorConnection &acc,
380 const nlohmann::json &svcJson,
382 bool isEngine)
const {
385 idPath.push_back(
id);
388 HWClientDetails clientDetails;
389 for (
auto &client : svcJson.at(
"clientDetails")) {
390 HWClientDetail clientDetail;
391 for (
auto &
detail : client.items()) {
392 if (
detail.key() ==
"relAppIDPath")
394 else if (
detail.key() ==
"servicePort")
396 else if (
detail.key() ==
"channelAssignments") {
397 for (
auto &chan :
detail.value().items()) {
398 ChannelAssignment chanAssign;
399 for (
auto &assign : chan.value().items())
400 if (assign.key() ==
"type")
401 chanAssign.type = assign.value();
403 chanAssign.implOptions[assign.key()] = getAny(assign.value());
404 clientDetail.channelAssignments[chan.key()] = chanAssign;
407 clientDetail.implOptions[
detail.key()] = getAny(
detail.value());
409 clientDetails.push_back(clientDetail);
413 ServiceImplDetails svcDetails;
414 std::string implName;
416 for (
auto &
detail : svcJson.items()) {
417 if (
detail.key() ==
"appID" ||
detail.key() ==
"clientDetails")
419 if (
detail.key() ==
"serviceImplName")
420 implName =
detail.value();
421 else if (
detail.key() ==
"service")
422 service =
detail.value().get<std::string>();
428 services::Service *svc =
nullptr;
429 auto activeServiceIter = activeServices.find(service);
430 if (activeServiceIter != activeServices.end()) {
431 services::Service::Type svcType =
432 services::ServiceRegistry::lookupServiceType(
433 activeServiceIter->second->getServiceSymbol());
434 svc = activeServiceIter->second->getChildService(svcType, idPath, implName,
435 svcDetails, clientDetails);
437 services::Service::Type svcType =
438 services::ServiceRegistry::lookupServiceType(service);
440 acc.createEngine(implName, idPath, svcDetails, clientDetails);
443 acc.getService(svcType, idPath, implName, svcDetails, clientDetails);
448 activeServices[service] = svc;
452std::vector<services::Service *>
454 const nlohmann::json &svcsJson,
456 std::vector<services::Service *> ret;
457 auto svcsIter = svcsJson.find(
"services");
458 if (svcsIter == svcsJson.end())
461 for (
auto &svc : svcsIter.value())
462 ret.emplace_back(getService(idPath, acc, svc, activeServices));
466std::vector<std::unique_ptr<BundlePort>>
469 const nlohmann::json &instJson)
const {
470 std::vector<std::unique_ptr<BundlePort>> ret;
471 auto clientPortsIter = instJson.find(
"clientPorts");
472 if (clientPortsIter == instJson.end())
475 for (
auto &content : clientPortsIter.value()) {
477 std::string serviceName =
"";
478 if (
auto f = content.find(
"servicePort"); f != content.end())
480 auto svcIter = activeServices.find(serviceName);
481 if (svcIter == activeServices.end()) {
484 if (svcIter = activeServices.find(
""); svcIter == activeServices.end())
485 throw std::runtime_error(
486 "Malformed manifest: could not find active service '" +
489 services::Service *svc = svcIter->second;
491 std::string typeName = content.at(
"typeID");
492 auto type = getType(typeName);
494 throw std::runtime_error(
495 "Malformed manifest: could not find port type '" + typeName +
"'");
496 const BundleType *bundleType =
dynamic_cast<const BundleType *
>(*type);
498 throw std::runtime_error(
"Malformed manifest: type '" + typeName +
499 "' is not a bundle type");
503 BundlePort *svcPort = svc->getPort(idPath, bundleType);
505 ret.emplace_back(svcPort);
514const Type *
parseType(
const nlohmann::json &typeJson, Context &ctxt);
516BundleType *parseBundleType(
const nlohmann::json &typeJson, Context &cache) {
517 assert(typeJson.at(
"mnemonic") ==
"bundle");
519 std::vector<std::tuple<std::string, BundleType::Direction, const Type *>>
521 for (
auto &chanJson : typeJson[
"channels"]) {
522 std::string dirStr = chanJson.at(
"direction");
523 BundleType::Direction dir;
525 dir = BundleType::Direction::To;
526 else if (dirStr ==
"from")
527 dir = BundleType::Direction::From;
529 throw std::runtime_error(
"Malformed manifest: unknown direction '" +
531 channels.emplace_back(chanJson.at(
"name"), dir,
534 return new BundleType(typeJson.at(
"id"), channels);
537ChannelType *parseChannelType(
const nlohmann::json &typeJson, Context &cache) {
538 assert(typeJson.at(
"mnemonic") ==
"channel");
539 return new ChannelType(typeJson.at(
"id"),
543Type *parseInt(
const nlohmann::json &typeJson, Context &cache) {
544 assert(typeJson.at(
"mnemonic") ==
"int");
545 std::string sign = typeJson.at(
"signedness");
546 uint64_t width = typeJson.at(
"hwBitwidth");
547 Type::ID
id = typeJson.at(
"id");
549 if (sign ==
"signed")
550 return new SIntType(
id, width);
551 else if (sign ==
"unsigned")
552 return new UIntType(
id, width);
553 else if (sign ==
"signless" && width == 0)
555 return new VoidType(
id);
556 else if (sign ==
"signless" && width > 0)
557 return new BitsType(
id, width);
559 throw std::runtime_error(
"Malformed manifest: unknown sign '" + sign +
"'");
562StructType *parseStruct(
const nlohmann::json &typeJson, Context &cache) {
563 assert(typeJson.at(
"mnemonic") ==
"struct");
564 std::vector<std::pair<std::string, const Type *>> fields;
565 for (
auto &fieldJson : typeJson[
"fields"])
566 fields.emplace_back(fieldJson.at(
"name"),
568 return new StructType(typeJson.at(
"id"), fields);
571ArrayType *parseArray(
const nlohmann::json &typeJson, Context &cache) {
572 assert(typeJson.at(
"mnemonic") ==
"array");
573 uint64_t size = typeJson.at(
"size");
574 return new ArrayType(typeJson.at(
"id"),
575 parseType(typeJson.at(
"element"), cache), size);
578using TypeParser = std::function<Type *(
const nlohmann::json &, Context &)>;
579const std::map<std::string_view, TypeParser> typeParsers = {
580 {
"bundle", parseBundleType},
581 {
"channel", parseChannelType},
582 {
"std::any", [](
const nlohmann::json &typeJson,
583 Context &cache) {
return new AnyType(typeJson.at(
"id")); }},
585 {
"struct", parseStruct},
586 {
"array", parseArray},
591const Type *
parseType(
const nlohmann::json &typeJson, Context &cache) {
593 if (typeJson.is_string())
594 id = typeJson.get<std::string>();
596 id = typeJson.at(
"id");
597 if (std::optional<const Type *> t = cache.getType(
id))
599 if (typeJson.is_string())
600 throw std::runtime_error(
"malformed manifest: unknown type '" +
id +
"'");
603 std::string mnemonic = typeJson.at(
"mnemonic");
604 auto f = typeParsers.find(mnemonic);
605 if (f != typeParsers.end())
606 t = f->second(typeJson, cache);
612 cache.registerType(t);
618 return ::parseType(typeJson, ctxt);
622 for (
auto &typeJson : typesJson)
623 _typeTable.push_back(
parseType(typeJson));
630Manifest::Manifest(Context &ctxt,
const std::string &jsonManifest)
631 : impl(new
Impl(ctxt, jsonManifest)) {}
633Manifest::~Manifest() {
delete impl; }
635uint32_t Manifest::getApiVersion()
const {
636 return impl->at(
"apiVersion").get<uint32_t>();
639std::vector<ModuleInfo> Manifest::getModuleInfos()
const {
640 std::vector<ModuleInfo> ret;
641 for (
auto &[symbol, info] : impl->getSymbolInfo())
646Accelerator *Manifest::buildAccelerator(AcceleratorConnection &acc)
const {
648 return acc.takeOwnership(impl->buildAccelerator(acc));
649 }
catch (
const std::exception &e) {
650 std::string msg =
"malformed manifest: " + std::string(e.what());
651 if (getApiVersion() == 0)
652 msg +=
" (schema version 0 is not considered stable)";
653 throw std::runtime_error(msg);
657const std::vector<const Type *> &Manifest::getTypeTable()
const {
658 return impl->getTypeTable();
667 auto printAny = [&os](std::any a) {
668 if (
auto *c = std::any_cast<Constant>(&a))
669 a = std::any_cast<Constant>(a).value;
671 const std::type_info &t = a.type();
672 if (t ==
typeid(std::string))
673 os << std::any_cast<std::string>(a);
674 else if (t ==
typeid(int64_t))
675 os << std::any_cast<int64_t>(a);
676 else if (t ==
typeid(uint64_t))
677 os << std::any_cast<uint64_t>(a);
678 else if (t ==
typeid(
double))
679 os << std::any_cast<double>(a);
680 else if (t ==
typeid(
bool))
681 os << std::any_cast<bool>(a);
682 else if (t ==
typeid(std::nullptr_t))
689 os << *m.name <<
" ";
691 os << *m.version <<
" ";
692 if (m.repo || m.commitHash) {
697 os <<
"@" << *m.commitHash;
701 os <<
": " << *m.summary;
704 if (!m.constants.empty()) {
705 os <<
" Constants:\n";
706 for (
auto &e : m.constants) {
707 os <<
" " << e.first <<
": ";
713 if (!m.extra.empty()) {
714 os <<
" Extra metadata:\n";
715 for (
auto &e : m.extra) {
716 os <<
" " << e.first <<
": ";
727 ret.insert(ret.end(), b.begin(), b.end());
731std::string AppIDPath::toStr()
const {
732 std::ostringstream os;
743 if (a.size() != b.size())
744 return a.size() < b.size();
745 for (
size_t i = 0, e = a.size(); i < e; ++i)
755 os <<
"[" << *
id.idx <<
"]";
758std::ostream &
operator<<(std::ostream &os,
const AppIDPath &path) {
759 for (
size_t i = 0, e = path.size(); i < e; ++i) {
assert(baseType &&"element must be base type")
static ParseResult parseType(Type &result, StringRef name, AsmParser &parser)
Parse a type defined by this dialect.
static std::optional< AppID > parseID(const nlohmann::json &jsonID)
AcceleratorConnection::ServiceTable ServiceTable
static AppIDPath parseIDPath(const nlohmann::json &jsonIDPath)
static AppID parseIDChecked(const nlohmann::json &jsonID)
static ServicePortDesc parseServicePort(const nlohmann::json &jsonPort)
static llvm::raw_ostream & operator<<(llvm::raw_ostream &os, const bool a)
const std::vector< const Type * > & getTypeTable() const
Get the ordered list of types from the manifest.
std::optional< const Type * > getType(Type::ID id) const
const Type * parseType(const nlohmann::json &typeJson)
auto at(const std::string &key) const
void populateTypes(const nlohmann::json &typesJson)
Parse all the types and populate the types table.
std::optional< ModuleInfo > getModInfo(const nlohmann::json &) const
void createEngine(AcceleratorConnection &, AppIDPath appID, const nlohmann::json &) const
TODO: Hack.
std::vector< std::unique_ptr< BundlePort > > getBundlePorts(AcceleratorConnection &acc, AppIDPath idPath, const ServiceTable &activeServices, const nlohmann::json &instJson) const
Get the bundle ports for the instance at 'idPath' and specified in 'instJson'.
std::vector< std::unique_ptr< Instance > > getChildInstances(AppIDPath idPath, AcceleratorConnection &acc, const ServiceTable &activeServices, const nlohmann::json &instJson) const
Build the set of child instances (recursively) for the module instance description.
nlohmann::json manifestJson
std::vector< services::Service * > getServices(AppIDPath idPath, AcceleratorConnection &, const nlohmann::json &, ServiceTable &activeServices) const
Get all the services in the description of an instance.
std::map< std::string, const ModuleInfo > symbolInfoCache
services::Service * getService(AppIDPath idPath, AcceleratorConnection &, const nlohmann::json &, ServiceTable &activeServices, bool isEngine=false) const
Get a Service for the service specified in 'json'.
const std::map< std::string, const ModuleInfo > & getSymbolInfo() const
std::any getAny(const nlohmann::json &value) const
Convert the json value to a 'std::any', which can be exposed outside of this file.
void parseModuleMetadata(ModuleInfo &info, const nlohmann::json &mod) const
std::unique_ptr< Accelerator > buildAccelerator(AcceleratorConnection &acc) const
Build a dynamic API for the Accelerator connection 'acc' based on the manifest stored herein.
void parseModuleConsts(ModuleInfo &info, const nlohmann::json &mod) const
void scanServiceDecls(AcceleratorConnection &, const nlohmann::json &, ServiceTable &) const
Go through the "service_decls" section of the manifest and populate the services table as appropriate...
std::vector< const Type * > _typeTable
std::unique_ptr< Instance > getChildInstance(AppIDPath idPath, AcceleratorConnection &acc, ServiceTable activeServices, const nlohmann::json &childJson) const
Get a single child instance.
Impl(int port)
Start a server on the given port. -1 means to let the OS pick a port.
std::optional< uint32_t > idx