Skip to content

Commit d1057fe

Browse files
committed
[COMGR][Cache] Add environment variables AMD_COMGR_CACHE_DIR and AMD_COMGR_CACHE_POLICY
Set the AMD_COMGR_CACHE_DIR variable to "" during tests. co-authored by anjenner and jmmartinez Change-Id: I3a3aa7e734805bd78542f38c1779f85a9d695f20
1 parent 5b04d3a commit d1057fe

File tree

5 files changed

+57
-6
lines changed

5 files changed

+57
-6
lines changed

amd/comgr/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ Comgr supports an environment variable to help locate LLVM:
114114
installation, which is currently used to locate the clang resource directory
115115
and clang binary path, allowing for additional optimizations.
116116

117+
Comgr utilizes a cache to preserve the results of compilations between executions.
118+
The cache's status (enabled/disabled), storage location for its results,
119+
and eviction policy can be manipulated through specific environment variables.
120+
If an issue arises during cache initialization, the execution will proceed with
121+
the cache turned off.
122+
123+
* `AMD_COMGR_CACHE_DIR`: When set to "", the cache is turned off. If assigned a
124+
value, that value is used as the path for cache storage. By default, it is
125+
directed to "$XDG_CACHE_HOME/comgr_cache" (which defaults to
126+
"$USER/.cache/comgr_cache" on Linux, and "%LOCALAPPDATA%\cache\comgr_cache"
127+
on Windows).
128+
* `AMD_COMGR_CACHE_POLICY`: If assigned a value, the string is interpreted and
129+
applied to the cache pruning policy. The cache is pruned only upon program
130+
termination. The string format aligns with [Clang's ThinLTO cache pruning policy](https://clang.llvm.org/docs/ThinLTO.html#cache-pruning).
131+
The default policy is set as: "prune_interval=1h:prune_expiration=0h:cache_size=75%:cache_size_bytes=30g:cache_size_files=0".
132+
117133
Comgr also supports some environment variables to aid in debugging. These
118134
include:
119135

amd/comgr/src/comgr-env.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
#include "llvm/ADT/Twine.h"
3838
#include "llvm/Support/VirtualFileSystem.h"
3939

40-
#include <fstream>
41-
#include <memory>
42-
#include <stdlib.h>
43-
4440
using namespace llvm;
4541

4642
namespace COMGR {
@@ -79,5 +75,28 @@ llvm::StringRef getLLVMPath() {
7975
return EnvLLVMPath;
8076
}
8177

78+
StringRef getCachePolicy() {
79+
static const char *EnvCachePolicy = std::getenv("AMD_COMGR_CACHE_POLICY");
80+
return EnvCachePolicy;
81+
}
82+
83+
StringRef getCacheDirectory() {
84+
static const char *EnvCacheDirectory = std::getenv("AMD_COMGR_CACHE_DIR");
85+
if (EnvCacheDirectory)
86+
return EnvCacheDirectory;
87+
88+
// mark Result as static to keep it cached across calls
89+
static SmallString<256> Result;
90+
if (!Result.empty())
91+
return Result;
92+
93+
if (sys::path::cache_directory(Result)) {
94+
sys::path::append(Result, Twine("comgr_cache"));
95+
return Result;
96+
}
97+
98+
return "";
99+
}
100+
82101
} // namespace env
83102
} // namespace COMGR

amd/comgr/src/comgr-env.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ bool needTimeStatistics();
5959
/// otherwise return the default LLVM path.
6060
llvm::StringRef getLLVMPath();
6161

62+
/// If environment variable AMD_COMGR_CACHE_POLICY is set, return the
63+
/// environment variable, otherwise return empty
64+
llvm::StringRef getCachePolicy();
65+
66+
/// If environment variable AMD_COMGR_CACHE_DIR is set, return the environment
67+
/// variable, otherwise return the default path: On Linux it's typically
68+
/// $HOME/.cache/comgr_cache (depends on XDG_CACHE_HOME)
69+
llvm::StringRef getCacheDirectory();
70+
6271
} // namespace env
6372
} // namespace COMGR
6473

amd/comgr/test-lit/lit.cfg.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414

1515
if not config.comgr_disable_spirv:
1616
config.available_features.add("comgr-has-spirv")
17+
18+
# By default, disable the cache for the tests.
19+
# Test for the cache must explicitly enable this variable.
20+
config.environment['AMD_COMGR_CACHE_DIR'] = ""

amd/comgr/test/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,12 @@ endif()
193193
add_dependencies(check-comgr ${name})
194194
# Windows binaries have no equivalent to RPATH, so we must set their PATH to
195195
# include the .lib/.dll directory.
196-
if (NOT(UNIX))
196+
if (UNIX)
197197
set_tests_properties(${test_name}
198-
PROPERTIES ENVIRONMENT "PATH=$<TARGET_LINKER_FILE_DIR:amd_comgr>")
198+
PROPERTIES ENVIRONMENT "AMD_COMGR_CACHE_DIR=;")
199+
else()
200+
set_tests_properties(${test_name}
201+
PROPERTIES ENVIRONMENT "PATH=$<TARGET_LINKER_FILE_DIR:amd_comgr>;AMD_COMGR_CACHE_DIR=;")
199202
endif()
200203
endmacro()
201204

0 commit comments

Comments
 (0)