Skip to content

Commit 6d883ad

Browse files
authored
[SYCL] Add AOT support for device memory sanitizer (#16705)
This PR adds AOT device msan library targets for PVC and CPU. These AOT msan libdevice will be linked in AOT compilation. --------- Signed-off-by: jinge90 <[email protected]>
1 parent 0129333 commit 6d883ad

File tree

5 files changed

+75
-14
lines changed

5 files changed

+75
-14
lines changed

clang/lib/Driver/ToolChains/SYCL.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,13 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
611611
{"libsycl-asan-cpu", "internal"},
612612
{"libsycl-asan-dg2", "internal"},
613613
{"libsycl-asan-pvc", "internal"}};
614-
const SYCLDeviceLibsList SYCLDeviceMsanLibs = {{"libsycl-msan", "internal"}};
614+
const SYCLDeviceLibsList SYCLDeviceMsanLibs = {
615+
{"libsycl-msan", "internal"},
616+
{"libsycl-msan-cpu", "internal"},
617+
// Currently, we only provide aot msan libdevice for PVC and CPU.
618+
// For DG2, we just use libsycl-msan as placeholder.
619+
{"libsycl-msan", "internal"},
620+
{"libsycl-msan-pvc", "internal"}};
615621
#endif
616622

617623
const SYCLDeviceLibsList SYCLNativeCpuDeviceLibs = {
@@ -769,7 +775,7 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
769775
if (SanitizeVal == "address")
770776
addSingleLibrary(SYCLDeviceAsanLibs[sanitizer_lib_idx]);
771777
else if (SanitizeVal == "memory")
772-
addLibraries(SYCLDeviceMsanLibs);
778+
addSingleLibrary(SYCLDeviceMsanLibs[sanitizer_lib_idx]);
773779
#endif
774780

775781
if (isNativeCPU)

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

+23
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,26 @@
355355
// SYCL_DEVICE_MSAN_MACRO-SAME: "USE_SYCL_DEVICE_MSAN"
356356
// SYCL_DEVICE_MSAN_MACRO: llvm-link{{.*}} "-only-needed"
357357
// SYCL_DEVICE_MSAN_MACRO-SAME: "{{.*}}libsycl-msan.bc"
358+
359+
/// ###########################################################################
360+
/// test behavior of linking libsycl-msan-pvc for PVC target AOT compilation when msan flag is applied.
361+
// RUN: %clangxx -fsycl -fsycl-targets=intel_gpu_pvc --no-offload-new-driver %s --sysroot=%S/Inputs/SYCL \
362+
// RUN: -Xarch_device -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_PVC
363+
// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xsycl-target-backend "-device pvc" --no-offload-new-driver %s \
364+
// RUN: --sysroot=%S/Inputs/SYCL -Xarch_device -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_PVC
365+
// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xsycl-target-backend=spir64_gen "-device pvc" --no-offload-new-driver %s \
366+
// RUN: --sysroot=%S/Inputs/SYCL -Xarch_device -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_PVC
367+
// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xsycl-target-backend "-device 12.60.7" --no-offload-new-driver %s \
368+
// RUN: --sysroot=%S/Inputs/SYCL -Xarch_device -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_PVC
369+
// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xs "-device 12.60.7" --no-offload-new-driver %s --sysroot=%S/Inputs/SYCL \
370+
// RUN: -Xarch_device -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_PVC
371+
// SYCL_DEVICE_LIB_MSAN_PVC: llvm-link{{.*}} "{{.*}}libsycl-crt.bc"
372+
// SYCL_DEVICE_LIB_MSAN_PVC-SAME: "{{.*}}libsycl-msan-pvc.bc"
373+
374+
375+
/// ###########################################################################
376+
/// test behavior of linking libsycl-msan-cpu for CPU target AOT compilation when msan flag is applied.
377+
// RUN: %clangxx -fsycl -fsycl-targets=spir64_x86_64 --no-offload-new-driver %s --sysroot=%S/Inputs/SYCL \
378+
// RUN: -Xarch_device -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_CPU
379+
// SYCL_DEVICE_LIB_MSAN_CPU: llvm-link{{.*}} "{{.*}}libsycl-crt.bc"
380+
// SYCL_DEVICE_LIB_MSAN_CPU-SAME: "{{.*}}libsycl-msan-cpu.bc"

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

+13
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,16 @@
352352
// SYCL_DEVICE_MSAN_MACRO: "-cc1"
353353
// SYCL_DEVICE_MSAN_MACRO-SAME: "USE_SYCL_DEVICE_MSAN"
354354
// SYCL_DEVICE_MSAN_MACRO: libsycl-msan.new.o
355+
356+
/// test behavior of msan libdevice linking when -fsanitize=memory is available for AOT targets
357+
// RUN: %clangxx -fsycl -fsycl-targets=intel_gpu_pvc --offload-new-driver %s --sysroot=%S/Inputs/SYCL \
358+
// RUN: -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_PVC
359+
// SYCL_DEVICE_LIB_MSAN_PVC: clang-linker-wrapper{{.*}} "-sycl-device-libraries
360+
// SYCL_DEVICE_LIB_MSAN_PVC-SAME: {{.*}}libsycl-msan-pvc.new.o
361+
362+
/// test behavior of msan libdevice linking when -fsanitize=memory is available for AOT targets
363+
// RUN: %clangxx -fsycl -fsycl-targets=spir64_x86_64 --offload-new-driver %s --sysroot=%S/Inputs/SYCL \
364+
// RUN: -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_CPU
365+
// SYCL_DEVICE_LIB_MSAN_CPU: clang-linker-wrapper{{.*}} "-sycl-device-libraries
366+
// SYCL_DEVICE_LIB_MSAN_CPU-SAME: {{.*}}libsycl-msan-cpu.new.o
367+

libdevice/cmake/modules/SYCLLibdevice.cmake

+25-12
Original file line numberDiff line numberDiff line change
@@ -248,46 +248,46 @@ if (NOT MSVC AND UR_SANITIZER_INCLUDE_DIR)
248248
-I${UR_SANITIZER_INCLUDE_DIR}
249249
-I${CMAKE_CURRENT_SOURCE_DIR})
250250

251-
set(asan_pvc_compile_opts_obj -fsycl -c
251+
set(sanitizer_pvc_compile_opts_obj -fsycl -c
252252
${sanitizer_generic_compile_opts}
253253
${sycl_pvc_target_opt}
254254
-D__LIBDEVICE_PVC__)
255255

256-
set(asan_cpu_compile_opts_obj -fsycl -c
256+
set(sanitizer_cpu_compile_opts_obj -fsycl -c
257257
${sanitizer_generic_compile_opts}
258258
${sycl_cpu_target_opt}
259259
-D__LIBDEVICE_CPU__)
260260

261-
set(asan_dg2_compile_opts_obj -fsycl -c
261+
set(sanitizer_dg2_compile_opts_obj -fsycl -c
262262
${sanitizer_generic_compile_opts}
263263
${sycl_dg2_target_opt}
264264
-D__LIBDEVICE_DG2__)
265265

266-
set(asan_pvc_compile_opts_bc ${bc_device_compile_opts}
266+
set(sanitizer_pvc_compile_opts_bc ${bc_device_compile_opts}
267267
${sanitizer_generic_compile_opts}
268268
-D__LIBDEVICE_PVC__)
269269

270-
set(asan_cpu_compile_opts_bc ${bc_device_compile_opts}
270+
set(sanitizer_cpu_compile_opts_bc ${bc_device_compile_opts}
271271
${sanitizer_generic_compile_opts}
272272
-D__LIBDEVICE_CPU__)
273273

274-
set(asan_dg2_compile_opts_bc ${bc_device_compile_opts}
274+
set(sanitizer_dg2_compile_opts_bc ${bc_device_compile_opts}
275275
${sanitizer_generic_compile_opts}
276276
-D__LIBDEVICE_DG2__)
277277

278-
set(asan_pvc_compile_opts_obj-new-offload -fsycl -c --offload-new-driver
278+
set(sanitizer_pvc_compile_opts_obj-new-offload -fsycl -c --offload-new-driver
279279
-foffload-lto=thin
280280
${sanitizer_generic_compile_opts}
281281
${sycl_pvc_target_opt}
282282
-D__LIBDEVICE_PVC__)
283283

284-
set(asan_cpu_compile_opts_obj-new-offload -fsycl -c --offload-new-driver
284+
set(sanitizer_cpu_compile_opts_obj-new-offload -fsycl -c --offload-new-driver
285285
-foffload-lto=thin
286286
${sanitizer_generic_compile_opts}
287287
${sycl_cpu_target_opt}
288288
-D__LIBDEVICE_CPU__)
289289

290-
set(asan_dg2_compile_opts_obj-new-offload -fsycl -c --offload-new-driver
290+
set(sanitizer_dg2_compile_opts_obj-new-offload -fsycl -c --offload-new-driver
291291
-foffload-lto=thin
292292
${sanitizer_generic_compile_opts}
293293
${sycl_dg2_target_opt}
@@ -373,16 +373,16 @@ else()
373373
-I${CMAKE_CURRENT_SOURCE_DIR})
374374

375375
# asan aot
376-
set(asan_filetypes obj obj-new-offload bc)
376+
set(sanitizer_filetypes obj obj-new-offload bc)
377377
set(asan_devicetypes pvc cpu dg2)
378378

379-
foreach(asan_ft IN LISTS asan_filetypes)
379+
foreach(asan_ft IN LISTS sanitizer_filetypes)
380380
foreach(asan_device IN LISTS asan_devicetypes)
381381
compile_lib_ext(libsycl-asan-${asan_device}
382382
SRC sanitizer/asan_rtl.cpp
383383
FILETYPE ${asan_ft}
384384
DEPENDENCIES ${asan_obj_deps}
385-
OPTS ${asan_${asan_device}_compile_opts_${asan_ft}})
385+
OPTS ${sanitizer_${asan_device}_compile_opts_${asan_ft}})
386386
endforeach()
387387
endforeach()
388388

@@ -393,6 +393,19 @@ else()
393393
EXTRA_OPTS -fno-sycl-instrument-device-code
394394
-I${UR_SANITIZER_INCLUDE_DIR}
395395
-I${CMAKE_CURRENT_SOURCE_DIR})
396+
397+
set(msan_devicetypes pvc cpu)
398+
399+
foreach(msan_ft IN LISTS sanitizer_filetypes)
400+
foreach(msan_device IN LISTS msan_devicetypes)
401+
compile_lib_ext(libsycl-msan-${msan_device}
402+
SRC sanitizer/msan_rtl.cpp
403+
FILETYPE ${msan_ft}
404+
DEPENDENCIES ${msan_obj_deps}
405+
OPTS ${sanitizer_${msan_device}_compile_opts_${msan_ft}})
406+
endforeach()
407+
endforeach()
408+
396409
endif()
397410
endif()
398411

libdevice/sanitizer/msan_rtl.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ DEVICE_EXTERN_C_NOINLINE uptr __msan_get_shadow(uptr addr, uint32_t as) {
201201
MSAN_DEBUG(__spirv_ocl_printf(__msan_print_launchinfo, (void *)launch_info,
202202
launch_info->GlobalShadowOffset));
203203

204+
#if defined(__LIBDEVICE_PVC__)
205+
shadow_ptr = __msan_get_shadow_pvc(addr, as);
206+
#elif defined(__LIBDEVICE_CPU__)
207+
shadow_ptr = __msan_get_shadow_cpu(addr);
208+
#else
204209
if (LIKELY(launch_info->DeviceTy == DeviceType::CPU)) {
205210
shadow_ptr = __msan_get_shadow_cpu(addr);
206211
} else if (launch_info->DeviceTy == DeviceType::GPU_PVC) {
@@ -209,6 +214,7 @@ DEVICE_EXTERN_C_NOINLINE uptr __msan_get_shadow(uptr addr, uint32_t as) {
209214
MSAN_DEBUG(__spirv_ocl_printf(__msan_print_unsupport_device_type,
210215
launch_info->DeviceTy));
211216
}
217+
#endif
212218

213219
MSAN_DEBUG(__spirv_ocl_printf(__msan_print_shadow, (void *)addr, as,
214220
(void *)shadow_ptr, *(u8 *)shadow_ptr));

0 commit comments

Comments
 (0)