From 75d3dd1f9e393efc98f8eedf322ba7501d5c12c7 Mon Sep 17 00:00:00 2001 From: jinge90 Date: Tue, 21 Jan 2025 15:41:00 +0800 Subject: [PATCH 1/2] [SYCL] Add AOT support for device memory sanitizer --- clang/lib/Driver/ToolChains/SYCL.cpp | 10 ++++-- clang/test/Driver/sycl-device-lib.cpp | 13 ++++++++ libdevice/cmake/modules/SYCLLibdevice.cmake | 37 ++++++++++++++------- libdevice/sanitizer/msan_rtl.cpp | 6 ++++ 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/clang/lib/Driver/ToolChains/SYCL.cpp b/clang/lib/Driver/ToolChains/SYCL.cpp index adaf227b1abcb..570e23d71ce1d 100644 --- a/clang/lib/Driver/ToolChains/SYCL.cpp +++ b/clang/lib/Driver/ToolChains/SYCL.cpp @@ -555,7 +555,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 = { @@ -713,7 +719,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) diff --git a/clang/test/Driver/sycl-device-lib.cpp b/clang/test/Driver/sycl-device-lib.cpp index 9e07edf2287fa..a80cdfbd847d7 100644 --- a/clang/test/Driver/sycl-device-lib.cpp +++ b/clang/test/Driver/sycl-device-lib.cpp @@ -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 + diff --git a/libdevice/cmake/modules/SYCLLibdevice.cmake b/libdevice/cmake/modules/SYCLLibdevice.cmake index 64979f2b65e4a..daa9726a81dd3 100644 --- a/libdevice/cmake/modules/SYCLLibdevice.cmake +++ b/libdevice/cmake/modules/SYCLLibdevice.cmake @@ -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} @@ -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() @@ -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() diff --git a/libdevice/sanitizer/msan_rtl.cpp b/libdevice/sanitizer/msan_rtl.cpp index 8f3babd709038..4645cab519a8c 100644 --- a/libdevice/sanitizer/msan_rtl.cpp +++ b/libdevice/sanitizer/msan_rtl.cpp @@ -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) { @@ -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)); From 8bfc6363be9dcca6be6ffe6c15a5026cdee96d7d Mon Sep 17 00:00:00 2001 From: jinge90 Date: Mon, 27 Jan 2025 11:47:12 +0800 Subject: [PATCH 2/2] add lit test for old model Signed-off-by: jinge90 --- .../test/Driver/sycl-device-lib-old-model.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/clang/test/Driver/sycl-device-lib-old-model.cpp b/clang/test/Driver/sycl-device-lib-old-model.cpp index b4e3a3f9cf164..fd65dde48338a 100644 --- a/clang/test/Driver/sycl-device-lib-old-model.cpp +++ b/clang/test/Driver/sycl-device-lib-old-model.cpp @@ -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"