diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp index 9e271a2c6ed0..fa6d711ae3de 100644 --- a/lld/ELF/LTO.cpp +++ b/lld/ELF/LTO.cpp @@ -150,10 +150,10 @@ std::vector BitcodeCompiler::compile() { // specified, configure LTO to use it as the cache directory. lto::NativeObjectCache Cache; if (!Config->ThinLTOCacheDir.empty()) - Cache = lto::localCache(Config->ThinLTOCacheDir, - [&](size_t Task, StringRef Path) { - Files[Task] = check(MemoryBuffer::getFile(Path)); - }); + Cache = check(lto::localCache( + Config->ThinLTOCacheDir, [&](size_t Task, StringRef Path) { + Files[Task] = check(MemoryBuffer::getFile(Path)); + })); checkError(LTOObj->run( [&](size_t Task) { diff --git a/lld/test/ELF/lto/cache.ll b/lld/test/ELF/lto/cache.ll index a7475758b703..2697461d8cc8 100644 --- a/lld/test/ELF/lto/cache.ll +++ b/lld/test/ELF/lto/cache.ll @@ -3,7 +3,7 @@ ; RUN: opt -module-hash -module-summary %s -o %t.o ; RUN: opt -module-hash -module-summary %p/Inputs/cache.ll -o %t2.o -; RUN: rm -Rf %t.cache && mkdir %t.cache +; RUN: rm -Rf %t.cache ; RUN: ld.lld --thinlto-cache-dir=%t.cache -o %t3.o %t2.o %t.o ; RUN: ls %t.cache | count 2 diff --git a/llvm/include/llvm/LTO/Caching.h b/llvm/include/llvm/LTO/Caching.h index 769f4cd9cc76..fe18009b14c7 100644 --- a/llvm/include/llvm/LTO/Caching.h +++ b/llvm/include/llvm/LTO/Caching.h @@ -28,8 +28,10 @@ namespace lto { typedef std::function AddFileFn; /// Create a local file system cache which uses the given cache directory and -/// file callback. -NativeObjectCache localCache(StringRef CacheDirectoryPath, AddFileFn AddFile); +/// file callback. This function also creates the cache directory if it does not +/// already exist. +Expected localCache(StringRef CacheDirectoryPath, + AddFileFn AddFile); } // namespace lto } // namespace llvm diff --git a/llvm/lib/LTO/Caching.cpp b/llvm/lib/LTO/Caching.cpp index f635369df8d7..fc898c49998c 100644 --- a/llvm/lib/LTO/Caching.cpp +++ b/llvm/lib/LTO/Caching.cpp @@ -46,8 +46,11 @@ static void commitEntry(StringRef TempFilename, StringRef EntryPath) { } } -NativeObjectCache lto::localCache(StringRef CacheDirectoryPath, - AddFileFn AddFile) { +Expected lto::localCache(StringRef CacheDirectoryPath, + AddFileFn AddFile) { + if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath)) + return errorCodeToError(EC); + return [=](unsigned Task, StringRef Key) -> AddStreamFn { // First, see if we have a cache hit. SmallString<64> EntryPath; diff --git a/llvm/test/ThinLTO/X86/cache-config.ll b/llvm/test/ThinLTO/X86/cache-config.ll index a947969f6690..9a92dc317746 100644 --- a/llvm/test/ThinLTO/X86/cache-config.ll +++ b/llvm/test/ThinLTO/X86/cache-config.ll @@ -1,4 +1,4 @@ -; RUN: rm -rf %t.cache && mkdir %t.cache +; RUN: rm -rf %t.cache ; RUN: opt -module-hash -module-summary %s -o %t.bc ; RUN: llvm-lto2 -o %t.o %t.bc -cache-dir %t.cache -r=%t.bc,globalfunc,plx diff --git a/llvm/test/ThinLTO/X86/cache.ll b/llvm/test/ThinLTO/X86/cache.ll index b796b00fc5d5..d654d3468e3e 100644 --- a/llvm/test/ThinLTO/X86/cache.ll +++ b/llvm/test/ThinLTO/X86/cache.ll @@ -10,7 +10,7 @@ ; RUN: ls %t.cache | count 1 ; Verify that enabling caching is ignoring module without hash with llvm-lto2 -; RUN: rm -Rf %t.cache && mkdir %t.cache +; RUN: rm -Rf %t.cache ; RUN: llvm-lto2 -o %t.o %t2.bc %t.bc -cache-dir %t.cache \ ; RUN: -r=%t2.bc,_main,plx \ ; RUN: -r=%t2.bc,_globalfunc,lx \ @@ -30,7 +30,7 @@ ; RUN: ls %t.cache | count 3 ; Verify that enabling caching is working with llvm-lto2 -; RUN: rm -Rf %t.cache && mkdir %t.cache +; RUN: rm -Rf %t.cache ; RUN: llvm-lto2 -o %t.o %t2.bc %t.bc -cache-dir %t.cache \ ; RUN: -r=%t2.bc,_main,plx \ ; RUN: -r=%t2.bc,_globalfunc,lx \ diff --git a/llvm/test/ThinLTO/X86/empty_module_with_cache.ll b/llvm/test/ThinLTO/X86/empty_module_with_cache.ll index 3e16c395a893..42692fd4c6e0 100644 --- a/llvm/test/ThinLTO/X86/empty_module_with_cache.ll +++ b/llvm/test/ThinLTO/X86/empty_module_with_cache.ll @@ -8,7 +8,7 @@ ; RUN: ls %t.cache | count 3 ; Verify that enabling caching is working with llvm-lto2 -; RUN: rm -Rf %t.cache && mkdir %t.cache +; RUN: rm -Rf %t.cache ; RUN: llvm-lto2 -o %t.o %t2.bc %t.bc -cache-dir %t.cache \ ; RUN: -r=%t2.bc,_main,plx ; RUN: ls %t.cache | count 2 @@ -25,7 +25,7 @@ ; RUN: ls %t.cache | count 1 ; Verify that caching is disabled for module without hash, with llvm-lto2 -; RUN: rm -Rf %t.cache && mkdir %t.cache +; RUN: rm -Rf %t.cache ; RUN: llvm-lto2 -o %t.o %t2.bc %t.bc -cache-dir %t.cache \ ; RUN: -r=%t2.bc,_main,plx ; RUN: ls %t.cache | count 0 diff --git a/llvm/test/tools/gold/X86/cache.ll b/llvm/test/tools/gold/X86/cache.ll index cef983c4a1ac..8d22a8606df3 100644 --- a/llvm/test/tools/gold/X86/cache.ll +++ b/llvm/test/tools/gold/X86/cache.ll @@ -2,7 +2,7 @@ ; RUN: opt -module-summary %s -o %t.o ; RUN: opt -module-summary %p/Inputs/cache.ll -o %t2.o -; RUN: rm -Rf %t.cache && mkdir %t.cache +; RUN: rm -Rf %t.cache ; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold.so \ ; RUN: --plugin-opt=thinlto \ ; RUN: --plugin-opt=cache-dir=%t.cache \ @@ -16,7 +16,7 @@ ; RUN: opt -module-hash -module-summary %s -o %t.o ; RUN: opt -module-hash -module-summary %p/Inputs/cache.ll -o %t2.o -; RUN: rm -Rf %t.cache && mkdir %t.cache +; RUN: rm -Rf %t.cache ; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold.so \ ; RUN: --plugin-opt=thinlto \ ; RUN: --plugin-opt=cache-dir=%t.cache \ diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp index c372552778fc..123d90df8904 100644 --- a/llvm/tools/gold/gold-plugin.cpp +++ b/llvm/tools/gold/gold-plugin.cpp @@ -835,7 +835,7 @@ static ld_plugin_status allSymbolsReadHook() { NativeObjectCache Cache; if (!options::cache_dir.empty()) - Cache = localCache(options::cache_dir, AddFile); + Cache = check(localCache(options::cache_dir, AddFile)); check(Lto->run(AddStream, Cache)); diff --git a/llvm/tools/llvm-lto2/llvm-lto2.cpp b/llvm/tools/llvm-lto2/llvm-lto2.cpp index b112f028e416..4b3251545e34 100644 --- a/llvm/tools/llvm-lto2/llvm-lto2.cpp +++ b/llvm/tools/llvm-lto2/llvm-lto2.cpp @@ -284,7 +284,7 @@ int main(int argc, char **argv) { NativeObjectCache Cache; if (!CacheDir.empty()) - Cache = localCache(CacheDir, AddFile); + Cache = check(localCache(CacheDir, AddFile), "failed to create cache"); check(Lto.run(AddStream, Cache), "LTO::run failed"); }