Skip to content

Commit f495024

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 8314da0 commit f495024

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
@@ -125,6 +125,22 @@ These include:
125125
certain runtime headers. If this is not set, it has a default value of
126126
"${ROCM_PATH}/llvm".
127127

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

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 {
@@ -278,5 +274,28 @@ StringRef getHIPPath() { return getDetector()->getHIPPath(); }
278274

279275
StringRef getLLVMPath() { return getDetector()->getLLVMPath(); }
280276

277+
StringRef getCachePolicy() {
278+
static const char *EnvCachePolicy = std::getenv("AMD_COMGR_CACHE_POLICY");
279+
return EnvCachePolicy;
280+
}
281+
282+
StringRef getCacheDirectory() {
283+
static const char *EnvCacheDirectory = std::getenv("AMD_COMGR_CACHE_DIR");
284+
if (EnvCacheDirectory)
285+
return EnvCacheDirectory;
286+
287+
// mark Result as static to keep it cached across calls
288+
static SmallString<256> Result;
289+
if (!Result.empty())
290+
return Result;
291+
292+
if (sys::path::cache_directory(Result)) {
293+
sys::path::append(Result, Twine("comgr_cache"));
294+
return Result;
295+
}
296+
297+
return "";
298+
}
299+
281300
} // namespace env
282301
} // namespace COMGR

amd/comgr/src/comgr-env.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ llvm::StringRef getHIPPath();
6666
/// otherwise return the default LLVM path.
6767
llvm::StringRef getLLVMPath();
6868

69+
/// If environment variable AMD_COMGR_CACHE_POLICY is set, return the
70+
/// environment variable, otherwise return empty
71+
llvm::StringRef getCachePolicy();
72+
73+
/// If environment variable AMD_COMGR_CACHE_DIR is set, return the environment
74+
/// variable, otherwise return the default path: On Linux it's typically
75+
/// $HOME/.cache/comgr_cache (depends on XDG_CACHE_HOME)
76+
llvm::StringRef getCacheDirectory();
77+
6978
} // namespace env
7079
} // namespace COMGR
7180

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)