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