CIRCT 23.0.0git
Loading...
Searching...
No Matches
Manifest.cpp
Go to the documentation of this file.
1//===- Manifest.cpp - Metadata on the accelerator -------------------------===//
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 (lib/dialect/ESI/runtime/cpp/).
12//
13//===----------------------------------------------------------------------===//
14
15#include "esi/Manifest.h"
16#include "esi/Accelerator.h"
17#include "esi/Services.h"
18
19#if defined(__GNUC__)
20#pragma GCC diagnostic push
21#pragma GCC diagnostic ignored "-Wcovered-switch-default"
22#endif
23#include <nlohmann/json.hpp>
24#if defined(__GNUC__)
25#pragma GCC diagnostic pop
26#endif
27#include <sstream>
28
29using namespace ::esi;
30
31// This is a proxy class to the manifest JSON. It is used to avoid having to
32// include the JSON parser in the header. Forward references don't work since
33// nlohmann::json is a rather complex template.
34//
35// Plus, it allows us to hide some implementation functions from the header
36// file.
38 friend class ::esi::Manifest;
39
40public:
41 Impl(Context &ctxt, const std::string &jsonManifest);
42
43 auto at(const std::string &key) const { return manifestJson.at(key); }
44
45 // Get the module info (if any) for the module instance in 'json'.
46 std::optional<ModuleInfo> getModInfo(const nlohmann::json &) const;
47
48 /// Go through the "service_decls" section of the manifest and populate the
49 /// services table as appropriate.
50 void scanServiceDecls(AcceleratorConnection &, const nlohmann::json &,
51 ServiceTable &) const;
52
53 void createEngine(AcceleratorConnection &, AppIDPath appID,
54 const nlohmann::json &) const;
55
56 /// Get a Service for the service specified in 'json'. Update the
57 /// activeServices table. TODO: re-using this for the engines section is a
58 /// terrible hack. Figure out a better way.
59 services::Service *getService(AppIDPath idPath, AcceleratorConnection &,
60 const nlohmann::json &,
61 ServiceTable &activeServices,
62 bool isEngine = false) const;
63
64 /// Get all the services in the description of an instance. Update the active
65 /// services table.
66 std::vector<services::Service *>
67 getServices(AppIDPath idPath, AcceleratorConnection &, const nlohmann::json &,
68 ServiceTable &activeServices) const;
69
70 /// Get the bundle ports for the instance at 'idPath' and specified in
71 /// 'instJson'. Look them up in 'activeServies'.
72 std::vector<std::unique_ptr<BundlePort>>
73 getBundlePorts(AcceleratorConnection &acc, AppIDPath idPath,
74 const ServiceTable &activeServices,
75 const nlohmann::json &instJson) const;
76
77 /// Build the set of child instances (recursively) for the module instance
78 /// description.
79 std::vector<std::unique_ptr<Instance>>
80 getChildInstances(AppIDPath idPath, AcceleratorConnection &acc,
81 const ServiceTable &activeServices,
82 const nlohmann::json &instJson) const;
83
84 /// Get a single child instance. Implicitly copy the active services table so
85 /// that it can be safely updated for the child's branch of the tree.
86 std::unique_ptr<Instance>
87 getChildInstance(AppIDPath idPath, AcceleratorConnection &acc,
88 ServiceTable activeServices,
89 const nlohmann::json &childJson) const;
90
91 /// Parse all the types and populate the types table.
92 void populateTypes(const nlohmann::json &typesJson);
93
94 /// Get the ordered list of types from the manifest.
95 const std::vector<const Type *> &getTypeTable() const { return _typeTable; }
96
97 /// Build a dynamic API for the Accelerator connection 'acc' based on the
98 /// manifest stored herein.
99 std::unique_ptr<Accelerator>
100 buildAccelerator(AcceleratorConnection &acc) const;
101
102 const Type *parseType(const nlohmann::json &typeJson);
103
104 const std::map<std::string, const ModuleInfo> &getSymbolInfo() const {
105 return symbolInfoCache;
106 }
107
108private:
110 std::vector<const Type *> _typeTable;
111
112 std::optional<const Type *> getType(Type::ID id) const {
113 return ctxt.getType(id);
114 }
115
116 std::any getAny(const nlohmann::json &value) const;
117 void parseModuleMetadata(ModuleInfo &info, const nlohmann::json &mod) const;
118 void parseModuleConsts(ModuleInfo &info, const nlohmann::json &mod) const;
119
120 // The parsed json.
121 nlohmann::json manifestJson;
122 // Cache the module info for each symbol.
123 std::map<std::string, const ModuleInfo> symbolInfoCache;
124};
125
126//===----------------------------------------------------------------------===//
127// Simple JSON -> object parsers.
128//===----------------------------------------------------------------------===//
129
130static std::optional<AppID> parseID(const nlohmann::json &jsonID) {
131 if (!jsonID.is_object())
132 return std::nullopt;
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);
138 return std::nullopt;
139}
140
141static AppID parseIDChecked(const nlohmann::json &jsonID) {
142 std::optional<AppID> id = parseID(jsonID);
143 if (!id)
144 throw std::runtime_error("Malformed manifest: invalid appID");
145 return *id;
146}
147static AppIDPath parseIDPath(const nlohmann::json &jsonIDPath) {
148 AppIDPath ret;
149 for (auto &idJson : jsonIDPath)
150 ret.push_back(parseIDChecked(idJson));
151 return ret;
152}
153
154static ServicePortDesc parseServicePort(const nlohmann::json &jsonPort) {
155 return ServicePortDesc{jsonPort.at("serviceName").get<std::string>(),
156 jsonPort.at("port").get<std::string>()};
157}
158
159/// Convert the json value to a 'std::any', which can be exposed outside of this
160/// file.
161std::any Manifest::Impl::getAny(const nlohmann::json &value) const {
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());
166
167 // If this can be converted to a constant, do so.
168 if (ret.size() != 2 || !ret.contains("type") || !ret.contains("value"))
169 return ret;
170 std::any value = ret.at("value");
171 std::any typeID = ret.at("type");
172 if (typeID.type() != typeid(std::string))
173 return ret;
174 std::optional<const Type *> type =
175 getType(std::any_cast<std::string>(type));
176 if (!type)
177 return ret;
178 // TODO: Check or guide the conversion of the value to the type based on the
179 // type.
180 return Constant{value, type};
181 };
182
183 auto getArray = [this](const nlohmann::json &json) -> std::any {
184 std::vector<std::any> ret;
185 for (auto &e : json)
186 ret.push_back(getAny(e));
187 return ret;
188 };
189
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);
207 else
208 throw std::runtime_error("Unknown type in manifest: " +
209 innerValue.dump(2));
210 };
211
212 std::optional<AppID> appid = parseID(value);
213 if (appid)
214 return *appid;
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"))};
218}
219
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());
227
228 auto value = [&](const std::string &key) -> std::optional<std::string> {
229 auto f = mod.find(key);
230 if (f == mod.end())
231 return std::nullopt;
232 return f.value();
233 };
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");
239}
240
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);
246 if (c)
247 info.constants[item.key()] = *c;
248 else
249 // If the value isn't a "proper" constant, present it as one with no type.
250 info.constants[item.key()] = Constant{value, std::nullopt};
251 }
252}
253
254//===----------------------------------------------------------------------===//
255// Manifest::Impl class implementation.
256//===----------------------------------------------------------------------===//
257
258Manifest::Impl::Impl(Context &ctxt, const std::string &manifestStr)
259 : ctxt(ctxt) {
260 manifestJson = nlohmann::ordered_json::parse(manifestStr);
261
262 try {
263 // Populate the types table first since anything else might need it.
264 populateTypes(manifestJson.at("types"));
265
266 // Populate the symbol info cache.
267 for (auto &mod : manifestJson.at("modules")) {
268 ModuleInfo info;
269 if (mod.contains("symInfo"))
270 parseModuleMetadata(info, mod.at("symInfo"));
271 if (mod.contains("symConsts"))
272 parseModuleConsts(info, mod.at("symConsts"));
273 symbolInfoCache.insert(make_pair(mod.at("symbol"), info));
274 }
275 } catch (const std::exception &e) {
276 std::string msg = "malformed manifest: " + std::string(e.what());
277 if (manifestJson.at("apiVersion") == 0)
278 msg += " (schema version 0 is not considered stable)";
279 throw std::runtime_error(msg);
280 }
281}
282
283std::unique_ptr<Accelerator>
284Manifest::Impl::buildAccelerator(AcceleratorConnection &acc) const {
285 ServiceTable activeSvcs;
286
287 auto designJson = manifestJson.at("design");
288
289 // Create all of the engines at the top level of the design.
290 // TODO: support engines at lower levels.
291 auto enginesIter = designJson.find("engines");
292 if (enginesIter != designJson.end())
293 for (auto &engineDesc : enginesIter.value())
294 createEngine(acc, {}, engineDesc);
295
296 // Get the initial active services table. Update it as we descend down.
297 auto svcDecls = manifestJson.at("serviceDeclarations");
298 scanServiceDecls(acc, svcDecls, activeSvcs);
299
300 // Get the services instantiated at the top level.
301 std::vector<services::Service *> services =
302 getServices({}, acc, designJson, activeSvcs);
303
304 // Get the ports at the top level.
305 auto ports = getBundlePorts(acc, {}, activeSvcs, designJson);
306
307 return std::make_unique<Accelerator>(
308 getModInfo(designJson),
309 getChildInstances({}, acc, activeSvcs, designJson), services,
310 std::move(ports));
311}
312
313std::optional<ModuleInfo>
314Manifest::Impl::getModInfo(const nlohmann::json &json) const {
315 auto instOfIter = json.find("instOf");
316 if (instOfIter == json.end())
317 return std::nullopt;
318 auto f = symbolInfoCache.find(instOfIter.value());
319 if (f != symbolInfoCache.end())
320 return f->second;
321 return std::nullopt;
322}
323
324/// TODO: Hack. This method is a giant hack to reuse the getService method for
325/// engines. It works, but it ain't pretty and it ain't right.
326void Manifest::Impl::createEngine(AcceleratorConnection &acc, AppIDPath idPath,
327 const nlohmann::json &eng) const {
328 ServiceTable dummy;
329 getService(idPath, acc, eng, dummy, /*isEngine=*/true);
330}
331
332void Manifest::Impl::scanServiceDecls(AcceleratorConnection &acc,
333 const nlohmann::json &svcDecls,
334 ServiceTable &activeServices) const {
335 for (auto &svcDecl : svcDecls) {
336 // Get the implementation details.
337 ServiceImplDetails svcDetails;
338 for (auto &detail : svcDecl.items())
339 svcDetails[detail.key()] = getAny(detail.value());
340
341 // Create the service.
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, /*id=*/{}, /*implName=*/"",
349 /*details=*/svcDetails, /*clients=*/{});
350 if (svc)
351 activeServices[svcDecl.at("symbol")] = svc;
352 }
353}
354
355std::vector<std::unique_ptr<Instance>>
356Manifest::Impl::getChildInstances(AppIDPath idPath, AcceleratorConnection &acc,
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())
362 return ret;
363 for (auto &child : childrenIter.value())
364 ret.emplace_back(getChildInstance(idPath, acc, activeServices, child));
365 return ret;
366}
367
368std::unique_ptr<Instance>
369Manifest::Impl::getChildInstance(AppIDPath idPath, AcceleratorConnection &acc,
370 ServiceTable activeServices,
371 const nlohmann::json &child) const {
372 AppID childID = parseIDChecked(child.at("appID"));
373 idPath.push_back(childID);
374
375 std::vector<services::Service *> services =
376 getServices(idPath, acc, child, activeServices);
377
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));
383}
384
385services::Service *Manifest::Impl::getService(AppIDPath idPath,
386 AcceleratorConnection &acc,
387 const nlohmann::json &svcJson,
388 ServiceTable &activeServices,
389 bool isEngine) const {
390
391 AppID id = parseIDChecked(svcJson.at("appID"));
392 idPath.push_back(id);
393
394 // Get all the client info, including the implementation details.
395 HWClientDetails clientDetails;
396 for (auto &client : svcJson.at("clientDetails")) {
397 HWClientDetail clientDetail;
398 for (auto &detail : client.items()) {
399 if (detail.key() == "relAppIDPath")
400 clientDetail.relPath = parseIDPath(detail.value());
401 else if (detail.key() == "servicePort")
402 clientDetail.port = parseServicePort(detail.value());
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();
409 else
410 chanAssign.implOptions[assign.key()] = getAny(assign.value());
411 clientDetail.channelAssignments[chan.key()] = chanAssign;
412 }
413 } else
414 clientDetail.implOptions[detail.key()] = getAny(detail.value());
415 }
416 clientDetails.push_back(clientDetail);
417 }
418
419 // Get the implementation details.
420 ServiceImplDetails svcDetails;
421 std::string implName;
422 std::string service;
423 for (auto &detail : svcJson.items()) {
424 if (detail.key() == "appID" || detail.key() == "clientDetails")
425 continue;
426 if (detail.key() == "serviceImplName")
427 implName = detail.value();
428 else if (detail.key() == "service")
429 service = detail.value().get<std::string>();
430 else
431 svcDetails[detail.key()] = getAny(detail.value());
432 }
433
434 // Create the service.
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);
443 } else {
444 services::Service::Type svcType =
445 services::ServiceRegistry::lookupServiceType(service);
446 if (isEngine)
447 acc.createEngine(implName, idPath, svcDetails, clientDetails);
448 else
449 svc =
450 acc.getService(svcType, idPath, implName, svcDetails, clientDetails);
451 }
452
453 if (svc)
454 // Update the active services table.
455 activeServices[service] = svc;
456 return svc;
457}
458
459std::vector<services::Service *>
460Manifest::Impl::getServices(AppIDPath idPath, AcceleratorConnection &acc,
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())
466 return ret;
467
468 for (auto &svc : svcsIter.value())
469 ret.emplace_back(getService(idPath, acc, svc, activeServices));
470 return ret;
471}
472
473std::vector<std::unique_ptr<BundlePort>>
474Manifest::Impl::getBundlePorts(AcceleratorConnection &acc, AppIDPath idPath,
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())
480 return ret;
481
482 for (auto &content : clientPortsIter.value()) {
483 // Lookup the requested service in the active services table.
484 std::string serviceName = "";
485 if (auto f = content.find("servicePort"); f != content.end())
486 serviceName = parseServicePort(f.value()).name;
487 auto svcIter = activeServices.find(serviceName);
488 if (svcIter == activeServices.end()) {
489 // If a specific service isn't found, search for the default service
490 // (typically provided by a BSP).
491 if (svcIter = activeServices.find(""); svcIter == activeServices.end())
492 throw std::runtime_error(
493 "Malformed manifest: could not find active service '" +
494 serviceName + "'");
495 }
496 services::Service *svc = svcIter->second;
497
498 std::string typeName = content.at("typeID");
499 auto type = getType(typeName);
500 if (!type)
501 throw std::runtime_error(
502 "Malformed manifest: could not find port type '" + typeName + "'");
503 const BundleType *bundleType = dynamic_cast<const BundleType *>(*type);
504 if (!bundleType)
505 throw std::runtime_error("Malformed manifest: type '" + typeName +
506 "' is not a bundle type");
507
508 idPath.push_back(parseIDChecked(content.at("appID")));
509
510 BundlePort *svcPort = svc->getPort(idPath, bundleType);
511 if (svcPort)
512 ret.emplace_back(svcPort);
513 // Since we share idPath between iterations, pop the last element before the
514 // next iteration.
515 idPath.pop_back();
516 }
517 return ret;
518}
519
520namespace {
521const Type *parseType(const nlohmann::json &typeJson, Context &ctxt);
522
523BundleType *parseBundleType(const nlohmann::json &typeJson, Context &cache) {
524 assert(typeJson.at("mnemonic") == "bundle");
525
526 std::vector<std::tuple<std::string, BundleType::Direction, const Type *>>
527 channels;
528 for (auto &chanJson : typeJson["channels"]) {
529 std::string dirStr = chanJson.at("direction");
530 BundleType::Direction dir;
531 if (dirStr == "to")
532 dir = BundleType::Direction::To;
533 else if (dirStr == "from")
534 dir = BundleType::Direction::From;
535 else
536 throw std::runtime_error("Malformed manifest: unknown direction '" +
537 dirStr + "'");
538 channels.emplace_back(chanJson.at("name"), dir,
539 parseType(chanJson["type"], cache));
540 }
541 return new BundleType(typeJson.at("id"), channels);
542}
543
544ChannelType *parseChannelType(const nlohmann::json &typeJson, Context &cache) {
545 assert(typeJson.at("mnemonic") == "channel");
546 return new ChannelType(typeJson.at("id"),
547 parseType(typeJson.at("inner"), cache));
548}
549
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");
555
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)
561 // By convention, a zero-width signless integer is a void type.
562 return new VoidType(id);
563 else if (sign == "signless" && width > 0)
564 return new BitsType(id, width);
565 else
566 throw std::runtime_error("Malformed manifest: unknown sign '" + sign + "'");
567}
568
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"),
574 parseType(fieldJson["type"], cache));
575 return new StructType(typeJson.at("id"), fields);
576}
577
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);
583}
584
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);
590
591 // Parse the frames information.
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);
604 }
605 frames.push_back(frame);
606 }
607
608 return new WindowType(typeJson.at("id"), name, intoType, loweredType, frames);
609}
610
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));
615}
616
617TypeAliasType *parseTypeAlias(const nlohmann::json &typeJson, Context &cache) {
618 assert(typeJson.at("mnemonic") == "alias");
619 std::string id = typeJson.at("id");
620 std::string name = typeJson.value("name", "");
621
622 const Type *innerType = nullptr;
623 if (typeJson.contains("inner"))
624 innerType = parseType(typeJson.at("inner"), cache);
625 else
626 throw std::runtime_error("typealias missing inner type");
627
628 if (name.empty())
629 name = id;
630
631 return new TypeAliasType(id, name, innerType);
632}
633
634using TypeParser = std::function<Type *(const nlohmann::json &, Context &)>;
635const std::map<std::string_view, TypeParser> typeParsers = {
636 {"bundle", parseBundleType},
637 {"channel", parseChannelType},
638 {"std::any", [](const nlohmann::json &typeJson,
639 Context &cache) { return new AnyType(typeJson.at("id")); }},
640 {"int", parseInt},
641 {"alias", parseTypeAlias},
642 {"struct", parseStruct},
643 {"array", parseArray},
644 {"window", parseWindow},
645 {"list", parseList},
646
647};
648
649// Parse a type if it doesn't already exist in the cache.
650const Type *parseType(const nlohmann::json &typeJson, Context &cache) {
651 std::string id;
652 if (typeJson.is_string())
653 id = typeJson.get<std::string>();
654 else
655 id = typeJson.at("id");
656 if (std::optional<const Type *> t = cache.getType(id))
657 return *t;
658 if (typeJson.is_string())
659 throw std::runtime_error("malformed manifest: unknown type '" + id + "'");
660
661 Type *t;
662 std::string mnemonic = typeJson.at("mnemonic");
663 auto f = typeParsers.find(mnemonic);
664 if (f != typeParsers.end())
665 t = f->second(typeJson, cache);
666 else
667 // Types we don't know about are opaque.
668 t = new Type(id);
669
670 // Insert into the cache.
671 cache.registerType(t);
672 return t;
673}
674} // namespace
675
676const Type *Manifest::Impl::parseType(const nlohmann::json &typeJson) {
677 return ::parseType(typeJson, ctxt);
678}
679
680void Manifest::Impl::populateTypes(const nlohmann::json &typesJson) {
681 for (auto &typeJson : typesJson)
682 _typeTable.push_back(parseType(typeJson));
683}
684
685//===----------------------------------------------------------------------===//
686// Manifest class implementation.
687//===----------------------------------------------------------------------===//
688
689Manifest::Manifest(Context &ctxt, const std::string &jsonManifest)
690 : impl(new Impl(ctxt, jsonManifest)) {}
691
692Manifest::~Manifest() { delete impl; }
693
694uint32_t Manifest::getApiVersion() const {
695 return impl->at("apiVersion").get<uint32_t>();
696}
697
698std::vector<ModuleInfo> Manifest::getModuleInfos() const {
699 std::vector<ModuleInfo> ret;
700 for (auto &[symbol, info] : impl->getSymbolInfo())
701 ret.push_back(info);
702 return ret;
703}
704
705Accelerator *Manifest::buildAccelerator(AcceleratorConnection &acc) const {
706 try {
707 return acc.takeOwnership(impl->buildAccelerator(acc));
708 } catch (const std::exception &e) {
709 std::string msg = "malformed manifest: " + std::string(e.what());
710 if (getApiVersion() == 0)
711 msg += " (schema version 0 is not considered stable)";
712 throw std::runtime_error(msg);
713 }
714}
715
716const std::vector<const Type *> &Manifest::getTypeTable() const {
717 return impl->getTypeTable();
718}
719
720//===----------------------------------------------------------------------===//
721// POCO helpers.
722//===----------------------------------------------------------------------===//
723
724// Print a module info, including the extra metadata.
725std::ostream &operator<<(std::ostream &os, const ModuleInfo &m) {
726 auto printAny = [&os](std::any a) {
727 if (std::any_cast<Constant>(&a))
728 a = std::any_cast<Constant>(a).value;
729
730 const std::type_info &t = a.type();
731 if (t == typeid(std::string))
732 os << std::any_cast<std::string>(a);
733 else if (t == typeid(int64_t))
734 os << std::any_cast<int64_t>(a);
735 else if (t == typeid(uint64_t))
736 os << std::any_cast<uint64_t>(a);
737 else if (t == typeid(double))
738 os << std::any_cast<double>(a);
739 else if (t == typeid(bool))
740 os << std::any_cast<bool>(a);
741 else if (t == typeid(std::nullptr_t))
742 os << "null";
743 else
744 os << "unknown";
745 };
746
747 if (m.name)
748 os << *m.name << " ";
749 if (m.version)
750 os << *m.version << " ";
751 if (m.name || m.version)
752 os << std::endl;
753 if (m.repo || m.commitHash) {
754 os << " Version control: ";
755 if (m.repo)
756 os << *m.repo;
757 if (m.commitHash)
758 os << "@" << *m.commitHash;
759 os << std::endl;
760 }
761 if (m.summary)
762 os << " " << *m.summary;
763 os << "\n";
764
765 if (!m.constants.empty()) {
766 os << " Constants:\n";
767 for (auto &e : m.constants) {
768 os << " " << e.first << ": ";
769 printAny(e.second);
770 os << "\n";
771 }
772 }
773
774 if (!m.extra.empty()) {
775 os << " Extra metadata:\n";
776 for (auto &e : m.extra) {
777 os << " " << e.first << ": ";
778 printAny(e.second);
779 os << "\n";
780 }
781 }
782 return os;
783}
784
785namespace esi {
786AppIDPath AppIDPath::operator+(const AppIDPath &b) const {
787 AppIDPath ret = *this;
788 ret.insert(ret.end(), b.begin(), b.end());
789 return ret;
790}
791
792AppIDPath AppIDPath::parent() const {
793 AppIDPath ret = *this;
794 if (!ret.empty())
795 ret.pop_back();
796 return ret;
797}
798
799std::string AppIDPath::toStr() const {
800 std::ostringstream os;
801 os << *this;
802 return os.str();
803}
804
805bool operator<(const AppID &a, const AppID &b) {
806 if (a.name != b.name)
807 return a.name < b.name;
808 return a.idx < b.idx;
809}
810bool operator<(const AppIDPath &a, const AppIDPath &b) {
811 if (a.size() != b.size())
812 return a.size() < b.size();
813 for (size_t i = 0, e = a.size(); i < e; ++i)
814 if (a[i] != b[i])
815 return a[i] < b[i];
816 return false;
817}
818
819std::ostream &operator<<(std::ostream &os, const AppID &id) {
820 os << id.name;
821 if (id.idx)
822 os << "[" << *id.idx << "]";
823 return os;
824}
825std::ostream &operator<<(std::ostream &os, const AppIDPath &path) {
826 for (size_t i = 0, e = path.size(); i < e; ++i) {
827 if (i > 0)
828 os << '.';
829 os << path[i];
830 }
831 return os;
832}
833
834} // namespace esi
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)
Definition Manifest.cpp:130
static AppIDPath parseIDPath(const nlohmann::json &jsonIDPath)
Definition Manifest.cpp:147
static AppID parseIDChecked(const nlohmann::json &jsonID)
Definition Manifest.cpp:141
static ServicePortDesc parseServicePort(const nlohmann::json &jsonPort)
Definition Manifest.cpp:154
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.
Definition Manifest.cpp:95
std::optional< const Type * > getType(Type::ID id) const
Definition Manifest.cpp:112
const Type * parseType(const nlohmann::json &typeJson)
Definition Manifest.cpp:676
auto at(const std::string &key) const
Definition Manifest.cpp:43
void populateTypes(const nlohmann::json &typesJson)
Parse all the types and populate the types table.
Definition Manifest.cpp:680
std::optional< ModuleInfo > getModInfo(const nlohmann::json &) const
Definition Manifest.cpp:314
void createEngine(AcceleratorConnection &, AppIDPath appID, const nlohmann::json &) const
TODO: Hack.
Definition Manifest.cpp:326
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'.
Definition Manifest.cpp:474
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.
Definition Manifest.cpp:356
nlohmann::json manifestJson
Definition Manifest.cpp:121
std::vector< services::Service * > getServices(AppIDPath idPath, AcceleratorConnection &, const nlohmann::json &, ServiceTable &activeServices) const
Get all the services in the description of an instance.
Definition Manifest.cpp:460
std::map< std::string, const ModuleInfo > symbolInfoCache
Definition Manifest.cpp:123
services::Service * getService(AppIDPath idPath, AcceleratorConnection &, const nlohmann::json &, ServiceTable &activeServices, bool isEngine=false) const
Get a Service for the service specified in 'json'.
Definition Manifest.cpp:385
const std::map< std::string, const ModuleInfo > & getSymbolInfo() const
Definition Manifest.cpp:104
std::any getAny(const nlohmann::json &value) const
Convert the json value to a 'std::any', which can be exposed outside of this file.
Definition Manifest.cpp:161
void parseModuleMetadata(ModuleInfo &info, const nlohmann::json &mod) const
Definition Manifest.cpp:220
std::unique_ptr< Accelerator > buildAccelerator(AcceleratorConnection &acc) const
Build a dynamic API for the Accelerator connection 'acc' based on the manifest stored herein.
Definition Manifest.cpp:284
void parseModuleConsts(ModuleInfo &info, const nlohmann::json &mod) const
Definition Manifest.cpp:241
void scanServiceDecls(AcceleratorConnection &, const nlohmann::json &, ServiceTable &) const
Go through the "service_decls" section of the manifest and populate the services table as appropriate...
Definition Manifest.cpp:332
Context & ctxt
Definition Manifest.cpp:109
std::vector< const Type * > _typeTable
Definition Manifest.cpp:110
std::unique_ptr< Instance > getChildInstance(AppIDPath idPath, AcceleratorConnection &acc, ServiceTable activeServices, const nlohmann::json &childJson) const
Get a single child instance.
Definition Manifest.cpp:369
Impl(Context &ctxt, int port)
Start a server on the given port. -1 means to let the OS pick a port.
mlir::Type innerType(mlir::Type type)
Definition ESITypes.cpp:420
void info(Twine message)
Definition LSPUtils.cpp:20
Definition esi.py:1
std::string name
Definition Common.h:36
std::optional< uint32_t > idx
Definition Common.h:37