Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
LambdaRegistry.hpp
Go to the documentation of this file.
1#ifndef PROTEUS_LAMBDA_INTERFACE_HPP
2#define PROTEUS_LAMBDA_INTERFACE_HPP
3
4#include <llvm/ADT/DenseMap.h>
5#include <llvm/ADT/StringRef.h>
6#include <llvm/Demangle/Demangle.h>
7
9#include "proteus/Debug.h"
10#include "proteus/Error.h"
11#include "proteus/Logger.hpp"
12
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 PROTEUS_ENABLE_DEBUG
38 Logger::logs("proteus")
39 << "Operator " << Operator << "\n=> LambdaType to match " << LambdaType
40 << "\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#endif
47
48 const auto It = JitVariableMap.find(LambdaType);
49 if (It == JitVariableMap.end())
50 return std::nullopt;
51
52 return It;
53 }
54
56 PendingJitVariables.emplace_back(RC);
57 }
58
59 // The LambdaType input argument is created as a global variable in the
60 // ProteusPass, thus it has program-wide lifetime. Hence it is valid for
61 // LambdaTypeRef to store a reference to it.
62 inline void registerLambda(const char *LambdaType) {
63 const StringRef LambdaTypeRef{LambdaType};
64 PROTEUS_DBG(Logger::logs("proteus")
65 << "=> RegisterLambda " << LambdaTypeRef << "\n");
66 // Copy PendingJitVariables if there were changed, otherwise the runtime
67 // values for the lambda definition have not changed.
68 if (!PendingJitVariables.empty()) {
69 JitVariableMap[LambdaTypeRef] = PendingJitVariables;
70 PendingJitVariables.clear();
71 }
72 }
73
74 const SmallVector<RuntimeConstant> &getJitVariables(StringRef LambdaTypeRef) {
75 return JitVariableMap[LambdaTypeRef];
76 }
77
78 bool empty() { return JitVariableMap.empty(); }
79
80private:
81 explicit LambdaRegistry() = default;
82 SmallVector<RuntimeConstant> PendingJitVariables;
83 DenseMap<StringRef, SmallVector<RuntimeConstant>> JitVariableMap;
84};
85
86} // namespace proteus
87
88#endif
#define PROTEUS_DBG(x)
Definition Debug.h:7
Definition LambdaRegistry.hpp:19
void registerLambda(const char *LambdaType)
Definition LambdaRegistry.hpp:62
void pushJitVariable(RuntimeConstant &RC)
Definition LambdaRegistry.hpp:55
std::optional< DenseMap< StringRef, SmallVector< RuntimeConstant > >::iterator > matchJitVariableMap(StringRef FnName)
Definition LambdaRegistry.hpp:27
const SmallVector< RuntimeConstant > & getJitVariables(StringRef LambdaTypeRef)
Definition LambdaRegistry.hpp:74
static LambdaRegistry & instance()
Definition LambdaRegistry.hpp:21
bool empty()
Definition LambdaRegistry.hpp:78
static llvm::raw_ostream & logs(const std::string &Name)
Definition Logger.hpp:18
Definition JitEngine.cpp:20
Definition CompilerInterfaceTypes.h:30