Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
MemoryCache.h
Go to the documentation of this file.
1//===-- MemoryCache.h -- 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_H
12#define PROTEUS_JITCACHE_H
13
14#include "proteus/impl/Config.h"
17#include "proteus/impl/Utils.h"
18
19#include <llvm/ADT/StringRef.h>
20#include <llvm/Demangle/Demangle.h>
21
22#include <cstdint>
23#include <iostream>
24#include <unordered_map>
25
26namespace proteus {
27
28using namespace llvm;
29
30template <typename Function_t> class MemoryCache {
31public:
32 MemoryCache(const std::string &Label)
33 : Label(Label), DistributedRank(getDistributedRank()) {}
34 Function_t lookup(HashT &HashValue) {
35 TIMESCOPE("lookup");
36 Accesses++;
37
38 auto It = CacheMap.find(HashValue);
39 if (It == CacheMap.end())
40 return nullptr;
41
42 It->second.NumExecs++;
43 It->second.NumHits++;
44 Hits++;
45 return It->second.FunctionPtr;
46 }
47
48 void insert(HashT &HashValue, Function_t FunctionPtr, StringRef FnName) {
49 if (Config::get().ProteusDebugOutput) {
50 if (CacheMap.count(HashValue))
51 reportFatalError("MemoryCache collision detected");
52 }
53
54 auto &CacheEntry = CacheMap[HashValue];
55 CacheEntry.FunctionPtr = FunctionPtr;
56 CacheEntry.NumExecs = 1;
57 CacheEntry.NumHits = 0;
58 CacheEntry.FnName = FnName.str();
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 std::cout << " FnName " << JCE.FnName;
70 std::cout << "\n";
71 }
72 }
73
75 if (!Config::get().traceKernels())
76 return;
77
78 if (CacheMap.empty())
79 return;
80
81 struct KernelStats {
82 size_t Specializations = 0;
83 uint64_t TotalLaunches = 0;
84 };
85 std::unordered_map<std::string, KernelStats> Grouped;
86 for (const auto &[HashValue, JCE] : CacheMap) {
87 auto &KS = Grouped[JCE.FnName];
88 KS.Specializations++;
89 KS.TotalLaunches += JCE.NumExecs;
90 }
91
92 printf("[proteus][%s] === Kernel Trace (rank %s) ===\n", Label.c_str(),
93 DistributedRank.c_str());
94 for (const auto &[MangledName, KS] : Grouped) {
95 std::string Name = llvm::demangle(MangledName);
96 printf("[proteus][%s] %s rank=%s specializations=%zu launches=%lu\n",
97 Label.c_str(), Name.c_str(), DistributedRank.c_str(),
98 KS.Specializations, KS.TotalLaunches);
99 }
100 printf("[proteus][%s] === End Kernel Trace ===\n", Label.c_str());
101 }
102
103private:
104 struct MemoryCacheEntry {
105 Function_t FunctionPtr;
106 uint64_t NumExecs;
107 uint64_t NumHits;
108 std::string FnName;
109 };
110
111 std::unordered_map<HashT, MemoryCacheEntry> CacheMap;
112 uint64_t Hits = 0;
113 uint64_t Accesses = 0;
114 const std::string Label;
115 const std::string DistributedRank;
116};
117
118} // namespace proteus
119
120#endif
#define TIMESCOPE(x)
Definition TimeTracing.h:59
std::string getDistributedRank()
Definition Utils.h:55
static Config & get()
Definition Config.h:334
bool ProteusDebugOutput
Definition Config.h:350
Definition Hashing.h:21
Definition MemoryCache.h:30
void printStats()
Definition MemoryCache.h:61
void insert(HashT &HashValue, Function_t FunctionPtr, StringRef FnName)
Definition MemoryCache.h:48
void printKernelTrace()
Definition MemoryCache.h:74
MemoryCache(const std::string &Label)
Definition MemoryCache.h:32
Function_t lookup(HashT &HashValue)
Definition MemoryCache.h:34
Definition CompiledLibrary.h:7
Definition MemoryCache.h:26
void reportFatalError(const llvm::Twine &Reason, const char *FILE, unsigned Line)
Definition Error.cpp:14
Definition ObjectCache.h:27