CIRCT  20.0.0git
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 <memory>
24 #include <optional>
25 
26 namespace esi {
27 class AcceleratorConnection;
28 
29 /// AcceleratorConnections, Accelerators, and Manifests must all share a
30 /// context. It owns all the types, uniquifying them.
31 class Context {
32 public:
33  Context() : logger(std::make_unique<NullLogger>()) {}
34  Context(std::unique_ptr<Logger> logger) : logger(std::move(logger)) {}
35 
36  /// Create a context with a specific logger type.
37  template <typename T, typename... Args>
38  static Context withLogger(Args &&...args) {
39  return Context(std::make_unique<T>(args...));
40  }
41 
42  /// Resolve a type id to the type.
43  std::optional<const Type *> getType(Type::ID id) const {
44  if (auto f = types.find(id); f != types.end())
45  return f->second.get();
46  return std::nullopt;
47  }
48 
49  /// Register a type with the context. Takes ownership of the pointer type.
50  void registerType(Type *type);
51 
52  /// Connect to an accelerator backend.
53  std::unique_ptr<AcceleratorConnection> connect(std::string backend,
54  std::string connection);
55 
56  /// Register a logger with the accelerator. Assumes ownership of the logger.
57  void setLogger(std::unique_ptr<Logger> logger) {
58  if (!logger)
59  throw std::invalid_argument("logger must not be null");
60  this->logger = std::move(logger);
61  }
62  inline Logger &getLogger() { return *logger; }
63 
64 private:
65  std::unique_ptr<Logger> logger;
66 
67 private:
68  using TypeCache = std::map<Type::ID, std::unique_ptr<Type>>;
70 };
71 
72 } // namespace esi
73 
74 #endif // ESI_CONTEXT_H
AcceleratorConnections, Accelerators, and Manifests must all share a context.
Definition: Context.h:31
Logger & getLogger()
Definition: Context.h:62
TypeCache types
Definition: Context.h:69
static Context withLogger(Args &&...args)
Create a context with a specific logger type.
Definition: Context.h:38
std::map< Type::ID, std::unique_ptr< Type > > TypeCache
Definition: Context.h:68
void setLogger(std::unique_ptr< Logger > logger)
Register a logger with the accelerator. Assumes ownership of the logger.
Definition: Context.h:57
Context(std::unique_ptr< Logger > logger)
Definition: Context.h:34
std::unique_ptr< Logger > logger
Definition: Context.h:65
std::optional< const Type * > getType(Type::ID id) const
Resolve a type id to the type.
Definition: Context.h:43
std::unique_ptr< AcceleratorConnection > connect(std::string backend, std::string connection)
Connect to an accelerator backend.
Definition: Context.cpp:27
void registerType(Type *type)
Register a type with the context. Takes ownership of the pointer type.
Definition: Context.cpp:20
A logger that does nothing.
Definition: Logging.h:169
Root class of the ESI type system.
Definition: Types.h:27
std::string ID
Definition: Types.h:29
Definition: esi.py:1