CIRCT  19.0.0git
esiquery.cpp
Go to the documentation of this file.
1 //===- esiquery.cpp - ESI accelerator system query tool -------------------===//
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/Accelerator.h"
17 #include "esi/Manifest.h"
18 #include "esi/Services.h"
19 
20 #include <iostream>
21 #include <map>
22 #include <stdexcept>
23 
24 using namespace esi;
25 
26 void printInfo(std::ostream &os, AcceleratorConnection &acc);
27 void printHier(std::ostream &os, AcceleratorConnection &acc);
28 
29 int main(int argc, const char *argv[]) {
30  // TODO: find a command line parser library rather than doing this by hand.
31  if (argc < 3) {
32  std::cerr << "Expected usage: " << argv[0]
33  << " <backend> <connection specifier> [command]" << std::endl;
34  return -1;
35  }
36 
37  Context ctxt;
38 
39  const char *backend = argv[1];
40  const char *conn = argv[2];
41  std::string cmd;
42  if (argc > 3)
43  cmd = argv[3];
44 
45  try {
46  std::unique_ptr<AcceleratorConnection> acc = ctxt.connect(backend, conn);
47  const auto &info = *acc->getService<services::SysInfo>();
48 
49  if (cmd == "version")
50  std::cout << "ESI system version: " << info.getEsiVersion() << std::endl;
51  else if (cmd == "json_manifest")
52  std::cout << info.getJsonManifest() << std::endl;
53  else if (cmd == "info")
54  printInfo(std::cout, *acc);
55  else if (cmd == "hier")
56  printHier(std::cout, *acc);
57  else {
58  std::cout << "Connection successful." << std::endl;
59  if (!cmd.empty()) {
60  std::cerr << "Unknown command: " << cmd << std::endl;
61  return 1;
62  }
63  }
64  return 0;
65  } catch (std::exception &e) {
66  std::cerr << "Error: " << e.what() << std::endl;
67  return -1;
68  }
69 }
70 
71 void printInfo(std::ostream &os, AcceleratorConnection &acc) {
72  std::string jsonManifest =
73  acc.getService<services::SysInfo>()->getJsonManifest();
74  Manifest m(acc.getCtxt(), jsonManifest);
75  os << "API version: " << m.getApiVersion() << std::endl << std::endl;
76  os << "********************************" << std::endl;
77  os << "* Module information" << std::endl;
78  os << "********************************" << std::endl;
79  os << std::endl;
80  for (ModuleInfo mod : m.getModuleInfos())
81  os << "- " << mod;
82 
83  os << std::endl;
84  os << "********************************" << std::endl;
85  os << "* Type table" << std::endl;
86  os << "********************************" << std::endl;
87  os << std::endl;
88  size_t i = 0;
89  for (const Type *t : m.getTypeTable())
90  os << " " << i++ << ": " << t->getID() << std::endl;
91 }
92 
93 void printPort(std::ostream &os, const BundlePort &port,
94  std::string indent = "") {
95  os << indent << " " << port.getID() << ":" << std::endl;
96  for (const auto &[name, chan] : port.getChannels()) {
97  os << indent << " " << name << ": " << chan.getType()->getID()
98  << std::endl;
99  }
100 }
101 
102 void printInstance(std::ostream &os, const HWModule *d,
103  std::string indent = "") {
104  os << indent << "* Instance:";
105  if (auto inst = dynamic_cast<const Instance *>(d))
106  os << inst->getID() << std::endl;
107  else
108  os << "top" << std::endl;
109  os << indent << "* Ports:" << std::endl;
110  for (const BundlePort &port : d->getPortsOrdered())
111  printPort(os, port, indent + " ");
112  std::vector<const Instance *> children = d->getChildrenOrdered();
113  if (!children.empty()) {
114  os << indent << "* Children:" << std::endl;
115  for (const Instance *child : d->getChildrenOrdered())
116  printInstance(os, child, indent + " ");
117  }
118  os << std::endl;
119 }
120 
121 void printHier(std::ostream &os, AcceleratorConnection &acc) {
122  Manifest manifest(acc.getCtxt(),
124  std::unique_ptr<Accelerator> design = manifest.buildAccelerator(acc);
125  os << "********************************" << std::endl;
126  os << "* Design hierarchy" << std::endl;
127  os << "********************************" << std::endl;
128  os << std::endl;
129  printInstance(os, design.get());
130 }
Abstract class representing a connection to an accelerator.
Definition: Accelerator.h:78
Context & getCtxt() const
Definition: Accelerator.h:82
ServiceClass * getService(AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={})
Get a typed reference to a particular service type.
Definition: Accelerator.h:99
Services provide connections to 'bundles' – collections of named, unidirectional communication channe...
Definition: Ports.h:147
const std::map< std::string, ChannelPort & > & getChannels() const
Definition: Ports.h:168
AppID getID() const
Get the ID of the port.
Definition: Ports.h:160
AcceleratorConnections, Accelerators, and Manifests must all share a context.
Definition: Context.h:30
Represents either the top level or an instance of a hardware module.
Definition: Design.h:47
std::vector< const Instance * > getChildrenOrdered() const
Get a vector of the module's children in a deterministic order.
Definition: Design.h:60
std::vector< std::reference_wrapper< BundlePort > > getPortsOrdered() const
Get the module's ports in a deterministic order.
Definition: Design.h:69
Subclass of HWModule which represents a submodule instance.
Definition: Design.h:91
Class to parse a manifest.
Definition: Manifest.h:39
std::unique_ptr< Accelerator > buildAccelerator(AcceleratorConnection &acc) const
Definition: Manifest.cpp:538
std::vector< ModuleInfo > getModuleInfos() const
Definition: Manifest.cpp:530
uint32_t getApiVersion() const
Definition: Manifest.cpp:526
const std::vector< const Type * > & getTypeTable() const
The Type Table is an ordered list of types.
Definition: Manifest.cpp:542
Root class of the ESI type system.
Definition: Types.h:27
Information about the Accelerator system.
Definition: Services.h:77
virtual std::string getJsonManifest() const
Return the JSON-formatted system manifest.
Definition: Services.cpp:32
virtual uint32_t getEsiVersion() const =0
Get the ESI version number to check version compatibility.
void printHier(std::ostream &os, AcceleratorConnection &acc)
Definition: esiquery.cpp:121
void printInfo(std::ostream &os, AcceleratorConnection &acc)
Definition: esiquery.cpp:71
void printInstance(std::ostream &os, const HWModule *d, std::string indent="")
Definition: esiquery.cpp:102
int main(int argc, const char *argv[])
Definition: esiquery.cpp:29
void printPort(std::ostream &os, const BundlePort &port, std::string indent="")
Definition: esiquery.cpp:93
Definition: esi.py:1