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