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 [[maybe_unused]] ArrayRef<RuntimeConstant> RCArray) {
51#if PROTEUS_ENABLE_DEBUG
52 if (CacheMap.count(HashValue))
53 PROTEUS_FATAL_ERROR("JitCache collision detected");
54#endif
55
56 auto &CacheEntry = CacheMap[HashValue];
57 CacheEntry.FunctionPtr = FunctionPtr;
58 CacheEntry.NumExecs = 1;
59 CacheEntry.NumHits = 0;
60
61#if PROTEUS_ENABLE_DEBUG
62 CacheEntry.FnName = FnName.str();
63 CacheEntry.RCVector = SmallVector<RuntimeConstant>{RCArray};
64#endif
65 }
66
67 void printStats() {
68 // outs() << "JitCache hits " << Hits << " total " << Accesses << "\n";
69 // Use printf to avoid re-ordering outputs by outs() in HIP.
70 printf("JitCache hits %lu total %lu\n", Hits, Accesses);
71 for (const auto &[HashValue, JCE] : CacheMap) {
72 std::cout << "HashValue " << HashValue.toString() << " NumExecs "
73 << JCE.NumExecs << " NumHits " << JCE.NumHits;
74#if PROTEUS_ENABLE_DEBUG
75 // outs() << " FnName " << JCE.FnName << " RCs [";
76 printf(" FnName %s RCs [", JCE.FnName.c_str());
77 for (auto &RC : JCE.RCVector)
78 // outs() << RC.Int64Val << ", ";
79 printf("%ld, ", RC.Value.Int64Val);
80 // outs() << "]";
81 printf("]");
82#endif
83 // outs() << "\n";
84 printf("\n");
85 }
86 }
87
89
90private:
91 struct JitCacheEntry {
92 Function_t FunctionPtr;
93 uint64_t NumExecs;
94 uint64_t NumHits;
95#if PROTEUS_ENABLE_DEBUG
96 std::string FnName;
97 SmallVector<RuntimeConstant> RCVector;
98#endif
99 };
100
101 std::unordered_map<HashT, JitCacheEntry> CacheMap;
102 // Use the executable binary path when hashing to differentiate between
103 // same-named kernels generated by other executables.
104 uint64_t Hits = 0;
105 uint64_t Accesses = 0;
106};
107
108} // namespace proteus
109
110#endif
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:4
#define TIMESCOPE(x)
Definition TimeTracing.hpp:64
Definition Hashing.hpp:19
Definition JitCache.hpp:32
void insert(HashT &HashValue, Function_t FunctionPtr, StringRef FnName, ArrayRef< RuntimeConstant > RCArray)
Definition JitCache.hpp:48
Function_t lookup(HashT &HashValue)
Definition JitCache.hpp:34
void printStats()
Definition JitCache.hpp:67
JitCache()
Definition JitCache.hpp:88
Definition Dispatcher.cpp:14