@@ -4296,9 +4296,16 @@ pi_result piProgramBuild(pi_program Program, pi_uint32 NumDevices,
42964296 ze_device_handle_t ZeDevice = DeviceList[0 ]->ZeDevice ;
42974297 ze_context_handle_t ZeContext = Program->Context ->ZeContext ;
42984298 ze_module_handle_t ZeModule = nullptr ;
4299- ZE_CALL (zeModuleCreate, (ZeContext, ZeDevice, &ZeModuleDesc, &ZeModule,
4299+ ze_result_t ZeResult = ZE_CALL (zeModuleCreate, (ZeContext, ZeDevice, &ZeModuleDesc, &ZeModule,
43004300 &Program->ZeBuildLog ));
4301-
4301+ if (ZeResult != ZE_RESULT_SUCCESS) {
4302+ // We need to clear Program state to avoid double destroy of zeModule in
4303+ // case where SYCL RT calls piProgramRelease().
4304+ // We should not return with an error code here due to the comments below.
4305+ Program->ZeModule = nullptr ;
4306+ Program->Code .reset ();
4307+ Program->State = _pi_program::Invalid;
4308+ }
43024309 // The call to zeModuleCreate does not report an error if there are
43034310 // unresolved symbols because it thinks these could be resolved later via a
43044311 // call to zeModuleDynamicLink. However, modules created with piProgramBuild
@@ -4307,10 +4314,8 @@ pi_result piProgramBuild(pi_program Program, pi_uint32 NumDevices,
43074314 ze_result_t ZeResult = checkUnresolvedSymbols (ZeModule, &Program->ZeBuildLog );
43084315 if (ZeResult != ZE_RESULT_SUCCESS) {
43094316 // remove ZeModule that is associated with the failed program
4310- ZE_CALL_NOCHECK (zeModuleDestroy, (ZeModule));
4311- // Also set Program->ZeModule nullptr to avoid double destroy of zeModule in
4312- // case where SYCL RT calls piProgramRelease().
4313- Program->ZeModule = nullptr ;
4317+ ZE_CALL (zeModuleDestroy, (ZeModule));
4318+
43144319 if (ZeResult == ZE_RESULT_ERROR_MODULE_LINK_FAILURE)
43154320 return PI_BUILD_PROGRAM_FAILURE;
43164321 return mapError (ZeResult);
0 commit comments