@@ -4296,9 +4296,16 @@ pi_result piProgramBuild(pi_program Program, pi_uint32 NumDevices,
4296
4296
ze_device_handle_t ZeDevice = DeviceList[0 ]->ZeDevice ;
4297
4297
ze_context_handle_t ZeContext = Program->Context ->ZeContext ;
4298
4298
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,
4300
4300
&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
+ }
4302
4309
// The call to zeModuleCreate does not report an error if there are
4303
4310
// unresolved symbols because it thinks these could be resolved later via a
4304
4311
// call to zeModuleDynamicLink. However, modules created with piProgramBuild
@@ -4307,10 +4314,8 @@ pi_result piProgramBuild(pi_program Program, pi_uint32 NumDevices,
4307
4314
ze_result_t ZeResult = checkUnresolvedSymbols (ZeModule, &Program->ZeBuildLog );
4308
4315
if (ZeResult != ZE_RESULT_SUCCESS) {
4309
4316
// 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
+
4314
4319
if (ZeResult == ZE_RESULT_ERROR_MODULE_LINK_FAILURE)
4315
4320
return PI_BUILD_PROGRAM_FAILURE;
4316
4321
return mapError (ZeResult);
0 commit comments