@@ -1248,24 +1248,38 @@ ur_result_t ur_queue_handle_t_::synchronize() {
1248
1248
ZE2UR_CALL (zeHostSynchronize, (zeEvent));
1249
1249
Event->Completed = true ;
1250
1250
UR_CALL (urEventRelease (Event));
1251
+
1251
1252
// Cleanup all events from the synced command list.
1252
1253
auto EventListToCleanup = std::move (ImmCmdList->second .EventList );
1253
1254
ImmCmdList->second .EventList .clear ();
1254
1255
CleanupEventListFromResetCmdList (EventListToCleanup, true );
1255
1256
return UR_RESULT_SUCCESS;
1256
1257
};
1257
1258
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
+ }
1267
1280
}
1268
1281
}
1282
+ }
1269
1283
LastCommandEvent = nullptr ;
1270
1284
1271
1285
// With the entire queue synchronized, the active barriers must be done so we
0 commit comments