Skip to content

Commit beb7277

Browse files
authored
[SYCL][L0] Remove ZeModule when program build failed (#5541)
When a sycl::program is attempted to build, a ZeModule is created. When the attempt failed, we need to clean up the ZeModule that is associated with the failed program to avoid memory leak.
1 parent 718c0b1 commit beb7277

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

+26-17
Original file line numberDiff line numberDiff line change
@@ -4305,28 +4305,37 @@ pi_result piProgramBuild(pi_program Program, pi_uint32 NumDevices,
43054305
ze_device_handle_t ZeDevice = DeviceList[0]->ZeDevice;
43064306
ze_context_handle_t ZeContext = Program->Context->ZeContext;
43074307
ze_module_handle_t ZeModule = nullptr;
4308-
ZE_CALL(zeModuleCreate, (ZeContext, ZeDevice, &ZeModuleDesc, &ZeModule,
4309-
&Program->ZeBuildLog));
4310-
4311-
// The call to zeModuleCreate does not report an error if there are
4312-
// unresolved symbols because it thinks these could be resolved later via a
4313-
// call to zeModuleDynamicLink. However, modules created with piProgramBuild
4314-
// are supposed to be fully linked and ready to use. Therefore, do an extra
4315-
// check now for unresolved symbols.
4316-
ze_result_t ZeResult = checkUnresolvedSymbols(ZeModule, &Program->ZeBuildLog);
4317-
if (ZeResult == ZE_RESULT_ERROR_MODULE_LINK_FAILURE) {
4318-
return PI_BUILD_PROGRAM_FAILURE;
4319-
} else if (ZeResult != ZE_RESULT_SUCCESS) {
4320-
return mapError(ZeResult);
4308+
4309+
pi_result Result = PI_SUCCESS;
4310+
Program->State = _pi_program::Exe;
4311+
ze_result_t ZeResult =
4312+
ZE_CALL_NOCHECK(zeModuleCreate, (ZeContext, ZeDevice, &ZeModuleDesc,
4313+
&ZeModule, &Program->ZeBuildLog));
4314+
if (ZeResult != ZE_RESULT_SUCCESS) {
4315+
// We adjust pi_program below to avoid attempting to release zeModule when
4316+
// RT calls piProgramRelease().
4317+
ZeModule = nullptr;
4318+
Program->State = _pi_program::Invalid;
4319+
Result = mapError(ZeResult);
4320+
} else {
4321+
// The call to zeModuleCreate does not report an error if there are
4322+
// unresolved symbols because it thinks these could be resolved later via a
4323+
// call to zeModuleDynamicLink. However, modules created with
4324+
// piProgramBuild are supposed to be fully linked and ready to use.
4325+
// Therefore, do an extra check now for unresolved symbols.
4326+
ZeResult = checkUnresolvedSymbols(ZeModule, &Program->ZeBuildLog);
4327+
if (ZeResult != ZE_RESULT_SUCCESS) {
4328+
Program->State = _pi_program::Invalid;
4329+
Result = (ZeResult == ZE_RESULT_ERROR_MODULE_LINK_FAILURE)
4330+
? PI_BUILD_PROGRAM_FAILURE
4331+
: mapError(ZeResult);
4332+
}
43214333
}
43224334

43234335
// We no longer need the IL / native code.
43244336
Program->Code.reset();
4325-
43264337
Program->ZeModule = ZeModule;
4327-
Program->State = _pi_program::Exe;
4328-
4329-
return PI_SUCCESS;
4338+
return Result;
43304339
}
43314340

43324341
pi_result piProgramGetBuildInfo(pi_program Program, pi_device Device,

0 commit comments

Comments
 (0)