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