Ignore:
Timestamp:
Apr 4, 2017, 12:09:16 PM (2 years ago)
Author:
cameron
Message:

ParabixDriver/ObjectCache? separate compilation and linking: initial check-in with wc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/object_cache.cpp

    r5267 r5391  
    66#include <llvm/Support/Path.h>
    77#include <llvm/IR/Module.h>
     8
     9
    810#ifdef OBJECT_CACHE_DEBUG
    911#include <iostream>
     
    2022// file exists.
    2123//
    22 ICGrepObjectCache::ICGrepObjectCache(const std::string &dir): CacheDir(dir) {}
     24ParabixObjectCache::ParabixObjectCache(const std::string &dir): CacheDir(dir) {}
    2325
    24 ICGrepObjectCache::ICGrepObjectCache() {
     26ParabixObjectCache::ParabixObjectCache() {
    2527    // $HOME/.cache/icgrep
    2628    // TODO use path::user_cache_directory once we have llvm >= 3.7.
    2729    sys::path::home_directory(CacheDir);
    28     sys::path::append(CacheDir, ".cache", "icgrep");
     30    std::string Version = PARABIX_VERSION;
     31    std::string Date = __DATE__;
     32    std::string Time = __TIME__;
     33    std::string DateStamp = Date.substr(7) + Date.substr(0,3) + (Date[4] == ' ' ? Date.substr(5,1) : Date.substr(4,2));
     34    std::string CacheSubDir = "Parabix" + Version + "_" + DateStamp + "@" + Time;
     35    sys::path::append(CacheDir, ".cache", CacheSubDir);
    2936}
    3037
    31 ICGrepObjectCache::~ICGrepObjectCache() {}
     38ParabixObjectCache::~ParabixObjectCache() {}
    3239
    33 void ICGrepObjectCache::notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) {
     40void ParabixObjectCache::notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) {
    3441    const std::string &ModuleID = M->getModuleIdentifier();
     42   
    3543    Path CacheName(CacheDir);
    36     if (!getCacheFilename(ModuleID, CacheName))
    37         return;
     44    if (!getCacheFilename(ModuleID, CacheName)) return;
    3845    if (!CacheDir.empty())      // Re-creating an existing directory is fine.
    3946        sys::fs::create_directories(Twine(CacheDir));
     
    4754}
    4855
    49 std::unique_ptr<MemoryBuffer> ICGrepObjectCache::getObject(const Module* M) {
     56
     57bool ParabixObjectCache::loadCachedObjectFile(const Module* M) {
     58    const std::string ModuleID = M->getModuleIdentifier();
     59    auto f = cachedObjectMap.find(ModuleID);
     60    if (f != cachedObjectMap.end()) {
     61        return true;
     62    }
     63    Path CachedObjectName(CacheDir);
     64    if (!getCacheFilename(ModuleID, CachedObjectName)) return false;
     65    ErrorOr<std::unique_ptr<MemoryBuffer>> KernelObjectBuffer = MemoryBuffer::getFile(CachedObjectName.c_str(), -1, false);
     66    if (!KernelObjectBuffer) return false;
     67    // Make a copy so that the JIT engine can freely modify it.
     68    cachedObjectMap.emplace(ModuleID, std::move(KernelObjectBuffer.get()));
     69    return true;
     70}
     71
     72
     73std::unique_ptr<MemoryBuffer> ParabixObjectCache::getObject(const Module* M) {
    5074    const std::string &ModuleID = M->getModuleIdentifier();
    51     Path CacheName(CacheDir);
    52     if (!getCacheFilename(ModuleID, CacheName))
     75    auto f = cachedObjectMap.find(ModuleID);
     76    if (f == cachedObjectMap.end()) {
    5377        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;
     78    }
    6079#ifdef OBJECT_CACHE_DEBUG
    6180    std::cerr << "Found cached object." << std::endl;
    6281#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());
     82    // Return a copy of the buffer, for MCJIT to modify, if necessary.
     83    return MemoryBuffer::getMemBufferCopy(f->second.get()->getBuffer());
    6884}
    6985
    70 bool ICGrepObjectCache::getCacheFilename(const std::string &ModID, Path &CacheName) {
     86bool ParabixObjectCache::getCacheFilename(const std::string &ModID, Path &CacheName) {
    7187#ifdef OBJECT_CACHE_DEBUG
    7288    std::cerr << "ModuleID: " << ModID << std::endl;
    7389#endif
    74     const std::string Prefix("grepcode:");
     90    const std::string Prefix("Parabix:");
    7591    size_t PrefixLength = Prefix.length();
    7692    if (ModID.substr(0, PrefixLength) != Prefix)
Note: See TracChangeset for help on using the changeset viewer.