Skip to content

Commit e57fd94

Browse files
authored
[SYCL] Add device sanitizer libraries (#11786)
We are working on sanitizer for device code which is based on LLVM sanitizer which consists of compiler instrumentation, devicelib implementation for instrumentation, runtime support. Compiler will instrument some "__asan_*" function call in user's device image if sanitizer flags is added in building flag and the implementation of these "__asan_*" is located in libsycl-sanitizer.o which will be linked with user's device image if needed automatically via current devicelib infrastructure. This PR adds this new devicelib with dummy implementation, all cmake, test, clang driver update are finished in this PR. The libsycl-sanitizer.o will be invovled when "-fsantize=address" is used together with sycl flags. Device sanitizer developers will gradually add real implementation into libsycl-santizer.o in future PRs. --------- Signed-off-by: jinge90 <[email protected]>
1 parent 5c84218 commit e57fd94

File tree

6 files changed

+75
-0
lines changed

6 files changed

+75
-0
lines changed

clang/lib/Driver/ToolChains/SYCL.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
269269
{"libsycl-itt-user-wrappers", "internal"},
270270
{"libsycl-itt-compiler-wrappers", "internal"},
271271
{"libsycl-itt-stubs", "internal"}};
272+
#if !defined(_WIN32)
273+
const SYCLDeviceLibsList SYCLDeviceSanitizerLibs = {
274+
{"libsycl-sanitizer", "internal"}};
275+
#endif
272276

273277
auto addLibraries = [&](const SYCLDeviceLibsList &LibsList) {
274278
for (const DeviceLibOptInfo &Lib : LibsList) {
@@ -298,6 +302,17 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
298302
options::OPT_fno_sycl_instrument_device_code, true))
299303
addLibraries(SYCLDeviceAnnotationLibs);
300304

305+
#if !defined(_WIN32)
306+
if (Arg *A = Args.getLastArg(options::OPT_fsanitize_EQ,
307+
options::OPT_fno_sanitize_EQ)) {
308+
if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
309+
A->getValues().size() == 1) {
310+
std::string SanitizeVal = A->getValue();
311+
if (SanitizeVal == "address")
312+
addLibraries(SYCLDeviceSanitizerLibs);
313+
}
314+
}
315+
#endif
301316
return LibraryList;
302317
}
303318

clang/test/Driver/Inputs/SYCL/lib/libsycl-sanitizer.o

Whitespace-only changes.

clang/test/Driver/sycl-device-lib.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,29 @@
211211
// SYCL_LLVM_LINK_DEVICE_LIB_SPIRV_CPU_AOT-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64_x86_64-unknown-unknown" "-input={{.*}}libsycl-itt-compiler-wrappers.o" "-output={{.*}}libsycl-itt-compiler-wrappers-{{.*}}.o" "-unbundle"
212212
// SYCL_LLVM_LINK_DEVICE_LIB_SPIRV_CPU_AOT-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64_x86_64-unknown-unknown" "-input={{.*}}libsycl-itt-stubs.o" "-output={{.*}}libsycl-itt-stubs-{{.*}}.o" "-unbundle"
213213
// SYCL_LLVM_LINK_DEVICE_LIB_SPIRV_CPU_AOT-NEXT: llvm-link{{.*}} "-only-needed" "{{.*}}" "-o" "{{.*}}.bc" "--suppress-warnings"
214+
215+
/// ###########################################################################
216+
/// test behavior of libsycl-sanitizer.o linking when -fsanitize=address is available
217+
// RUN: %clangxx -fsycl %s --sysroot=%S/Inputs/SYCL -fsanitize=address -### 2>&1 \
218+
// RUN: | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_SANITIZER
219+
// SYCL_DEVICE_LIB_SANITIZER: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-crt.o" "-output={{.*}}libsycl-crt-{{.*}}.o" "-unbundle"
220+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-complex.o" "-output={{.*}}libsycl-complex-{{.*}}.o" "-unbundle"
221+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-complex-fp64.o" "-output={{.*}}libsycl-complex-fp64-{{.*}}.o" "-unbundle"
222+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-cmath.o" "-output={{.*}}libsycl-cmath-{{.*}}.o" "-unbundle"
223+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-cmath-fp64.o" "-output={{.*}}libsycl-cmath-fp64-{{.*}}.o" "-unbundle"
224+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-imf.o" "-output={{.*}}libsycl-imf-{{.*}}.o" "-unbundle"
225+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-imf-fp64.o" "-output={{.*}}libsycl-imf-fp64-{{.*}}.o" "-unbundle"
226+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-imf-bf16.o" "-output={{.*}}libsycl-imf-bf16-{{.*}}.o" "-unbundle"
227+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-fallback-cassert.o" "-output={{.*}}libsycl-fallback-cassert-{{.*}}.o" "-unbundle"
228+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-fallback-cstring.o" "-output={{.*}}libsycl-fallback-cstring-{{.*}}.o" "-unbundle"
229+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-fallback-complex.o" "-output={{.*}}libsycl-fallback-complex-{{.*}}.o" "-unbundle"
230+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-fallback-complex-fp64.o" "-output={{.*}}libsycl-fallback-complex-fp64-{{.*}}.o" "-unbundle"
231+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-fallback-cmath.o" "-output={{.*}}libsycl-fallback-cmath-{{.*}}.o" "-unbundle"
232+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-fallback-cmath-fp64.o" "-output={{.*}}libsycl-fallback-cmath-fp64-{{.*}}.o" "-unbundle"
233+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-fallback-imf.o" "-output={{.*}}libsycl-fallback-imf-{{.*}}.o" "-unbundle"
234+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-fallback-imf-fp64.o" "-output={{.*}}libsycl-fallback-imf-fp64-{{.*}}.o" "-unbundle"
235+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-fallback-imf-bf16.o" "-output={{.*}}libsycl-fallback-imf-bf16-{{.*}}.o" "-unbundle"
236+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-itt-user-wrappers.o" "-output={{.*}}libsycl-itt-user-wrappers-{{.*}}.o" "-unbundle"
237+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-itt-compiler-wrappers.o" "-output={{.*}}libsycl-itt-compiler-wrappers-{{.*}}.o" "-unbundle"
238+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-itt-stubs.o" "-output={{.*}}libsycl-itt-stubs-{{.*}}.o" "-unbundle"
239+
// SYCL_DEVICE_LIB_SANITIZER-NEXT: clang-offload-bundler{{.*}} "-type=o" "-targets=sycl-spir64-unknown-unknown" "-input={{.*}}libsycl-sanitizer.o" "-output={{.*}}libsycl-sanitizer-{{.*}}.o" "-unbundle"

libdevice/cmake/modules/SYCLLibdevice.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ set(cmath_obj_deps device_math.h device.h sycl-compiler)
108108
set(imf_obj_deps device_imf.hpp imf_half.hpp imf_bf16.hpp imf_rounding_op.hpp imf_impl_utils.hpp device.h sycl-compiler)
109109
set(itt_obj_deps device_itt.h spirv_vars.h device.h sycl-compiler)
110110
set(bfloat16_obj_deps sycl-headers sycl-compiler)
111+
if (NOT MSVC)
112+
set(sanitizer_obj_deps device.h sycl-compiler)
113+
endif()
111114

112115
add_devicelib_obj(libsycl-itt-stubs SRC itt_stubs.cpp DEP ${itt_obj_deps})
113116
add_devicelib_obj(libsycl-itt-compiler-wrappers SRC itt_compiler_wrappers.cpp DEP ${itt_obj_deps})
@@ -124,6 +127,8 @@ add_devicelib_obj(libsycl-imf-bf16 SRC imf_wrapper_bf16.cpp DEP ${imf_obj_deps})
124127
add_devicelib_obj(libsycl-bfloat16 SRC bfloat16_wrapper.cpp DEP ${cmath_obj_deps} )
125128
if(MSVC)
126129
add_devicelib_obj(libsycl-msvc-math SRC msvc_math.cpp DEP ${cmath_obj_deps})
130+
else()
131+
add_devicelib_obj(libsycl-sanitizer SRC sanitizer_utils.cpp DEP ${sanitizer_obj_deps})
127132
endif()
128133

129134
add_fallback_devicelib(libsycl-fallback-cassert SRC fallback-cassert.cpp DEP ${crt_obj_deps} EXTRA_ARGS -fno-sycl-instrument-device-code)

libdevice/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#define DEVICE_EXTERN_C DEVICE_EXTERNAL EXTERN_C
2626
#define DEVICE_EXTERN_C_INLINE \
2727
DEVICE_EXTERNAL EXTERN_C __attribute__((always_inline))
28+
#define DEVICE_EXTERN_C_NOINLINE \
29+
DEVICE_EXTERNAL EXTERN_C __attribute__((noinline))
2830
#endif // __SPIR__ || __NVPTX__
2931

3032
#if defined(__SPIR__) || defined(__LIBDEVICE_HOST_IMPL__)

libdevice/sanitizer_utils.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//==--- sanitizer_utils.cpp - device sanitizer util inserted by compiler ---==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "device.h"
10+
#include <cstdint>
11+
12+
using uptr = uint64_t;
13+
#if defined(__SPIR__)
14+
// TODO: add real implementation in __asan_load_n.
15+
DEVICE_EXTERN_C_NOINLINE
16+
void __asan_load_n(uptr addr, unsigned n) {
17+
(void)addr;
18+
(void)n;
19+
return;
20+
}
21+
22+
DEVICE_EXTERN_C_NOINLINE
23+
void __asan_load4(uptr addr) { __asan_load_n(addr, 4); }
24+
25+
DEVICE_EXTERN_C_NOINLINE
26+
void __asan_load8(uptr addr) { __asan_load_n(addr, 8); }
27+
#endif // __SPIR__

0 commit comments

Comments
 (0)