CIRCT 22.0.0git
Loading...
Searching...
No Matches
Context.h
Go to the documentation of this file.
1//===- Context.h - Accelerator context --------------------------*- 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// NOLINTNEXTLINE(llvm-header-guard)
16#ifndef ESI_CONTEXT_H
17#define ESI_CONTEXT_H
18
19#include "esi/Logging.h"
20#include "esi/Types.h"
21
22#include <exception>
23#include <map>
24#include <memory>
25#include <vector>
26
27namespace esi {
28class AcceleratorConnection;
29
30/// AcceleratorConnections, Accelerators, and Manifests must all share a
31/// context. It owns all the types, uniquifying them. It also owns the
32/// connections (which own the Accelerators). When it is destroyed, all
33/// connections are disconnected and the objects are destroyed.
34class Context {
35public:
36 Context();
37 Context(std::unique_ptr<Logger> logger);
38 ~Context();
39
40 /// Disconnect from all accelerators associated with this context.
41 void disconnectAll();
42
43 /// Create a context with a specific logger type.
44 template <typename T, typename... Args>
45 static std::unique_ptr<Context> withLogger(Args &&...args) {
46 return std::make_unique<Context>(std::make_unique<T>(args...));
47 }
48
49 /// Resolve a type id to the type.
50 std::optional<const Type *> getType(Type::ID id) const {
51 if (auto f = types.find(id); f != types.end())
52 return f->second.get();
53 return std::nullopt;
54 }
55
56 /// Register a type with the context. Takes ownership of the pointer type.
57 void registerType(Type *type);
58
59 /// Connect to an accelerator backend. Retains ownership internally and
60 /// returns a non-owning pointer.
61 AcceleratorConnection *connect(std::string backend, std::string connection);
62
63 /// Register a logger with the accelerator. Assumes ownership of the logger.
64 void setLogger(std::unique_ptr<Logger> logger) {
65 if (!logger)
66 throw std::invalid_argument("logger must not be null");
67 this->logger = std::move(logger);
68 }
69 inline Logger &getLogger() { return *logger; }
70
71private:
72 std::unique_ptr<Logger> logger;
73 std::vector<std::unique_ptr<AcceleratorConnection>> connections;
74
75private:
76 using TypeCache = std::map<Type::ID, std::unique_ptr<Type>>;
78};
79
80} // namespace esi
81
82#endif // ESI_CONTEXT_H
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:79
AcceleratorConnections, Accelerators, and Manifests must all share a context.
Definition Context.h:34
void disconnectAll()
Disconnect from all accelerators associated with this context.
Definition Context.cpp:27
TypeCache types
Definition Context.h:77
Logger & getLogger()
Definition Context.h:69
std::optional< const Type * > getType(Type::ID id) const
Resolve a type id to the type.
Definition Context.h:50
std::vector< std::unique_ptr< AcceleratorConnection > > connections
Definition Context.h:73
AcceleratorConnection * connect(std::string backend, std::string connection)
Connect to an accelerator backend.
std::map< Type::ID, std::unique_ptr< Type > > TypeCache
Definition Context.h:76
void setLogger(std::unique_ptr< Logger > logger)
Register a logger with the accelerator. Assumes ownership of the logger.
Definition Context.h:64
std::unique_ptr< Logger > logger
Definition Context.h:72
void registerType(Type *type)
Register a type with the context. Takes ownership of the pointer type.
Definition Context.cpp:33
static std::unique_ptr< Context > withLogger(Args &&...args)
Create a context with a specific logger type.
Definition Context.h:45
Root class of the ESI type system.
Definition Types.h:34
std::string ID
Definition Types.h:36
Definition esi.py:1