From 4207edeab3700352857c2509511b5aba83b78a8d Mon Sep 17 00:00:00 2001 From: "Zhao, Maosu" Date: Mon, 13 Jan 2025 23:27:55 -0800 Subject: [PATCH 1/3] [DevMSAN] Support device memory sanitizer for DG2 GPU device --- libdevice/sanitizer/msan_rtl.cpp | 23 ++++++++++++++++++++ sycl/cmake/modules/FetchUnifiedRuntime.cmake | 2 +- sycl/cmake/modules/UnifiedRuntimeTag.cmake | 2 +- sycl/test-e2e/MemorySanitizer/lit.local.cfg | 3 ++- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/libdevice/sanitizer/msan_rtl.cpp b/libdevice/sanitizer/msan_rtl.cpp index 8f3babd709038..333165d9d8a41 100644 --- a/libdevice/sanitizer/msan_rtl.cpp +++ b/libdevice/sanitizer/msan_rtl.cpp @@ -136,6 +136,27 @@ inline uptr __msan_get_shadow_cpu(uptr addr) { return addr ^ 0x500000000000ULL; } +inline uptr __msan_get_shadow_dg2(uptr addr, uint32_t as) { + if (as == ADDRESS_SPACE_GENERIC) { + ConvertGenericPointer(addr, as); + } + + if (as != ADDRESS_SPACE_GLOBAL || !(addr & 0xffff'0000'0000'0000ULL)) + return (uptr)((__SYCL_GLOBAL__ MsanLaunchInfo *)__MsanLaunchInfo.get()) + ->CleanShadow; + + // Device USM only + auto shadow_begin = ((__SYCL_GLOBAL__ MsanLaunchInfo *)__MsanLaunchInfo.get()) + ->GlobalShadowOffset; + auto shadow_end = ((__SYCL_GLOBAL__ MsanLaunchInfo *)__MsanLaunchInfo.get()) + ->GlobalShadowOffsetEnd; + if (addr < shadow_begin) { + return addr + (shadow_begin - 0xffff'8000'0000'0000ULL); + } else { + return addr - (0xffff'ffff'ffff'ffffULL - shadow_end); + } +} + inline uptr __msan_get_shadow_pvc(uptr addr, uint32_t as) { if (as == ADDRESS_SPACE_GENERIC) { ConvertGenericPointer(addr, as); @@ -205,6 +226,8 @@ DEVICE_EXTERN_C_NOINLINE uptr __msan_get_shadow(uptr addr, uint32_t as) { shadow_ptr = __msan_get_shadow_cpu(addr); } else if (launch_info->DeviceTy == DeviceType::GPU_PVC) { shadow_ptr = __msan_get_shadow_pvc(addr, as); + } else if (launch_info->DeviceTy == DeviceType::GPU_DG2) { + shadow_ptr = __msan_get_shadow_dg2(addr, as); } else { MSAN_DEBUG(__spirv_ocl_printf(__msan_print_unsupport_device_type, launch_info->DeviceTy)); diff --git a/sycl/cmake/modules/FetchUnifiedRuntime.cmake b/sycl/cmake/modules/FetchUnifiedRuntime.cmake index 72841724fa01d..d6a0251d63c86 100644 --- a/sycl/cmake/modules/FetchUnifiedRuntime.cmake +++ b/sycl/cmake/modules/FetchUnifiedRuntime.cmake @@ -116,7 +116,7 @@ if(SYCL_UR_USE_FETCH_CONTENT) CACHE PATH "Path to external '${name}' adapter source dir" FORCE) endfunction() - set(UNIFIED_RUNTIME_REPO "https://github.com/oneapi-src/unified-runtime.git") + set(UNIFIED_RUNTIME_REPO "https://github.com/zhaomaosu/unified-runtime.git") include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/UnifiedRuntimeTag.cmake) set(UMF_BUILD_EXAMPLES OFF CACHE INTERNAL "EXAMPLES") diff --git a/sycl/cmake/modules/UnifiedRuntimeTag.cmake b/sycl/cmake/modules/UnifiedRuntimeTag.cmake index de4a5aa14251b..21a89ff2be285 100644 --- a/sycl/cmake/modules/UnifiedRuntimeTag.cmake +++ b/sycl/cmake/modules/UnifiedRuntimeTag.cmake @@ -5,4 +5,4 @@ # Merge pull request #2048 from RossBrunton/ross/refc # # Use reference counting on factories -set(UNIFIED_RUNTIME_TAG 7eae5c80a9e969bc12fda57c9cc0a0970f0cd17f) +set(UNIFIED_RUNTIME_TAG msan-support-dg2) diff --git a/sycl/test-e2e/MemorySanitizer/lit.local.cfg b/sycl/test-e2e/MemorySanitizer/lit.local.cfg index ae875daca989c..6b1c0ef7c2a3b 100644 --- a/sycl/test-e2e/MemorySanitizer/lit.local.cfg +++ b/sycl/test-e2e/MemorySanitizer/lit.local.cfg @@ -1,6 +1,7 @@ # TRACKER: https://github.com/intel/llvm/issues/16184 has_arch_gpu_intel_pvc = any('arch-intel_gpu_pvc' in T for T in config.sycl_dev_features.values()) -if not has_arch_gpu_intel_pvc: +has_arch_gpu_intel_dg2 = any('gpu-intel-dg2' in T for T in config.sycl_dev_features.values()) +if not has_arch_gpu_intel_pvc and not has_arch_gpu_intel_dg2: config.unsupported_features += ['gpu'] else: # TRACKER for PVC + igc-dev: https://github.com/intel/llvm/issues/16401 From c34fc24998e1913e3e923b46bb4c9f9799c8b635 Mon Sep 17 00:00:00 2001 From: "Zhao, Maosu" Date: Mon, 13 Jan 2025 23:35:45 -0800 Subject: [PATCH 2/3] Remove unnecessary macros --- libdevice/include/sanitizer_defs.hpp | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/libdevice/include/sanitizer_defs.hpp b/libdevice/include/sanitizer_defs.hpp index dd5d0d94604be..16f23a52ae5a8 100644 --- a/libdevice/include/sanitizer_defs.hpp +++ b/libdevice/include/sanitizer_defs.hpp @@ -34,21 +34,10 @@ enum ADDRESS_SPACE : uint32_t { #if defined(__SPIR__) || defined(__SPIRV__) -#if defined(__SYCL_DEVICE_ONLY__) - -#define __USE_SPIR_BUILTIN__ 1 - #ifndef SYCL_EXTERNAL #define SYCL_EXTERNAL #endif // SYCL_EXTERNAL -#else // __SYCL_DEVICE_ONLY__ - -#define __USE_SPIR_BUILTIN__ 0 - -#endif // __SYCL_DEVICE_ONLY__ - -#if __USE_SPIR_BUILTIN__ extern SYCL_EXTERNAL int __spirv_ocl_printf(const __SYCL_CONSTANT__ char *Format, ...); @@ -63,7 +52,6 @@ extern SYCL_EXTERNAL __attribute__((convergent)) void __spirv_ControlBarrier(uint32_t Execution, uint32_t Memory, uint32_t Semantics); extern "C" SYCL_EXTERNAL void __devicelib_exit(); -#endif // __USE_SPIR_BUILTIN__ __SYCL_GLOBAL__ void *ToGlobal(void *ptr) { return __spirv_GenericCastToPtrExplicit_ToGlobal(ptr, 5); From ceaa1f02312bf013aad5bffddf790b215e30ff04 Mon Sep 17 00:00:00 2001 From: "Zhao, Maosu" Date: Tue, 18 Feb 2025 19:21:10 -0800 Subject: [PATCH 3/3] Merge UR part --- .../source/loader/layers/sanitizer/msan/msan_shadow.cpp | 2 +- .../source/loader/layers/sanitizer/msan/msan_shadow.hpp | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/unified-runtime/source/loader/layers/sanitizer/msan/msan_shadow.cpp b/unified-runtime/source/loader/layers/sanitizer/msan/msan_shadow.cpp index 8b210a2717ddb..d4446d4dcf06b 100644 --- a/unified-runtime/source/loader/layers/sanitizer/msan/msan_shadow.cpp +++ b/unified-runtime/source/loader/layers/sanitizer/msan/msan_shadow.cpp @@ -141,7 +141,7 @@ ur_result_t MsanShadowMemoryGPU::Setup() { // To reserve very large amount of GPU virtual memroy, the pStart param // should be beyond the SVM range, so that GFX driver will automatically // switch to reservation on the GPU heap. - const void *StartAddress = (void *)(0x100'0000'0000'0000ULL); + const void *StartAddress = (void *)GetStartAddress(); // TODO: Protect Bad Zone auto Result = getContext()->urDdiTable.VirtualMem.pfnReserve( Context, StartAddress, ShadowSize, (void **)&ShadowBegin); diff --git a/unified-runtime/source/loader/layers/sanitizer/msan/msan_shadow.hpp b/unified-runtime/source/loader/layers/sanitizer/msan/msan_shadow.hpp index 51847e490786b..29b4a6d96ac18 100644 --- a/unified-runtime/source/loader/layers/sanitizer/msan/msan_shadow.hpp +++ b/unified-runtime/source/loader/layers/sanitizer/msan/msan_shadow.hpp @@ -103,6 +103,8 @@ struct MsanShadowMemoryGPU : public MsanShadowMemory { virtual size_t GetShadowSize() = 0; + virtual uptr GetStartAddress() { return 0; } + private: ur_result_t EnqueueMapShadow(ur_queue_handle_t Queue, uptr Ptr, uptr Size, std::vector &EventWaitList, @@ -134,6 +136,8 @@ struct MsanShadowMemoryPVC final : public MsanShadowMemoryGPU { uptr MemToShadow(uptr Ptr) override; size_t GetShadowSize() override { return 0x8000'0000'0000ULL; } + + uptr GetStartAddress() override { return 0x100'0000'0000'0000ULL; } }; /// Shadow Memory layout of GPU DG2 device