source: icGREP/icgrep-devel/icgrep/object_cache.cpp @ 5377

Last change on this file since 5377 was 5267, checked in by nmedfort, 3 years ago

Code clean-up. Removed Pablo Call, SetIthBit? and Prototype.

File size: 3.0 KB
Line 
1#include <string>
2
3#include "object_cache.h"
4#include <llvm/Support/raw_ostream.h>
5#include <llvm/Support/FileSystem.h>
6#include <llvm/Support/Path.h>
7#include <llvm/IR/Module.h>
8#ifdef OBJECT_CACHE_DEBUG
9#include <iostream>
10#endif
11
12using namespace llvm;
13
14//===----------------------------------------------------------------------===//
15// Object cache (based on tools/lli/lli.cpp, LLVM 3.6.1)
16//
17// This object cache implementation writes cached objects to disk to the
18// directory specified by CacheDir, using a filename provided in the module
19// descriptor. The cache tries to load a saved object using that path if the
20// file exists.
21//
22ICGrepObjectCache::ICGrepObjectCache(const std::string &dir): CacheDir(dir) {}
23
24ICGrepObjectCache::ICGrepObjectCache() {
25    // $HOME/.cache/icgrep
26    // TODO use path::user_cache_directory once we have llvm >= 3.7.
27    sys::path::home_directory(CacheDir);
28    sys::path::append(CacheDir, ".cache", "icgrep");
29}
30
31ICGrepObjectCache::~ICGrepObjectCache() {}
32
33void ICGrepObjectCache::notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) {
34    const std::string &ModuleID = M->getModuleIdentifier();
35    Path CacheName(CacheDir);
36    if (!getCacheFilename(ModuleID, CacheName))
37        return;
38    if (!CacheDir.empty())      // Re-creating an existing directory is fine.
39        sys::fs::create_directories(Twine(CacheDir));
40    std::error_code EC;
41    raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None);
42    outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
43    outfile.close();
44#ifdef OBJECT_CACHE_DEBUG
45    std::cerr << "Cache created: " << CacheName.c_str() << std::endl;
46#endif
47}
48
49std::unique_ptr<MemoryBuffer> ICGrepObjectCache::getObject(const Module* M) {
50    const std::string &ModuleID = M->getModuleIdentifier();
51    Path CacheName(CacheDir);
52    if (!getCacheFilename(ModuleID, CacheName))
53        return nullptr;
54    // Load the object from the cache filename
55    ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer =
56        MemoryBuffer::getFile(CacheName.c_str(), -1, false);
57    // If the file isn't there, that's OK.
58    if (!IRObjectBuffer)
59        return nullptr;
60#ifdef OBJECT_CACHE_DEBUG
61    std::cerr << "Found cached object." << std::endl;
62#endif
63    // MCJIT will want to write into this buffer, and we don't want that
64    // because the file has probably just been mmapped.  Instead we make
65    // a copy.  The filed-based buffer will be released when it goes
66    // out of scope.
67    return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
68}
69
70bool ICGrepObjectCache::getCacheFilename(const std::string &ModID, Path &CacheName) {
71#ifdef OBJECT_CACHE_DEBUG
72    std::cerr << "ModuleID: " << ModID << std::endl;
73#endif
74    const std::string Prefix("grepcode:");
75    size_t PrefixLength = Prefix.length();
76    if (ModID.substr(0, PrefixLength) != Prefix)
77        return false;
78
79    CacheName = CacheDir;
80    sys::path::append(CacheName, ModID.substr(PrefixLength) + ".o");
81#ifdef OBJECT_CACHE_DEBUG
82    std::cerr << "CacheName: " << CacheName.c_str() << std::endl;
83#endif
84    return true;
85}
Note: See TracBrowser for help on using the repository browser.