Skip to content

Commit 48484eb

Browse files
committed
[SYCL] Introduce sycl-sanitizer tool
sycl-sanitizer tool aims to help developers find memory leaks when using USM pointers.
1 parent c91b3b8 commit 48484eb

File tree

13 files changed

+954
-220
lines changed

13 files changed

+954
-220
lines changed

sycl/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ endif()
173173

174174
if (SYCL_ENABLE_XPTI_TRACING)
175175
set(XPTIFW_LIBS xpti xptifw)
176+
set(SYCL_TOOLS sycl-sanitizer)
176177
endif()
177178

178179
# SYCL toolchain builds all components: compiler, libraries, headers, etc.
@@ -199,6 +200,7 @@ add_custom_target( sycl-toolchain
199200
sycl-compiler
200201
sycl-ls
201202
${XPTIFW_LIBS}
203+
${SYCL_TOOLS}
202204
COMMENT "Building SYCL compiler toolchain..."
203205
)
204206

@@ -261,6 +263,7 @@ set( SYCL_TOOLCHAIN_DEPLOY_COMPONENTS
261263
pi_level_zero
262264
libsycldevice
263265
${XPTIFW_LIBS}
266+
${SYCL_TOOLS}
264267
)
265268

266269
if(OpenCL_INSTALL_KHRONOS_ICD_LOADER AND TARGET OpenCL-ICD)

sycl/include/CL/sycl/detail/usm_impl.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// ===--------------------------------------------------------------------=== //
88
#pragma once
99

10+
#include <CL/sycl/detail/common.hpp>
1011
#include <CL/sycl/detail/export.hpp>
1112
#include <CL/sycl/usm/usm_enums.hpp>
1213

@@ -17,13 +18,16 @@ namespace usm {
1718

1819
__SYCL_EXPORT void *alignedAlloc(size_t Alignment, size_t Bytes,
1920
const context &Ctxt, const device &Dev,
20-
cl::sycl::usm::alloc Kind);
21+
cl::sycl::usm::alloc Kind,
22+
const code_location &CL);
2123

2224
__SYCL_EXPORT void *alignedAllocHost(size_t Alignment, size_t Bytes,
2325
const context &Ctxt,
24-
cl::sycl::usm::alloc Kind);
26+
cl::sycl::usm::alloc Kind,
27+
const code_location &CL);
2528

26-
__SYCL_EXPORT void free(void *Ptr, const context &Ctxt);
29+
__SYCL_EXPORT void free(void *Ptr, const context &Ctxt,
30+
const code_location &CL);
2731

2832
} // namespace usm
2933
} // namespace detail

sycl/include/CL/sycl/usm.hpp

Lines changed: 197 additions & 133 deletions
Large diffs are not rendered by default.

sycl/include/CL/sycl/usm/usm_allocator.hpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#pragma once
99

1010
#include <CL/sycl/context.hpp>
11+
#include <CL/sycl/detail/common.hpp>
1112
#include <CL/sycl/detail/export.hpp>
1213
#include <CL/sycl/device.hpp>
1314
#include <CL/sycl/exception.hpp>
@@ -24,8 +25,10 @@ namespace sycl {
2425
__SYCL_EXPORT void *aligned_alloc(size_t alignment, size_t size,
2526
const device &dev, const context &ctxt,
2627
usm::alloc kind,
27-
const property_list &propList);
28-
__SYCL_EXPORT void free(void *ptr, const context &ctxt);
28+
const property_list &propList,
29+
const detail::code_location CL);
30+
__SYCL_EXPORT void free(void *ptr, const context &ctxt,
31+
const detail::code_location CL);
2932

3033
template <typename T, usm::alloc AllocKind, size_t Alignment = alignof(T)>
3134
class usm_allocator {
@@ -74,11 +77,12 @@ class usm_allocator {
7477
/// Allocates memory.
7578
///
7679
/// \param NumberOfElements is a count of elements to allocate memory for.
77-
T *allocate(size_t NumberOfElements) {
80+
T *allocate(size_t NumberOfElements, const detail::code_location CL =
81+
detail::code_location::current()) {
7882

7983
auto Result = reinterpret_cast<T *>(
8084
aligned_alloc(getAlignment(), NumberOfElements * sizeof(value_type),
81-
MDevice, MContext, AllocKind, MPropList));
85+
MDevice, MContext, AllocKind, MPropList, CL));
8286
if (!Result) {
8387
throw memory_allocation_error();
8488
}
@@ -89,9 +93,11 @@ class usm_allocator {
8993
///
9094
/// \param Ptr is a pointer to memory being deallocated.
9195
/// \param Size is a number of elements previously passed to allocate.
92-
void deallocate(T *Ptr, size_t) {
96+
void deallocate(
97+
T *Ptr, size_t,
98+
const detail::code_location CL = detail::code_location::current()) {
9399
if (Ptr) {
94-
free(Ptr, MContext);
100+
free(Ptr, MContext, CL);
95101
}
96102
}
97103

0 commit comments

Comments
 (0)