Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
MemoryCache.hpp
Go to the documentation of this file.
1//===-- MemoryCache.hpp -- In-memory code cache header implementation --===//
2//
3// Part of the Proteus Project, under the Apache License v2.0 with LLVM
4// Exceptions. See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef PROTEUS_JITCACHE_HPP
12#define PROTEUS_JITCACHE_HPP
13
14#include "proteus/Config.hpp"
15#include "proteus/Hashing.hpp"
17#include "proteus/Utils.h"
18
19#include <llvm/ADT/StringRef.h>
20
21#include <cstdint>
22#include <iostream>
23
24namespace proteus {
25
26using namespace llvm;
27
28template <typename Function_t> class MemoryCache {
29public:
30 MemoryCache(const std::string &Label)
31 : Label(Label), DistributedRank(getDistributedRank()) {}
32 Function_t lookup(HashT &HashValue) {
33 TIMESCOPE("lookup");
34 Accesses++;
35
36 auto It = CacheMap.find(HashValue);
37 if (It == CacheMap.end())
38 return nullptr;
39
40 It->second.NumExecs++;
41 It->second.NumHits++;
42 Hits++;
43 return It->second.FunctionPtr;
44 }
45
46 void insert(HashT &HashValue, Function_t FunctionPtr, StringRef FnName) {
47 if (Config::get().ProteusDebugOutput) {
48 if (CacheMap.count(HashValue))
49 reportFatalError("MemoryCache collision detected");
50 }
51
52 auto &CacheEntry = CacheMap[HashValue];
53 CacheEntry.FunctionPtr = FunctionPtr;
54 CacheEntry.NumExecs = 1;
55 CacheEntry.NumHits = 0;
56
57 if (Config::get().ProteusDebugOutput) {
58 CacheEntry.FnName = FnName.str();
59 }
60 }
61
62 void printStats() {
63 printf("[proteus][%s] MemoryCache rank %s hits %lu accesses %lu\n",
64 Label.c_str(), DistributedRank.c_str(), Hits, Accesses);
65 for (const auto &[HashValue, JCE] : CacheMap) {
66 std::cout << "[proteus][" << Label << "] MemoryCache rank "
67 << DistributedRank << " HashValue " << HashValue.toString()
68 << " NumExecs " << JCE.NumExecs << " NumHits " << JCE.NumHits;
70 printf(" FnName %s", JCE.FnName.c_str());
71 }
72 printf("\n");
73 }
74 }
75
76private:
77 struct MemoryCacheEntry {
78 Function_t FunctionPtr;
79 uint64_t NumExecs;
80 uint64_t NumHits;
81 std::string FnName;
82 };
83
84 std::unordered_map<HashT, MemoryCacheEntry> CacheMap;
85 uint64_t Hits = 0;
86 uint64_t Accesses = 0;
87 const std::string Label;
88 const std::string DistributedRank;
89};
90
91} // namespace proteus
92
93#endif
#define TIMESCOPE(x)
Definition TimeTracing.hpp:59
std::string getDistributedRank()
Definition Utils.h:33
static Config & get()
Definition Config.hpp:300
bool ProteusDebugOutput
Definition Config.hpp:316
Definition Hashing.hpp:21
Definition MemoryCache.hpp:28
void printStats()
Definition MemoryCache.hpp:62
void insert(HashT &HashValue, Function_t FunctionPtr, StringRef FnName)
Definition MemoryCache.hpp:46
MemoryCache(const std::string &Label)
Definition MemoryCache.hpp:30
Function_t lookup(HashT &HashValue)
Definition MemoryCache.hpp:32
Definition Helpers.h:141
Definition ObjectCacheChain.cpp:26
void reportFatalError(const llvm::Twine &Reason, const char *FILE, unsigned Line)
Definition Error.cpp:14
Definition ObjectCache.hpp:27