Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
LambdaRegistry.h
Go to the documentation of this file.
1#ifndef PROTEUS_LAMBDA_INTERFACE_H
2#define PROTEUS_LAMBDA_INTERFACE_H
3
5#include "proteus/Error.h"
9
10#include <llvm/ADT/DenseMap.h>
11#include <llvm/ADT/StringRef.h>
12#include <llvm/Demangle/Demangle.h>
13namespace proteus {
14
15using namespace llvm;
16
17// The LambdaRegistry stores the unique lambda type symbol and the values of Jit
18// member variables in a map for retrieval by the Jit engines.
20public:
22 static LambdaRegistry Singleton;
23 return Singleton;
24 }
25
26 LambdaRegistry(const LambdaRegistry &) = delete;
30
31 std::optional<DenseMap<StringRef, SmallVector<RuntimeConstant>>::iterator>
32 matchJitVariableMap(StringRef FnName) {
33 std::string Operator = llvm::demangle(FnName.str());
34 std::size_t Sep = Operator.rfind("::operator()");
35 if (Sep == std::string::npos) {
36 PROTEUS_DBG(Logger::logs("proteus")
37 << "... SKIP ::operator() not found\n");
38 return std::nullopt;
39 }
40
41 StringRef LambdaType = StringRef{Operator}.slice(0, Sep);
42 if (Config::get().ProteusDebugOutput) {
43 Logger::logs("proteus")
44 << "Operator " << Operator << "\n=> LambdaType to match "
45 << LambdaType << "\n";
46 Logger::logs("proteus") << "Available Keys\n";
47 for (auto &[Key, Val] : JitVariableMap) {
48 Logger::logs("proteus") << "\tKey: " << Key << "\n";
49 }
50 Logger::logs("proteus") << "===\n";
51 }
52
53 const auto It = JitVariableMap.find(LambdaType);
54 if (It == JitVariableMap.end())
55 return std::nullopt;
56
57 return It;
58 }
59
60 void dump() {
61 Logger::logs("proteus") << "Dumping pending registry \n";
62 for (const auto &[Key, Value] : PendingJitVariableMap) {
63 Logger::logs("proteus") << Key << " : " << "\n";
64 for (const auto &V : Value) {
65 Logger::logs("proteus") << ", " << V.Value.DoubleVal;
66 }
67 Logger::logs("proteus") << "\n";
68 }
69 Logger::logs("proteus") << "Dumping finalized registry \n";
70 for (const auto &[Key, Value] : JitVariableMap) {
71 Logger::logs("proteus") << Key << " : " << "\n";
72 for (const auto &V : Value) {
73 Logger::logs("proteus") << ", " << V.Value.DoubleVal;
74 }
75 Logger::logs("proteus") << "\n";
76 }
77 }
78
79 void setJitVariable(const char *LambdaType, RuntimeConstant &RC) {
80 assert(PendingJitVariableMap.contains(LambdaType) &&
81 "Lambda must be registered prior to register JIT variable!");
82 PendingJitVariableMap[LambdaType].emplace_back(RC);
83 }
84
85 inline void registerLambda(const char *LambdaType) {
86 const StringRef LambdaTypeRef{LambdaType};
87 PROTEUS_DBG(Logger::logs("proteus")
88 << "=> RegisterLambda " << LambdaTypeRef << "\n");
89 // Copy PendingJitVariables if there were changed, otherwise the runtime
90 // values for the lambda definition have not changed.
92 if (!PendingJitVariableMap[LambdaTypeRef].empty()) {
93 JitVariableMap[LambdaTypeRef] = PendingJitVariableMap[LambdaTypeRef];
94 PendingJitVariableMap[LambdaTypeRef].clear();
95 }
96 }
97
98 const SmallVector<RuntimeConstant> &getJitVariables(StringRef LambdaTypeRef) {
99 return JitVariableMap[LambdaTypeRef];
100 }
101
102 bool empty() { return JitVariableMap.empty(); }
103
104private:
105 explicit LambdaRegistry() = default;
106 DenseMap<StringRef, SmallVector<RuntimeConstant>> JitVariableMap;
107 DenseMap<StringRef, SmallVector<RuntimeConstant>> PendingJitVariableMap;
108};
109
110} // namespace proteus
111
112#endif
#define PROTEUS_DBG(x)
Definition Debug.h:9
static Config & get()
Definition Config.h:334
Definition LambdaRegistry.h:19
void registerLambda(const char *LambdaType)
Definition LambdaRegistry.h:85
void setJitVariable(const char *LambdaType, RuntimeConstant &RC)
Definition LambdaRegistry.h:79
std::optional< DenseMap< StringRef, SmallVector< RuntimeConstant > >::iterator > matchJitVariableMap(StringRef FnName)
Definition LambdaRegistry.h:32
void dump()
Definition LambdaRegistry.h:60
LambdaRegistry & operator=(LambdaRegistry &&)=delete
LambdaRegistry(const LambdaRegistry &)=delete
LambdaRegistry & operator=(const LambdaRegistry &)=delete
const SmallVector< RuntimeConstant > & getJitVariables(StringRef LambdaTypeRef)
Definition LambdaRegistry.h:98
static LambdaRegistry & instance()
Definition LambdaRegistry.h:21
LambdaRegistry(LambdaRegistry &&)=delete
bool empty()
Definition LambdaRegistry.h:102
static llvm::raw_ostream & logs(const std::string &Name)
Definition Logger.h:19
Definition CompiledLibrary.h:7
Definition MemoryCache.h:27
Definition CompilerInterfaceTypes.h:72