+//===----------------------------------------------------------------------===//
+// Object cache
+//
+// This object cache implementation writes cached objects to disk to the
+// directory specified by CacheDir, using a filename provided in the module
+// descriptor. The cache tries to load a saved object using that path if the
+// file exists. CacheDir defaults to "", in which case objects are cached
+// alongside their originating bitcodes.
+//
+class LLIObjectCache : public ObjectCache {
+public:
+ LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) {
+ // Add trailing '/' to cache dir if necessary.
+ if (!this->CacheDir.empty() &&
+ this->CacheDir[this->CacheDir.size() - 1] != '/')
+ this->CacheDir += '/';
+ }
+ virtual ~LLIObjectCache() {}
+
+ void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
+ const std::string ModuleID = M->getModuleIdentifier();
+ std::string CacheName;
+ if (!getCacheFilename(ModuleID, CacheName))
+ return;
+ if (!CacheDir.empty()) { // Create user-defined cache dir.
+ SmallString<128> dir(CacheName);
+ sys::path::remove_filename(dir);
+ sys::fs::create_directories(Twine(dir));
+ }
+ std::error_code EC;
+ raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None);
+ outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
+ outfile.close();
+ }
+
+ std::unique_ptr<MemoryBuffer> getObject(const Module* M) override {
+ const std::string ModuleID = M->getModuleIdentifier();
+ std::string CacheName;
+ if (!getCacheFilename(ModuleID, CacheName))
+ return nullptr;
+ // Load the object from the cache filename
+ ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer =
+ MemoryBuffer::getFile(CacheName.c_str(), -1, false);
+ // If the file isn't there, that's OK.
+ if (!IRObjectBuffer)
+ return nullptr;
+ // MCJIT will want to write into this buffer, and we don't want that
+ // because the file has probably just been mmapped. Instead we make
+ // a copy. The filed-based buffer will be released when it goes
+ // out of scope.
+ return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
+ }
+
+private:
+ std::string CacheDir;
+
+ bool getCacheFilename(const std::string &ModID, std::string &CacheName) {
+ std::string Prefix("file:");
+ size_t PrefixLength = Prefix.length();
+ if (ModID.substr(0, PrefixLength) != Prefix)
+ return false;
+ std::string CacheSubdir = ModID.substr(PrefixLength);
+#if defined(_WIN32)
+ // Transform "X:\foo" => "/X\foo" for convenience.
+ if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
+ CacheSubdir[1] = CacheSubdir[0];
+ CacheSubdir[0] = '/';
+ }
+#endif
+ CacheName = CacheDir + CacheSubdir;
+ size_t pos = CacheName.rfind('.');
+ CacheName.replace(pos, CacheName.length() - pos, ".o");
+ return true;
+ }
+};
+
+static ExecutionEngine *EE = nullptr;
+static LLIObjectCache *CacheManager = nullptr;