Changeset 5394 for icGREP


Ignore:
Timestamp:
Apr 6, 2017, 11:44:08 AM (2 years ago)
Author:
cameron
Message:

Object cache system with signature checking complete

Location:
icGREP/icgrep-devel/icgrep
Files:
4 edited

Legend:

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

    r5392 r5394  
    77#include <llvm/IR/Module.h>
    88
    9 
    10 #define OBJECT_CACHE_DEBUG
    119#ifdef OBJECT_CACHE_DEBUG
    1210#include <iostream>
     
    3937ParabixObjectCache::~ParabixObjectCache() {}
    4038
     39// A new module has been compiled.   If it is cacheable and no conflicting module
     40// exists, write it out. 
    4141void ParabixObjectCache::notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) {
    4242    const std::string &ModuleID = M->getModuleIdentifier();
    43    
     43    auto f = cachedObjectMap.find(ModuleID);
     44    if (f!= cachedObjectMap.end()) return;
    4445    Path CacheName(CacheDir);
    4546    if (!getCacheFilename(ModuleID, CacheName)) return;
     
    5152    outfile.close();
    5253#ifdef OBJECT_CACHE_DEBUG
    53     std::cerr << "Cache created: " << CacheName.c_str() << std::endl;
     54    std::cerr << "Cache file created: " << CacheName.c_str() << std::endl;
    5455#endif
     56    auto s = kernelSignatureMap.find(ModuleID);  // Check for a kernel signature.
     57    if (s != kernelSignatureMap.end()) {
     58        if (s->second == ModuleID) return;  // No signature is written when the signature is the ModuleID.
     59        sys::path::replace_extension(CacheName, ".sig");
     60        raw_fd_ostream sigfile(CacheName, EC, sys::fs::F_None);
     61        sigfile << s->second;
     62        sigfile.close();
     63#ifdef OBJECT_CACHE_DEBUG
     64        std::cerr << "Signature file created: " << CacheName.c_str() << std::endl;
     65#endif
     66    }
    5567}
    5668
    57 
    58 bool ParabixObjectCache::loadCachedObjectFile(const Module* M) {
    59     const std::string ModuleID = M->getModuleIdentifier();
    60     auto f = cachedObjectMap.find(ModuleID);
    61     if (f != cachedObjectMap.end()) {
    62         return true;
     69bool ParabixObjectCache::loadCachedObjectFile(std::string ModuleID, std::string signature) {
     70    // Have we already seen this module before.
     71    auto s = kernelSignatureMap.find(ModuleID);
     72    if (s!= kernelSignatureMap.end()) {
     73        if (s->second != signature) {
     74#ifdef OBJECT_CACHE_DEBUG
     75            std::cerr << "loadCachedObjectFile:  conflicting signatures for the same moduleID! " << ModuleID << std::endl;
     76#endif
     77            return false;
     78        }
     79        // A cached entry exists if it has already been loaded.
     80        return cachedObjectMap.find(ModuleID) != cachedObjectMap.end();
    6381    }
     82    // Confirm that the module is cacheable.
    6483    Path CachedObjectName(CacheDir);
    6584    if (!getCacheFilename(ModuleID, CachedObjectName)) return false;
     85    //
     86    // Save the signature.
     87    kernelSignatureMap.emplace(ModuleID, signature);
     88    //
     89    // Now checkfor a cache file.
    6690    ErrorOr<std::unique_ptr<MemoryBuffer>> KernelObjectBuffer = MemoryBuffer::getFile(CachedObjectName.c_str(), -1, false);
    6791    if (!KernelObjectBuffer) return false;
     92    //
     93    if (ModuleID != signature) {
     94        // Confirm the signature.
     95        sys::path::replace_extension(CachedObjectName, ".sig");
     96        ErrorOr<std::unique_ptr<MemoryBuffer>> SignatureBuffer = MemoryBuffer::getFile(CachedObjectName.c_str(), -1, false);
     97        if (!SignatureBuffer) {
     98#ifdef OBJECT_CACHE_DEBUG
     99            std::cerr << "signature file expected but not Found. " << ModuleID << std::endl;
     100#endif
     101            return false;
     102        }
     103        StringRef loadedSig = SignatureBuffer.get()->getBuffer();
     104        StringRef computedSig = signature;
     105        if (!computedSig.equals(loadedSig)) {
     106#ifdef OBJECT_CACHE_DEBUG
     107            std::cerr << "computed signature does not match stored signature: " << ModuleID << std::endl;
     108#endif
     109            return false;
     110        }
     111        // Signature is confirmed.
     112#ifdef OBJECT_CACHE_DEBUG
     113        std::cerr << "loadCachedObjectFile: computed signature matches stored signature. " << ModuleID << std::endl;
     114#endif
     115    }
     116#ifdef OBJECT_CACHE_DEBUG
     117    std::cerr << "Found cached object." << CachedObjectName.c_str() << std::endl;
     118#endif
    68119    // Make a copy so that the JIT engine can freely modify it.
    69120    cachedObjectMap.emplace(ModuleID, std::move(KernelObjectBuffer.get()));
     
    71122}
    72123
     124/*  May need this.
     125 
     126void ParabixObjectCache::removeCacheFile(std::string ModuleID) {
     127    Path CacheName(CacheDir);
     128    if (!getCacheFilename(ModuleID, CacheName)) return;
     129    sys::fs::remove(CacheName);
     130    // Also remove a signature file, if present.
     131    sys::path::replace_extension(CacheName, ".sig");
     132    sys::fs::remove(CacheName);
     133}
     134*/
    73135
    74136std::unique_ptr<MemoryBuffer> ParabixObjectCache::getObject(const Module* M) {
     
    79141    }
    80142#ifdef OBJECT_CACHE_DEBUG
    81     std::cerr << "Found cached object." << std::endl;
     143    std::cerr << "Object retrieved by engine. "<< ModuleID << std::endl;
    82144#endif
    83145    // Return a copy of the buffer, for MCJIT to modify, if necessary.
     
    86148
    87149bool ParabixObjectCache::getCacheFilename(const std::string &ModID, Path &CacheName) {
    88 #ifdef OBJECT_CACHE_DEBUG
    89     std::cerr << "ModuleID: " << ModID << std::endl;
    90 #endif
    91150    const std::string Prefix("Parabix:");
    92151    size_t PrefixLength = Prefix.length();
  • icGREP/icgrep-devel/icgrep/object_cache.h

    r5391 r5394  
    1111#include <llvm/ExecutionEngine/ObjectCache.h>
    1212#include <llvm/Support/MemoryBuffer.h>
     13#include <llvm/ADT/StringRef.h>
    1314#include <string>
    1415#include <map>
     
    3536
    3637        void notifyObjectCompiled(const llvm::Module *M, llvm::MemoryBufferRef Obj) override;
    37         bool loadCachedObjectFile(const llvm::Module* M);
     38        bool loadCachedObjectFile(std::string ModuleID, std::string signature);
    3839        std::unique_ptr<llvm::MemoryBuffer> getObject(const llvm::Module* M) override;
    3940   
    4041    private:
     42        std::map<std::string, std::string> kernelSignatureMap;
    4143        std::map<std::string, std::unique_ptr<llvm::MemoryBuffer>> cachedObjectMap;
    4244        using Path = llvm::SmallString<128>;
  • icGREP/icgrep-devel/icgrep/toolchain.cpp

    r5391 r5394  
    379379void ParabixDriver::linkAndFinalize() {
    380380    for (auto kb : mKernelList) {
     381        Module * saveM = iBuilder->getModule();
    381382        std::unique_ptr<Module> km = kb->createKernelStub();
    382         if (!(mCache && mCache->loadCachedObjectFile(km.get()))) {
    383             Module * saveM = iBuilder->getModule();
     383        std::string moduleID = km->getModuleIdentifier();
     384        std::string signature;
     385        if (kb->moduleIDisSignature()) {
     386            signature = moduleID;
     387        }
     388        else {
     389            kb->generateKernelSignature(signature);
     390        }
     391        if (!(mCache && mCache->loadCachedObjectFile(moduleID, signature))) {
    384392            iBuilder->setModule(km.get());
    385393            kb->generateKernel();
    386             iBuilder->setModule(saveM);
    387         }
     394        }
     395        iBuilder->setModule(saveM);
    388396        mEngine->addModule(std::move(km));
    389397    }
  • icGREP/icgrep-devel/icgrep/wc.cpp

    r5391 r5394  
    163163    pxDriver.addKernelCall(s2pk, {&ByteStream}, {&BasisBits});
    164164   
    165     PabloKernel wck(iBuilder, "wc",
     165    PabloKernel wck(iBuilder, "Parabix:wc",
    166166        {Binding{iBuilder->getStreamSetTy(8, 1), "u8bit"}},
    167167        {},
Note: See TracChangeset for help on using the changeset viewer.