source: icGREP/icgrep-devel/icgrep/toolchain/object_cache.h

Last change on this file was 6209, checked in by nmedfort, 8 months ago

Initial cache janitor daemon test

File size: 2.6 KB
Line 
1/*
2 *  Copyright (c) 2017 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#ifndef OBJECT_CACHE_H
8#define OBJECT_CACHE_H
9
10#include <llvm/ADT/SmallString.h>
11#include <llvm/ExecutionEngine/ObjectCache.h>
12#include <llvm/ADT/StringRef.h>
13#include <boost/container/flat_map.hpp>
14#include <string>
15
16namespace llvm { class Module; }
17namespace llvm { class MemoryBuffer; }
18namespace llvm { class MemoryBufferRef; }
19namespace llvm { class LLVMContext; }
20namespace kernel { class Kernel; }
21namespace kernel { class KernelBuilder; }
22
23// The ParabixObjectCache is a two-level cache compatible with the requirements
24// of the LLVM ExecutionEngine as well as the Parabix Kernel builder infrastructure.
25//
26// The ParabixObjectCache allows the CPUEngineInstance to look up cached modules based on a
27// module stub that contains only the necessary Module ID and signature (loadCachedObjectFile).
28// If found, the module object file is immediately loaded into the cachedObjectMap,
29// and later made available to the ExecutionEngine as needed.  Otherwise, false is
30// return to signal that a cached File is not found.  The CPUEngineInstance can then
31// apply the necessary kernel builder to build the full module IR before passing
32// it to the ExecutionEngine.
33//
34
35class ParabixObjectCache final : public llvm::ObjectCache {
36    template <typename K, typename V>
37    using Map = boost::container::flat_map<K, V>;
38    using ModuleCache = Map<std::string, std::pair<llvm::Module *, std::unique_ptr<llvm::MemoryBuffer>>>;
39    using Instance = std::unique_ptr<ParabixObjectCache>;
40public:
41    using Path = llvm::SmallString<128>;
42
43    static bool checkForCachedKernel(const std::unique_ptr<kernel::KernelBuilder> & b, kernel::Kernel * const kernel) noexcept;
44
45    static void initializeCacheSystems() noexcept;
46
47    static ParabixObjectCache * getInstance() {
48        return mInstance.get();
49    }
50
51    bool loadCachedObjectFile(const std::unique_ptr<kernel::KernelBuilder> & idb, kernel::Kernel * const kernel);
52
53    void notifyObjectCompiled(const llvm::Module * M, llvm::MemoryBufferRef Obj) override;
54
55    std::unique_ptr<llvm::MemoryBuffer> getObject(const llvm::Module * M) override;
56
57protected:
58
59    ParabixObjectCache();
60    void loadCacheSettings() noexcept;
61    void saveCacheSettings() noexcept;
62
63private:
64    void initiateCacheCleanUp() noexcept;
65    bool requiresCacheCleanUp() noexcept;
66private:
67    static Instance     mInstance;
68    ModuleCache         mCachedObject;
69    Path                mCachePath;
70};
71
72#endif
Note: See TracBrowser for help on using the repository browser.