Skip to content

Commit 3b4b4b8

Browse files
authored
Partially revert "[DevSAN] Change DeviceType to specialization constant (#19798)" (#20091)
This reverts commit 91a91c3 util L0 support to check if specialization constants exist.
1 parent 3e2e025 commit 3b4b4b8

File tree

12 files changed

+40
-109
lines changed

12 files changed

+40
-109
lines changed

libdevice/include/sanitizer_defs.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ extern SYCL_EXTERNAL __attribute__((convergent)) void
6161
__spirv_ControlBarrier(int32_t Execution, int32_t Memory,
6262
int32_t Semantics) noexcept;
6363

64-
template <typename T>
65-
extern SYCL_EXTERNAL T __spirv_SpecConstant(int ID, T default_value) noexcept;
66-
6764
extern "C" SYCL_EXTERNAL void __devicelib_exit();
6865

6966
#endif // __SPIR__ || __SPIRV__

libdevice/include/sanitizer_utils.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//===----------------------------------------------------------------------===//
88
#pragma once
99

10-
#include "sanitizer_common/sanitizer_libdevice.hpp"
1110
#include "sanitizer_defs.hpp"
11+
#include "spirv_vars.h"
1212

1313
#if defined(__SPIR__) || defined(__SPIRV__)
1414

@@ -49,11 +49,6 @@ inline __SYCL_PRIVATE__ void *ToPrivate(void *ptr) {
4949
return __spirv_GenericCastToPtrExplicit_ToPrivate(ptr, 7);
5050
}
5151

52-
inline DeviceType GetDeviceTy() {
53-
return static_cast<DeviceType>(
54-
__spirv_SpecConstant(SPEC_CONSTANT_DEVICE_TYPE_ID, 0));
55-
}
56-
5752
template <typename T> T Memset(T ptr, int value, size_t size) {
5853
for (size_t i = 0; i < size; i++) {
5954
ptr[i] = value;

libdevice/sanitizer/asan_rtl.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,16 @@ inline uptr MemToShadow(uptr addr, uint32_t as,
264264
#elif defined(__LIBDEVICE_DG2__)
265265
shadow_ptr = MemToShadow_DG2(addr, as, debug);
266266
#else
267-
if (GetDeviceTy() == DeviceType::CPU) {
267+
auto launch_info = (__SYCL_GLOBAL__ const AsanRuntimeData *)__AsanLaunchInfo;
268+
if (launch_info->DeviceTy == DeviceType::CPU) {
268269
shadow_ptr = MemToShadow_CPU(addr);
269-
} else if (GetDeviceTy() == DeviceType::GPU_PVC) {
270+
} else if (launch_info->DeviceTy == DeviceType::GPU_PVC) {
270271
shadow_ptr = MemToShadow_PVC(addr, as, debug);
271-
} else if (GetDeviceTy() == DeviceType::GPU_DG2) {
272+
} else if (launch_info->DeviceTy == DeviceType::GPU_DG2) {
272273
shadow_ptr = MemToShadow_DG2(addr, as, debug);
273274
} else {
274275
ASAN_DEBUG(__spirv_ocl_printf(__asan_print_unsupport_device_type,
275-
(int)GetDeviceTy()));
276+
(int)launch_info->DeviceTy));
276277
ReportUnknownDevice(debug);
277278
return 0;
278279
}
@@ -888,7 +889,8 @@ DEVICE_EXTERN_C_NOINLINE void __asan_set_shadow_private(uptr shadow, uptr size,
888889
static __SYCL_CONSTANT__ const char __asan_print_private_base[] =
889890
"[kernel] set_private_base: %llu -> %p\n";
890891

891-
inline void SetPrivateBaseImpl(__SYCL_PRIVATE__ void *ptr) {
892+
DEVICE_EXTERN_C_NOINLINE void
893+
__asan_set_private_base(__SYCL_PRIVATE__ void *ptr) {
892894
auto launch_info = (__SYCL_GLOBAL__ const AsanRuntimeData *)__AsanLaunchInfo;
893895
const size_t sid = SubGroupLinearId();
894896
if (!launch_info || sid >= ASAN_MAX_SG_PRIVATE ||
@@ -902,17 +904,4 @@ inline void SetPrivateBaseImpl(__SYCL_PRIVATE__ void *ptr) {
902904
SubGroupBarrier();
903905
}
904906

905-
DEVICE_EXTERN_C_NOINLINE void
906-
__asan_set_private_base(__SYCL_PRIVATE__ void *ptr) {
907-
#if defined(__LIBDEVICE_CPU__)
908-
return;
909-
#elif defined(__LIBDEVICE_DG2__) || defined(__LIBDEVICE_PVC__)
910-
SetPrivateBaseImpl(ptr);
911-
#else
912-
if (GetDeviceTy() == DeviceType::CPU)
913-
return;
914-
SetPrivateBaseImpl(ptr);
915-
#endif
916-
}
917-
918907
#endif // __SPIR__ || __SPIRV__

libdevice/sanitizer/msan_rtl.cpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,16 @@ inline uptr MemToShadow(uptr addr, uint32_t as) {
216216
#elif defined(__LIBDEVICE_CPU__)
217217
shadow_ptr = MemToShadow_CPU(addr);
218218
#else
219-
if (GetDeviceTy() == DeviceType::CPU) {
219+
if (LIKELY(GetMsanLaunchInfo->DeviceTy == DeviceType::CPU)) {
220220
shadow_ptr = MemToShadow_CPU(addr);
221-
} else if (GetDeviceTy() == DeviceType::GPU_PVC) {
221+
} else if (GetMsanLaunchInfo->DeviceTy == DeviceType::GPU_PVC) {
222222
shadow_ptr = MemToShadow_PVC(addr, as);
223-
} else if (GetDeviceTy() == DeviceType::GPU_DG2) {
223+
} else if (GetMsanLaunchInfo->DeviceTy == DeviceType::GPU_DG2) {
224224
shadow_ptr = MemToShadow_DG2(addr, as);
225225
} else {
226226
shadow_ptr = GetMsanLaunchInfo->CleanShadow;
227-
MSAN_DEBUG(
228-
__spirv_ocl_printf(__msan_print_unsupport_device_type, GetDeviceTy()));
227+
MSAN_DEBUG(__spirv_ocl_printf(__msan_print_unsupport_device_type,
228+
GetMsanLaunchInfo->DeviceTy));
229229
}
230230
#endif
231231

@@ -273,17 +273,17 @@ inline uptr MemToOrigin(uptr addr, uint32_t as) {
273273
#elif defined(__LIBDEVICE_CPU__)
274274
origin_ptr = MemToOrigin_CPU(addr);
275275
#else
276-
if (GetDeviceTy() == DeviceType::CPU) {
276+
if (LIKELY(GetMsanLaunchInfo->DeviceTy == DeviceType::CPU)) {
277277
origin_ptr = MemToOrigin_CPU(aligned_addr);
278-
} else if (GetDeviceTy() == DeviceType::GPU_PVC) {
278+
} else if (GetMsanLaunchInfo->DeviceTy == DeviceType::GPU_PVC) {
279279
origin_ptr = MemToOrigin_PVC(aligned_addr, as);
280-
} else if (GetDeviceTy() == DeviceType::GPU_DG2) {
280+
} else if (GetMsanLaunchInfo->DeviceTy == DeviceType::GPU_DG2) {
281281
origin_ptr = MemToOrigin_DG2(aligned_addr, as);
282282
} else {
283283
// Return clean shadow (0s) by default
284284
origin_ptr = GetMsanLaunchInfo->CleanShadow;
285-
MSAN_DEBUG(
286-
__spirv_ocl_printf(__msan_print_unsupport_device_type, GetDeviceTy()));
285+
MSAN_DEBUG(__spirv_ocl_printf(__msan_print_unsupport_device_type,
286+
GetMsanLaunchInfo->DeviceTy));
287287
}
288288
#endif
289289

@@ -743,7 +743,8 @@ DEVICE_EXTERN_C_NOINLINE void __msan_unpoison_shadow(uptr ptr, uint32_t as,
743743
static __SYCL_CONSTANT__ const char __msan_print_private_base[] =
744744
"[kernel] __msan_set_private_base(sid=%llu): %p\n";
745745

746-
inline void SetPrivateBaseImpl(__SYCL_PRIVATE__ void *ptr) {
746+
DEVICE_EXTERN_C_NOINLINE void
747+
__msan_set_private_base(__SYCL_PRIVATE__ void *ptr) {
747748
const size_t sid = SubGroupLinearId();
748749
if (!GetMsanLaunchInfo || sid >= MSAN_MAX_SG_PRIVATE ||
749750
GetMsanLaunchInfo->PrivateShadowOffset == 0 ||
@@ -757,19 +758,6 @@ inline void SetPrivateBaseImpl(__SYCL_PRIVATE__ void *ptr) {
757758
SubGroupBarrier();
758759
}
759760

760-
DEVICE_EXTERN_C_NOINLINE void
761-
__msan_set_private_base(__SYCL_PRIVATE__ void *ptr) {
762-
#if defined(__LIBDEVICE_CPU__)
763-
return;
764-
#elif defined(__LIBDEVICE_DG2__) || defined(__LIBDEVICE_PVC__)
765-
SetPrivateBaseImpl(ptr);
766-
#else
767-
if (GetDeviceTy() == DeviceType::CPU)
768-
return;
769-
SetPrivateBaseImpl(ptr);
770-
#endif
771-
}
772-
773761
static __SYCL_CONSTANT__ const char __msan_print_strided_copy_unsupport_type[] =
774762
"[kernel] __msan_unpoison_strided_copy: unsupported type(%d)\n";
775763

libdevice/sanitizer/tsan_rtl.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ inline __SYCL_GLOBAL__ RawShadow *MemToShadow(uptr addr, uint32_t as) {
127127
#elif defined(__LIBDEVICE_PVC__)
128128
shadow_ptr = MemToShadow_PVC(addr, as);
129129
#else
130-
if (GetDeviceTy() == DeviceType::CPU) {
130+
if (TsanLaunchInfo->DeviceTy == DeviceType::CPU) {
131131
shadow_ptr = MemToShadow_CPU(addr, as);
132-
} else if (GetDeviceTy() == DeviceType::GPU_PVC) {
132+
} else if (TsanLaunchInfo->DeviceTy == DeviceType::GPU_PVC) {
133133
shadow_ptr = MemToShadow_PVC(addr, as);
134134
} else {
135135
TSAN_DEBUG(__spirv_ocl_printf(__tsan_print_unsupport_device_type,
136-
(int)GetDeviceTy()));
136+
(int)TsanLaunchInfo->DeviceTy));
137137
return nullptr;
138138
}
139139
#endif
@@ -186,16 +186,10 @@ inline void DoReportRace(__SYCL_GLOBAL__ RawShadow *s, AccessType type,
186186
return;
187187
}
188188

189-
#if defined(__LIBDEVICE_CPU__)
190-
#elif defined(__LIBDEVICE_DG2__) || defined(__LIBDEVICE_PVC__)
191-
if (as == ADDRESS_SPACE_GENERIC) {
189+
if (as == ADDRESS_SPACE_GENERIC &&
190+
TsanLaunchInfo->DeviceTy != DeviceType::CPU) {
192191
ConvertGenericPointer(addr, as);
193192
}
194-
#else
195-
if (as == ADDRESS_SPACE_GENERIC && GetDeviceTy() != DeviceType::CPU) {
196-
ConvertGenericPointer(addr, as);
197-
}
198-
#endif
199193

200194
// Check if current address already being recorded before.
201195
for (uint32_t i = 0; i < TsanLaunchInfo->RecordedReportCount; i++) {
@@ -473,7 +467,7 @@ DEVICE_EXTERN_C_NOINLINE void __tsan_cleanup_private(uptr addr, size_t size) {
473467
#elif defined(__LIBDEVICE_PVC__)
474468
return;
475469
#else
476-
if (GetDeviceTy() != DeviceType::CPU)
470+
if (TsanLaunchInfo->DeviceTy != DeviceType::CPU)
477471
return;
478472

479473
__tsan_cleanup_private_cpu_impl(addr, size);

unified-runtime/source/loader/layers/sanitizer/asan/asan_interceptor.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -643,19 +643,6 @@ ur_result_t AsanInterceptor::insertProgram(ur_program_handle_t Program) {
643643
if (m_ProgramMap.find(Program) != m_ProgramMap.end()) {
644644
return UR_RESULT_SUCCESS;
645645
}
646-
auto CI = getContextInfo(GetContext(Program));
647-
auto DI = getDeviceInfo(CI->DeviceList[0]);
648-
ur_specialization_constant_info_t SpecConstantInfo{
649-
SPEC_CONSTANT_DEVICE_TYPE_ID, sizeof(DeviceType), &DI->Type};
650-
ur_result_t URes =
651-
getContext()->urDdiTable.Program.pfnSetSpecializationConstants(
652-
Program, 1, &SpecConstantInfo);
653-
if (URes != UR_RESULT_SUCCESS) {
654-
UR_LOG_L(getContext()->logger, DEBUG,
655-
"Set specilization constant for device type failed: {}, the "
656-
"program may not be sanitized or is created from binary.",
657-
URes);
658-
}
659646
m_ProgramMap.emplace(Program, std::make_shared<ProgramInfo>(Program));
660647
return UR_RESULT_SUCCESS;
661648
}
@@ -825,6 +812,7 @@ ur_result_t AsanInterceptor::prepareLaunch(
825812
// Prepare asan runtime data
826813
LaunchInfo.Data.Host.GlobalShadowOffset = DeviceInfo->Shadow->ShadowBegin;
827814
LaunchInfo.Data.Host.GlobalShadowOffsetEnd = DeviceInfo->Shadow->ShadowEnd;
815+
LaunchInfo.Data.Host.DeviceTy = DeviceInfo->Type;
828816
LaunchInfo.Data.Host.Debug = getContext()->Options.Debug ? 1 : 0;
829817

830818
// Write shadow memory offset for local memory
@@ -886,14 +874,16 @@ ur_result_t AsanInterceptor::prepareLaunch(
886874

887875
UR_LOG_L(getContext()->logger, INFO,
888876
"LaunchInfo {} (GlobalShadow={}, LocalShadow={}, PrivateBase={}, "
889-
"PrivateShadow={}, LocalArgs={}, NumLocalArgs={}, Debug={})",
877+
"PrivateShadow={}, LocalArgs={}, NumLocalArgs={}, "
878+
"Device={}, Debug={})",
890879
(void *)LaunchInfo.Data.getDevicePtr(),
891880
(void *)LaunchInfo.Data.Host.GlobalShadowOffset,
892881
(void *)LaunchInfo.Data.Host.LocalShadowOffset,
893882
(void *)LaunchInfo.Data.Host.PrivateBase,
894883
(void *)LaunchInfo.Data.Host.PrivateShadowOffset,
895884
(void *)LaunchInfo.Data.Host.LocalArgs,
896-
LaunchInfo.Data.Host.NumLocalArgs, LaunchInfo.Data.Host.Debug);
885+
LaunchInfo.Data.Host.NumLocalArgs,
886+
ToString(LaunchInfo.Data.Host.DeviceTy), LaunchInfo.Data.Host.Debug);
897887

898888
return UR_RESULT_SUCCESS;
899889
}

unified-runtime/source/loader/layers/sanitizer/asan/asan_libdevice.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct AsanRuntimeData {
6969
LocalArgsInfo *LocalArgs = nullptr; // Ordered by ArgIndex
7070
uint32_t NumLocalArgs = 0;
7171

72+
DeviceType DeviceTy = DeviceType::UNKNOWN;
7273
uint32_t Debug = 0;
7374

7475
int ReportFlag = 0;

unified-runtime/source/loader/layers/sanitizer/msan/msan_interceptor.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -384,19 +384,6 @@ ur_result_t MsanInterceptor::insertProgram(ur_program_handle_t Program) {
384384
if (m_ProgramMap.find(Program) != m_ProgramMap.end()) {
385385
return UR_RESULT_SUCCESS;
386386
}
387-
auto CI = getContextInfo(GetContext(Program));
388-
auto DI = getDeviceInfo(CI->DeviceList[0]);
389-
ur_specialization_constant_info_t SpecConstantInfo{
390-
SPEC_CONSTANT_DEVICE_TYPE_ID, sizeof(DeviceType), &DI->Type};
391-
ur_result_t URes =
392-
getContext()->urDdiTable.Program.pfnSetSpecializationConstants(
393-
Program, 1, &SpecConstantInfo);
394-
if (URes != UR_RESULT_SUCCESS) {
395-
UR_LOG_L(getContext()->logger, DEBUG,
396-
"Set specilization constant for device type failed: {}, the "
397-
"program may not be sanitized or is created from binary.",
398-
URes);
399-
}
400387
m_ProgramMap.emplace(Program, std::make_shared<ProgramInfo>(Program));
401388
return UR_RESULT_SUCCESS;
402389
}
@@ -505,6 +492,7 @@ ur_result_t MsanInterceptor::prepareLaunch(
505492
LaunchInfo.Data.Host.GlobalShadowOffset = DeviceInfo->Shadow->ShadowBegin;
506493
LaunchInfo.Data.Host.GlobalShadowOffsetEnd = DeviceInfo->Shadow->ShadowEnd;
507494

495+
LaunchInfo.Data.Host.DeviceTy = DeviceInfo->Type;
508496
LaunchInfo.Data.Host.Debug = getContext()->Options.Debug ? 1 : 0;
509497
LaunchInfo.Data.Host.IsRecover = getContext()->Options.Recover ? 1 : 0;
510498

@@ -611,15 +599,16 @@ ur_result_t MsanInterceptor::prepareLaunch(
611599
UR_LOG_L(getContext()->logger, INFO,
612600
"LaunchInfo {} (GlobalShadow={}, LocalShadow={}, PrivateBase={}, "
613601
"PrivateShadow={}, CleanShadow={}, LocalArgs={}, NumLocalArgs={}, "
614-
"Debug={})",
602+
"Device={}, Debug={})",
615603
(void *)LaunchInfo.Data.getDevicePtr(),
616604
(void *)LaunchInfo.Data.Host.GlobalShadowOffset,
617605
(void *)LaunchInfo.Data.Host.LocalShadowOffset,
618606
(void *)LaunchInfo.Data.Host.PrivateBase,
619607
(void *)LaunchInfo.Data.Host.PrivateShadowOffset,
620608
(void *)LaunchInfo.Data.Host.CleanShadow,
621609
(void *)LaunchInfo.Data.Host.LocalArgs,
622-
LaunchInfo.Data.Host.NumLocalArgs, LaunchInfo.Data.Host.Debug);
610+
LaunchInfo.Data.Host.NumLocalArgs,
611+
ToString(LaunchInfo.Data.Host.DeviceTy), LaunchInfo.Data.Host.Debug);
623612

624613
ur_result_t URes =
625614
getContext()->urDdiTable.Enqueue.pfnDeviceGlobalVariableWrite(

unified-runtime/source/loader/layers/sanitizer/msan/msan_libdevice.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct MsanRuntimeData {
6464

6565
uintptr_t CleanShadow = 0;
6666

67+
DeviceType DeviceTy = DeviceType::UNKNOWN;
6768
uint32_t Debug = 0;
6869
uint32_t IsRecover = 0;
6970

unified-runtime/source/loader/layers/sanitizer/sanitizer_common/sanitizer_libdevice.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ namespace ur_sanitizer_layer {
2121

2222
enum class DeviceType : uint32_t { UNKNOWN = 0, CPU, GPU_PVC, GPU_DG2 };
2323

24-
// Try to use a larger ID number to avoid conflict with user ID.
25-
#define SPEC_CONSTANT_DEVICE_TYPE_ID (INT32_MAX - 1)
26-
2724
inline const char *ToString(DeviceType Type) {
2825
switch (Type) {
2926
case DeviceType::UNKNOWN:

0 commit comments

Comments
 (0)