Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYCL] Add AOT support for device memory sanitizer #16705

Merged
merged 6 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions clang/lib/Driver/ToolChains/SYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,13 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
{"libsycl-asan-cpu", "internal"},
{"libsycl-asan-dg2", "internal"},
{"libsycl-asan-pvc", "internal"}};
const SYCLDeviceLibsList SYCLDeviceMsanLibs = {{"libsycl-msan", "internal"}};
const SYCLDeviceLibsList SYCLDeviceMsanLibs = {
{"libsycl-msan", "internal"},
{"libsycl-msan-cpu", "internal"},
// Currently, we only provide aot msan libdevice for PVC and CPU.
// For DG2, we just use libsycl-msan as placeholder.
{"libsycl-msan", "internal"},
{"libsycl-msan-pvc", "internal"}};
#endif

const SYCLDeviceLibsList SYCLNativeCpuDeviceLibs = {
Expand Down Expand Up @@ -769,7 +775,7 @@ SYCL::getDeviceLibraries(const Compilation &C, const llvm::Triple &TargetTriple,
if (SanitizeVal == "address")
addSingleLibrary(SYCLDeviceAsanLibs[sanitizer_lib_idx]);
else if (SanitizeVal == "memory")
addLibraries(SYCLDeviceMsanLibs);
addSingleLibrary(SYCLDeviceMsanLibs[sanitizer_lib_idx]);
#endif

if (isNativeCPU)
Expand Down
23 changes: 23 additions & 0 deletions clang/test/Driver/sycl-device-lib-old-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,26 @@
// SYCL_DEVICE_MSAN_MACRO-SAME: "USE_SYCL_DEVICE_MSAN"
// SYCL_DEVICE_MSAN_MACRO: llvm-link{{.*}} "-only-needed"
// SYCL_DEVICE_MSAN_MACRO-SAME: "{{.*}}libsycl-msan.bc"

/// ###########################################################################
/// test behavior of linking libsycl-msan-pvc for PVC target AOT compilation when msan flag is applied.
// RUN: %clangxx -fsycl -fsycl-targets=intel_gpu_pvc --no-offload-new-driver %s --sysroot=%S/Inputs/SYCL \
// RUN: -Xarch_device -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_PVC
// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xsycl-target-backend "-device pvc" --no-offload-new-driver %s \
// RUN: --sysroot=%S/Inputs/SYCL -Xarch_device -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_PVC
// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xsycl-target-backend=spir64_gen "-device pvc" --no-offload-new-driver %s \
// RUN: --sysroot=%S/Inputs/SYCL -Xarch_device -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_PVC
// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xsycl-target-backend "-device 12.60.7" --no-offload-new-driver %s \
// RUN: --sysroot=%S/Inputs/SYCL -Xarch_device -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_PVC
// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xs "-device 12.60.7" --no-offload-new-driver %s --sysroot=%S/Inputs/SYCL \
// RUN: -Xarch_device -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_PVC
// SYCL_DEVICE_LIB_MSAN_PVC: llvm-link{{.*}} "{{.*}}libsycl-crt.bc"
// SYCL_DEVICE_LIB_MSAN_PVC-SAME: "{{.*}}libsycl-msan-pvc.bc"


/// ###########################################################################
/// test behavior of linking libsycl-msan-cpu for CPU target AOT compilation when msan flag is applied.
// RUN: %clangxx -fsycl -fsycl-targets=spir64_x86_64 --no-offload-new-driver %s --sysroot=%S/Inputs/SYCL \
// RUN: -Xarch_device -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_CPU
// SYCL_DEVICE_LIB_MSAN_CPU: llvm-link{{.*}} "{{.*}}libsycl-crt.bc"
// SYCL_DEVICE_LIB_MSAN_CPU-SAME: "{{.*}}libsycl-msan-cpu.bc"
13 changes: 13 additions & 0 deletions clang/test/Driver/sycl-device-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,16 @@
// SYCL_DEVICE_MSAN_MACRO: "-cc1"
// SYCL_DEVICE_MSAN_MACRO-SAME: "USE_SYCL_DEVICE_MSAN"
// SYCL_DEVICE_MSAN_MACRO: libsycl-msan.new.o

/// test behavior of msan libdevice linking when -fsanitize=memory is available for AOT targets
// RUN: %clangxx -fsycl -fsycl-targets=intel_gpu_pvc --offload-new-driver %s --sysroot=%S/Inputs/SYCL \
// RUN: -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_PVC
// SYCL_DEVICE_LIB_MSAN_PVC: clang-linker-wrapper{{.*}} "-sycl-device-libraries
// SYCL_DEVICE_LIB_MSAN_PVC-SAME: {{.*}}libsycl-msan-pvc.new.o

/// test behavior of msan libdevice linking when -fsanitize=memory is available for AOT targets
// RUN: %clangxx -fsycl -fsycl-targets=spir64_x86_64 --offload-new-driver %s --sysroot=%S/Inputs/SYCL \
// RUN: -fsanitize=memory -### 2>&1 | FileCheck %s -check-prefix=SYCL_DEVICE_LIB_MSAN_CPU
// SYCL_DEVICE_LIB_MSAN_CPU: clang-linker-wrapper{{.*}} "-sycl-device-libraries
// SYCL_DEVICE_LIB_MSAN_CPU-SAME: {{.*}}libsycl-msan-cpu.new.o

37 changes: 25 additions & 12 deletions libdevice/cmake/modules/SYCLLibdevice.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -248,46 +248,46 @@ if (NOT MSVC AND UR_SANITIZER_INCLUDE_DIR)
-I${UR_SANITIZER_INCLUDE_DIR}
-I${CMAKE_CURRENT_SOURCE_DIR})

set(asan_pvc_compile_opts_obj -fsycl -c
set(sanitizer_pvc_compile_opts_obj -fsycl -c
${sanitizer_generic_compile_opts}
${sycl_pvc_target_opt}
-D__LIBDEVICE_PVC__)

set(asan_cpu_compile_opts_obj -fsycl -c
set(sanitizer_cpu_compile_opts_obj -fsycl -c
${sanitizer_generic_compile_opts}
${sycl_cpu_target_opt}
-D__LIBDEVICE_CPU__)

set(asan_dg2_compile_opts_obj -fsycl -c
set(sanitizer_dg2_compile_opts_obj -fsycl -c
${sanitizer_generic_compile_opts}
${sycl_dg2_target_opt}
-D__LIBDEVICE_DG2__)

set(asan_pvc_compile_opts_bc ${bc_device_compile_opts}
set(sanitizer_pvc_compile_opts_bc ${bc_device_compile_opts}
${sanitizer_generic_compile_opts}
-D__LIBDEVICE_PVC__)

set(asan_cpu_compile_opts_bc ${bc_device_compile_opts}
set(sanitizer_cpu_compile_opts_bc ${bc_device_compile_opts}
${sanitizer_generic_compile_opts}
-D__LIBDEVICE_CPU__)

set(asan_dg2_compile_opts_bc ${bc_device_compile_opts}
set(sanitizer_dg2_compile_opts_bc ${bc_device_compile_opts}
${sanitizer_generic_compile_opts}
-D__LIBDEVICE_DG2__)

set(asan_pvc_compile_opts_obj-new-offload -fsycl -c --offload-new-driver
set(sanitizer_pvc_compile_opts_obj-new-offload -fsycl -c --offload-new-driver
-foffload-lto=thin
${sanitizer_generic_compile_opts}
${sycl_pvc_target_opt}
-D__LIBDEVICE_PVC__)

set(asan_cpu_compile_opts_obj-new-offload -fsycl -c --offload-new-driver
set(sanitizer_cpu_compile_opts_obj-new-offload -fsycl -c --offload-new-driver
-foffload-lto=thin
${sanitizer_generic_compile_opts}
${sycl_cpu_target_opt}
-D__LIBDEVICE_CPU__)

set(asan_dg2_compile_opts_obj-new-offload -fsycl -c --offload-new-driver
set(sanitizer_dg2_compile_opts_obj-new-offload -fsycl -c --offload-new-driver
-foffload-lto=thin
${sanitizer_generic_compile_opts}
${sycl_dg2_target_opt}
Expand Down Expand Up @@ -373,16 +373,16 @@ else()
-I${CMAKE_CURRENT_SOURCE_DIR})

# asan aot
set(asan_filetypes obj obj-new-offload bc)
set(sanitizer_filetypes obj obj-new-offload bc)
set(asan_devicetypes pvc cpu dg2)

foreach(asan_ft IN LISTS asan_filetypes)
foreach(asan_ft IN LISTS sanitizer_filetypes)
foreach(asan_device IN LISTS asan_devicetypes)
compile_lib_ext(libsycl-asan-${asan_device}
SRC sanitizer/asan_rtl.cpp
FILETYPE ${asan_ft}
DEPENDENCIES ${asan_obj_deps}
OPTS ${asan_${asan_device}_compile_opts_${asan_ft}})
OPTS ${sanitizer_${asan_device}_compile_opts_${asan_ft}})
endforeach()
endforeach()

Expand All @@ -393,6 +393,19 @@ else()
EXTRA_OPTS -fno-sycl-instrument-device-code
-I${UR_SANITIZER_INCLUDE_DIR}
-I${CMAKE_CURRENT_SOURCE_DIR})

set(msan_devicetypes pvc cpu)

foreach(msan_ft IN LISTS sanitizer_filetypes)
foreach(msan_device IN LISTS msan_devicetypes)
compile_lib_ext(libsycl-msan-${msan_device}
SRC sanitizer/msan_rtl.cpp
FILETYPE ${msan_ft}
DEPENDENCIES ${msan_obj_deps}
OPTS ${sanitizer_${msan_device}_compile_opts_${msan_ft}})
endforeach()
endforeach()

endif()
endif()

Expand Down
6 changes: 6 additions & 0 deletions libdevice/sanitizer/msan_rtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ DEVICE_EXTERN_C_NOINLINE uptr __msan_get_shadow(uptr addr, uint32_t as) {
MSAN_DEBUG(__spirv_ocl_printf(__msan_print_launchinfo, (void *)launch_info,
launch_info->GlobalShadowOffset));

#if defined(__LIBDEVICE_PVC__)
shadow_ptr = __msan_get_shadow_pvc(addr, as);
#elif defined(__LIBDEVICE_CPU__)
shadow_ptr = __msan_get_shadow_cpu(addr);
#else
if (LIKELY(launch_info->DeviceTy == DeviceType::CPU)) {
shadow_ptr = __msan_get_shadow_cpu(addr);
} else if (launch_info->DeviceTy == DeviceType::GPU_PVC) {
Expand All @@ -209,6 +214,7 @@ DEVICE_EXTERN_C_NOINLINE uptr __msan_get_shadow(uptr addr, uint32_t as) {
MSAN_DEBUG(__spirv_ocl_printf(__msan_print_unsupport_device_type,
launch_info->DeviceTy));
}
#endif

MSAN_DEBUG(__spirv_ocl_printf(__msan_print_shadow, (void *)addr, as,
(void *)shadow_ptr, *(u8 *)shadow_ptr));
Expand Down