CIRCT 23.0.0git
Loading...
Searching...
No Matches
Engines.h
Go to the documentation of this file.
1//===- Engines.h - Implement port communication -----------------*- C++ -*-===//
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//
13//===----------------------------------------------------------------------===//
14//
15// Engines (as in DMA engine) implement the actual communication between the
16// host and the accelerator. They are low level of the ESI runtime API and are
17// not intended to be used directly by users.
18//
19// They are called "engines" rather than "DMA engines" since communication need
20// not be implemented via DMA.
21//
22//===----------------------------------------------------------------------===//
23
24// NOLINTNEXTLINE(llvm-header-guard)
25#ifndef ESI_ENGINGES_H
26#define ESI_ENGINGES_H
27
28#include "esi/Common.h"
29#include "esi/Ports.h"
30#include "esi/Services.h"
31#include "esi/Utils.h"
32
33#include <cassert>
34#include <future>
35
36namespace esi {
37class Accelerator;
38
39/// Engines implement the actual channel communication between the host and the
40/// accelerator. Engines can support multiple channels. They are low level of
41/// the ESI runtime API and are not intended to be used directly by users.
42class Engine {
43public:
45 virtual ~Engine() = default;
46 /// Start the engine, if applicable.
47 virtual void connect() { connected = true; };
48 /// Stop the engine, if applicable. Guaranteed to be called (via the
49 /// connection's disconnect()) while the Accelerator and its MMIO regions are
50 /// still alive, so port teardown here may safely touch them. This method
51 /// _must_ be idempotent.
52 virtual void disconnect() { connected = false; };
53 /// Get a port for a channel, from the cache if it exists or create it. An
54 /// engine may override this method if different behavior is desired.
55 virtual ChannelPort &requestPort(AppIDPath idPath,
56 const std::string &channelName,
57 BundleType::Direction dir, const Type *type);
58
59protected:
60 /// Each engine needs to know how to create a ports. This method is called if
61 /// a port doesn't exist in the engine cache.
62 virtual std::unique_ptr<ChannelPort>
63 createPort(AppIDPath idPath, const std::string &channelName,
64 BundleType::Direction dir, const Type *type) = 0;
65
68
69private:
70 std::map<std::pair<AppIDPath, std::string>, std::unique_ptr<ChannelPort>>
72};
73
74/// Since engines can support multiple channels BUT not necessarily all of the
75/// channels in a bundle, a mapping from bundle channels to engines is needed.
78
79public:
80 /// Request ports for all the channels in a bundle. If the engine doesn't
81 /// exist for a particular channel, skip said channel.
82 PortMap requestPorts(const AppIDPath &idPath,
83 const BundleType *bundleType) const;
84
85private:
86 /// Set a particlar engine for a particular channel. Should only be called by
87 /// AcceleratorConnection while registering engines.
88 void setEngine(const std::string &channelName, Engine *engine);
89 std::map<std::string, Engine *> bundleEngineMap;
90};
91
92namespace registry {
93
94/// Create an engine by name. This is the primary way to create engines for
95/// "normal" backends.
96std::unique_ptr<Engine> createEngine(AcceleratorConnection &conn,
97 const std::string &dmaEngineName,
98 AppIDPath idPath,
99 const ServiceImplDetails &details,
100 const HWClientDetails &clients);
101
102namespace internal {
103
104/// Engines can register themselves for pluggable functionality.
105using EngineCreate = std::function<std::unique_ptr<Engine>(
106 AcceleratorConnection &conn, AppIDPath idPath,
107 const ServiceImplDetails &details, const HWClientDetails &clients)>;
108void registerEngine(const std::string &name, EngineCreate create);
109
110/// Helper struct to register engines.
111template <typename TEngine>
113 RegisterEngine(const char *name) { registerEngine(name, &TEngine::create); }
114};
115
116#define CONCAT_(prefix, suffix) prefix##suffix
117#define CONCAT(prefix, suffix) CONCAT_(prefix, suffix)
118#define REGISTER_ENGINE(Name, TEngine) \
119 static ::esi::registry::internal::RegisterEngine<TEngine> CONCAT( \
120 __register_engine__, __LINE__)(Name)
121
122} // namespace internal
123} // namespace registry
124
125} // namespace esi
126
127#endif // ESI_PORTS_H
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:96
Since engines can support multiple channels BUT not necessarily all of the channels in a bundle,...
Definition Engines.h:76
PortMap requestPorts(const AppIDPath &idPath, const BundleType *bundleType) const
Request ports for all the channels in a bundle.
Definition Engines.cpp:516
std::map< std::string, Engine * > bundleEngineMap
Definition Engines.h:89
void setEngine(const std::string &channelName, Engine *engine)
Set a particlar engine for a particular channel.
Definition Engines.cpp:530
Bundles represent a collection of channels.
Definition Types.h:104
Unidirectional channels are the basic communication primitive between the host and accelerator.
Definition Ports.h:115
Engines implement the actual channel communication between the host and the accelerator.
Definition Engines.h:42
bool connected
Definition Engines.h:66
virtual void connect()
Start the engine, if applicable.
Definition Engines.h:47
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:503
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.
virtual ~Engine()=default
virtual void disconnect()
Stop the engine, if applicable.
Definition Engines.h:52
Engine(AcceleratorConnection &conn)
Definition Engines.h:44
AcceleratorConnection & conn
Definition Engines.h:67
std::map< std::pair< AppIDPath, std::string >, std::unique_ptr< ChannelPort > > ownedPorts
Definition Engines.h:71
Root class of the ESI type system.
Definition Types.h:36
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:107
void registerEngine(const std::string &name, EngineCreate create)
Definition Engines.cpp:566
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:555
Definition esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition Common.h:108
std::map< std::string, ChannelPort & > PortMap
Definition Ports.h:33
std::vector< HWClientDetail > HWClientDetails
Definition Common.h:107
Helper struct to register engines.
Definition Engines.h:112