Skip to content

Commit 1684c28

Browse files
zhaomaosubb-ur
authored andcommitted
Change DeviceType to specialization constant (#19798)
This will allow backend compiler to eliminte unnecessary code to reduce kernel code size.
1 parent 8d5f02f commit 1684c28

File tree

7 files changed

+46
-14
lines changed

7 files changed

+46
-14
lines changed

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
}

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;

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(

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

source/loader/layers/sanitizer/sanitizer_common/sanitizer_libdevice.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ 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+
2427
inline const char *ToString(DeviceType Type) {
2528
switch (Type) {
2629
case DeviceType::UNKNOWN:

source/loader/layers/sanitizer/tsan/tsan_interceptor.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,19 @@ ur_result_t TsanInterceptor::insertProgram(ur_program_handle_t Program) {
307307
if (m_ProgramMap.find(Program) != m_ProgramMap.end()) {
308308
return UR_RESULT_SUCCESS;
309309
}
310+
auto CI = getContextInfo(GetContext(Program));
311+
auto DI = getDeviceInfo(CI->DeviceList[0]);
312+
ur_specialization_constant_info_t SpecConstantInfo{
313+
SPEC_CONSTANT_DEVICE_TYPE_ID, sizeof(DeviceType), &DI->Type};
314+
ur_result_t URes =
315+
getContext()->urDdiTable.Program.pfnSetSpecializationConstants(
316+
Program, 1, &SpecConstantInfo);
317+
if (URes != UR_RESULT_SUCCESS) {
318+
UR_LOG_L(getContext()->logger, DEBUG,
319+
"Set specilization constant for device type failed: {}, the "
320+
"program may not be sanitized or is created from binary.",
321+
URes);
322+
}
310323
m_ProgramMap.emplace(Program, Program);
311324
return UR_RESULT_SUCCESS;
312325
}
@@ -425,7 +438,6 @@ ur_result_t TsanInterceptor::prepareLaunch(std::shared_ptr<ContextInfo> &,
425438
// Prepare launch info data
426439
LaunchInfo.Data.Host.GlobalShadowOffset = DI->Shadow->ShadowBegin;
427440
LaunchInfo.Data.Host.GlobalShadowOffsetEnd = DI->Shadow->ShadowEnd;
428-
LaunchInfo.Data.Host.DeviceTy = DI->Type;
429441
LaunchInfo.Data.Host.Debug = getContext()->Options.Debug ? 1 : 0;
430442

431443
const size_t *LocalWorkSize = LaunchInfo.LocalWorkSize.data();

source/loader/layers/sanitizer/tsan/tsan_libdevice.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ struct TsanRuntimeData {
9999
// The last one is to record global state
100100
VectorClock Clock[kThreadSlotCount + 1];
101101

102-
DeviceType DeviceTy = DeviceType::UNKNOWN;
103-
104102
uint32_t Debug = 0;
105103

106104
int Lock = 0;

0 commit comments

Comments
 (0)