Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
JitCache.hpp
Go to the documentation of this file.
1//===-- JitCache.hpp -- JIT 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#include <mutex>
17
19#include "proteus/Hashing.hpp"
21#include "proteus/Utils.h"
22
23#include <llvm/ADT/DenseMap.h>
24#include <llvm/ADT/SmallVector.h>
25#include <llvm/ADT/StringRef.h>
26#include <llvm/Config/llvm-config.h>
27
28namespace proteus {
29
30using namespace llvm;
31
32template <typename Function_t> class JitCache {
33public:
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,
49 [[maybe_unused]] StringRef FnName) {
50#if PROTEUS_ENABLE_DEBUG
51 if (CacheMap.count(HashValue))
52 PROTEUS_FATAL_ERROR("JitCache collision detected");
53#endif
54
55 auto &CacheEntry = CacheMap[HashValue];
56 CacheEntry.FunctionPtr = FunctionPtr;
57 CacheEntry.NumExecs = 1;
58 CacheEntry.NumHits = 0;
59
60#if PROTEUS_ENABLE_DEBUG
61 CacheEntry.FnName = FnName.str();
62#endif
63 }
64
65 void printStats() {
66 // outs() << "JitCache hits " << Hits << " total " << Accesses << "\n";
67 // Use printf to avoid re-ordering outputs by outs() in HIP.
68 printf("JitCache hits %lu total %lu\n", Hits, Accesses);
69 for (const auto &[HashValue, JCE] : CacheMap) {
70 std::cout << "HashValue " << HashValue.toString() << " NumExecs "
71 << JCE.NumExecs << " NumHits " << JCE.NumHits;
72#if PROTEUS_ENABLE_DEBUG
73 printf(" FnName %s", JCE.FnName.c_str());
74#endif
75 printf("\n");
76 }
77 }
78
80
81private:
82 struct JitCacheEntry {
83 Function_t FunctionPtr;
84 uint64_t NumExecs;
85 uint64_t NumHits;
86#if PROTEUS_ENABLE_DEBUG
87 std::string FnName;
88#endif
89 };
90
91 std::unordered_map<HashT, JitCacheEntry> CacheMap;
92 // Use the executable binary path when hashing to differentiate between
93 // same-named kernels generated by other executables.
94 uint64_t Hits = 0;
95 uint64_t Accesses = 0;
96};
97
98} // namespace proteus
99
100#endif
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:7
#define TIMESCOPE(x)
Definition TimeTracing.hpp:64
Definition Hashing.hpp:20
Definition JitCache.hpp:32
void insert(HashT &HashValue, Function_t FunctionPtr, StringRef FnName)
Definition JitCache.hpp:48
Function_t lookup(HashT &HashValue)
Definition JitCache.hpp:34
void printStats()
Definition JitCache.hpp:65
JitCache()
Definition JitCache.hpp:79
Definition Helpers.h:76
Definition CppJitModule.cpp:21