19#include <nlohmann/json.hpp>
31 friend class ::esi::Manifest;
39 std::optional<ModuleInfo>
getModInfo(
const nlohmann::json &)
const;
44 ServiceTable &)
const;
46 void createEngine(AcceleratorConnection &, AppIDPath appID,
47 const nlohmann::json &)
const;
52 services::Service *
getService(AppIDPath idPath, AcceleratorConnection &,
53 const nlohmann::json &,
54 ServiceTable &activeServices,
55 bool isEngine =
false)
const;
59 std::vector<services::Service *>
60 getServices(AppIDPath idPath, AcceleratorConnection &,
const nlohmann::json &,
61 ServiceTable &activeServices)
const;
65 std::vector<std::unique_ptr<BundlePort>>
67 const ServiceTable &activeServices,
68 const nlohmann::json &instJson)
const;
72 std::vector<std::unique_ptr<Instance>>
74 const ServiceTable &activeServices,
75 const nlohmann::json &instJson)
const;
79 std::unique_ptr<Instance>
81 ServiceTable activeServices,
82 const nlohmann::json &childJson)
const;
92 std::unique_ptr<Accelerator>
95 const Type *
parseType(
const nlohmann::json &typeJson);
105 std::optional<const Type *>
getType(Type::ID
id)
const {
106 return ctxt.getType(
id);
109 std::any
getAny(
const nlohmann::json &value)
const;
123static std::optional<AppID>
parseID(
const nlohmann::json &jsonID) {
124 if (!jsonID.is_object())
126 std::optional<uint32_t> idx;
127 if (jsonID.contains(
"index"))
128 idx = jsonID.at(
"index").get<uint32_t>();
129 if (jsonID.contains(
"name") && jsonID.size() <= 2)
130 return AppID(jsonID.at(
"name").get<std::string>(), idx);
135 std::optional<AppID>
id =
parseID(jsonID);
137 throw std::runtime_error(
"Malformed manifest: invalid appID");
142 for (
auto &idJson : jsonIDPath)
148 return ServicePortDesc{jsonPort.at(
"serviceName").get<std::string>(),
149 jsonPort.at(
"port").get<std::string>()};
155 auto getObject = [
this](
const nlohmann::json &
json) -> std::any {
156 std::map<std::string, std::any> ret;
157 for (
auto &e :
json.items())
158 ret[e.key()] =
getAny(e.value());
161 if (ret.size() != 2 || !ret.contains(
"type") || !ret.contains(
"value"))
163 std::any value = ret.at(
"value");
164 std::any typeID = ret.at(
"type");
165 if (typeID.type() !=
typeid(std::string))
167 std::optional<const Type *> type =
168 getType(std::any_cast<std::string>(type));
173 return Constant{value, type};
176 auto getArray = [
this](
const nlohmann::json &
json) -> std::any {
177 std::vector<std::any> ret;
183 auto getValue = [&](
const nlohmann::json &innerValue) -> std::any {
184 if (innerValue.is_string())
185 return innerValue.get<std::string>();
186 else if (innerValue.is_number_unsigned())
187 return innerValue.get<uint64_t>();
188 else if (innerValue.is_number_integer())
189 return innerValue.get<int64_t>();
190 else if (innerValue.is_number_float())
191 return innerValue.get<
double>();
192 else if (innerValue.is_boolean())
193 return innerValue.get<
bool>();
194 else if (innerValue.is_null())
195 return innerValue.get<std::nullptr_t>();
196 else if (innerValue.is_object())
197 return getObject(innerValue);
198 else if (innerValue.is_array())
199 return getArray(innerValue);
201 throw std::runtime_error(
"Unknown type in manifest: " +
205 std::optional<AppID> appid =
parseID(value);
208 if (!value.is_object() || !value.contains(
"type") || !value.contains(
"value"))
209 return getValue(value);
210 return Constant{getValue(value.at(
"value")),
getType(value.at(
"type"))};
214 const nlohmann::json &mod)
const {
215 for (
auto &extra : mod.items())
216 if (extra.key() !=
"name" && extra.key() !=
"summary" &&
217 extra.key() !=
"version" && extra.key() !=
"repo" &&
218 extra.key() !=
"commitHash")
219 info.extra[extra.key()] = getAny(extra.value());
221 auto value = [&](
const std::string &key) -> std::optional<std::string> {
222 auto f = mod.find(key);
227 info.name = value(
"name");
228 info.summary = value(
"summary");
229 info.version = value(
"version");
230 info.repo = value(
"repo");
231 info.commitHash = value(
"commitHash");
235 const nlohmann::json &mod)
const {
236 for (
auto &item : mod.items()) {
237 std::any value = getAny(item.value());
238 auto *c = std::any_cast<Constant>(&value);
240 info.constants[item.key()] = *c;
243 info.constants[item.key()] = Constant{value, std::nullopt};
253 manifestJson = nlohmann::ordered_json::parse(manifestStr);
262 if (mod.contains(
"symInfo"))
264 if (mod.contains(
"symConsts"))
268 }
catch (
const std::exception &e) {
269 std::string msg =
"malformed manifest: " + std::string(e.what());
271 msg +=
" (schema version 0 is not considered stable)";
272 throw std::runtime_error(msg);
276std::unique_ptr<Accelerator>
278 ServiceTable activeSvcs;
280 auto designJson = manifestJson.at(
"design");
284 auto enginesIter = designJson.find(
"engines");
285 if (enginesIter != designJson.end())
286 for (
auto &engineDesc : enginesIter.value())
287 createEngine(acc, {}, engineDesc);
290 auto svcDecls = manifestJson.at(
"serviceDeclarations");
291 scanServiceDecls(acc, svcDecls, activeSvcs);
294 std::vector<services::Service *> services =
295 getServices({}, acc, designJson, activeSvcs);
298 auto ports = getBundlePorts(acc, {}, activeSvcs, designJson);
300 return std::make_unique<Accelerator>(
301 getModInfo(designJson),
302 getChildInstances({}, acc, activeSvcs, designJson), services, ports);
305std::optional<ModuleInfo>
307 auto instOfIter =
json.find(
"instOf");
308 if (instOfIter ==
json.end())
310 auto f = symbolInfoCache.find(instOfIter.value());
311 if (f != symbolInfoCache.end())
319 const nlohmann::json &eng)
const {
321 getService(idPath, acc, eng, dummy,
true);
325 const nlohmann::json &svcDecls,
326 ServiceTable &activeServices)
const {
327 for (
auto &svcDecl : svcDecls) {
329 ServiceImplDetails svcDetails;
330 for (
auto &
detail : svcDecl.items())
334 auto serviceNameIter = svcDecl.find(
"serviceName");
335 std::string serviceName;
336 if (serviceNameIter != svcDecl.end())
337 serviceName = serviceNameIter.value();
338 services::Service::Type svcId =
339 services::ServiceRegistry::lookupServiceType(serviceName);
340 auto svc = acc.getService(svcId, {},
"",
343 activeServices[svcDecl.at(
"symbol")] = svc;
347std::vector<std::unique_ptr<Instance>>
349 const ServiceTable &activeServices,
350 const nlohmann::json &instJson)
const {
351 std::vector<std::unique_ptr<Instance>> ret;
352 auto childrenIter = instJson.find(
"children");
353 if (childrenIter == instJson.end())
355 for (
auto &child : childrenIter.value())
356 ret.emplace_back(getChildInstance(idPath, acc, activeServices, child));
360std::unique_ptr<Instance>
362 ServiceTable activeServices,
363 const nlohmann::json &child)
const {
365 idPath.push_back(childID);
367 std::vector<services::Service *> services =
368 getServices(idPath, acc, child, activeServices);
370 auto children = getChildInstances(idPath, acc, activeServices, child);
371 auto ports = getBundlePorts(acc, idPath, activeServices, child);
372 return std::make_unique<Instance>(
parseIDChecked(child.at(
"appID")),
373 getModInfo(child), std::move(children),
378 AcceleratorConnection &acc,
379 const nlohmann::json &svcJson,
380 ServiceTable &activeServices,
381 bool isEngine)
const {
384 idPath.push_back(
id);
387 HWClientDetails clientDetails;
388 for (
auto &client : svcJson.at(
"clientDetails")) {
389 HWClientDetail clientDetail;
390 for (
auto &
detail : client.items()) {
391 if (
detail.key() ==
"relAppIDPath")
393 else if (
detail.key() ==
"servicePort")
395 else if (
detail.key() ==
"channelAssignments") {
396 for (
auto &chan :
detail.value().items()) {
397 ChannelAssignment chanAssign;
398 for (
auto &assign : chan.value().items())
399 if (assign.key() ==
"type")
400 chanAssign.type = assign.value();
402 chanAssign.implOptions[assign.key()] = getAny(assign.value());
403 clientDetail.channelAssignments[chan.key()] = chanAssign;
406 clientDetail.implOptions[
detail.key()] = getAny(
detail.value());
408 clientDetails.push_back(clientDetail);
412 ServiceImplDetails svcDetails;
413 std::string implName;
415 for (
auto &
detail : svcJson.items()) {
416 if (
detail.key() ==
"appID" ||
detail.key() ==
"clientDetails")
418 if (
detail.key() ==
"serviceImplName")
419 implName =
detail.value();
420 else if (
detail.key() ==
"service")
421 service =
detail.value().get<std::string>();
427 services::Service *svc =
nullptr;
428 auto activeServiceIter = activeServices.find(service);
429 if (activeServiceIter != activeServices.end()) {
430 services::Service::Type svcType =
431 services::ServiceRegistry::lookupServiceType(
432 activeServiceIter->second->getServiceSymbol());
433 svc = activeServiceIter->second->getChildService(svcType, idPath, implName,
434 svcDetails, clientDetails);
436 services::Service::Type svcType =
437 services::ServiceRegistry::lookupServiceType(service);
439 acc.createEngine(implName, idPath, svcDetails, clientDetails);
442 acc.getService(svcType, idPath, implName, svcDetails, clientDetails);
447 activeServices[service] = svc;
451std::vector<services::Service *>
453 const nlohmann::json &svcsJson,
454 ServiceTable &activeServices)
const {
455 std::vector<services::Service *> ret;
456 auto svcsIter = svcsJson.find(
"services");
457 if (svcsIter == svcsJson.end())
460 for (
auto &svc : svcsIter.value())
461 ret.emplace_back(getService(idPath, acc, svc, activeServices));
465std::vector<std::unique_ptr<BundlePort>>
467 const ServiceTable &activeServices,
468 const nlohmann::json &instJson)
const {
469 std::vector<std::unique_ptr<BundlePort>> ret;
470 auto clientPortsIter = instJson.find(
"clientPorts");
471 if (clientPortsIter == instJson.end())
474 for (
auto &content : clientPortsIter.value()) {
476 std::string serviceName =
"";
477 if (
auto f = content.find(
"servicePort"); f != content.end())
479 auto svcIter = activeServices.find(serviceName);
480 if (svcIter == activeServices.end()) {
483 if (svcIter = activeServices.find(
""); svcIter == activeServices.end())
484 throw std::runtime_error(
485 "Malformed manifest: could not find active service '" +
488 services::Service *svc = svcIter->second;
490 std::string typeName = content.at(
"typeID");
491 auto type = getType(typeName);
493 throw std::runtime_error(
494 "Malformed manifest: could not find port type '" + typeName +
"'");
495 const BundleType *bundleType =
dynamic_cast<const BundleType *
>(*type);
497 throw std::runtime_error(
"Malformed manifest: type '" + typeName +
498 "' is not a bundle type");
502 BundlePort *svcPort = svc->getPort(idPath, bundleType);
504 ret.emplace_back(svcPort);
515BundleType *parseBundleType(
const nlohmann::json &typeJson,
Context &cache) {
516 assert(typeJson.at(
"mnemonic") ==
"bundle");
518 std::vector<std::tuple<std::string, BundleType::Direction, const Type *>>
520 for (
auto &chanJson : typeJson[
"channels"]) {
521 std::string dirStr = chanJson.at(
"direction");
522 BundleType::Direction dir;
524 dir = BundleType::Direction::To;
525 else if (dirStr ==
"from")
526 dir = BundleType::Direction::From;
528 throw std::runtime_error(
"Malformed manifest: unknown direction '" +
530 channels.emplace_back(chanJson.at(
"name"), dir,
533 return new BundleType(typeJson.at(
"id"), channels);
536ChannelType *parseChannelType(
const nlohmann::json &typeJson,
Context &cache) {
537 assert(typeJson.at(
"mnemonic") ==
"channel");
538 return new ChannelType(typeJson.at(
"id"),
542Type *parseInt(
const nlohmann::json &typeJson,
Context &cache) {
543 assert(typeJson.at(
"mnemonic") ==
"int");
544 std::string sign = typeJson.at(
"signedness");
545 uint64_t width = typeJson.at(
"hwBitwidth");
546 Type::ID
id = typeJson.at(
"id");
548 if (sign ==
"signed")
549 return new SIntType(
id, width);
550 else if (sign ==
"unsigned")
551 return new UIntType(
id, width);
552 else if (sign ==
"signless" && width == 0)
554 return new VoidType(
id);
555 else if (sign ==
"signless" && width > 0)
556 return new BitsType(
id, width);
558 throw std::runtime_error(
"Malformed manifest: unknown sign '" + sign +
"'");
561StructType *parseStruct(
const nlohmann::json &typeJson,
Context &cache) {
562 assert(typeJson.at(
"mnemonic") ==
"struct");
563 std::vector<std::pair<std::string, const Type *>> fields;
564 for (
auto &fieldJson : typeJson[
"fields"])
565 fields.emplace_back(fieldJson.at(
"name"),
567 return new StructType(typeJson.at(
"id"), fields);
570ArrayType *parseArray(
const nlohmann::json &typeJson,
Context &cache) {
571 assert(typeJson.at(
"mnemonic") ==
"array");
572 uint64_t size = typeJson.at(
"size");
573 return new ArrayType(typeJson.at(
"id"),
574 parseType(typeJson.at(
"element"), cache), size);
577using TypeParser = std::function<Type *(
const nlohmann::json &,
Context &)>;
578const std::map<std::string_view, TypeParser> typeParsers = {
579 {
"bundle", parseBundleType},
580 {
"channel", parseChannelType},
581 {
"std::any", [](
const nlohmann::json &typeJson,
582 Context &cache) {
return new AnyType(typeJson.at(
"id")); }},
584 {
"struct", parseStruct},
585 {
"array", parseArray},
592 if (typeJson.is_string())
593 id = typeJson.get<std::string>();
595 id = typeJson.at(
"id");
596 if (std::optional<const Type *> t = cache.getType(
id))
598 if (typeJson.is_string())
599 throw std::runtime_error(
"malformed manifest: unknown type '" +
id +
"'");
602 std::string mnemonic = typeJson.at(
"mnemonic");
603 auto f = typeParsers.find(mnemonic);
604 if (f != typeParsers.end())
605 t = f->second(typeJson, cache);
611 cache.registerType(t);
617 return ::parseType(typeJson, ctxt);
621 for (
auto &typeJson : typesJson)
622 _typeTable.push_back(
parseType(typeJson));
629Manifest::Manifest(
Context &ctxt,
const std::string &jsonManifest)
630 : impl(new
Impl(ctxt, jsonManifest)) {}
632Manifest::~Manifest() {
delete impl; }
634uint32_t Manifest::getApiVersion()
const {
635 return impl->at(
"apiVersion").get<uint32_t>();
638std::vector<ModuleInfo> Manifest::getModuleInfos()
const {
639 std::vector<ModuleInfo> ret;
640 for (
auto &[symbol, info] : impl->getSymbolInfo())
645Accelerator *Manifest::buildAccelerator(AcceleratorConnection &acc)
const {
647 return acc.takeOwnership(impl->buildAccelerator(acc));
648 }
catch (
const std::exception &e) {
649 std::string msg =
"malformed manifest: " + std::string(e.what());
650 if (getApiVersion() == 0)
651 msg +=
" (schema version 0 is not considered stable)";
652 throw std::runtime_error(msg);
656const std::vector<const Type *> &Manifest::getTypeTable()
const {
657 return impl->getTypeTable();
666 auto printAny = [&os](std::any a) {
667 if (std::any_cast<Constant>(&a))
668 a = std::any_cast<Constant>(a).value;
670 const std::type_info &t = a.type();
671 if (t ==
typeid(std::string))
672 os << std::any_cast<std::string>(a);
673 else if (t ==
typeid(int64_t))
674 os << std::any_cast<int64_t>(a);
675 else if (t ==
typeid(uint64_t))
676 os << std::any_cast<uint64_t>(a);
677 else if (t ==
typeid(
double))
678 os << std::any_cast<double>(a);
679 else if (t ==
typeid(
bool))
680 os << std::any_cast<bool>(a);
681 else if (t ==
typeid(std::nullptr_t))
688 os << *m.name <<
" ";
690 os << *m.version <<
" ";
691 if (m.name || m.version)
693 if (m.repo || m.commitHash) {
694 os <<
" Version control: ";
698 os <<
"@" << *m.commitHash;
702 os <<
" " << *m.summary;
705 if (!m.constants.empty()) {
706 os <<
" Constants:\n";
707 for (
auto &e : m.constants) {
708 os <<
" " << e.first <<
": ";
714 if (!m.extra.empty()) {
715 os <<
" Extra metadata:\n";
716 for (
auto &e : m.extra) {
717 os <<
" " << e.first <<
": ";
728 ret.insert(ret.end(), b.begin(), b.end());
739std::string AppIDPath::toStr()
const {
740 std::ostringstream os;
751 if (a.size() != b.size())
752 return a.size() < b.size();
753 for (
size_t i = 0, e = a.size(); i < e; ++i)
762 os <<
"[" << *
id.idx <<
"]";
766 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)
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)
This class provides a thread-safe interface to access the analysis results.
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