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 <cstdint>
15#include <iostream>
16
17#include <llvm/ADT/StringRef.h>
18
19#include "proteus/Hashing.hpp"
21#include "proteus/Utils.h"
22
23namespace proteus {
24
25using namespace llvm;
26
27template <typename Function_t> class MemoryCache {
28public:
29 MemoryCache(const std::string &Label)
30 : Label(Label), DistributedRank(getDistributedRank()) {}
31 Function_t lookup(HashT &HashValue) {
32 TIMESCOPE("lookup");
33 Accesses++;
34
35 auto It = CacheMap.find(HashValue);
36 if (It == CacheMap.end())
37 return nullptr;
38
39 It->second.NumExecs++;
40 It->second.NumHits++;
41 Hits++;
42 return It->second.FunctionPtr;
43 }
44
45 void insert(HashT &HashValue, Function_t FunctionPtr, StringRef FnName) {
46 if (Config::get().ProteusDebugOutput) {
47 if (CacheMap.count(HashValue))
48 PROTEUS_FATAL_ERROR("MemoryCache collision detected");
49 }
50
51 auto &CacheEntry = CacheMap[HashValue];
52 CacheEntry.FunctionPtr = FunctionPtr;
53 CacheEntry.NumExecs = 1;
54 CacheEntry.NumHits = 0;
55
56 if (Config::get().ProteusDebugOutput) {
57 CacheEntry.FnName = FnName.str();
58 }
59 }
60
61 void printStats() {
62 printf("[proteus][%s] MemoryCache rank %s hits %lu accesses %lu\n",
63 Label.c_str(), DistributedRank.c_str(), Hits, Accesses);
64 for (const auto &[HashValue, JCE] : CacheMap) {
65 std::cout << "[proteus][" << Label << "] MemoryCache rank "
66 << DistributedRank << " HashValue " << HashValue.toString()
67 << " NumExecs " << JCE.NumExecs << " NumHits " << JCE.NumHits;
69 printf(" FnName %s", JCE.FnName.c_str());
70 }
71 printf("\n");
72 }
73 }
74
75private:
76 struct MemoryCacheEntry {
77 Function_t FunctionPtr;
78 uint64_t NumExecs;
79 uint64_t NumHits;
80 std::string FnName;
81 };
82
83 std::unordered_map<HashT, MemoryCacheEntry> CacheMap;
84 uint64_t Hits = 0;
85 uint64_t Accesses = 0;
86 const std::string Label;
87 const std::string DistributedRank;
88};
89
90} // namespace proteus
91
92#endif
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:7
#define TIMESCOPE(x)
Definition TimeTracing.hpp:64
std::string getDistributedRank()
Definition Utils.h:32
static Config & get()
Definition Config.hpp:298
bool ProteusDebugOutput
Definition Config.hpp:314
Definition Hashing.hpp:20
Definition MemoryCache.hpp:27
void printStats()
Definition MemoryCache.hpp:61
void insert(HashT &HashValue, Function_t FunctionPtr, StringRef FnName)
Definition MemoryCache.hpp:45
MemoryCache(const std::string &Label)
Definition MemoryCache.hpp:29
Function_t lookup(HashT &HashValue)
Definition MemoryCache.hpp:31
Definition Helpers.h:138
Definition StorageCache.cpp:24