Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
JitStorageCache.hpp
Go to the documentation of this file.
1//===-- JitStorageCache.hpp -- JIT storage-based cache header impl. --===//
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_JITSTOREDCACHE_HPP
12#define PROTEUS_JITSTOREDCACHE_HPP
13
14#include <cstdint>
15#include <filesystem>
16#include <llvm/Bitcode/BitcodeWriter.h>
17#include <llvm/IR/Module.h>
18#include <llvm/Support/ErrorHandling.h>
19#include <llvm/Support/MemoryBuffer.h>
20#include <llvm/Support/MemoryBufferRef.h>
21
22#include <llvm/ADT/StringRef.h>
23
24#include "proteus/Hashing.hpp"
25#include "proteus/Utils.h"
26
27namespace proteus {
28
29using namespace llvm;
30
31// NOTE: Storage cache assumes that stored code is re-usable across runs!
32// TODO: Source code changes should invalidate the cache. Also, if storing
33// assembly (PTX) or binary (ELF), then device globals may have different
34// addresses that render it invalid. In this case, store LLVM IR to re-link
35// globals.
36template <typename Function_t> class JitStorageCache {
37public:
38 JitStorageCache() { std::filesystem::create_directory(StorageDirectory); }
39
40 std::unique_ptr<MemoryBuffer> lookup(HashT &HashValue) {
41 TIMESCOPE("object lookup");
42 Accesses++;
43
44 std::string Filebase =
45 StorageDirectory + "/cache-jit-" + HashValue.toString();
46
47 auto CacheBuf = MemoryBuffer::getFile(Filebase + ".o");
48 if (!CacheBuf)
49 return nullptr;
50
51 Hits++;
52 return std::move(CacheBuf.get());
53 }
54
55 void store(HashT &HashValue, MemoryBufferRef ObjBufRef) {
56 TIMESCOPE("Store cache");
57
58 std::string Filebase =
59 StorageDirectory + "/cache-jit-" + HashValue.toString();
60
61 saveToFile(Filebase + ".o", StringRef{ObjBufRef.getBufferStart(),
62 ObjBufRef.getBufferSize()});
63 }
64
65 void printStats() {
66 // Use printf to avoid re-ordering outputs by outs() in HIP.
67 printf("JitStorageCache hits %lu total %lu\n", Hits, Accesses);
68 }
69
70private:
71 uint64_t Hits = 0;
72 uint64_t Accesses = 0;
73 const std::string StorageDirectory = ".proteus";
74};
75
76} // namespace proteus
77
78#endif
#define TIMESCOPE(x)
Definition TimeTracing.hpp:35
void saveToFile(llvm::StringRef Filepath, T &&Data)
Definition Utils.h:23
Definition Hashing.hpp:19
std::string toString() const
Definition Hashing.hpp:27
Definition JitStorageCache.hpp:36
void printStats()
Definition JitStorageCache.hpp:65
std::unique_ptr< MemoryBuffer > lookup(HashT &HashValue)
Definition JitStorageCache.hpp:40
JitStorageCache()
Definition JitStorageCache.hpp:38
void store(HashT &HashValue, MemoryBufferRef ObjBufRef)
Definition JitStorageCache.hpp:55
Definition JitEngine.cpp:20