Skip to content

Commit b8aeec4

Browse files
author
Ekaterina Romanova
committed
[ThinLTO] Added a couple of C LTO API interfaces to control the cache policy.
- thinlto_codegen_set_cache_size_bytes to control the absolute size of cache directory. - thinlto_codegen_set_cache_size_files the size and amount of files in cache directory. These functions have been supported in C++ LTO API for a long time, but were absent in C LTO API. Differential Revision: https://reviews.llvm.org/D42446 llvm-svn: 326537
1 parent dfcd846 commit b8aeec4

File tree

6 files changed

+96
-3
lines changed

6 files changed

+96
-3
lines changed

llvm/include/llvm-c/lto.h

+23-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ typedef bool lto_bool_t;
4444
* @{
4545
*/
4646

47-
#define LTO_API_VERSION 21
47+
#define LTO_API_VERSION 22
4848

4949
/**
5050
* \since prior to LTO_API_VERSION=3
@@ -816,6 +816,28 @@ extern void thinlto_codegen_set_final_cache_size_relative_to_available_space(
816816
extern void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg,
817817
unsigned expiration);
818818

819+
/**
820+
* Sets the maximum size of the cache directory (in bytes). A value over the
821+
* amount of available space on the disk will be reduced to the amount of
822+
* available space. An unspecified default value will be applied. A value of 0
823+
* will be ignored.
824+
*
825+
* \since LTO_API_VERSION=22
826+
*/
827+
extern void thinlto_codegen_set_cache_size_bytes(thinlto_code_gen_t cg,
828+
unsigned max_size_bytes);
829+
830+
/**
831+
* Sets the maximum number of files in the cache directory. An unspecified
832+
* default value will be applied. A value of 0 will be ignored.
833+
*
834+
* \since LTO_API_VERSION=22
835+
*/
836+
extern void thinlto_codegen_set_cache_size_files(thinlto_code_gen_t cg,
837+
unsigned max_size_files);
838+
839+
840+
819841
/**
820842
* @} // endgroup LLVMCTLTO_CACHING
821843
*/

llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h

+15
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,21 @@ class ThinLTOCodeGenerator {
184184
CacheOptions.Policy.MaxSizePercentageOfAvailableSpace = Percentage;
185185
}
186186

187+
/// Cache policy: the maximum size for the cache directory in bytes. A value
188+
/// over the amount of available space on the disk will be reduced to the
189+
/// amount of available space. A value of 0 will be ignored.
190+
void setCacheMaxSizeBytes(unsigned MaxSizeBytes) {
191+
if (MaxSizeBytes)
192+
CacheOptions.Policy.MaxSizeBytes = MaxSizeBytes;
193+
}
194+
195+
/// Cache policy: the maximum number of files in the cache directory. A value
196+
/// of 0 will be ignored.
197+
void setCacheMaxSizeFiles(unsigned MaxSizeFiles) {
198+
if (MaxSizeFiles)
199+
CacheOptions.Policy.MaxSizeFiles = MaxSizeFiles;
200+
}
201+
187202
/**@}*/
188203

189204
/// Set the path to a directory where to save temporaries at various stages of

llvm/test/ThinLTO/X86/cache.ll

+34
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,40 @@
8080
; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache --thinlto-cache-pruning-interval 0
8181
; RUN: not ls %t.cache/llvmcache-foo
8282

83+
; Verify that specifying max size for the cache directory prunes it to this
84+
; size, removing the largest files first.
85+
; RUN: rm -Rf %t.cache && mkdir %t.cache
86+
; Create cache files with different sizes.
87+
; Only 8B, 16B and 76B files should stay after pruning.
88+
; RUN: %python -c "print(' ' * 1023)" > %t.cache/llvmcache-foo-1024
89+
; RUN: %python -c "print(' ' * 15)" > %t.cache/llvmcache-foo-16
90+
; RUN: %python -c "print(' ' * 7)" > %t.cache/llvmcache-foo-8
91+
; RUN: %python -c "print(' ' * 75)" > %t.cache/llvmcache-foo-76
92+
; RUN: %python -c "print(' ' * 76)" > %t.cache/llvmcache-foo-77
93+
; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache --thinlto-cache-max-size-bytes 100
94+
; RUN: ls %t.cache/llvmcache-foo-16
95+
; RUN: ls %t.cache/llvmcache-foo-8
96+
; RUN: ls %t.cache/llvmcache-foo-76
97+
; RUN: not ls %t.cache/llvmcache-foo-1024
98+
; RUN: not ls %t.cache/llvmcache-foo-77
99+
100+
; Verify that specifying max number of files in the cache directory prunes
101+
; it to this amount, removing the largest files first.
102+
; RUN: rm -Rf %t.cache && mkdir %t.cache
103+
; Create cache files with different sizes.
104+
; Only 8B and 16B files should stay after pruning.
105+
; RUN: %python -c "print(' ' * 1023)" > %t.cache/llvmcache-foo-1024
106+
; RUN: %python -c "print(' ' * 15)" > %t.cache/llvmcache-foo-16
107+
; RUN: %python -c "print(' ' * 7)" > %t.cache/llvmcache-foo-8
108+
; RUN: %python -c "print(' ' * 75)" > %t.cache/llvmcache-foo-76
109+
; RUN: %python -c "print(' ' * 76)" > %t.cache/llvmcache-foo-77
110+
; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache --thinlto-cache-max-size-files 2
111+
; RUN: ls %t.cache/llvmcache-foo-16
112+
; RUN: ls %t.cache/llvmcache-foo-8
113+
; RUN: not ls %t.cache/llvmcache-foo-76
114+
; RUN: not ls %t.cache/llvmcache-foo-1024
115+
; RUN: not ls %t.cache/llvmcache-foo-77
116+
83117
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
84118
target triple = "x86_64-apple-macosx10.11.0"
85119

llvm/tools/llvm-lto/llvm-lto.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ static cl::opt<int>
160160
ThinLTOCachePruningInterval("thinlto-cache-pruning-interval",
161161
cl::init(1200), cl::desc("Set ThinLTO cache pruning interval."));
162162

163+
static cl::opt<int>
164+
ThinLTOCacheMaxSizeBytes("thinlto-cache-max-size-bytes",
165+
cl::desc("Set ThinLTO cache pruning directory maximum size in bytes."));
166+
167+
static cl::opt<int>
168+
ThinLTOCacheMaxSizeFiles("thinlto-cache-max-size-files", cl::init(1000000),
169+
cl::desc("Set ThinLTO cache pruning directory maximum number of files."));
170+
163171
static cl::opt<std::string> ThinLTOSaveTempsPrefix(
164172
"thinlto-save-temps",
165173
cl::desc("Save ThinLTO temp files using filenames created by adding "
@@ -475,6 +483,8 @@ class ThinLTOProcessing {
475483
ThinGenerator.setTargetOptions(Options);
476484
ThinGenerator.setCacheDir(ThinLTOCacheDir);
477485
ThinGenerator.setCachePruningInterval(ThinLTOCachePruningInterval);
486+
ThinGenerator.setCacheMaxSizeFiles(ThinLTOCacheMaxSizeFiles);
487+
ThinGenerator.setCacheMaxSizeBytes(ThinLTOCacheMaxSizeBytes);
478488
ThinGenerator.setFreestanding(EnableFreestanding);
479489

480490
// Add all the exported symbols to the table of symbols to preserve.

llvm/tools/lto/lto.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,16 @@ void thinlto_codegen_set_final_cache_size_relative_to_available_space(
586586
return unwrap(cg)->setMaxCacheSizeRelativeToAvailableSpace(Percentage);
587587
}
588588

589+
void thinlto_codegen_set_cache_size_bytes(
590+
thinlto_code_gen_t cg, unsigned MaxSizeBytes) {
591+
return unwrap(cg)->setCacheMaxSizeBytes(MaxSizeBytes);
592+
}
593+
594+
void thinlto_codegen_set_cache_size_files(
595+
thinlto_code_gen_t cg, unsigned MaxSizeFiles) {
596+
return unwrap(cg)->setCacheMaxSizeFiles(MaxSizeFiles);
597+
}
598+
589599
void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg,
590600
const char *save_temps_dir) {
591601
return unwrap(cg)->setSaveTempsDir(save_temps_dir);

llvm/tools/lto/lto.exports

+4-2
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,17 @@ thinlto_codegen_set_pic_model
5656
thinlto_codegen_set_cache_dir
5757
thinlto_codegen_set_cache_pruning_interval
5858
thinlto_codegen_set_cache_entry_expiration
59+
thinlto_codegen_set_final_cache_size_relative_to_available_space
60+
thinlto_codegen_set_cache_size_bytes
61+
thinlto_codegen_set_cache_size_files
5962
thinlto_codegen_set_savetemps_dir
6063
thinlto_codegen_set_cpu
6164
thinlto_debug_options
6265
lto_module_is_thinlto
6366
thinlto_codegen_add_must_preserve_symbol
6467
thinlto_codegen_add_cross_referenced_symbol
65-
thinlto_codegen_set_final_cache_size_relative_to_available_space
6668
thinlto_codegen_set_codegen_only
6769
thinlto_codegen_disable_codegen
6870
thinlto_module_get_num_object_files
6971
thinlto_module_get_object_file
70-
thinlto_set_generated_objects_dir
72+
thinlto_set_generated_objects_dir

0 commit comments

Comments
 (0)