20#pragma GCC diagnostic push
21#pragma GCC diagnostic ignored "-Wcovered-switch-default"
23#include <nlohmann/json.hpp>
25#pragma GCC diagnostic pop
38 friend class ::esi::Manifest;
46 std::optional<ModuleInfo>
getModInfo(
const nlohmann::json &)
const;
51 ServiceTable &)
const;
53 void createEngine(AcceleratorConnection &, AppIDPath appID,
54 const nlohmann::json &)
const;
59 services::Service *
getService(AppIDPath idPath, AcceleratorConnection &,
60 const nlohmann::json &,
61 ServiceTable &activeServices,
62 bool isEngine =
false)
const;
66 std::vector<services::Service *>
67 getServices(AppIDPath idPath, AcceleratorConnection &,
const nlohmann::json &,
68 ServiceTable &activeServices)
const;
72 std::vector<std::unique_ptr<BundlePort>>
74 const ServiceTable &activeServices,
75 const nlohmann::json &instJson)
const;
79 std::vector<std::unique_ptr<Instance>>
81 const ServiceTable &activeServices,
82 const nlohmann::json &instJson)
const;
86 std::unique_ptr<Instance>
88 ServiceTable activeServices,
89 const nlohmann::json &childJson)
const;
99 std::unique_ptr<Accelerator>
102 const Type *
parseType(
const nlohmann::json &typeJson);
112 std::optional<const Type *>
getType(Type::ID
id)
const {
113 return ctxt.getType(
id);
116 std::any
getAny(
const nlohmann::json &value)
const;
130static std::optional<AppID>
parseID(
const nlohmann::json &jsonID) {
131 if (!jsonID.is_object())
133 std::optional<uint32_t> idx;
134 if (jsonID.contains(
"index"))
135 idx = jsonID.at(
"index").get<uint32_t>();
136 if (jsonID.contains(
"name") && jsonID.size() <= 2)
137 return AppID(jsonID.at(
"name").get<std::string>(), idx);
142 std::optional<AppID>
id =
parseID(jsonID);
144 throw std::runtime_error(
"Malformed manifest: invalid appID");
149 for (
auto &idJson : jsonIDPath)
155 return ServicePortDesc{jsonPort.at(
"serviceName").get<std::string>(),
156 jsonPort.at(
"port").get<std::string>()};
162 auto getObject = [
this](
const nlohmann::json &
json) -> std::any {
163 std::map<std::string, std::any> ret;
164 for (
auto &e :
json.items())
165 ret[e.key()] =
getAny(e.value());
168 if (ret.size() != 2 || !ret.contains(
"type") || !ret.contains(
"value"))
170 std::any value = ret.at(
"value");
171 std::any typeID = ret.at(
"type");
172 if (typeID.type() !=
typeid(std::string))
174 std::optional<const Type *> type =
175 getType(std::any_cast<std::string>(type));
180 return Constant{value, type};
183 auto getArray = [
this](
const nlohmann::json &
json) -> std::any {
184 std::vector<std::any> ret;
190 auto getValue = [&](
const nlohmann::json &innerValue) -> std::any {
191 if (innerValue.is_string())
192 return innerValue.get<std::string>();
193 else if (innerValue.is_number_unsigned())
194 return innerValue.get<uint64_t>();
195 else if (innerValue.is_number_integer())
196 return innerValue.get<int64_t>();
197 else if (innerValue.is_number_float())
198 return innerValue.get<
double>();
199 else if (innerValue.is_boolean())
200 return innerValue.get<
bool>();
201 else if (innerValue.is_null())
202 return innerValue.get<std::nullptr_t>();
203 else if (innerValue.is_object())
204 return getObject(innerValue);
205 else if (innerValue.is_array())
206 return getArray(innerValue);
208 throw std::runtime_error(
"Unknown type in manifest: " +
212 std::optional<AppID> appid =
parseID(value);
215 if (!value.is_object() || !value.contains(
"type") || !value.contains(
"value"))
216 return getValue(value);
217 return Constant{getValue(value.at(
"value")),
getType(value.at(
"type"))};
221 const nlohmann::json &mod)
const {
222 for (
auto &extra : mod.items())
223 if (extra.key() !=
"name" && extra.key() !=
"summary" &&
224 extra.key() !=
"version" && extra.key() !=
"repo" &&
225 extra.key() !=
"commitHash")
226 info.extra[extra.key()] = getAny(extra.value());
228 auto value = [&](
const std::string &key) -> std::optional<std::string> {
229 auto f = mod.find(key);
234 info.name = value(
"name");
235 info.summary = value(
"summary");
236 info.version = value(
"version");
237 info.repo = value(
"repo");
238 info.commitHash = value(
"commitHash");
242 const nlohmann::json &mod)
const {
243 for (
auto &item : mod.items()) {
244 std::any value = getAny(item.value());
245 auto *c = std::any_cast<Constant>(&value);
247 info.constants[item.key()] = *c;
250 info.constants[item.key()] = Constant{value, std::nullopt};
260 manifestJson = nlohmann::ordered_json::parse(manifestStr);
269 if (mod.contains(
"symInfo"))
271 if (mod.contains(
"symConsts"))
275 }
catch (
const std::exception &e) {
276 std::string msg =
"malformed manifest: " + std::string(e.what());
278 msg +=
" (schema version 0 is not considered stable)";
279 throw std::runtime_error(msg);
283std::unique_ptr<Accelerator>
285 ServiceTable activeSvcs;
287 auto designJson = manifestJson.at(
"design");
291 auto enginesIter = designJson.find(
"engines");
292 if (enginesIter != designJson.end())
293 for (
auto &engineDesc : enginesIter.value())
294 createEngine(acc, {}, engineDesc);
297 auto svcDecls = manifestJson.at(
"serviceDeclarations");
298 scanServiceDecls(acc, svcDecls, activeSvcs);
301 std::vector<services::Service *> services =
302 getServices({}, acc, designJson, activeSvcs);
305 auto ports = getBundlePorts(acc, {}, activeSvcs, designJson);
307 return std::make_unique<Accelerator>(
308 getModInfo(designJson),
309 getChildInstances({}, acc, activeSvcs, designJson), services,
313std::optional<ModuleInfo>
315 auto instOfIter =
json.find(
"instOf");
316 if (instOfIter ==
json.end())
318 auto f = symbolInfoCache.find(instOfIter.value());
319 if (f != symbolInfoCache.end())
327 const nlohmann::json &eng)
const {
329 getService(idPath, acc, eng, dummy,
true);
333 const nlohmann::json &svcDecls,
334 ServiceTable &activeServices)
const {
335 for (
auto &svcDecl : svcDecls) {
337 ServiceImplDetails svcDetails;
338 for (
auto &
detail : svcDecl.items())
342 auto serviceNameIter = svcDecl.find(
"serviceName");
343 std::string serviceName;
344 if (serviceNameIter != svcDecl.end())
345 serviceName = serviceNameIter.value();
346 services::Service::Type svcId =
347 services::ServiceRegistry::lookupServiceType(serviceName);
348 auto svc = acc.getService(svcId, {},
"",
351 activeServices[svcDecl.at(
"symbol")] = svc;
355std::vector<std::unique_ptr<Instance>>
357 const ServiceTable &activeServices,
358 const nlohmann::json &instJson)
const {
359 std::vector<std::unique_ptr<Instance>> ret;
360 auto childrenIter = instJson.find(
"children");
361 if (childrenIter == instJson.end())
363 for (
auto &child : childrenIter.value())
364 ret.emplace_back(getChildInstance(idPath, acc, activeServices, child));
368std::unique_ptr<Instance>
370 ServiceTable activeServices,
371 const nlohmann::json &child)
const {
373 idPath.push_back(childID);
375 std::vector<services::Service *> services =
376 getServices(idPath, acc, child, activeServices);
378 auto children = getChildInstances(idPath, acc, activeServices, child);
379 auto ports = getBundlePorts(acc, idPath, activeServices, child);
380 return std::make_unique<Instance>(
parseIDChecked(child.at(
"appID")),
381 getModInfo(child), std::move(children),
382 services, std::move(ports));
386 AcceleratorConnection &acc,
387 const nlohmann::json &svcJson,
388 ServiceTable &activeServices,
389 bool isEngine)
const {
392 idPath.push_back(
id);
395 HWClientDetails clientDetails;
396 for (
auto &client : svcJson.at(
"clientDetails")) {
397 HWClientDetail clientDetail;
398 for (
auto &
detail : client.items()) {
399 if (
detail.key() ==
"relAppIDPath")
401 else if (
detail.key() ==
"servicePort")
403 else if (
detail.key() ==
"channelAssignments") {
404 for (
auto &chan :
detail.value().items()) {
405 ChannelAssignment chanAssign;
406 for (
auto &assign : chan.value().items())
407 if (assign.key() ==
"type")
408 chanAssign.type = assign.value();
410 chanAssign.implOptions[assign.key()] = getAny(assign.value());
411 clientDetail.channelAssignments[chan.key()] = chanAssign;
414 clientDetail.implOptions[
detail.key()] = getAny(
detail.value());
416 clientDetails.push_back(clientDetail);
420 ServiceImplDetails svcDetails;
421 std::string implName;
423 for (
auto &
detail : svcJson.items()) {
424 if (
detail.key() ==
"appID" ||
detail.key() ==
"clientDetails")
426 if (
detail.key() ==
"serviceImplName")
427 implName =
detail.value();
428 else if (
detail.key() ==
"service")
429 service =
detail.value().get<std::string>();
435 services::Service *svc =
nullptr;
436 auto activeServiceIter = activeServices.find(service);
437 if (activeServiceIter != activeServices.end()) {
438 services::Service::Type svcType =
439 services::ServiceRegistry::lookupServiceType(
440 activeServiceIter->second->getServiceSymbol());
441 svc = activeServiceIter->second->getChildService(svcType, idPath, implName,
442 svcDetails, clientDetails);
444 services::Service::Type svcType =
445 services::ServiceRegistry::lookupServiceType(service);
447 acc.createEngine(implName, idPath, svcDetails, clientDetails);
450 acc.getService(svcType, idPath, implName, svcDetails, clientDetails);
455 activeServices[service] = svc;
459std::vector<services::Service *>
461 const nlohmann::json &svcsJson,
462 ServiceTable &activeServices)
const {
463 std::vector<services::Service *> ret;
464 auto svcsIter = svcsJson.find(
"services");
465 if (svcsIter == svcsJson.end())
468 for (
auto &svc : svcsIter.value())
469 ret.emplace_back(getService(idPath, acc, svc, activeServices));
473std::vector<std::unique_ptr<BundlePort>>
475 const ServiceTable &activeServices,
476 const nlohmann::json &instJson)
const {
477 std::vector<std::unique_ptr<BundlePort>> ret;
478 auto clientPortsIter = instJson.find(
"clientPorts");
479 if (clientPortsIter == instJson.end())
482 for (
auto &content : clientPortsIter.value()) {
484 std::string serviceName =
"";
485 if (
auto f = content.find(
"servicePort"); f != content.end())
487 auto svcIter = activeServices.find(serviceName);
488 if (svcIter == activeServices.end()) {
491 if (svcIter = activeServices.find(
""); svcIter == activeServices.end())
492 throw std::runtime_error(
493 "Malformed manifest: could not find active service '" +
496 services::Service *svc = svcIter->second;
498 std::string typeName = content.at(
"typeID");
499 auto type = getType(typeName);
501 throw std::runtime_error(
502 "Malformed manifest: could not find port type '" + typeName +
"'");
503 const BundleType *bundleType =
dynamic_cast<const BundleType *
>(*type);
505 throw std::runtime_error(
"Malformed manifest: type '" + typeName +
506 "' is not a bundle type");
510 BundlePort *svcPort = svc->getPort(idPath, bundleType);
512 ret.emplace_back(svcPort);
523BundleType *parseBundleType(
const nlohmann::json &typeJson,
Context &cache) {
524 assert(typeJson.at(
"mnemonic") ==
"bundle");
526 std::vector<std::tuple<std::string, BundleType::Direction, const Type *>>
528 for (
auto &chanJson : typeJson[
"channels"]) {
529 std::string dirStr = chanJson.at(
"direction");
530 BundleType::Direction dir;
532 dir = BundleType::Direction::To;
533 else if (dirStr ==
"from")
534 dir = BundleType::Direction::From;
536 throw std::runtime_error(
"Malformed manifest: unknown direction '" +
538 channels.emplace_back(chanJson.at(
"name"), dir,
541 return new BundleType(typeJson.at(
"id"), channels);
544ChannelType *parseChannelType(
const nlohmann::json &typeJson,
Context &cache) {
545 assert(typeJson.at(
"mnemonic") ==
"channel");
546 return new ChannelType(typeJson.at(
"id"),
550Type *parseInt(
const nlohmann::json &typeJson,
Context &cache) {
551 assert(typeJson.at(
"mnemonic") ==
"int");
552 std::string sign = typeJson.at(
"signedness");
553 uint64_t width = typeJson.at(
"hwBitwidth");
554 Type::ID
id = typeJson.at(
"id");
556 if (sign ==
"signed")
557 return new SIntType(
id, width);
558 else if (sign ==
"unsigned")
559 return new UIntType(
id, width);
560 else if (sign ==
"signless" && width == 0)
562 return new VoidType(
id);
563 else if (sign ==
"signless" && width > 0)
564 return new BitsType(
id, width);
566 throw std::runtime_error(
"Malformed manifest: unknown sign '" + sign +
"'");
569StructType *parseStruct(
const nlohmann::json &typeJson,
Context &cache) {
570 assert(typeJson.at(
"mnemonic") ==
"struct");
571 std::vector<std::pair<std::string, const Type *>> fields;
572 for (
auto &fieldJson : typeJson[
"fields"])
573 fields.emplace_back(fieldJson.at(
"name"),
575 return new StructType(typeJson.at(
"id"), fields);
578ArrayType *parseArray(
const nlohmann::json &typeJson,
Context &cache) {
579 assert(typeJson.at(
"mnemonic") ==
"array");
580 uint64_t size = typeJson.at(
"size");
581 return new ArrayType(typeJson.at(
"id"),
582 parseType(typeJson.at(
"element"), cache), size);
585WindowType *parseWindow(
const nlohmann::json &typeJson,
Context &cache) {
586 assert(typeJson.at(
"mnemonic") ==
"window");
587 std::string name = typeJson.at(
"name");
588 const Type *intoType =
parseType(typeJson.at(
"into"), cache);
589 const Type *loweredType =
parseType(typeJson.at(
"loweredType"), cache);
592 std::vector<WindowType::Frame> frames;
593 for (
auto &frameJson : typeJson.at(
"frames")) {
594 WindowType::Frame frame;
595 frame.name = frameJson.at(
"name");
596 for (
auto &fieldJson : frameJson.at(
"fields")) {
597 WindowType::Field field;
598 field.name = fieldJson.at(
"name");
599 if (fieldJson.contains(
"numItems"))
600 field.numItems = fieldJson.at(
"numItems");
601 if (fieldJson.contains(
"bulkCountWidth"))
602 field.bulkCountWidth = fieldJson.at(
"bulkCountWidth");
603 frame.fields.push_back(field);
605 frames.push_back(frame);
608 return new WindowType(typeJson.at(
"id"), name, intoType, loweredType, frames);
611ListType *parseList(
const nlohmann::json &typeJson,
Context &cache) {
612 assert(typeJson.at(
"mnemonic") ==
"list");
613 return new ListType(typeJson.at(
"id"),
614 parseType(typeJson.at(
"element"), cache));
617using TypeParser = std::function<Type *(
const nlohmann::json &,
Context &)>;
618const std::map<std::string_view, TypeParser> typeParsers = {
619 {
"bundle", parseBundleType},
620 {
"channel", parseChannelType},
621 {
"std::any", [](
const nlohmann::json &typeJson,
622 Context &cache) {
return new AnyType(typeJson.at(
"id")); }},
624 {
"struct", parseStruct},
625 {
"array", parseArray},
626 {
"window", parseWindow},
634 if (typeJson.is_string())
635 id = typeJson.get<std::string>();
637 id = typeJson.at(
"id");
638 if (std::optional<const Type *> t = cache.getType(
id))
640 if (typeJson.is_string())
641 throw std::runtime_error(
"malformed manifest: unknown type '" +
id +
"'");
644 std::string mnemonic = typeJson.at(
"mnemonic");
645 auto f = typeParsers.find(mnemonic);
646 if (f != typeParsers.end())
647 t = f->second(typeJson, cache);
653 cache.registerType(t);
659 return ::parseType(typeJson, ctxt);
663 for (
auto &typeJson : typesJson)
664 _typeTable.push_back(
parseType(typeJson));
671Manifest::Manifest(
Context &ctxt,
const std::string &jsonManifest)
672 : impl(new
Impl(ctxt, jsonManifest)) {}
674Manifest::~Manifest() {
delete impl; }
676uint32_t Manifest::getApiVersion()
const {
677 return impl->at(
"apiVersion").get<uint32_t>();
680std::vector<ModuleInfo> Manifest::getModuleInfos()
const {
681 std::vector<ModuleInfo> ret;
682 for (
auto &[symbol, info] : impl->getSymbolInfo())
687Accelerator *Manifest::buildAccelerator(AcceleratorConnection &acc)
const {
689 return acc.takeOwnership(impl->buildAccelerator(acc));
690 }
catch (
const std::exception &e) {
691 std::string msg =
"malformed manifest: " + std::string(e.what());
692 if (getApiVersion() == 0)
693 msg +=
" (schema version 0 is not considered stable)";
694 throw std::runtime_error(msg);
698const std::vector<const Type *> &Manifest::getTypeTable()
const {
699 return impl->getTypeTable();
708 auto printAny = [&os](std::any a) {
709 if (std::any_cast<Constant>(&a))
710 a = std::any_cast<Constant>(a).value;
712 const std::type_info &t = a.type();
713 if (t ==
typeid(std::string))
714 os << std::any_cast<std::string>(a);
715 else if (t ==
typeid(int64_t))
716 os << std::any_cast<int64_t>(a);
717 else if (t ==
typeid(uint64_t))
718 os << std::any_cast<uint64_t>(a);
719 else if (t ==
typeid(
double))
720 os << std::any_cast<double>(a);
721 else if (t ==
typeid(
bool))
722 os << std::any_cast<bool>(a);
723 else if (t ==
typeid(std::nullptr_t))
730 os << *m.name <<
" ";
732 os << *m.version <<
" ";
733 if (m.name || m.version)
735 if (m.repo || m.commitHash) {
736 os <<
" Version control: ";
740 os <<
"@" << *m.commitHash;
744 os <<
" " << *m.summary;
747 if (!m.constants.empty()) {
748 os <<
" Constants:\n";
749 for (
auto &e : m.constants) {
750 os <<
" " << e.first <<
": ";
756 if (!m.extra.empty()) {
757 os <<
" Extra metadata:\n";
758 for (
auto &e : m.extra) {
759 os <<
" " << e.first <<
": ";
770 ret.insert(ret.end(), b.begin(), b.end());
781std::string AppIDPath::toStr()
const {
782 std::ostringstream os;
793 if (a.size() != b.size())
794 return a.size() < b.size();
795 for (
size_t i = 0, e = a.size(); i < e; ++i)
804 os <<
"[" << *
id.idx <<
"]";
808 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(Context &ctxt, int port)
Start a server on the given port. -1 means to let the OS pick a port.
std::optional< uint32_t > idx