Skip to content

Commit c2682e9

Browse files
committed
[L0] don't delete program handle in kernel release and check work dim size
- Decrement the program handle reference count & release the L0 resources if the refcnt == 0, but don't delete the program handle in the kernel teardown to avoid invalid memory access during program handle teardown. Signed-off-by: Spruit, Neil R <[email protected]>
1 parent 8868230 commit c2682e9

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

source/adapters/level_zero/kernel.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueKernelLaunch(
8282
uint32_t WG[3]{};
8383

8484
// global_work_size of unused dimensions must be set to 1
85-
UR_ASSERT(WorkDim == 3 || GlobalWorkSize[2] == 1,
86-
UR_RESULT_ERROR_INVALID_VALUE);
87-
UR_ASSERT(WorkDim >= 2 || GlobalWorkSize[1] == 1,
88-
UR_RESULT_ERROR_INVALID_VALUE);
85+
if (WorkDim >= 2) {
86+
UR_ASSERT(WorkDim >= 2 || GlobalWorkSize[1] == 1,
87+
UR_RESULT_ERROR_INVALID_VALUE);
88+
if (WorkDim == 3) {
89+
UR_ASSERT(WorkDim == 3 || GlobalWorkSize[2] == 1,
90+
UR_RESULT_ERROR_INVALID_VALUE);
91+
}
92+
}
8993
if (LocalWorkSize) {
9094
// L0
9195
UR_ASSERT(LocalWorkSize[0] < (std::numeric_limits<uint32_t>::max)(),
@@ -642,8 +646,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelRelease(
642646
if (IndirectAccessTrackingEnabled) {
643647
UR_CALL(urContextRelease(KernelProgram->Context));
644648
}
645-
// do a release on the program this kernel was part of
646-
UR_CALL(urProgramRelease(KernelProgram));
649+
// do a release on the program this kernel was part of without delete of the
650+
// program handle
651+
KernelProgram->ur_release_program_resources(false);
652+
647653
delete Kernel;
648654

649655
return UR_RESULT_SUCCESS;

source/adapters/level_zero/program.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -837,18 +837,32 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithNativeHandle(
837837
}
838838

839839
ur_program_handle_t_::~ur_program_handle_t_() {
840+
if (!resourcesReleased) {
841+
ur_release_program_resources(true);
842+
}
843+
}
844+
845+
void ur_program_handle_t_::ur_release_program_resources(bool deletion) {
840846
// According to Level Zero Specification, all kernels and build logs
841847
// must be destroyed before the Module can be destroyed. So, be sure
842848
// to destroy build log before destroying the module.
843-
for (auto &ZeBuildLogPair : this->ZeBuildLogMap) {
844-
ZE_CALL_NOCHECK(zeModuleBuildLogDestroy, (ZeBuildLogPair.second));
849+
if (!deletion) {
850+
if (!RefCount.decrementAndTest()) {
851+
return;
852+
}
845853
}
854+
if (!resourcesReleased) {
855+
for (auto &ZeBuildLogPair : this->ZeBuildLogMap) {
856+
ZE_CALL_NOCHECK(zeModuleBuildLogDestroy, (ZeBuildLogPair.second));
857+
}
846858

847-
if (ZeModule && OwnZeModule) {
848-
for (auto &ZeModulePair : this->ZeModuleMap) {
849-
ZE_CALL_NOCHECK(zeModuleDestroy, (ZeModulePair.second));
859+
if (ZeModule && OwnZeModule) {
860+
for (auto &ZeModulePair : this->ZeModuleMap) {
861+
ZE_CALL_NOCHECK(zeModuleDestroy, (ZeModulePair.second));
862+
}
863+
this->ZeModuleMap.clear();
850864
}
851-
this->ZeModuleMap.clear();
865+
resourcesReleased = true;
852866
}
853867
}
854868

source/adapters/level_zero/program.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ struct ur_program_handle_t_ : _ur_object {
100100
State{St}, ZeModule{nullptr}, ZeBuildLog{nullptr} {}
101101

102102
~ur_program_handle_t_();
103+
void ur_release_program_resources(bool deletion);
104+
105+
// Tracks the release state of the program handle to determine if the
106+
// internal handle needs to be released.
107+
bool resourcesReleased = false;
103108

104109
const ur_context_handle_t Context; // Context of the program.
105110

test/conformance/kernel/kernel_adapter_level_zero.match

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ urKernelSetArgMemObjTest.InvalidKernelArgumentIndex/Intel_R__oneAPI_Unified_Runt
1111
urKernelSetArgPointerTest.SuccessHost/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
1212
urKernelSetArgPointerTest.SuccessDevice/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
1313
urKernelSetArgPointerTest.SuccessShared/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
14-
urKernelSetArgPointerNegativeTest.InvalidNullHandleKernel/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
1514
urKernelSetArgPointerNegativeTest.InvalidKernelArgumentIndex/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
1615
urKernelSetArgSamplerTest.InvalidKernelArgumentIndex/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_
1716
urKernelSetArgValueTest.InvalidKernelArgumentIndex/Intel_R__oneAPI_Unified_Runtime_over_Level_Zero___{{.*}}_

0 commit comments

Comments
 (0)