Skip to content

Commit 2f9599c

Browse files
committed
Fix E2E test for kernel compiler
1 parent 1e0bd3a commit 2f9599c

File tree

2 files changed

+69
-119
lines changed

2 files changed

+69
-119
lines changed

sycl/source/detail/persistent_device_code_cache.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ LockCacheItem::~LockCacheItem() {
5454
}
5555

5656
// Returns true if the specified format is either SPIRV or a native binary.
57-
static bool
58-
IsSupportedImageFormat(ur::DeviceBinaryType Format) {
57+
static bool IsSupportedImageFormat(ur::DeviceBinaryType Format) {
5958
return Format == SYCL_DEVICE_BINARY_TYPE_SPIRV ||
6059
Format == SYCL_DEVICE_BINARY_TYPE_NATIVE;
6160
}
@@ -210,6 +209,16 @@ void PersistentDeviceCodeCache::repopulateCacheSizeFile(
210209
const std::string CacheSizeFileName = "cache_size.txt";
211210
const std::string CacheSizeFile = CacheRoot + "/" + CacheSizeFileName;
212211

212+
// Create cache root, if it does not exist.
213+
try {
214+
if (!OSUtil::isPathPresent(CacheRoot))
215+
OSUtil::makeDir(CacheRoot.c_str());
216+
} catch (...) {
217+
throw sycl::exception(make_error_code(errc::runtime),
218+
"Failed to create cache root directory: " +
219+
CacheRoot);
220+
}
221+
213222
// If the cache size file is not present, calculate the size of the cache size
214223
// directory and write it to the file.
215224
if (!OSUtil::isPathPresent(CacheSizeFile)) {

sycl/test-e2e/KernelCompiler/kernel_compiler_cache_eviction.cpp

Lines changed: 58 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,20 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
// Tests on-disk cache and eviction with kernel_compiler.
10+
911
// REQUIRES: ocloc && (opencl || level_zero)
1012
// UNSUPPORTED: accelerator
1113

1214
// -- Test the kernel_compiler with OpenCL source.
1315
// RUN: %{build} -o %t.out
14-
// RUN: %{run} %t.out
15-
// RUN: %{l0_leak_check} %{run} %t.out
1616

1717
// -- Test again, with caching.
18-
// DEFINE: %{cache_vars} = env SYCL_CACHE_PERSISTENT=1 SYCL_CACHE_TRACE=5 SYCL_CACHE_DIR=%t/cache_dir
19-
// RUN: rm -rf %t/cache_dir
20-
// RUN: %{cache_vars} %t.out 2>&1 | FileCheck %s --check-prefixes=CHECK-WRITTEN-TO-CACHE
21-
// RUN: %{cache_vars} %t.out 2>&1 | FileCheck %s --check-prefixes=CHECK-READ-FROM-CACHE
22-
23-
// -- Add leak check.
18+
// DEFINE: %{cache_vars} = env SYCL_CACHE_PERSISTENT=1 SYCL_CACHE_TRACE=7 SYCL_CACHE_DIR=%t/cache_dir SYCL_CACHE_MAX_SIZE=23000
2419
// RUN: rm -rf %t/cache_dir
25-
// RUN: %{l0_leak_check} %{cache_vars} %t.out 2>&1 | FileCheck %s --check-prefixes=CHECK-WRITTEN-TO-CACHE
26-
// RUN: %{l0_leak_check} %{cache_vars} %t.out 2>&1 | FileCheck %s --check-prefixes=CHECK-READ-FROM-CACHE
20+
// RUN: %{cache_vars} %t.out 2>&1 | FileCheck %s --check-prefix=CHECK
2721

28-
// CHECK-WRITTEN-TO-CACHE: [Persistent Cache]: enabled
29-
// CHECK-WRITTEN-TO-CACHE-NOT: [kernel_compiler Persistent Cache]: using cached binary
30-
// CHECK-WRITTEN-TO-CACHE: [kernel_compiler Persistent Cache]: binary has been cached
31-
32-
// CHECK-READ-FROM-CACHE: [Persistent Cache]: enabled
33-
// CHECK-READ-FROM-CACHE-NOT: [kernel_compiler Persistent Cache]: binary has been cached
34-
// CHECK-READ-FROM-CACHE: [kernel_compiler Persistent Cache]: using cached binary
22+
// CHECK: [Persistent Cache]: enabled
3523

3624
#include <sycl/detail/core.hpp>
3725
#include <sycl/kernel_bundle.hpp>
@@ -47,54 +35,11 @@ __kernel void her_kernel(__global int *in, __global int *out) {
4735
}
4836
)===";
4937

50-
auto constexpr BadCLSource = R"===(
51-
__kernel void my_kernel(__global int *in, __global int *out) {
52-
size_t i = get_global_id(0) + no semi-colon!!
53-
out[i] = in[i]*2 + 100;
54-
}
55-
)===";
56-
/*
57-
Compile Log:
58-
1:3:34: error: use of undeclared identifier 'no'
59-
size_t i = get_global_id(0) + no semi-colon!!
60-
^
61-
1:3:36: error: expected ';' at end of declaration
62-
size_t i = get_global_id(0) + no semi-colon!!
63-
^
64-
;
65-
66-
Build failed with error code: -11
67-
68-
=============
69-
70-
*/
71-
7238
using namespace sycl;
7339

74-
void testSyclKernel(sycl::queue &Q, sycl::kernel Kernel, int multiplier,
75-
int added) {
76-
constexpr int N = 4;
77-
cl_int InputArray[N] = {0, 1, 2, 3};
78-
cl_int OutputArray[N] = {};
79-
80-
sycl::buffer InputBuf(InputArray, sycl::range<1>(N));
81-
sycl::buffer OutputBuf(OutputArray, sycl::range<1>(N));
82-
83-
Q.submit([&](sycl::handler &CGH) {
84-
CGH.set_arg(0, InputBuf.get_access<sycl::access::mode::read>(CGH));
85-
CGH.set_arg(1, OutputBuf.get_access<sycl::access::mode::write>(CGH));
86-
CGH.parallel_for(sycl::range<1>{N}, Kernel);
87-
});
88-
89-
sycl::host_accessor Out{OutputBuf};
90-
for (int I = 0; I < N; I++)
91-
assert(Out[I] == ((I * multiplier) + added));
92-
}
93-
9440
void test_build_and_run() {
9541
namespace syclex = sycl::ext::oneapi::experimental;
9642
using source_kb = sycl::kernel_bundle<sycl::bundle_state::ext_oneapi_source>;
97-
using exe_kb = sycl::kernel_bundle<sycl::bundle_state::executable>;
9843

9944
// only one device is supported at this time, so we limit the queue and
10045
// context to that
@@ -112,69 +57,66 @@ void test_build_and_run() {
11257
return;
11358
}
11459

60+
auto CreateAndVerifyKB = [](source_kb &kbSrc,
61+
std::vector<std::string> &&BuildFlags) {
62+
std::string log;
63+
std::vector<sycl::device> devs = kbSrc.get_devices();
64+
sycl::context ctxRes = kbSrc.get_context();
65+
sycl::backend beRes = kbSrc.get_backend();
66+
67+
auto kb =
68+
syclex::build(kbSrc, devs,
69+
syclex::properties{syclex::build_options{BuildFlags},
70+
syclex::save_log{&log}});
71+
72+
bool hasMyKernel = kb.ext_oneapi_has_kernel("my_kernel");
73+
bool hasHerKernel = kb.ext_oneapi_has_kernel("her_kernel");
74+
bool notExistKernel = kb.ext_oneapi_has_kernel("not_exist");
75+
assert(hasMyKernel && "my_kernel should exist, but doesn't");
76+
assert(hasHerKernel && "her_kernel should exist, but doesn't");
77+
assert(!notExistKernel && "non-existing kernel should NOT exist.");
78+
};
79+
11580
source_kb kbSrc = syclex::create_kernel_bundle_from_source(
11681
ctx, syclex::source_language::opencl, CLSource);
117-
// compilation of empty prop list, no devices
118-
exe_kb kbExe1 = syclex::build(kbSrc);
11982

12083
// compilation with props and devices
121-
std::string log;
12284
std::vector<std::string> flags{"-cl-fast-relaxed-math",
12385
"-cl-finite-math-only"};
124-
std::vector<sycl::device> devs = kbSrc.get_devices();
125-
sycl::context ctxRes = kbSrc.get_context();
126-
assert(ctxRes == ctx);
127-
sycl::backend beRes = kbSrc.get_backend();
128-
assert(beRes == ctx.get_backend());
129-
130-
exe_kb kbExe2 = syclex::build(
131-
kbSrc, devs,
132-
syclex::properties{syclex::build_options{flags}, syclex::save_log{&log}});
133-
134-
bool hasMyKernel = kbExe2.ext_oneapi_has_kernel("my_kernel");
135-
bool hasHerKernel = kbExe2.ext_oneapi_has_kernel("her_kernel");
136-
bool notExistKernel = kbExe2.ext_oneapi_has_kernel("not_exist");
137-
assert(hasMyKernel && "my_kernel should exist, but doesn't");
138-
assert(hasHerKernel && "her_kernel should exist, but doesn't");
139-
assert(!notExistKernel && "non-existing kernel should NOT exist, but does?");
140-
141-
sycl::kernel my_kernel = kbExe2.ext_oneapi_get_kernel("my_kernel");
142-
sycl::kernel her_kernel = kbExe2.ext_oneapi_get_kernel("her_kernel");
143-
144-
auto my_num_args = my_kernel.get_info<sycl::info::kernel::num_args>();
145-
assert(my_num_args == 2 && "my_kernel should take 2 args");
146-
147-
testSyclKernel(q, my_kernel, 2, 100);
148-
testSyclKernel(q, her_kernel, 5, 1000);
149-
}
15086

151-
void test_error() {
152-
namespace syclex = sycl::ext::oneapi::experimental;
153-
using source_kb = sycl::kernel_bundle<sycl::bundle_state::ext_oneapi_source>;
154-
using exe_kb = sycl::kernel_bundle<sycl::bundle_state::executable>;
155-
156-
// only one device is supported at this time, so we limit the queue and
157-
// context to that
158-
sycl::device d{sycl::default_selector_v};
159-
sycl::context ctx{d};
160-
sycl::queue q{ctx, d};
161-
162-
bool ok =
163-
q.get_device().ext_oneapi_can_compile(syclex::source_language::opencl);
164-
if (!ok) {
165-
return;
166-
}
167-
168-
try {
169-
source_kb kbSrc = syclex::create_kernel_bundle_from_source(
170-
ctx, syclex::source_language::opencl, BadCLSource);
171-
exe_kb kbExe1 = syclex::build(kbSrc);
172-
assert(false && "we should not be here.");
173-
} catch (sycl::exception &e) {
174-
// nice!
175-
assert(e.code() == sycl::errc::build);
176-
}
177-
// any other error will escape and cause the test to fail ( as it should ).
87+
// Device image #1
88+
// CHECK: [Persistent Cache]: Cache size file not present. Creating one.
89+
// CHECK-NEXT: [Persistent Cache]: Cache size file created.
90+
// CHECK-NEXT: [kernel_compiler Persistent Cache]: binary has been cached: [[DEVIMG1:.*]]
91+
// CHECK-NEXT: [Persistent Cache]: Updating the cache size file.
92+
CreateAndVerifyKB(kbSrc, {});
93+
94+
// Device image #2
95+
// CHECK-NEXT: [kernel_compiler Persistent Cache]: binary has been cached: [[DEVIMG2:.*]]
96+
// CHECK-NEXT: [Persistent Cache]: Updating the cache size file.
97+
CreateAndVerifyKB(kbSrc, {flags[0]});
98+
99+
// Device image #3
100+
// CHECK: [kernel_compiler Persistent Cache]: binary has been cached: [[DEVIMG3:.*]]
101+
// CHECK: [Persistent Cache]: Updating the cache size file.
102+
CreateAndVerifyKB(kbSrc, {flags[1]});
103+
104+
// Re-insert device image #1
105+
// CHECK: [kernel_compiler Persistent Cache]: using cached binary: [[DEVIMG1]]
106+
CreateAndVerifyKB(kbSrc, {});
107+
108+
// Device image #4
109+
// CHECK: [kernel_compiler Persistent Cache]: binary has been cached: [[DEVIMG4:.*]]
110+
// CHECK: [Persistent Cache]: Updating the cache size file.
111+
// CHECK: [Persistent Cache]: Cache eviction triggered.
112+
// CHECK: [Persistent Cache]: File removed: [[DEVIMG2]]
113+
// CHECK: [Persistent Cache]: File removed: [[DEVIMG3]]
114+
// CHECK: [Persistent Cache]: File removed: [[DEVIMG1]]
115+
CreateAndVerifyKB(kbSrc, {flags[0], flags[1]});
116+
117+
// Re-insert device image #4
118+
// CHECK: [kernel_compiler Persistent Cache]: using cached binary: [[DEVIMG4]]
119+
CreateAndVerifyKB(kbSrc, {flags[0], flags[1]});
178120
}
179121

180122
int main() {
@@ -184,7 +126,6 @@ int main() {
184126

185127
#ifdef SYCL_EXT_ONEAPI_KERNEL_COMPILER
186128
test_build_and_run();
187-
test_error();
188129
#else
189130
static_assert(false, "Kernel Compiler feature test macro undefined");
190131
#endif

0 commit comments

Comments
 (0)