Index: lld/ELF/LTO.cpp =================================================================== --- lld/ELF/LTO.cpp +++ lld/ELF/LTO.cpp @@ -27,7 +27,6 @@ #include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/WithColor.h" #include #include #include @@ -200,11 +199,12 @@ // used to prune the old cache file. The cache pruning will happen if (the // number of inputs+1) is greater than Policy.MaxSizeFiles. Therefore, I used // files.size() here. - if (Policy.MaxSizeFiles && (files.size() > Policy.MaxSizeFiles)) - WithColor::warning() - << "ThinLTO cache pruning happens since the total number of the files " - "created by the current link job exceeds the maximum number of " - "files; consider adjusting --thinlto-cache-policy.\n"; + if (Policy.MaxSizeFiles && (files.size() > Policy.MaxSizeFiles)) { + warn("ThinLTO cache pruning happens since the number of created files " + "exceeds the maximum number of files; consider adjusting " + "--thinlto-cache-policy."); + return; + } if (Policy.MaxSizeBytes > 0) { size_t Size = 0; @@ -213,10 +213,9 @@ Size += File->getBufferSize(); if (Size > config->thinLTOCachePolicy.MaxSizeBytes) - WithColor::warning() - << "ThinLTO cache pruning happens since the total size of the cache " - "files consumed by the current link job exceeds maximum cache " - "size; consider adjusting --thinlto-cache-policy.\n"; + warn("ThinLTO cache pruning happens since the total size of the cache " + "files consumed by the current link job exceeds maximum cache " + "size; consider adjusting --thinlto-cache-policy."); } } Index: lld/test/ELF/lto/cache-warnings.ll =================================================================== --- lld/test/ELF/lto/cache-warnings.ll +++ lld/test/ELF/lto/cache-warnings.ll @@ -1,31 +1,46 @@ ; REQUIRES: x86 -; NetBSD: noatime mounts currently inhibit 'touch' from updating atime +;; NetBSD: noatime mounts currently inhibit 'touch' from updating atime ; UNSUPPORTED: system-netbsd ; RUN: opt -module-hash -module-summary %s -o %t.o ; RUN: opt -module-hash -module-summary %p/Inputs/cache.ll -o %t2.o -; Create two cache files in order to test the warnings. +;; Create two cache files in order to test the warnings. ; RUN: rm -Rf %t.cache && mkdir %t.cache ; RUN: touch %t.cache/llvmcache-foo ; RUN: touch %t.cache/llvmcache-bar -; Check cache policies of the number of files. -; Case 1: A value of 0 disables the number of files based pruning. Therefore, there is no warning. +;; Check cache policies of the number of files. +;; Case 1: A value of 0 disables the number of files based pruning. Therefore, there is no warning. ; RUN: ld.lld -v --thinlto-cache-dir=%t.cache --thinlto-cache-policy=prune_interval=0s:cache_size_files=0 %t2.o %t.o -o %t3 2>&1 | FileCheck %s --implicit-check-not=warning -; Case 2: If the total number of the files created by the current link job is less than the maximum number of files, there is no warning. +;; Case 2: If the total number of the files created by the current link job is less than the maximum number of files, there is no warning. ; RUN: ld.lld -v --thinlto-cache-dir=%t.cache --thinlto-cache-policy=prune_interval=0s:cache_size_files=3 %t2.o %t.o -o %t3 2>&1 | FileCheck %s --implicit-check-not=warning -; Case 3: If the total number of the files created by the current link job exceeds the maximum number of files, a warning is given. +;; Case 3: If the total number of the files created by the current link job exceeds the maximum number of files, a warning is given. ; RUN: ld.lld --thinlto-cache-dir=%t.cache --thinlto-cache-policy=prune_interval=0s:cache_size_files=1 %t2.o %t.o -o %t3 2>&1 | FileCheck %s --check-prefix=WARN - -; Check cache policies of the cache size. -; Case 1: A value of 0 disables the absolute size-based pruning. Therefore, there is no warning. +;; Check cache policies of the cache size. +;; Case 1: A value of 0 disables the absolute size-based pruning. Therefore, there is no warning. ; RUN: ld.lld -v --thinlto-cache-dir=%t.cache --thinlto-cache-policy=prune_interval=0s:cache_size_bytes=0 %t2.o %t.o -o %t3 2>&1 | FileCheck %s --implicit-check-not=warning -; Case 2: If the total size of the cache files created by the current link job is less than the maximum size for the cache directory in bytes, there is no warning. -; RUN: ld.lld -v --thinlto-cache-dir=%t.cache --thinlto-cache-policy=prune_interval=0s:cache_size_bytes=1355 %t2.o %t.o -o %t3 2>&1 | FileCheck %s --implicit-check-not=warning -; Case 3: If the total size of the cache files created by the current link job exceeds the maximum size for the cache directory in bytes, a warning is given. -; RUN: ld.lld --thinlto-cache-dir=%t.cache --thinlto-cache-policy=prune_interval=0s:cache_size_bytes=1350 %t2.o %t.o -o %t3 2>&1 | FileCheck %s --check-prefix=WARN + +;; Get the total size of created cache files. +; RUN: rm -Rf %t.cache && mkdir %t.cache +; RUN: ld.lld --thinlto-cache-dir=%t.cache --thinlto-cache-policy=prune_interval=0s:cache_size_bytes=32k %t2.o %t.o -o %t3 2>&1 +; RUN: %python -c "import os, sys; os.chdir(r\"%t.cache\"); print(sum(os.path.getsize(filename) for filename in os.listdir('.') if os.path.isfile(filename) and filename.startswith(\"llvmcache-\")))" > %t.size.txt 2>&1 + +;; Case 2: If the total size of the cache files created by the current link job is less than the maximum size for the cache directory in bytes, there is no warning. +; RUN: cat %t.size.txt \ +; RUN: | %python -c "import sys; print(int(sys.stdin.read())+int(5))" > %t.size1.txt 2>&1 +; RUN: cat %t.size1.txt \ +; RUN: | %python -c "import subprocess, sys; size = int(sys.stdin.read()); cache_dir=r'%t.cache'; subprocess.run([r'ld.lld', '-v', '--thinlto-cache-dir={}'.format(cache_dir), '--thinlto-cache-policy=prune_interval=0s:cache_size_bytes={}'.format(size), r'%t2.o', r'%t.o', '-o', r'%t3'], shell=True)" | FileCheck %s --implicit-check-not=warning + +;; Case 3: If the total size of the cache files created by the current link job exceeds the maximum size for the cache directory in bytes, a warning is given. +; RUN: cat %t.size.txt \ +; RUN: | %python -c "import sys; print(int(sys.stdin.read())-int(5))" > %t.size2.txt 2>&1 +; RUN: cat %t.size2.txt \ +; RUN: | %python -c "import subprocess, sys; size = int(sys.stdin.read()); cache_dir=r'%t.cache'; subprocess.run([r'ld.lld', '-v', '--thinlto-cache-dir={}'.format(cache_dir), '--thinlto-cache-policy=prune_interval=0s:cache_size_bytes={}'.format(size), r'%t2.o', r'%t.o', '-o', r'%t3'], shell=True)" | FileCheck %s --implicit-check-not=warning + +;; Check just emit a warning the first time a file is pruned due to either of the size/number limits. +; RUN: ld.lld --thinlto-cache-dir=%t.cache --thinlto-cache-policy=prune_interval=0s:cache_size_files=1:cache_size_bytes=1 %t2.o %t.o -o %t3 2>&1 | FileCheck %s --check-prefix=WARN ; WARN: warning: ThinLTO cache pruning happens{{.*}}--thinlto-cache-policy. ; WARN-NOT: warning: ThinLTO cache pruning happens{{.*}}--thinlto-cache-policy.