Index: include/llvm-c/lto.h =================================================================== --- include/llvm-c/lto.h +++ include/llvm-c/lto.h @@ -44,7 +44,7 @@ * @{ */ -#define LTO_API_VERSION 21 +#define LTO_API_VERSION 22 /** * \since prior to LTO_API_VERSION=3 @@ -817,6 +817,28 @@ unsigned expiration); /** + * Sets the maximum size of the cache directory (in bytes). A value over the + * amount of available space on the disk will be reduced to the amount of + * available space. An unspecified default value will be applied. A value of 0 + * will be ignored. + * + * \since LTO_API_VERSION=22 + */ +extern void thinlto_codegen_set_cache_size_bytes(thinlto_code_gen_t cg, + unsigned max_size_bytes); + +/** + * Sets the maximum number of files in the cache directory. An unspecified + * default value will be applied. A value of 0 will be ignored. + * + * \since LTO_API_VERSION=22 + */ +extern void thinlto_codegen_set_cache_size_files(thinlto_code_gen_t cg, + unsigned max_size_files); + + + +/** * @} // endgroup LLVMCTLTO_CACHING */ Index: include/llvm/LTO/legacy/ThinLTOCodeGenerator.h =================================================================== --- include/llvm/LTO/legacy/ThinLTOCodeGenerator.h +++ include/llvm/LTO/legacy/ThinLTOCodeGenerator.h @@ -184,6 +184,21 @@ CacheOptions.Policy.MaxSizePercentageOfAvailableSpace = Percentage; } + /// Cache policy: the maximum size for the cache directory in bytes. A value + /// over the amount of available space on the disk will be reduced to the + /// amount of available space. A value of 0 will be ignored. + void setCacheMaxSizeBytes(unsigned MaxSizeBytes) { + if (MaxSizeBytes) + CacheOptions.Policy.MaxSizeBytes = MaxSizeBytes; + } + + /// Cache policy: the maximum number of files in the cache directory. A value + /// of 0 will be ignored. + void setCacheMaxSizeFiles(unsigned MaxSizeFiles) { + if (MaxSizeFiles) + CacheOptions.Policy.MaxSizeFiles = MaxSizeFiles; + } + /**@}*/ /// Set the path to a directory where to save temporaries at various stages of Index: test/ThinLTO/X86/cache.ll =================================================================== --- test/ThinLTO/X86/cache.ll +++ test/ThinLTO/X86/cache.ll @@ -80,6 +80,40 @@ ; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache --thinlto-cache-pruning-interval 0 ; RUN: not ls %t.cache/llvmcache-foo +; Verify that specifying max size for the cache directory prunes it to this +; size, removing the largest files first. +; RUN: rm -Rf %t.cache && mkdir %t.cache +; Create cache files with different sizes. +; Only 8B, 16B and 76B files should stay after pruning. +; RUN: %python -c "print(' ' * 1023)" > %t.cache/llvmcache-foo-1024 +; RUN: %python -c "print(' ' * 15)" > %t.cache/llvmcache-foo-16 +; RUN: %python -c "print(' ' * 7)" > %t.cache/llvmcache-foo-8 +; RUN: %python -c "print(' ' * 75)" > %t.cache/llvmcache-foo-76 +; RUN: %python -c "print(' ' * 76)" > %t.cache/llvmcache-foo-77 +; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache --thinlto-cache-max-size-bytes 100 +; RUN: ls %t.cache/llvmcache-foo-16 +; RUN: ls %t.cache/llvmcache-foo-8 +; RUN: ls %t.cache/llvmcache-foo-76 +; RUN: not ls %t.cache/llvmcache-foo-1024 +; RUN: not ls %t.cache/llvmcache-foo-77 + +; Verify that specifying max number of files in the cache directory prunes +; it to this amount, removing the largest files first. +; RUN: rm -Rf %t.cache && mkdir %t.cache +; Create cache files with different sizes. +; Only 8B and 16B files should stay after pruning. +; RUN: %python -c "print(' ' * 1023)" > %t.cache/llvmcache-foo-1024 +; RUN: %python -c "print(' ' * 15)" > %t.cache/llvmcache-foo-16 +; RUN: %python -c "print(' ' * 7)" > %t.cache/llvmcache-foo-8 +; RUN: %python -c "print(' ' * 75)" > %t.cache/llvmcache-foo-76 +; RUN: %python -c "print(' ' * 76)" > %t.cache/llvmcache-foo-77 +; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache --thinlto-cache-max-size-files 2 +; RUN: ls %t.cache/llvmcache-foo-16 +; RUN: ls %t.cache/llvmcache-foo-8 +; RUN: not ls %t.cache/llvmcache-foo-76 +; RUN: not ls %t.cache/llvmcache-foo-1024 +; RUN: not ls %t.cache/llvmcache-foo-77 + target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.11.0" Index: tools/llvm-lto/llvm-lto.cpp =================================================================== --- tools/llvm-lto/llvm-lto.cpp +++ tools/llvm-lto/llvm-lto.cpp @@ -160,6 +160,14 @@ ThinLTOCachePruningInterval("thinlto-cache-pruning-interval", cl::init(1200), cl::desc("Set ThinLTO cache pruning interval.")); +static cl::opt + ThinLTOCacheMaxSizeBytes("thinlto-cache-max-size-bytes", + cl::desc("Set ThinLTO cache pruning directory maximum size in bytes.")); + +static cl::opt + ThinLTOCacheMaxSizeFiles("thinlto-cache-max-size-files", cl::init(1000000), + cl::desc("Set ThinLTO cache pruning directory maximum number of files.")); + static cl::opt ThinLTOSaveTempsPrefix( "thinlto-save-temps", cl::desc("Save ThinLTO temp files using filenames created by adding " @@ -475,6 +483,8 @@ ThinGenerator.setTargetOptions(Options); ThinGenerator.setCacheDir(ThinLTOCacheDir); ThinGenerator.setCachePruningInterval(ThinLTOCachePruningInterval); + ThinGenerator.setCacheMaxSizeFiles(ThinLTOCacheMaxSizeFiles); + ThinGenerator.setCacheMaxSizeBytes(ThinLTOCacheMaxSizeBytes); ThinGenerator.setFreestanding(EnableFreestanding); // Add all the exported symbols to the table of symbols to preserve. Index: tools/lto/lto.cpp =================================================================== --- tools/lto/lto.cpp +++ tools/lto/lto.cpp @@ -586,6 +586,16 @@ return unwrap(cg)->setMaxCacheSizeRelativeToAvailableSpace(Percentage); } +void thinlto_codegen_set_cache_size_bytes( + thinlto_code_gen_t cg, unsigned MaxSizeBytes) { + return unwrap(cg)->setCacheMaxSizeBytes(MaxSizeBytes); +} + +void thinlto_codegen_set_cache_size_files( + thinlto_code_gen_t cg, unsigned MaxSizeFiles) { + return unwrap(cg)->setCacheMaxSizeFiles(MaxSizeFiles); +} + void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg, const char *save_temps_dir) { return unwrap(cg)->setSaveTempsDir(save_temps_dir); Index: tools/lto/lto.exports =================================================================== --- tools/lto/lto.exports +++ tools/lto/lto.exports @@ -56,15 +56,17 @@ thinlto_codegen_set_cache_dir thinlto_codegen_set_cache_pruning_interval thinlto_codegen_set_cache_entry_expiration +thinlto_codegen_set_final_cache_size_relative_to_available_space +thinlto_codegen_set_cache_size_bytes +thinlto_codegen_set_cache_size_files thinlto_codegen_set_savetemps_dir thinlto_codegen_set_cpu thinlto_debug_options lto_module_is_thinlto thinlto_codegen_add_must_preserve_symbol thinlto_codegen_add_cross_referenced_symbol -thinlto_codegen_set_final_cache_size_relative_to_available_space thinlto_codegen_set_codegen_only thinlto_codegen_disable_codegen thinlto_module_get_num_object_files thinlto_module_get_object_file -thinlto_set_generated_objects_dir \ No newline at end of file +thinlto_set_generated_objects_dir