Skip to content

Commit 91a91c3

Browse files
authored
[DevSAN] Change DeviceType to specialization constant (#19798)
This will allow backend compiler to eliminte unnecessary code to reduce kernel code size.
1 parent 403ee01 commit 91a91c3

File tree

13 files changed

+115
-40
lines changed

13 files changed

+115
-40
lines changed

devops/compat_ci_exclude.sycl-rel-6_3

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ NonUniformGroups/opportunistic_group.cpp
2222
# toolchain used to build the test, not the toolchain we're using SYCL RT from
2323
# to run against pre-built E2E binaries.
2424
Basic/device_config_file_consistency.cpp
25+
26+
# The ABI compatibility is unimportant to device sanitizer since the user must
27+
# re-build their code if they want to test their code with device sanitizer.
28+
AddressSanitizer/*
29+
MemorySanitizer/*
30+
ThreadSanitizer/*

libdevice/include/sanitizer_defs.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ 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+
6467
extern "C" SYCL_EXTERNAL void __devicelib_exit();
6568

6669
#endif // __SPIR__ || __SPIRV__

libdevice/include/sanitizer_utils.hpp

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

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

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

@@ -49,6 +49,11 @@ 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+
5257
template <typename T> T Memset(T ptr, int value, size_t size) {
5358
for (size_t i = 0; i < size; i++) {
5459
ptr[i] = value;

libdevice/sanitizer/asan_rtl.cpp

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

892-
DEVICE_EXTERN_C_NOINLINE void
893-
__asan_set_private_base(__SYCL_PRIVATE__ void *ptr) {
891+
inline void SetPrivateBaseImpl(__SYCL_PRIVATE__ void *ptr) {
894892
auto launch_info = (__SYCL_GLOBAL__ const AsanRuntimeData *)__AsanLaunchInfo;
895893
const size_t sid = SubGroupLinearId();
896894
if (!launch_info || sid >= ASAN_MAX_SG_PRIVATE ||
@@ -904,4 +902,17 @@ __asan_set_private_base(__SYCL_PRIVATE__ void *ptr) {
904902
SubGroupBarrier();
905903
}
906904

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+
907918
#endif // __SPIR__ || __SPIRV__

libdevice/sanitizer/msan_rtl.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,16 @@ inline uptr MemToShadow(uptr addr, uint32_t as) {
212212
#elif defined(__LIBDEVICE_CPU__)
213213
shadow_ptr = MemToShadow_CPU(addr);
214214
#else
215-
if (LIKELY(GetMsanLaunchInfo->DeviceTy == DeviceType::CPU)) {
215+
if (GetDeviceTy() == DeviceType::CPU) {
216216
shadow_ptr = MemToShadow_CPU(addr);
217-
} else if (GetMsanLaunchInfo->DeviceTy == DeviceType::GPU_PVC) {
217+
} else if (GetDeviceTy() == DeviceType::GPU_PVC) {
218218
shadow_ptr = MemToShadow_PVC(addr, as);
219-
} else if (GetMsanLaunchInfo->DeviceTy == DeviceType::GPU_DG2) {
219+
} else if (GetDeviceTy() == DeviceType::GPU_DG2) {
220220
shadow_ptr = MemToShadow_DG2(addr, as);
221221
} else {
222222
shadow_ptr = GetMsanLaunchInfo->CleanShadow;
223-
MSAN_DEBUG(__spirv_ocl_printf(__msan_print_unsupport_device_type,
224-
GetMsanLaunchInfo->DeviceTy));
223+
MSAN_DEBUG(
224+
__spirv_ocl_printf(__msan_print_unsupport_device_type, GetDeviceTy()));
225225
}
226226
#endif
227227

@@ -269,17 +269,17 @@ inline uptr MemToOrigin(uptr addr, uint32_t as) {
269269
#elif defined(__LIBDEVICE_CPU__)
270270
origin_ptr = MemToOrigin_CPU(addr);
271271
#else
272-
if (LIKELY(GetMsanLaunchInfo->DeviceTy == DeviceType::CPU)) {
272+
if (GetDeviceTy() == DeviceType::CPU) {
273273
origin_ptr = MemToOrigin_CPU(aligned_addr);
274-
} else if (GetMsanLaunchInfo->DeviceTy == DeviceType::GPU_PVC) {
274+
} else if (GetDeviceTy() == DeviceType::GPU_PVC) {
275275
origin_ptr = MemToOrigin_PVC(aligned_addr, as);
276-
} else if (GetMsanLaunchInfo->DeviceTy == DeviceType::GPU_DG2) {
276+
} else if (GetDeviceTy() == DeviceType::GPU_DG2) {
277277
origin_ptr = MemToOrigin_DG2(aligned_addr, as);
278278
} else {
279279
// Return clean shadow (0s) by default
280280
origin_ptr = GetMsanLaunchInfo->CleanShadow;
281-
MSAN_DEBUG(__spirv_ocl_printf(__msan_print_unsupport_device_type,
282-
GetMsanLaunchInfo->DeviceTy));
281+
MSAN_DEBUG(
282+
__spirv_ocl_printf(__msan_print_unsupport_device_type, GetDeviceTy()));
283283
}
284284
#endif
285285

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

740-
DEVICE_EXTERN_C_NOINLINE void
741-
__msan_set_private_base(__SYCL_PRIVATE__ void *ptr) {
740+
inline void SetPrivateBaseImpl(__SYCL_PRIVATE__ void *ptr) {
742741
const size_t sid = SubGroupLinearId();
743742
if (!GetMsanLaunchInfo || sid >= MSAN_MAX_SG_PRIVATE ||
744743
GetMsanLaunchInfo->PrivateShadowOffset == 0 ||
@@ -752,6 +751,19 @@ __msan_set_private_base(__SYCL_PRIVATE__ void *ptr) {
752751
SubGroupBarrier();
753752
}
754753

754+
DEVICE_EXTERN_C_NOINLINE void
755+
__msan_set_private_base(__SYCL_PRIVATE__ void *ptr) {
756+
#if defined(__LIBDEVICE_CPU__)
757+
return;
758+
#elif defined(__LIBDEVICE_DG2__) || defined(__LIBDEVICE_PVC__)
759+
SetPrivateBaseImpl(ptr);
760+
#else
761+
if (GetDeviceTy() == DeviceType::CPU)
762+
return;
763+
SetPrivateBaseImpl(ptr);
764+
#endif
765+
}
766+
755767
static __SYCL_CONSTANT__ const char __msan_print_strided_copy_unsupport_type[] =
756768
"[kernel] __msan_unpoison_strided_copy: unsupported type(%d)\n";
757769

libdevice/sanitizer/tsan_rtl.cpp

Lines changed: 12 additions & 6 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 (TsanLaunchInfo->DeviceTy == DeviceType::CPU) {
130+
if (GetDeviceTy() == DeviceType::CPU) {
131131
shadow_ptr = MemToShadow_CPU(addr, as);
132-
} else if (TsanLaunchInfo->DeviceTy == DeviceType::GPU_PVC) {
132+
} else if (GetDeviceTy() == 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)TsanLaunchInfo->DeviceTy));
136+
(int)GetDeviceTy()));
137137
return nullptr;
138138
}
139139
#endif
@@ -186,10 +186,16 @@ inline void DoReportRace(__SYCL_GLOBAL__ RawShadow *s, AccessType type,
186186
return;
187187
}
188188

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

194200
// Check if current address already being recorded before.
195201
for (uint32_t i = 0; i < TsanLaunchInfo->RecordedReportCount; i++) {
@@ -467,7 +473,7 @@ DEVICE_EXTERN_C_NOINLINE void __tsan_cleanup_private(uptr addr, size_t size) {
467473
#elif defined(__LIBDEVICE_PVC__)
468474
return;
469475
#else
470-
if (TsanLaunchInfo->DeviceTy != DeviceType::CPU)
476+
if (GetDeviceTy() != DeviceType::CPU)
471477
return;
472478

473479
__tsan_cleanup_private_cpu_impl(addr, size);

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,19 @@ 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+
}
646659
m_ProgramMap.emplace(Program, std::make_shared<ProgramInfo>(Program));
647660
return UR_RESULT_SUCCESS;
648661
}
@@ -812,7 +825,6 @@ ur_result_t AsanInterceptor::prepareLaunch(
812825
// Prepare asan runtime data
813826
LaunchInfo.Data.Host.GlobalShadowOffset = DeviceInfo->Shadow->ShadowBegin;
814827
LaunchInfo.Data.Host.GlobalShadowOffsetEnd = DeviceInfo->Shadow->ShadowEnd;
815-
LaunchInfo.Data.Host.DeviceTy = DeviceInfo->Type;
816828
LaunchInfo.Data.Host.Debug = getContext()->Options.Debug ? 1 : 0;
817829

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

875887
UR_LOG_L(getContext()->logger, INFO,
876888
"LaunchInfo {} (GlobalShadow={}, LocalShadow={}, PrivateBase={}, "
877-
"PrivateShadow={}, LocalArgs={}, NumLocalArgs={}, "
878-
"Device={}, Debug={})",
889+
"PrivateShadow={}, LocalArgs={}, NumLocalArgs={}, Debug={})",
879890
(void *)LaunchInfo.Data.getDevicePtr(),
880891
(void *)LaunchInfo.Data.Host.GlobalShadowOffset,
881892
(void *)LaunchInfo.Data.Host.LocalShadowOffset,
882893
(void *)LaunchInfo.Data.Host.PrivateBase,
883894
(void *)LaunchInfo.Data.Host.PrivateShadowOffset,
884895
(void *)LaunchInfo.Data.Host.LocalArgs,
885-
LaunchInfo.Data.Host.NumLocalArgs,
886-
ToString(LaunchInfo.Data.Host.DeviceTy), LaunchInfo.Data.Host.Debug);
896+
LaunchInfo.Data.Host.NumLocalArgs, LaunchInfo.Data.Host.Debug);
887897

888898
return UR_RESULT_SUCCESS;
889899
}

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

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

72-
DeviceType DeviceTy = DeviceType::UNKNOWN;
7372
uint32_t Debug = 0;
7473

7574
int ReportFlag = 0;

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,19 @@ 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+
}
387400
m_ProgramMap.emplace(Program, std::make_shared<ProgramInfo>(Program));
388401
return UR_RESULT_SUCCESS;
389402
}
@@ -492,7 +505,6 @@ ur_result_t MsanInterceptor::prepareLaunch(
492505
LaunchInfo.Data.Host.GlobalShadowOffset = DeviceInfo->Shadow->ShadowBegin;
493506
LaunchInfo.Data.Host.GlobalShadowOffsetEnd = DeviceInfo->Shadow->ShadowEnd;
494507

495-
LaunchInfo.Data.Host.DeviceTy = DeviceInfo->Type;
496508
LaunchInfo.Data.Host.Debug = getContext()->Options.Debug ? 1 : 0;
497509
LaunchInfo.Data.Host.IsRecover = getContext()->Options.Recover ? 1 : 0;
498510

@@ -599,16 +611,15 @@ ur_result_t MsanInterceptor::prepareLaunch(
599611
UR_LOG_L(getContext()->logger, INFO,
600612
"LaunchInfo {} (GlobalShadow={}, LocalShadow={}, PrivateBase={}, "
601613
"PrivateShadow={}, CleanShadow={}, LocalArgs={}, NumLocalArgs={}, "
602-
"Device={}, Debug={})",
614+
"Debug={})",
603615
(void *)LaunchInfo.Data.getDevicePtr(),
604616
(void *)LaunchInfo.Data.Host.GlobalShadowOffset,
605617
(void *)LaunchInfo.Data.Host.LocalShadowOffset,
606618
(void *)LaunchInfo.Data.Host.PrivateBase,
607619
(void *)LaunchInfo.Data.Host.PrivateShadowOffset,
608620
(void *)LaunchInfo.Data.Host.CleanShadow,
609621
(void *)LaunchInfo.Data.Host.LocalArgs,
610-
LaunchInfo.Data.Host.NumLocalArgs,
611-
ToString(LaunchInfo.Data.Host.DeviceTy), LaunchInfo.Data.Host.Debug);
622+
LaunchInfo.Data.Host.NumLocalArgs, LaunchInfo.Data.Host.Debug);
612623

613624
ur_result_t URes =
614625
getContext()->urDdiTable.Enqueue.pfnDeviceGlobalVariableWrite(

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

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

6565
uintptr_t CleanShadow = 0;
6666

67-
DeviceType DeviceTy = DeviceType::UNKNOWN;
6867
uint32_t Debug = 0;
6968
uint32_t IsRecover = 0;
7069

0 commit comments

Comments
 (0)