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 std::optional<DenseMap<StringRef, SmallVector<RuntimeConstant>>::iterator>
27 matchJitVariableMap(StringRef FnName) {
28 std::string Operator = llvm::demangle(FnName.str());
29 std::size_t Sep = Operator.rfind("::operator()");
30 if (Sep == std::string::npos) {
31 PROTEUS_DBG(Logger::logs("proteus")
32 << "... SKIP ::operator() not found\n");
33 return std::nullopt;
34 }
35
36 StringRef LambdaType = StringRef{Operator}.slice(0, Sep);
37 if (Config::get().ProteusDebugOutput) {
38 Logger::logs("proteus")
39 << "Operator " << Operator << "\n=> LambdaType to match "
40 << LambdaType << "\n";
41 Logger::logs("proteus") << "Available Keys\n";
42 for (auto &[Key, Val] : JitVariableMap) {
43 Logger::logs("proteus") << "\tKey: " << Key << "\n";
44 }
45 Logger::logs("proteus") << "===\n";
46 }
47
48 const auto It = JitVariableMap.find(LambdaType);
49 if (It == JitVariableMap.end())
50 return std::nullopt;
51
52 return It;
53 }
54
55 void dump() {
56 Logger::logs("proteus") << "Dumping pending registry \n";
57 for (const auto &[Key, Value] : PendingJitVariableMap) {
58 Logger::logs("proteus") << Key << " : " << "\n";
59 for (const auto &V : Value) {
60 Logger::logs("proteus") << ", " << V.Value.DoubleVal;
61 }
62 Logger::logs("proteus") << "\n";
63 }
64 Logger::logs("proteus") << "Dumping finalized registry \n";
65 for (const auto &[Key, Value] : JitVariableMap) {
66 Logger::logs("proteus") << Key << " : " << "\n";
67 for (const auto &V : Value) {
68 Logger::logs("proteus") << ", " << V.Value.DoubleVal;
69 }
70 Logger::logs("proteus") << "\n";
71 }
72 }
73
74 void setJitVariable(const char *LambdaType, RuntimeConstant &RC) {
75 assert(PendingJitVariableMap.contains(LambdaType) &&
76 "Lambda must be registered prior to register JIT variable!");
77 PendingJitVariableMap[LambdaType].emplace_back(RC);
78 }
79
80 inline void registerLambda(const char *LambdaType) {
81 const StringRef LambdaTypeRef{LambdaType};
82 PROTEUS_DBG(Logger::logs("proteus")
83 << "=> RegisterLambda " << LambdaTypeRef << "\n");
84 // Copy PendingJitVariables if there were changed, otherwise the runtime
85 // values for the lambda definition have not changed.
87 if (!PendingJitVariableMap[LambdaTypeRef].empty()) {
88 JitVariableMap[LambdaTypeRef] = PendingJitVariableMap[LambdaTypeRef];
89 PendingJitVariableMap[LambdaTypeRef].clear();
90 }
91 }
92
93 const SmallVector<RuntimeConstant> &getJitVariables(StringRef LambdaTypeRef) {
94 return JitVariableMap[LambdaTypeRef];
95 }
96
97 bool empty() { return JitVariableMap.empty(); }
98
99private:
100 explicit LambdaRegistry() = default;
101 DenseMap<StringRef, SmallVector<RuntimeConstant>> JitVariableMap;
102 DenseMap<StringRef, SmallVector<RuntimeConstant>> PendingJitVariableMap;
103};
104
105} // namespace proteus
106
107#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:80
void setJitVariable(const char *LambdaType, RuntimeConstant &RC)
Definition LambdaRegistry.h:74
std::optional< DenseMap< StringRef, SmallVector< RuntimeConstant > >::iterator > matchJitVariableMap(StringRef FnName)
Definition LambdaRegistry.h:27
void dump()
Definition LambdaRegistry.h:55
const SmallVector< RuntimeConstant > & getJitVariables(StringRef LambdaTypeRef)
Definition LambdaRegistry.h:93
static LambdaRegistry & instance()
Definition LambdaRegistry.h:21
bool empty()
Definition LambdaRegistry.h:97
static llvm::raw_ostream & logs(const std::string &Name)
Definition Logger.h:19
Definition CompiledLibrary.h:7
Definition MemoryCache.h:26
Definition CompilerInterfaceTypes.h:72