Skip to content

Commit 93046c5

Browse files
committed
check device type by device usm value
1 parent bb8700b commit 93046c5

File tree

5 files changed

+44
-33
lines changed

5 files changed

+44
-33
lines changed

source/loader/layers/sanitizer/asan_interceptor.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -605,12 +605,6 @@ SanitizerInterceptor::insertDevice(ur_device_handle_t Device,
605605

606606
DI = std::make_shared<ur_sanitizer_layer::DeviceInfo>(Device);
607607

608-
// Query device type
609-
DI->Type = GetDeviceType(Device);
610-
if (DI->Type == DeviceType::UNKNOWN) {
611-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
612-
}
613-
614608
// Query alignment
615609
UR_CALL(context.urDdiTable.Device.pfnGetInfo(
616610
Device, UR_DEVICE_INFO_MEM_BASE_ADDR_ALIGN, sizeof(DI->Alignment),
@@ -921,7 +915,7 @@ ur_result_t USMLaunchInfo::updateKernelInfo(const KernelInfo &KI) {
921915
USMLaunchInfo::~USMLaunchInfo() {
922916
[[maybe_unused]] ur_result_t Result;
923917
if (Data) {
924-
auto Type = GetDeviceType(Device);
918+
auto Type = GetDeviceType(Context, Device);
925919
if (Type == DeviceType::GPU_PVC || Type == DeviceType::GPU_DG2) {
926920
if (Data->PrivateShadowOffset) {
927921
Result = context.urDdiTable.USM.pfnFree(

source/loader/layers/sanitizer/common.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ struct SourceInfo {
138138

139139
enum class DeviceType : uint64_t { UNKNOWN = 0, CPU, GPU_PVC, GPU_DG2 };
140140

141+
inline const char *ToString(DeviceType Type) {
142+
switch (Type) {
143+
case DeviceType::UNKNOWN:
144+
return "UNKNOWN";
145+
case DeviceType::CPU:
146+
return "CPU";
147+
case DeviceType::GPU_PVC:
148+
return "PVC";
149+
case DeviceType::GPU_DG2:
150+
return "DG2";
151+
default:
152+
return "UNKNOWN";
153+
}
154+
}
155+
141156
bool IsInASanContext();
142157

143158
uptr MmapNoReserve(uptr Addr, uptr Size);

source/loader/layers/sanitizer/ur_sanddi.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ ur_result_t setupContext(ur_context_handle_t Context, uint32_t numDevices,
2626
auto hDevice = phDevices[i];
2727
std::shared_ptr<DeviceInfo> DI;
2828
UR_CALL(context.interceptor->insertDevice(hDevice, DI));
29+
DI->Type = GetDeviceType(Context, hDevice);
30+
if (DI->Type == DeviceType::UNKNOWN) {
31+
context.logger.error("Unsupport device");
32+
return UR_RESULT_ERROR_INVALID_DEVICE;
33+
}
34+
context.logger.info("Add {} into context {}", ToString(DI->Type),
35+
(void *)Context);
2936
if (!DI->ShadowOffset) {
3037
UR_CALL(DI->allocShadowMemory(Context));
3138
}

source/loader/layers/sanitizer/ur_sanitizer_utils.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,6 @@ ur_device_handle_t GetDevice(ur_queue_handle_t Queue) {
7272
return Device;
7373
}
7474

75-
std::string GetDeviceName(ur_device_handle_t Device) {
76-
size_t Size = 0;
77-
[[maybe_unused]] auto Res = context.urDdiTable.Device.pfnGetInfo(
78-
Device, UR_DEVICE_INFO_NAME, 0, nullptr, &Size);
79-
assert(Res == UR_RESULT_SUCCESS && "GetDeviceName() failed");
80-
81-
std::vector<char> NameBuf(Size);
82-
Res = context.urDdiTable.Device.pfnGetInfo(Device, UR_DEVICE_INFO_NAME,
83-
Size, NameBuf.data(), nullptr);
84-
assert(Res == UR_RESULT_SUCCESS);
85-
86-
return std::string(NameBuf.data(), Size - 1);
87-
}
88-
8975
ur_program_handle_t GetProgram(ur_kernel_handle_t Kernel) {
9076
ur_program_handle_t Program{};
9177
[[maybe_unused]] auto Result = context.urDdiTable.Kernel.pfnGetInfo(
@@ -129,7 +115,8 @@ ur_device_handle_t GetUSMAllocDevice(ur_context_handle_t Context,
129115
return Device;
130116
}
131117

132-
DeviceType GetDeviceType(ur_device_handle_t Device) {
118+
DeviceType GetDeviceType(ur_context_handle_t Context,
119+
ur_device_handle_t Device) {
133120
ur_device_type_t DeviceType = UR_DEVICE_TYPE_DEFAULT;
134121
[[maybe_unused]] auto Result = context.urDdiTable.Device.pfnGetInfo(
135122
Device, UR_DEVICE_INFO_TYPE, sizeof(DeviceType), &DeviceType, nullptr);
@@ -140,16 +127,24 @@ DeviceType GetDeviceType(ur_device_handle_t Device) {
140127
// TODO: Check fpga is fpga emulator
141128
return DeviceType::CPU;
142129
case UR_DEVICE_TYPE_GPU: {
143-
// Ref: https://github.com/intel/compute-runtime/blob/master/shared/source/dll/devices/devices_base.inl
144-
auto Name = GetDeviceName(Device);
145-
if (Name.rfind("Intel(R) Data Center GPU Max", 0) == 0) {
146-
return DeviceType::GPU_PVC;
147-
} else if ((Name.rfind("Intel(R) Arc(TM)", 0) == 0 &&
148-
Name != "Intel(R) Arc(TM) Graphics") ||
149-
Name.rfind("Intel(R) Data Center GPU Flex", 0) == 0) {
150-
return DeviceType::GPU_DG2;
130+
uptr Ptr;
131+
ur_result_t Result = context.urDdiTable.USM.pfnDeviceAlloc(
132+
Context, Device, nullptr, nullptr, 4, (void **)&Ptr);
133+
context.logger.debug("GetDeviceType: {}", (void *)Ptr);
134+
assert(Result == UR_RESULT_SUCCESS &&
135+
"getDeviceType() failed at allocating device USM");
136+
// FIXME: There's no API querying the address bits of device, so we guess it by the
137+
// value of device USM pointer (see "USM Allocation Range" in asan_shadow_setup.cpp)
138+
auto Type = DeviceType::UNKNOWN;
139+
if (Ptr >> 48 == 0xff00U) {
140+
Type = DeviceType::GPU_PVC;
141+
} else {
142+
Type = DeviceType::GPU_DG2;
151143
}
152-
return DeviceType::UNKNOWN;
144+
Result = context.urDdiTable.USM.pfnFree(Context, (void *)Ptr);
145+
assert(Result == UR_RESULT_SUCCESS &&
146+
"getDeviceType() failed at releasing device USM");
147+
return Type;
153148
}
154149
default:
155150
return DeviceType::UNKNOWN;

source/loader/layers/sanitizer/ur_sanitizer_utils.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ ur_context_handle_t GetContext(ur_queue_handle_t Queue);
3434
ur_context_handle_t GetContext(ur_program_handle_t Program);
3535
ur_context_handle_t GetContext(ur_kernel_handle_t Kernel);
3636
ur_device_handle_t GetDevice(ur_queue_handle_t Queue);
37-
std::string GetDeviceName(ur_device_handle_t Device);
38-
DeviceType GetDeviceType(ur_device_handle_t Device);
37+
DeviceType GetDeviceType(ur_context_handle_t Context,
38+
ur_device_handle_t Device);
3939
std::string GetKernelName(ur_kernel_handle_t Kernel);
4040
size_t GetDeviceLocalMemorySize(ur_device_handle_t Device);
4141
ur_program_handle_t GetProgram(ur_kernel_handle_t Kernel);

0 commit comments

Comments
 (0)