CIRCT 20.0.0git
Loading...
Searching...
No Matches
Engines.cpp
Go to the documentation of this file.
1//===- Engines.cpp --------------------------------------------------------===//
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/Engines.h"
16
17using namespace esi;
18
19namespace {
20/// Created by default when the DMA engine cannot be resolved. Throws the error
21/// upon trying to connect and creates ports which throw errors on their
22/// connection attempts.
23class UnknownEngine : public Engine {
24public:
25 UnknownEngine(AcceleratorConnection &conn, std::string engineName)
26 : engineName(engineName) {}
27 void connect() override {
28 throw std::runtime_error("Unknown engine '" + engineName + "'");
29 }
30
31 std::unique_ptr<ChannelPort> createPort(AppIDPath idPath,
32 const std::string &channelName,
34 const Type *type) override {
35 if (BundlePort::isWrite(dir))
36 return std::make_unique<UnknownWriteChannelPort>(
37 type,
38 "Unknown engine '" + engineName + "': cannot create write port");
39 else
40 return std::make_unique<UnknownReadChannelPort>(
41 type, "Unknown engine '" + engineName + "': cannot create read port");
42 }
43
44protected:
45 std::string engineName;
46};
47} // namespace
48
50 const std::string &channelName,
51 BundleType::Direction dir, const Type *type) {
52 auto portIter = ownedPorts.find(std::make_pair(idPath, channelName));
53 if (portIter != ownedPorts.end())
54 return *portIter->second;
55 std::unique_ptr<ChannelPort> port =
56 createPort(idPath, channelName, dir, type);
57 ChannelPort &ret = *port;
58 ownedPorts.emplace(std::make_pair(idPath, channelName), std::move(port));
59 return ret;
60}
61
63 const BundleType *bundleType) const {
64 PortMap ports;
65 for (auto [channelName, dir, type] : bundleType->getChannels()) {
66 auto engineIter = bundleEngineMap.find(channelName);
67 if (engineIter == bundleEngineMap.end())
68 continue;
69
70 ports.emplace(channelName, engineIter->second->requestPort(
71 idPath, channelName, dir, type));
72 }
73 return ports;
74}
75
76void BundleEngineMap::setEngine(const std::string &channelName,
77 Engine *engine) {
78 auto [it, inserted] = bundleEngineMap.try_emplace(channelName, engine);
79 if (!inserted)
80 throw std::runtime_error("Channel already exists in engine map");
81}
82
83//===----------------------------------------------------------------------===//
84// Registry
85//===----------------------------------------------------------------------===//
86namespace {
87class EngineRegistry {
88public:
89 static std::map<std::string, registry::internal::EngineCreate> &get() {
90 static EngineRegistry instance;
91 return instance.engineRegistry;
92 }
93
94private:
95 std::map<std::string, registry::internal::EngineCreate> engineRegistry;
96};
97} // namespace
98
99std::unique_ptr<Engine>
101 const std::string &dmaEngineName, AppIDPath idPath,
102 const ServiceImplDetails &details,
103 const HWClientDetails &clients) {
104 auto &reg = EngineRegistry::get();
105 auto it = reg.find(dmaEngineName);
106 if (it == reg.end())
107 return std::make_unique<UnknownEngine>(conn, dmaEngineName);
108 return it->second(conn, idPath, details, clients);
109}
110
111void registry::internal::registerEngine(const std::string &name,
112 EngineCreate create) {
113 auto tried = EngineRegistry::get().try_emplace(name, create);
114 if (!tried.second)
115 throw std::runtime_error("Engine already exists in registry");
116}
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:79
PortMap requestPorts(const AppIDPath &idPath, const BundleType *bundleType) const
Request ports for all the channels in a bundle.
Definition Engines.cpp:62
std::map< std::string, Engine * > bundleEngineMap
Definition Engines.h:81
void setEngine(const std::string &channelName, Engine *engine)
Set a particlar engine for a particular channel.
Definition Engines.cpp:76
Bundles represent a collection of channels.
Definition Types.h:44
const ChannelVector & getChannels() const
Definition Types.h:54
Unidirectional channels are the basic communication primitive between the host and accelerator.
Definition Ports.h:36
Engines implement the actual channel communication between the host and the accelerator.
Definition Engines.h:41
virtual void connect()
Start the engine, if applicable.
Definition Engines.h:45
virtual ChannelPort & requestPort(AppIDPath idPath, const std::string &channelName, BundleType::Direction dir, const Type *type)
Get a port for a channel, from the cache if it exists or create it.
Definition Engines.cpp:49
virtual std::unique_ptr< ChannelPort > createPort(AppIDPath idPath, const std::string &channelName, BundleType::Direction dir, const Type *type)=0
Each engine needs to know how to create a ports.
std::map< std::pair< AppIDPath, std::string >, std::unique_ptr< ChannelPort > > ownedPorts
Definition Engines.h:63
Root class of the ESI type system.
Definition Types.h:27
std::function< std::unique_ptr< Engine >(AcceleratorConnection &conn, AppIDPath idPath, const ServiceImplDetails &details, const HWClientDetails &clients)> EngineCreate
Engines can register themselves for pluggable functionality.
Definition Engines.h:99
void registerEngine(const std::string &name, EngineCreate create)
Definition Engines.cpp:111
std::unique_ptr< Engine > createEngine(AcceleratorConnection &conn, const std::string &dmaEngineName, AppIDPath idPath, const ServiceImplDetails &details, const HWClientDetails &clients)
Create an engine by name.
Definition Engines.cpp:100
Definition esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition Common.h:98
std::map< std::string, ChannelPort & > PortMap
Definition Ports.h:29
std::vector< HWClientDetail > HWClientDetails
Definition Common.h:97