@@ -1248,24 +1248,38 @@ ur_result_t ur_queue_handle_t_::synchronize() {
12481248 ZE2UR_CALL (zeHostSynchronize, (zeEvent));
12491249 Event->Completed = true ;
12501250 UR_CALL (urEventRelease (Event));
1251+
12511252 // Cleanup all events from the synced command list.
12521253 auto EventListToCleanup = std::move (ImmCmdList->second .EventList );
12531254 ImmCmdList->second .EventList .clear ();
12541255 CleanupEventListFromResetCmdList (EventListToCleanup, true );
12551256 return UR_RESULT_SUCCESS;
12561257 };
12571258
1258- for (auto &QueueMap : {ComputeQueueGroupsByTID, CopyQueueGroupsByTID})
1259- for (auto &QueueGroup : QueueMap) {
1260- if (Device->ImmCommandListUsed ) {
1261- for (auto ImmCmdList : QueueGroup.second .ImmCmdLists )
1262- syncImmCmdList (this , ImmCmdList);
1263- } else {
1264- for (auto &ZeQueue : QueueGroup.second .ZeQueues )
1265- if (ZeQueue)
1266- ZE2UR_CALL (zeHostSynchronize, (ZeQueue));
1259+ // Do nothing if the queue is empty
1260+ if (!LastCommandEvent)
1261+ return UR_RESULT_SUCCESS;
1262+
1263+ // For in-order queue just wait for the last command.
1264+ // If event is discarded then it can be in reset state or underlying level
1265+ // zero handle can have device scope, so we can't synchronize the last event.
1266+ if (isInOrderQueue () && !LastCommandEvent->IsDiscarded ) {
1267+ ZE2UR_CALL (zeHostSynchronize, (LastCommandEvent->ZeEvent ));
1268+ } else {
1269+ // Otherwise sync all L0 queues/immediate command-lists.
1270+ for (auto &QueueMap : {ComputeQueueGroupsByTID, CopyQueueGroupsByTID}) {
1271+ for (auto &QueueGroup : QueueMap) {
1272+ if (Device->ImmCommandListUsed ) {
1273+ for (auto ImmCmdList : QueueGroup.second .ImmCmdLists )
1274+ syncImmCmdList (this , ImmCmdList);
1275+ } else {
1276+ for (auto &ZeQueue : QueueGroup.second .ZeQueues )
1277+ if (ZeQueue)
1278+ ZE2UR_CALL (zeHostSynchronize, (ZeQueue));
1279+ }
12671280 }
12681281 }
1282+ }
12691283 LastCommandEvent = nullptr ;
12701284
12711285 // With the entire queue synchronized, the active barriers must be done so we
0 commit comments