llvm.org GIT mirror llvm / 4442b6e
Add an "-object-cache-dir=<string>" option to LLI. This option specifies the root path to which object files managed by the LLIObjectCache instance should be written. This option defaults to "", in which case objects are cached in the same directory as the bitcode they are derived from. The load-object-a.ll test has been rewritten to use this option to support testing in environments where the test directory is not writable. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@198852 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 6 years ago
2 changed file(s) with 29 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
0 ; REQUIRES: shell
11
22 ; This first line will generate the .o files for the next run line
3 ; RUN: %lli_mcjit -extra-module=%p/Inputs/multi-module-b.ll -extra-module=%p/Inputs/multi-module-c.ll -enable-cache-manager %s
3 ; RUN: mkdir -p %t.cachedir
4 ; RUN: %lli_mcjit -extra-module=%p/Inputs/multi-module-b.ll -extra-module=%p/Inputs/multi-module-c.ll -enable-cache-manager -object-cache-dir=%t.cachedir %s
45
56 ; This line tests MCJIT object loading
6 ; RUN: %lli_mcjit -extra-object=%p/Inputs/multi-module-b.o -extra-object=%p/Inputs/multi-module-c.o %s
7 ; RUN: %lli_mcjit -extra-object=%t.cachedir/%p/Inputs/multi-module-b.o -extra-object=%t.cachedir/%p/Inputs/multi-module-c.o %s
78
89 ; These lines put the object files into an archive
9 ; RUN: llvm-ar r %p/Inputs/load-object.a %p/Inputs/multi-module-b.o
10 ; RUN: llvm-ar r %p/Inputs/load-object.a %p/Inputs/multi-module-c.o
10 ; RUN: llvm-ar r %t.cachedir/%p/Inputs/load-object.a %t.cachedir/%p/Inputs/multi-module-b.o
11 ; RUN: llvm-ar r %t.cachedir/%p/Inputs/load-object.a %t.cachedir/%p/Inputs/multi-module-c.o
1112
1213 ; This line test MCJIT archive loading
13 ; RUN: %lli_mcjit -extra-archive=%p/Inputs/load-object.a %s
14
15 ; These lines clean up our temporary files
16 ; RUN: rm -f %p/Inputs/load-object-a.o
17 ; RUN: rm -f %p/Inputs/multi-module-b.o
18 ; RUN: rm -f %p/Inputs/multi-module-c.o
19 ; RUN: rm -f %p/Inputs/load-object.a
14 ; RUN: %lli_mcjit -extra-archive=%t.cachedir/%p/Inputs/load-object.a %s
2015
2116 declare i32 @FB()
2217
152152
153153 cl::opt
154154 EnableCacheManager("enable-cache-manager",
155 cl::desc("Use cache manager to save/load mdoules."),
155 cl::desc("Use cache manager to save/load mdoules"),
156156 cl::init(false));
157
158 cl::opt
159 ObjectCacheDir("object-cache-dir",
160 cl::desc("Directory to store cached object files "
161 "(must be user writable)"),
162 cl::init(""));
157163
158164 cl::opt
159165 FakeArgv0("fake-argv0",
239245 //===----------------------------------------------------------------------===//
240246 // Object cache
241247 //
242 // This object cache implementation writes cached objects to disk using a
243 // filename provided in the module descriptor and tries to load a saved object
244 // using that filename if the file exists.
248 // This object cache implementation writes cached objects to disk to the
249 // directory specified by CacheDir, using a filename provided in the module
250 // descriptor. The cache tries to load a saved object using that path if the
251 // file exists. CacheDir defaults to "", in which case objects are cached
252 // alongside their originating bitcodes.
245253 //
246254 class LLIObjectCache : public ObjectCache {
247255 public:
248 LLIObjectCache() { }
256 LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) {
257 // Add trailing '/' to cache dir if necessary.
258 if (!this->CacheDir.empty() && this->CacheDir.back() != '/')
259 this->CacheDir += '/';
260 }
249261 virtual ~LLIObjectCache() {}
250262
251263 virtual void notifyObjectCompiled(const Module *M, const MemoryBuffer *Obj) {
254266 if (!getCacheFilename(ModuleID, CacheName))
255267 return;
256268 std::string errStr;
269 if (!CacheDir.empty()) // Create user-defined cache dir.
270 sys::fs::create_directories(CacheName.substr(0, CacheName.rfind('/')));
257271 raw_fd_ostream outfile(CacheName.c_str(), errStr, sys::fs::F_Binary);
258272 outfile.write(Obj->getBufferStart(), Obj->getBufferSize());
259273 outfile.close();
278292 }
279293
280294 private:
295 std::string CacheDir;
296
281297 bool getCacheFilename(const std::string &ModID, std::string &CacheName) {
282298 std::string Prefix("file:");
283299 size_t PrefixLength = Prefix.length();
284300 if (ModID.substr(0, PrefixLength) != Prefix)
285301 return false;
286 CacheName = ModID.substr(PrefixLength);
302 CacheName = CacheDir + ModID.substr(PrefixLength);
287303 size_t pos = CacheName.rfind('.');
288304 CacheName.replace(pos, CacheName.length() - pos, ".o");
289305 return true;
475491 }
476492
477493 if (EnableCacheManager) {
478 CacheManager = new LLIObjectCache;
494 CacheManager = new LLIObjectCache(ObjectCacheDir);
479495 EE->setObjectCache(CacheManager);
480496 }
481497