Merged
fix(machine): Improve ctrl cache replay performance #6382
可惜春残风雨又Add on Sep 16
fix(machine): Improve ctrl cache replay performance #6382
Merged
可惜春残风雨又Add on Sep 16
Total: 10 files changed+414-34
@@ -125,12 +125,21 @@ void DeviceExecuteContext::GELaunchRunCached(DevStartArgs* startArgs, PushTaskEn
125 this->devProg = startArgs->devProg;125 this->devProg = startArgs->devProg;
126 PerfEnd(PERF_EVT_CONTROL_FLOW_INIT);126 PerfEnd(PERF_EVT_CONTROL_FLOW_INIT);
127 PerfBegin(PERF_EVT_CONTROL_FLOW);127 PerfBegin(PERF_EVT_CONTROL_FLOW);
128+ const bool fullCache = devProg->ctrlFlowCacheAnchor->IsActivatedFullCache(startArgs);
128 for (size_t index = 0; index < devProg->ctrlFlowCacheAnchor->deviceTaskCount; index++) {129 for (size_t index = 0; index < devProg->ctrlFlowCacheAnchor->deviceTaskCount; index++) {
129 DynDeviceTask* dynTask = reinterpret_cast<DynDeviceTask*>(130 DynDeviceTask* dynTask = reinterpret_cast<DynDeviceTask*>(
130 devProg->ctrlFlowCacheAnchor->deviceTaskCacheList[index].dynTaskBase);131 devProg->ctrlFlowCacheAnchor->deviceTaskCacheList[index].dynTaskBase);
131- devProg->ctrlFlowCacheAnchor->PredCountDataRestore(dynTask);132+ devProg->ctrlFlowCacheAnchor->PredCountPingPongSwap(dynTask);
132- devProg->ctrlFlowCacheAnchor->ReadyQueueDataRestore(dynTask, startArgs->nrValidAic);133+ if (startArgs->devProg->devArgs.enableAicoreResolve) {
133- devProg->ctrlFlowCacheAnchor->DieReadyQueueDataRestore(dynTask, startArgs->nrValidAic);134+ devProg->ctrlFlowCacheAnchor->DrcoPredCountDataRestore(dynTask);
135+ }
136+ // The Bitmap remains unchanged in the sche.
137+ // For the full cache scenario, BitmapDataRestoreTask only needs to be executed once.
138+ if (!fullCache || !devProg->ctrlFlowCacheAnchor->bitmapRestoreDone) {
139+ devProg->ctrlFlowCacheAnchor->BitmapDataRestoreTask(dynTask);
140+ }
141+ devProg->ctrlFlowCacheAnchor->ReadyQueueDataPingPongSwap(dynTask, startArgs->nrValidAic);
142+ devProg->ctrlFlowCacheAnchor->DieReadyQueuePingPongSwap(dynTask, startArgs->nrValidAic);
134 devProg->ctrlFlowCacheAnchor->MixTaskDataRestore(dynTask);143 devProg->ctrlFlowCacheAnchor->MixTaskDataRestore(dynTask);
135 taskContext.UpdateReadyTaskNum(dynTask->readyQueueBackup->readyTaskNum);144 taskContext.UpdateReadyTaskNum(dynTask->readyQueueBackup->readyTaskNum);
136 145 
@@ -149,6 +158,21 @@ void DeviceExecuteContext::GELaunchRunCached(DevStartArgs* startArgs, PushTaskEn
149 PerfEnd(PERF_EVT_CONTROL_FLOW);158 PerfEnd(PERF_EVT_CONTROL_FLOW);
150}159}
151 160 
161+void DeviceExecuteContext::PingPongRestoreAll(DevStartArgs* startArgs)
162+{
163+ const bool isFullCache = devProg->ctrlFlowCacheAnchor->IsActivatedFullCache(startArgs);
164+ for (size_t index = 0; index < devProg->ctrlFlowCacheAnchor->deviceTaskCount; index++) {
165+ DynDeviceTask* dynTask = reinterpret_cast<DynDeviceTask*>(
166+ devProg->ctrlFlowCacheAnchor->deviceTaskCacheList[index].dynTaskBase);
167+ devProg->ctrlFlowCacheAnchor->PredCountPingPongRestore(dynTask);
168+ devProg->ctrlFlowCacheAnchor->ReadyQueueDataPingPongRestore(dynTask);
169+ devProg->ctrlFlowCacheAnchor->DieReadyQueuePingPongRestore(dynTask);
170+ }
171+ if (isFullCache) {
172+ devProg->ctrlFlowCacheAnchor->bitmapRestoreDone = true;
173+ }
174+}
175+ 
152int DeviceExecuteContext::RunControlFlow(DevStartArgs* startArgs)176int DeviceExecuteContext::RunControlFlow(DevStartArgs* startArgs)
153{177{
154 PerfBegin(PERF_EVT_CONTROL_FLOW);178 PerfBegin(PERF_EVT_CONTROL_FLOW);
@@ -214,11 +238,12 @@ int DeviceExecuteContext::GELaunch(DevStartArgs* startArgs, PushTaskEntry tPushT
214int DeviceExecuteContext::GELaunchPartialCache(DevStartArgs* startArgs, PushTaskEntry tPushTask)238int DeviceExecuteContext::GELaunchPartialCache(DevStartArgs* startArgs, PushTaskEntry tPushTask)
215{239{
216 int ret = DEVICE_MACHINE_OK;240 int ret = DEVICE_MACHINE_OK;
241+ const bool isActivatedCache = devProg->ctrlFlowCacheAnchor->IsActivatedCache(startArgs);
217 DEV_TRACE_DEBUG(242 DEV_TRACE_DEBUG(
218 CtrlEvent(none(), Workspace(Range(startArgs->contextWorkspaceAddr,243 CtrlEvent(none(), Workspace(Range(startArgs->contextWorkspaceAddr,
219 startArgs->contextWorkspaceAddr + startArgs->contextWorkspaceSize))));244 startArgs->contextWorkspaceAddr + startArgs->contextWorkspaceSize))));
220 245 
221- if (devProg->ctrlFlowCacheAnchor->IsActivatedPartialCache(startArgs)) {246+ if (isActivatedCache) {
222 controlFlowCacheActivated = true;247 controlFlowCacheActivated = true;
223 DEV_TRACE_DEBUG(CtrlEvent(none(), ControlFlowCachePartRunCache(devProg->ctrlFlowCacheAnchor->deviceTaskCount,248 DEV_TRACE_DEBUG(CtrlEvent(none(), ControlFlowCachePartRunCache(devProg->ctrlFlowCacheAnchor->deviceTaskCount,
224 devProg->ctrlFlowCacheAnchor->rootTaskCount)));249 devProg->ctrlFlowCacheAnchor->rootTaskCount)));
@@ -236,6 +261,9 @@ int DeviceExecuteContext::GELaunchPartialCache(DevStartArgs* startArgs, PushTask
236 }261 }
237 DEV_IF_INFO { workspace.LogTuningSummary(); }262 DEV_IF_INFO { workspace.LogTuningSummary(); }
238 }263 }
264+ if (isActivatedCache) {
265+ PingPongRestoreAll(startArgs);
266+ }
239 return ret;267 return ret;
240}268}
241 269 
@@ -109,6 +109,10 @@ struct DeviceExecuteContext {
109 109 
110 void GELaunchRunCached(DevStartArgs* startArgs, PushTaskEntry tPushTask);110 void GELaunchRunCached(DevStartArgs* startArgs, PushTaskEntry tPushTask);
111 111 
112+ /* Post-launch ping-pong refresh of the inactive buffers (predCount + ready/die queues),
113+ * deferred to the callers so the partial-cache flow can run it after RunControlFlow. */
114+ void PingPongRestoreAll(DevStartArgs* startArgs);
115+ 
112 int RunControlFlow(DevStartArgs* startArgs);116 int RunControlFlow(DevStartArgs* startArgs);
113 117 
114 int GELaunchFullCacheRunControlFlow(DevStartArgs* startArgs, PushTaskEntry tPushTask);118 int GELaunchFullCacheRunControlFlow(DevStartArgs* startArgs, PushTaskEntry tPushTask);
@@ -311,7 +311,11 @@ int DeviceStitchContext::MoveTo(DynDeviceTask* dynTask)
311 for (int i = 0; i < size; ++i) {311 for (int i = 0; i < size; ++i) {
312 auto& funcDup = dynTask->stitchedList[i];312 auto& funcDup = dynTask->stitchedList[i];
313 auto* src = funcDup.GetSource();313 auto* src = funcDup.GetSource();
314- dynTask->dynFuncDataCacheList[i] = {src, &funcDup.GetOperationCurrPredCount(0), src->GetCalleeIndexAddr(),314+ dynTask->dynFuncDataCacheList[i] = {src,
315+ &funcDup.GetOperationCurrPredCount(0),
316+ funcDup.GetOperationPredCountPingPong(PRED_COUNT_PING),
317+ funcDup.GetOperationPredCountPingPong(PRED_COUNT_PONG),
318+ src->GetCalleeIndexAddr(),
315 funcDup.DupDataForDynFuncData()};319 funcDup.DupDataForDynFuncData()};
316 dynTask->devTask.mixTaskData.opWrapList[i] = PtrToValue(src->GetOpWrapListAddr());320 dynTask->devTask.mixTaskData.opWrapList[i] = PtrToValue(src->GetOpWrapListAddr());
317 }321 }
@@ -209,7 +209,7 @@ public:
209 return;209 return;
210 }210 }
211 211 
212- if (devCtrlFlowCache->IsActivatedPartialCache(devStartArgs)) {212+ if (devCtrlFlowCache->IsActivatedCache(devStartArgs)) {
213 DEV_INFO("ControlFlowCache: activated");213 DEV_INFO("ControlFlowCache: activated");
214 // Actual run214 // Actual run
215 if (!devCtrlFlowCache->isRelocDataDev) {215 if (!devCtrlFlowCache->isRelocDataDev) {
@@ -218,7 +218,6 @@ public:
218 reinterpret_cast<uint64_t>(devCtrlFlowCache));218 reinterpret_cast<uint64_t>(devCtrlFlowCache));
219 devCtrlFlowCache->RuntimeAddrRelocProgram(0, reinterpret_cast<uint64_t>(devProg));219 devCtrlFlowCache->RuntimeAddrRelocProgram(0, reinterpret_cast<uint64_t>(devProg));
220 }220 }
221- 
222 devCtrlFlowCache->IncastOutcastAddrRestore();221 devCtrlFlowCache->IncastOutcastAddrRestore();
223 devCtrlFlowCache->IncastOutcastAddrReloc(0, devStartArgs->contextWorkspaceAddr, devStartArgs);222 devCtrlFlowCache->IncastOutcastAddrReloc(0, devStartArgs->contextWorkspaceAddr, devStartArgs);
224 if (devCtrlFlowCache->workspaceAddr != devStartArgs->contextWorkspaceAddr) {223 if (devCtrlFlowCache->workspaceAddr != devStartArgs->contextWorkspaceAddr) {
@@ -226,7 +225,9 @@ public:
226 devCtrlFlowCache->TaskAddrRestoreWorkspace();225 devCtrlFlowCache->TaskAddrRestoreWorkspace();
227 devCtrlFlowCache->TaskAddrRelocWorkspace(0, devStartArgs->contextWorkspaceAddr, devStartArgs);226 devCtrlFlowCache->TaskAddrRelocWorkspace(0, devStartArgs->contextWorkspaceAddr, devStartArgs);
228 }227 }
229- devProg->ResetRerun();228+ if (!devCtrlFlowCache->IsActivatedFullCache(devStartArgs)) {
229+ devProg->ResetRerun();
230+ }
230 }231 }
231 }232 }
232 233 
@@ -165,7 +165,8 @@ void DevAscendFunction::InitOperationDynamicField(
165 uint64_t outcastSize = outcastTensorList.size();165 uint64_t outcastSize = outcastTensorList.size();
166 uint64_t expressionSize = expressionTable->GetPrimaryExpressionSize();166 uint64_t expressionSize = expressionTable->GetPrimaryExpressionSize();
167 167 
168- uint64_t predCountListDataSize = AlignUp(operationSize * sizeof(predcount_t), sizeof(uint64_t));168+ uint64_t predCountListDataSize = PRED_COUNT_PINGPONG_NUM *
169+ AlignUp(operationSize * sizeof(predcount_t), sizeof(uint64_t));
169 uint64_t incastDataSize = AlignUp(incastSize * sizeof(void*), sizeof(uint64_t));170 uint64_t incastDataSize = AlignUp(incastSize * sizeof(void*), sizeof(uint64_t));
170 uint64_t outcastDataSize = AlignUp(outcastSize * sizeof(void*), sizeof(uint64_t));171 uint64_t outcastDataSize = AlignUp(outcastSize * sizeof(void*), sizeof(uint64_t));
171 uint64_t expressionDataSize = AlignUp(expressionSize * sizeof(uint64_t), sizeof(uint64_t));172 uint64_t expressionDataSize = AlignUp(expressionSize * sizeof(uint64_t), sizeof(uint64_t));
@@ -25,6 +25,10 @@ constexpr int ARG_ATTR_TYPE = 4;
25const uint32_t RAW_TENSOR_OFFSET_SIZE = 63;25const uint32_t RAW_TENSOR_OFFSET_SIZE = 63;
26const uint32_t RAW_TENSOR_DESC_PRE_SIZE = 8;26const uint32_t RAW_TENSOR_DESC_PRE_SIZE = 8;
27 27 
28+inline constexpr uint32_t PRED_COUNT_PING = 0;
29+inline constexpr uint32_t PRED_COUNT_PONG = 1;
30+inline constexpr uint32_t PRED_COUNT_PINGPONG_NUM = 2;
31+ 
28struct DevAscendFunctionDuppedData {32struct DevAscendFunctionDuppedData {
29 DevAscendFunction* source_;33 DevAscendFunction* source_;
30 DevAscendFunctionDuppedOperation operationList_;34 DevAscendFunctionDuppedOperation operationList_;
@@ -56,6 +60,21 @@ struct DevAscendFunctionDuppedData {
56 return GET_DATA(predcount_t, data_, operationList_.predCountBase, index);60 return GET_DATA(predcount_t, data_, operationList_.predCountBase, index);
57 }61 }
58 62 
63+ /* Ctrl-flow-cache ping-pong layout: two back-to-back predcount slices. Stride is one
64+ * slice's span in elements, including the 8-byte alignment padding the encode inserts. */
65+ uint32_t GetPredCountPingPongStride() const
66+ {
67+ uint64_t sliceBytes = operationList_.size * sizeof(predcount_t);
68+ sliceBytes = (sliceBytes + sizeof(uint64_t) - 1) / sizeof(uint64_t) * sizeof(uint64_t);
69+ return static_cast<uint32_t>(sliceBytes / sizeof(predcount_t));
70+ }
71+ predcount_t* GetOperationPredCountPingPong(uint32_t pos)
72+ {
73+ uint64_t byteOffset = operationList_.predCountBase +
74+ static_cast<uint64_t>(pos) * GetPredCountPingPongStride() * sizeof(predcount_t);
75+ return &GET_DATA(predcount_t, data_, byteOffset, 0);
76+ }
77+ 
59 uint32_t GetStitchSize() const { return operationList_.stitchCount; }78 uint32_t GetStitchSize() const { return operationList_.stitchCount; }
60 const DevAscendFunctionDuppedStitchList& GetStitch(int index) const79 const DevAscendFunctionDuppedStitchList& GetStitch(int index) const
61 {80 {
@@ -244,6 +263,10 @@ struct DevAscendFunctionDupped {
244 return DupData()->GetOperationCurrPredCount(arg);263 return DupData()->GetOperationCurrPredCount(arg);
245 };264 };
246 inline predcount_t& GetOperationCurrPredCount(int arg) { return DupData()->GetOperationCurrPredCount(arg); };265 inline predcount_t& GetOperationCurrPredCount(int arg) { return DupData()->GetOperationCurrPredCount(arg); };
266+ inline predcount_t* GetOperationPredCountPingPong(uint32_t pos)
267+ {
268+ return DupData()->GetOperationPredCountPingPong(pos);
269+ };
247 inline const auto& GetOperationStitch(int arg, bool maybeNull = true) const270 inline const auto& GetOperationStitch(int arg, bool maybeNull = true) const
248 {271 {
249 return DupData()->GetOperationStitch(arg, maybeNull);272 return DupData()->GetOperationStitch(arg, maybeNull);
@@ -80,12 +80,43 @@ void DevControlFlowCache::PredCountDataBackup(DynDeviceTaskBase* base)
80 }80 }
81 dynDataBackup->predCountBackup = predCountBackup;81 dynDataBackup->predCountBackup = predCountBackup;
82 DevMemcpyS(dynDataBackup->predCountBackup, backupSize, &duppedData->GetOperationCurrPredCount(0), backupSize);82 DevMemcpyS(dynDataBackup->predCountBackup, backupSize, &duppedData->GetOperationCurrPredCount(0), backupSize);
83+ DevMemcpyS(duppedData->GetOperationPredCountPingPong(PRED_COUNT_PONG), backupSize,
84+ dynDataBackup->predCountBackup, backupSize);
83 85 
84 BitmapDataBackup(dynDataCache, dynDataBackup);86 BitmapDataBackup(dynDataCache, dynDataBackup);
85 }87 }
86}88}
87 89 
88-void DevControlFlowCache::PredCountDataRestore(DynDeviceTaskBase* base)90+void DevControlFlowCache::PredCountPingPongSwap(DynDeviceTaskBase* base)
91+{
92+ DynFuncHeader* dynFuncDataList = base->GetDynFuncDataList();
93+ DynFuncDataCache* dynFuncDataCacheList = base->dynFuncDataCacheList;
94+ for (size_t dupIndex = 0; dupIndex < dynFuncDataList->Size(); ++dupIndex) {
95+ DynFuncDataCache& dynDataCache = dynFuncDataCacheList->At(dupIndex);
96+ std::swap(dynDataCache.predCountPing, dynDataCache.predCountPong);
97+ dynDataCache.predCount = dynDataCache.predCountPing;
98+ }
99+}
100+ 
101+void DevControlFlowCache::DrcoPredCountDataRestore(DynDeviceTaskBase* base)
102+{
103+ DynFuncHeader* dynFuncDataList = base->GetDynFuncDataList();
104+ DynFuncDataBackup* dynFuncDataBackupList = base->dynFuncDataBackupList;
105+ for (size_t dupIndex = 0; dupIndex < dynFuncDataList->Size(); ++dupIndex) {
106+ DynFuncData* dynData = &dynFuncDataList->At(dupIndex);
107+ if (dynData->drcoRootFuncData.predCount == nullptr) {
108+ continue;
109+ }
110+ int32_t* drcoPredCount = dynData->drcoRootFuncData.predCount;
111+ predcount_t* predCountBackup = dynFuncDataBackupList->At(dupIndex).predCountBackup;
112+ uint32_t opSize = base->dynFuncDataCacheList->At(dupIndex).duppedData->GetOperationSize();
113+ for (uint32_t i = 0; i < opSize; ++i) {
114+ drcoPredCount[i] = static_cast<int32_t>(predCountBackup[i]);
115+ }
116+ }
117+}
118+ 
119+void DevControlFlowCache::PredCountPingPongRestore(DynDeviceTaskBase* base)
89{120{
90 DynFuncHeader* dynFuncDataList = base->GetDynFuncDataList();121 DynFuncHeader* dynFuncDataList = base->GetDynFuncDataList();
91 DynFuncDataCache* dynFuncDataCacheList = base->dynFuncDataCacheList;122 DynFuncDataCache* dynFuncDataCacheList = base->dynFuncDataCacheList;
@@ -94,20 +125,18 @@ void DevControlFlowCache::PredCountDataRestore(DynDeviceTaskBase* base)
94 DynFuncDataCache* dynDataCache = &dynFuncDataCacheList->At(dupIndex);125 DynFuncDataCache* dynDataCache = &dynFuncDataCacheList->At(dupIndex);
95 DynFuncDataBackup* dynDataBackup = &dynFuncDataBackupList->At(dupIndex);126 DynFuncDataBackup* dynDataBackup = &dynFuncDataBackupList->At(dupIndex);
96 DevAscendFunctionDuppedData* duppedData = dynDataCache->duppedData;127 DevAscendFunctionDuppedData* duppedData = dynDataCache->duppedData;
97- uint32_t opSize = duppedData->GetOperationSize();128+ size_t backupSize = sizeof(predcount_t) * duppedData->GetOperationSize();
98- size_t backupSize = sizeof(predcount_t) * opSize;129+ DevMemcpyS(dynDataCache->predCountPong, backupSize, dynDataBackup->predCountBackup, backupSize);
99- DevMemcpyS(&duppedData->GetOperationCurrPredCount(0), backupSize, dynDataBackup->predCountBackup, backupSize);130+ }
131+}
100 132 
101- DynFuncData* dynData = &dynFuncDataList->At(dupIndex);133+void DevControlFlowCache::BitmapDataRestoreTask(DynDeviceTaskBase* base)
102- if (dynData->drcoRootFuncData.predCount != nullptr) {134+{
103- // drco predCount is int32_t on aicore side, while cache backup is uint16_t(predcount_t)135+ DynFuncHeader* dynFuncDataList = base->GetDynFuncDataList();
104- int32_t* drcoPredCount = dynData->drcoRootFuncData.predCount;136+ DynFuncDataCache* dynFuncDataCacheList = base->dynFuncDataCacheList;
105- for (uint32_t i = 0; i < opSize; ++i) {137+ DynFuncDataBackup* dynFuncDataBackupList = base->dynFuncDataBackupList;
106- drcoPredCount[i] = static_cast<int32_t>(dynDataBackup->predCountBackup[i]);138+ for (size_t dupIndex = 0; dupIndex < dynFuncDataList->Size(); ++dupIndex) {
107- }139+ BitmapDataRestore(&dynFuncDataCacheList->At(dupIndex), &dynFuncDataBackupList->At(dupIndex));
108- }
109- 
110- BitmapDataRestore(dynDataCache, dynDataBackup);
111 }140 }
112}141}
113 142 
@@ -125,11 +154,20 @@ void DevControlFlowCache::ReadyQueueDataBackup(DynDeviceTaskBase* base)
125 if (readyQueueBackupElem == nullptr) {154 if (readyQueueBackupElem == nullptr) {
126 return;155 return;
127 }156 }
157+ uint32_t* readyQueueShadowElem = reinterpret_cast<uint32_t*>(AllocateCache(backupSize));
158+ if (readyQueueShadowElem == nullptr) {
159+ return;
160+ }
128 161 
129 new (&readyQueueBackup->queueList[i])162 new (&readyQueueBackup->queueList[i])
130 ReadyCoreFunctionQueueUnsafe(base->readyQueue[i]->Capacity(), readyQueueBackupElem);163 ReadyCoreFunctionQueueUnsafe(base->readyQueue[i]->Capacity(), readyQueueBackupElem);
131 readyQueueBackup->queueList[i] = *base->readyQueue[i];164 readyQueueBackup->queueList[i] = *base->readyQueue[i];
132 readyTaskNum += base->readyQueue[i]->UnsafeSize();165 readyTaskNum += base->readyQueue[i]->UnsafeSize();
166+ 
167+ readyQueueBackup->pingElem[i] = base->readyQueue[i]->Data();
168+ readyQueueBackup->pongElem[i] = readyQueueShadowElem;
169+ DevMemcpyS(readyQueueShadowElem, backupSize, readyQueueBackup->queueList[i].Data(),
170+ sizeof(uint32_t) * readyQueueBackup->queueList[i].Size());
133 }171 }
134 readyQueueBackup->readyTaskNum = readyTaskNum;172 readyQueueBackup->readyTaskNum = readyTaskNum;
135 173 
@@ -232,12 +270,29 @@ void DevControlFlowCache::DrcoReadyQueueDataRestore(DynDeviceTaskBase* base, uin
232 base->drcoRootFuncList->devTaskFinished = 0;270 base->drcoRootFuncList->devTaskFinished = 0;
233}271}
234 272 
235-void DevControlFlowCache::ReadyQueueDataRestore(DynDeviceTaskBase* base, uint32_t nrValidAic)273+void DevControlFlowCache::ReadyQueueDataPingPongRestore(DynDeviceTaskBase* base)
274+{
275+ ReadyQueueCache* readyQueueBackup = base->readyQueueBackup;
276+ for (size_t i = 0; i < READY_QUEUE_SIZE; i++) {
277+ /* elem_ targets the buffer this launch just dirtied; refresh the other one. */
278+ uint32_t* inactiveElem = (base->readyQueue[i]->Data() == readyQueueBackup->pingElem[i]) ?
279+ readyQueueBackup->pongElem[i] :
280+ readyQueueBackup->pingElem[i];
281+ size_t capBytes = sizeof(uint32_t) * readyQueueBackup->queueList[i].Capacity();
282+ size_t copyBytes = sizeof(uint32_t) * readyQueueBackup->queueList[i].Size();
283+ DevMemcpyS(inactiveElem, capBytes, readyQueueBackup->queueList[i].Data(), copyBytes);
284+ }
285+}
286+ 
287+void DevControlFlowCache::ReadyQueueDataPingPongSwap(DynDeviceTaskBase* base, uint32_t nrValidAic)
236{288{
237 ReadyQueueCache* readyQueueBackup = base->readyQueueBackup;289 ReadyQueueCache* readyQueueBackup = base->readyQueueBackup;
238 base->devTask.coreFunctionCnt = readyQueueBackup->coreFunctionCnt;290 base->devTask.coreFunctionCnt = readyQueueBackup->coreFunctionCnt;
239 for (size_t i = 0; i < READY_QUEUE_SIZE; i++) {291 for (size_t i = 0; i < READY_QUEUE_SIZE; i++) {
240- *base->readyQueue[i] = readyQueueBackup->queueList[i];292+ uint32_t* inactiveElem = (base->readyQueue[i]->Data() == readyQueueBackup->pingElem[i]) ?
293+ readyQueueBackup->pongElem[i] :
294+ readyQueueBackup->pingElem[i];
295+ base->readyQueue[i]->FillFrom(readyQueueBackup->queueList[i], inactiveElem);
241 }296 }
242 297 
243 // for aicore-resolve298 // for aicore-resolve
@@ -261,6 +316,8 @@ void DevControlFlowCache::DieReadyQueueDataBackup(DynDeviceTaskBase* base)
261 base->devTask.dieReadyFunctionQue.readyDieAicCoreFunctionQue[i - DIE_NUM]);316 base->devTask.dieReadyFunctionQue.readyDieAicCoreFunctionQue[i - DIE_NUM]);
262 if (dieReadyQueue == nullptr) {317 if (dieReadyQueue == nullptr) {
263 new (&dieReadyQueueBackup->queueList[i]) ReadyCoreFunctionQueueUnsafe(0, nullptr);318 new (&dieReadyQueueBackup->queueList[i]) ReadyCoreFunctionQueueUnsafe(0, nullptr);
319+ dieReadyQueueBackup->pingElem[i] = nullptr;
320+ dieReadyQueueBackup->pongElem[i] = nullptr;
264 continue;321 continue;
265 }322 }
266 size_t backupSize = sizeof(uint32_t) * dieReadyQueue->Capacity();323 size_t backupSize = sizeof(uint32_t) * dieReadyQueue->Capacity();
@@ -268,17 +325,50 @@ void DevControlFlowCache::DieReadyQueueDataBackup(DynDeviceTaskBase* base)
268 if (dieReadyQueueBackupElem == nullptr) {325 if (dieReadyQueueBackupElem == nullptr) {
269 return;326 return;
270 }327 }
328+ uint32_t* dieReadyQueueShadowElem = reinterpret_cast<uint32_t*>(AllocateCache(backupSize));
329+ if (dieReadyQueueShadowElem == nullptr) {
330+ return;
331+ }
271 332 
272 new (&dieReadyQueueBackup->queueList[i])333 new (&dieReadyQueueBackup->queueList[i])
273 ReadyCoreFunctionQueueUnsafe(dieReadyQueue->Capacity(), dieReadyQueueBackupElem);334 ReadyCoreFunctionQueueUnsafe(dieReadyQueue->Capacity(), dieReadyQueueBackupElem);
274 dieReadyQueueBackup->queueList[i] = *dieReadyQueue;335 dieReadyQueueBackup->queueList[i] = *dieReadyQueue;
275 readyTaskNum += dieReadyQueue->UnsafeSize();336 readyTaskNum += dieReadyQueue->UnsafeSize();
337+ 
338+ /* Ping-pong, same protocol as ReadyQueueDataBackup. */
339+ dieReadyQueueBackup->pingElem[i] = dieReadyQueue->Data();
340+ dieReadyQueueBackup->pongElem[i] = dieReadyQueueShadowElem;
341+ DevMemcpyS(dieReadyQueueShadowElem, backupSize, dieReadyQueueBackup->queueList[i].Data(),
342+ sizeof(uint32_t) * dieReadyQueueBackup->queueList[i].Size());
276 }343 }
277 dieReadyQueueBackup->readyTaskNum = readyTaskNum;344 dieReadyQueueBackup->readyTaskNum = readyTaskNum;
278 base->dieReadyQueueBackup = dieReadyQueueBackup;345 base->dieReadyQueueBackup = dieReadyQueueBackup;
279}346}
280 347 
281-void DevControlFlowCache::DieReadyQueueDataRestore(DynDeviceTaskBase* base, uint32_t nrValidAic)348+void DevControlFlowCache::DieReadyQueuePingPongRestore(DynDeviceTaskBase* base)
349+{
350+ DieReadyQueueCache* dieReadyQueueBackup = base->dieReadyQueueBackup;
351+ if (dieReadyQueueBackup == nullptr) {
352+ return;
353+ }
354+ for (size_t i = 0; i < DIE_READY_QUEUE_SIZE * DIE_NUM; i++) {
355+ ReadyCoreFunctionQueue* dieReadyQueue = reinterpret_cast<ReadyCoreFunctionQueue*>(
356+ (i < DIE_NUM) ? base->devTask.dieReadyFunctionQue.readyDieAivCoreFunctionQue[i] :
357+ base->devTask.dieReadyFunctionQue.readyDieAicCoreFunctionQue[i - DIE_NUM]);
358+ if (dieReadyQueue == nullptr) {
359+ continue;
360+ }
361+ /* elem_ targets the buffer this launch just dirtied; refresh the other one. */
362+ uint32_t* inactiveElem = (dieReadyQueue->Data() == dieReadyQueueBackup->pingElem[i]) ?
363+ dieReadyQueueBackup->pongElem[i] :
364+ dieReadyQueueBackup->pingElem[i];
365+ size_t capBytes = sizeof(uint32_t) * dieReadyQueueBackup->queueList[i].Capacity();
366+ size_t copyBytes = sizeof(uint32_t) * dieReadyQueueBackup->queueList[i].Size();
367+ DevMemcpyS(inactiveElem, capBytes, dieReadyQueueBackup->queueList[i].Data(), copyBytes);
368+ }
369+}
370+ 
371+void DevControlFlowCache::DieReadyQueuePingPongSwap(DynDeviceTaskBase* base, uint32_t nrValidAic)
282{372{
283 DieReadyQueueCache* dieReadyQueueBackup = base->dieReadyQueueBackup;373 DieReadyQueueCache* dieReadyQueueBackup = base->dieReadyQueueBackup;
284 if (dieReadyQueueBackup == nullptr) {374 if (dieReadyQueueBackup == nullptr) {
@@ -292,7 +382,11 @@ void DevControlFlowCache::DieReadyQueueDataRestore(DynDeviceTaskBase* base, uint
292 if (dieReadyQueue == nullptr) {382 if (dieReadyQueue == nullptr) {
293 continue;383 continue;
294 }384 }
295- *dieReadyQueue = dieReadyQueueBackup->queueList[i];385+ /* Ping-pong: rebind to the buffer the previous launch's shadow restore refreshed. */
386+ uint32_t* inactiveElem = (dieReadyQueue->Data() == dieReadyQueueBackup->pingElem[i]) ?
387+ dieReadyQueueBackup->pongElem[i] :
388+ dieReadyQueueBackup->pingElem[i];
389+ dieReadyQueue->FillFrom(dieReadyQueueBackup->queueList[i], inactiveElem);
296 }390 }
297 391 
298 if (base->drcoRootFuncList == nullptr) {392 if (base->drcoRootFuncList == nullptr) {
@@ -887,6 +981,10 @@ void DevControlFlowCache::DieReadyQueueReloc(RelocRange& relocCtrlCache, DynDevi
887 for (size_t i = 0; i < DIE_READY_QUEUE_SIZE * DIE_NUM; i++) {981 for (size_t i = 0; i < DIE_READY_QUEUE_SIZE * DIE_NUM; i++) {
888 dieReadyQueueBackup->queueList[i].Reloc(relocCtrlCache);982 dieReadyQueueBackup->queueList[i].Reloc(relocCtrlCache);
889 }983 }
984+ for (size_t i = 0; i < DIE_READY_QUEUE_SIZE * DIE_NUM; i++) {
985+ relocCtrlCache.RelocNullable(dieReadyQueueBackup->pingElem[i]);
986+ relocCtrlCache.RelocNullable(dieReadyQueueBackup->pongElem[i]);
987+ }
890 }988 }
891}989}
892 990 
@@ -911,6 +1009,8 @@ void DevControlFlowCache::RelocDuppedDataAndDynFuncData(RelocRange& relocProgram
911 relocProgram.RelocNullable(dynData->cceBinaryIndexList);1009 relocProgram.RelocNullable(dynData->cceBinaryIndexList);
912 1010 
913 relocCtrlCache.Reloc(dynDataCache->predCount);1011 relocCtrlCache.Reloc(dynDataCache->predCount);
1012+ relocCtrlCache.RelocNullable(dynDataCache->predCountPing);
1013+ relocCtrlCache.RelocNullable(dynDataCache->predCountPong);
914 relocCtrlCache.RelocNullable(dynDataBackup->predCountBackup);1014 relocCtrlCache.RelocNullable(dynDataBackup->predCountBackup);
915 relocCtrlCache.RelocNullable(dynDataBackup->rawTensorAddrBackup);1015 relocCtrlCache.RelocNullable(dynDataBackup->rawTensorAddrBackup);
916 relocCtrlCache.RelocNullable(dynDataBackup->deadEndHubBitmapBackup);1016 relocCtrlCache.RelocNullable(dynDataBackup->deadEndHubBitmapBackup);
@@ -951,6 +1051,10 @@ void DevControlFlowCache::TaskAddrRelocProgramAndCtrlCache(uint64_t srcProgram,
951 for (size_t i = 0; i < READY_QUEUE_SIZE; i++) {1051 for (size_t i = 0; i < READY_QUEUE_SIZE; i++) {
952 readyQueueBackup->queueList[i].Reloc(relocCtrlCache);1052 readyQueueBackup->queueList[i].Reloc(relocCtrlCache);
953 }1053 }
1054+ for (size_t i = 0; i < READY_QUEUE_SIZE; i++) {
1055+ relocCtrlCache.RelocNullable(readyQueueBackup->pingElem[i]);
1056+ relocCtrlCache.RelocNullable(readyQueueBackup->pongElem[i]);
1057+ }
954 1058 
955 for (size_t i = 0; i < npu::tile_fwk::DRCO_QUEUE_MAX; i++) {1059 for (size_t i = 0; i < npu::tile_fwk::DRCO_QUEUE_MAX; i++) {
956 if (readyQueueBackup->globalReadyQueueList[i].ptr != nullptr) {1060 if (readyQueueBackup->globalReadyQueueList[i].ptr != nullptr) {
@@ -42,6 +42,9 @@ struct ReadyQueueCache {
42 ReadyCoreFunctionQueueUnsafe queueList[READY_QUEUE_SIZE];42 ReadyCoreFunctionQueueUnsafe queueList[READY_QUEUE_SIZE];
43 uint32_t readyTaskNum;43 uint32_t readyTaskNum;
44 44 
45+ uint32_t* pingElem[READY_QUEUE_SIZE];
46+ uint32_t* pongElem[READY_QUEUE_SIZE];
47+ 
45 npu::tile_fwk::DrcoGlobalReadyQueuePtr globalReadyQueueList[npu::tile_fwk::DRCO_QUEUE_MAX];48 npu::tile_fwk::DrcoGlobalReadyQueuePtr globalReadyQueueList[npu::tile_fwk::DRCO_QUEUE_MAX];
46 npu::tile_fwk::PerCorePendingQueue* perCorePendingQueueList[npu::tile_fwk::MAX_AICORE_NUM_FOR_QUEUE]{};49 npu::tile_fwk::PerCorePendingQueue* perCorePendingQueueList[npu::tile_fwk::MAX_AICORE_NUM_FOR_QUEUE]{};
47};50};
@@ -50,6 +53,10 @@ struct DieReadyQueueCache {
50 uint32_t coreFunctionCnt;53 uint32_t coreFunctionCnt;
51 ReadyCoreFunctionQueueUnsafe queueList[DIE_READY_QUEUE_SIZE * DIE_NUM];54 ReadyCoreFunctionQueueUnsafe queueList[DIE_READY_QUEUE_SIZE * DIE_NUM];
52 uint32_t readyTaskNum;55 uint32_t readyTaskNum;
56+ 
57+ /* Ping-pong element buffers, same protocol as ReadyQueueCache. */
58+ uint32_t* pingElem[DIE_READY_QUEUE_SIZE * DIE_NUM];
59+ uint32_t* pongElem[DIE_READY_QUEUE_SIZE * DIE_NUM];
53};60};
54 61 
55struct MixTaskDataCache {62struct MixTaskDataCache {
@@ -61,6 +68,8 @@ struct MixTaskDataCache {
61struct DynFuncDataCache {68struct DynFuncDataCache {
62 DevAscendFunction* devFunc;69 DevAscendFunction* devFunc;
63 predcount_t* predCount;70 predcount_t* predCount;
71+ predcount_t* predCountPing;
72+ predcount_t* predCountPong;
64 int* calleeList;73 int* calleeList;
65 DevAscendFunctionDuppedData* duppedData;74 DevAscendFunctionDuppedData* duppedData;
66 75 
@@ -242,10 +251,13 @@ struct DevControlFlowCache {
242 uint64_t contextWorkspaceAddr;251 uint64_t contextWorkspaceAddr;
243 /* Filled in caching */252 /* Filled in caching */
244 DevRelocVector<DeviceTaskCache> deviceTaskCacheList;253 DevRelocVector<DeviceTaskCache> deviceTaskCacheList;
254+ 
255+ bool bitmapRestoreDone{false};
245 /* Filled in caching */256 /* Filled in caching */
246 DevRelocVector<uint8_t> cacheData;257 DevRelocVector<uint8_t> cacheData;
247 258 
248 uint64_t workspaceAddr;259 uint64_t workspaceAddr;
260+ 
249#define ctrlFlowLastField cacheData261#define ctrlFlowLastField cacheData
250 uint64_t dataSize;262 uint64_t dataSize;
251 uint8_t data[0];263 uint8_t data[0];
@@ -354,7 +366,7 @@ struct DevControlFlowCache {
354 return true;366 return true;
355 }367 }
356 368 
357- inline bool IsActivatedPartialCache(DevStartArgsBase* startArgs) const369+ inline bool IsActivatedCache(DevStartArgsBase* startArgs) const
358 {370 {
359 if (!isActivated) {371 if (!isActivated) {
360 return false;372 return false;
@@ -405,17 +417,27 @@ struct DevControlFlowCache {
405 417 
406 void PredCountDataBackup(DynDeviceTaskBase* base);418 void PredCountDataBackup(DynDeviceTaskBase* base);
407 419 
408- void PredCountDataRestore(DynDeviceTaskBase* base);420+ void PredCountPingPongSwap(DynDeviceTaskBase* base);
421+ 
422+ void DrcoPredCountDataRestore(DynDeviceTaskBase* base);
423+ 
424+ void PredCountPingPongRestore(DynDeviceTaskBase* base);
425+ 
426+ void BitmapDataRestoreTask(DynDeviceTaskBase* base);
409 427 
410 void ReadyQueueDataBackup(DynDeviceTaskBase* base);428 void ReadyQueueDataBackup(DynDeviceTaskBase* base);
411 429 
412- void ReadyQueueDataRestore(DynDeviceTaskBase* base, uint32_t nrValidAic);430+ void ReadyQueueDataPingPongSwap(DynDeviceTaskBase* base, uint32_t nrValidAic);
431+ 
432+ void ReadyQueueDataPingPongRestore(DynDeviceTaskBase* base);
413 433 
414 void DrcoReadyQueueDataRestore(DynDeviceTaskBase* base, uint32_t nrValidAic);434 void DrcoReadyQueueDataRestore(DynDeviceTaskBase* base, uint32_t nrValidAic);
415 435 
416 void DieReadyQueueDataBackup(DynDeviceTaskBase* base);436 void DieReadyQueueDataBackup(DynDeviceTaskBase* base);
417 437 
418- void DieReadyQueueDataRestore(DynDeviceTaskBase* base, uint32_t nrValidAic);438+ void DieReadyQueuePingPongSwap(DynDeviceTaskBase* base, uint32_t nrValidAic);
439+ 
440+ void DieReadyQueuePingPongRestore(DynDeviceTaskBase* base);
419 441 
420 void MixTaskDataBackup(DynDeviceTaskBase* base);442 void MixTaskDataBackup(DynDeviceTaskBase* base);
421 443 
@@ -48,6 +48,16 @@ struct QueueGeneric {
48 48 
49 __attribute__((always_inline)) uint32_t Size() const { return tail_ - head_; }49 __attribute__((always_inline)) uint32_t Size() const { return tail_ - head_; }
50 50 
51+ __attribute__((always_inline)) T* Data() const { return elem_; }
52+ 
53+ void FillFrom(const QueueGeneric& rhs, T* elem)
54+ {
55+ ASSERT(SchedErr::READY_QUEUE_OVERFLOW, rhs.Size() <= Capacity());
56+ head_ = rhs.head_;
57+ tail_ = rhs.tail_;
58+ elem_ = elem;
59+ }
60+ 
51 std::string Str() const61 std::string Str() const
52 {62 {
53 std::stringstream ss;63 std::stringstream ss;
@@ -132,6 +132,8 @@ TEST(CtrlFlowCacheDrcoUt, PredCountDataRestore_CoversDrcoPredCount)
132 std::array<uint8_t, 1024> dupBuf{};132 std::array<uint8_t, 1024> dupBuf{};
133 DynFuncDataCache& cache = dyntask->dynFuncDataCacheList[0];133 DynFuncDataCache& cache = dyntask->dynFuncDataCacheList[0];
134 cache.duppedData = SetupDuppedData(dupBuf, 1);134 cache.duppedData = SetupDuppedData(dupBuf, 1);
135+ cache.predCountPing = nullptr;
136+ cache.predCountPong = nullptr;
135 cache.predCount = nullptr;137 cache.predCount = nullptr;
136 cache.calleeList = nullptr;138 cache.calleeList = nullptr;
137 cache.devFunc = nullptr;139 cache.devFunc = nullptr;
@@ -147,10 +149,191 @@ TEST(CtrlFlowCacheDrcoUt, PredCountDataRestore_CoversDrcoPredCount)
147 DevControlFlowCache ctrl;149 DevControlFlowCache ctrl;
148 SetupCtrlCache(ctrl, cacheBuf);150 SetupCtrlCache(ctrl, cacheBuf);
149 151 
150- ctrl.PredCountDataRestore(dyntask.get());152+ ctrl.PredCountPingPongSwap(dyntask.get());
153+ ctrl.DrcoPredCountDataRestore(dyntask.get());
154+ ctrl.BitmapDataRestoreTask(dyntask.get());
151 SUCCEED();155 SUCCEED();
152}156}
153 157 
158+TEST(CtrlFlowCacheDrcoUt, PredCountPingPongRoundTrip)
159+{
160+ DeviceWorkspaceAllocator workspace;
161+ auto dyntask = std::make_unique<DynDeviceTask>(workspace);
162+ 
163+ std::array<uint8_t, sizeof(DynFuncHeader) + 8 * sizeof(DynFuncData)> hdrBuf{};
164+ SetupDynFuncHeader(dyntask.get(), hdrBuf, 2);
165+ 
166+ std::array<std::array<uint8_t, 1024>, 2> dupBuf{};
167+ std::array<DevAscendFunctionDuppedData*, 2> dupped{};
168+ /* 16KB on the stack exceeds the -Wframe-larger-than budget; scratch does not need stack storage. */
169+ static std::array<std::array<uint8_t, 8192>, 2> devFuncBuf{};
170+ const uint32_t opCounts[2] = {3, 4};
171+ const predcount_t initVals[2][4] = {{3, 1, 4, 0}, {5, 9, 2, 6}};
172+ for (int i = 0; i < 2; ++i) {
173+ dupped[i] = SetupDuppedData(dupBuf[i], opCounts[i]);
174+ DynFuncDataCache& cache = dyntask->dynFuncDataCacheList[i];
175+ cache.duppedData = dupped[i];
176+ cache.predCountPing = dupped[i]->GetOperationPredCountPingPong(PRED_COUNT_PING);
177+ cache.predCountPong = dupped[i]->GetOperationPredCountPingPong(PRED_COUNT_PONG);
178+ cache.predCount = cache.predCountPing;
179+ cache.devFunc = reinterpret_cast<DevAscendFunction*>(devFuncBuf[i].data());
180+ cache.calleeList = nullptr;
181+ dyntask->dynFuncDataBackupList[i] = DynFuncDataBackup{};
182+ for (uint32_t j = 0; j < opCounts[i]; ++j) {
183+ dupped[i]->GetOperationCurrPredCount(j) = initVals[i][j];
184+ }
185+ }
186+ 
187+ std::vector<uint8_t> cacheBuf;
188+ DevControlFlowCache ctrl;
189+ SetupCtrlCache(ctrl, cacheBuf);
190+ 
191+ ctrl.PredCountDataBackup(dyntask.get());
192+ 
193+ // Record: both slices carry the initial snapshot; predCount aliases slice 0.
194+ for (int i = 0; i < 2; ++i) {
195+ for (uint32_t j = 0; j < opCounts[i]; ++j) {
196+ EXPECT_EQ(dyntask->dynFuncDataCacheList[i].predCountPing[j], initVals[i][j]);
197+ EXPECT_EQ(dyntask->dynFuncDataCacheList[i].predCountPong[j], initVals[i][j]);
198+ }
199+ }
200+ 
201+ // Simulate back-to-back launches with the shadow-restore protocol: pre-push only swaps the
202+ // slice pointers onto the clean copy (no memcpy - it was refreshed by the previous launch's
203+ // shadow), sche dirties it during execution, and the post-push shadow refreshes the OTHER
204+ // slice for the next launch. Every launch must consume initial values.
205+ auto runLaunch = [&]() {
206+ ctrl.PredCountPingPongSwap(dyntask.get());
207+ for (int i = 0; i < 2; ++i) {
208+ EXPECT_EQ(dyntask->dynFuncDataCacheList[i].predCount, dyntask->dynFuncDataCacheList[i].predCountPing);
209+ for (uint32_t j = 0; j < opCounts[i]; ++j) {
210+ EXPECT_EQ(dyntask->dynFuncDataCacheList[i].predCount[j], initVals[i][j]);
211+ }
212+ }
213+ for (int i = 0; i < 2; ++i) {
214+ for (uint32_t j = 0; j < opCounts[i]; ++j) {
215+ dyntask->dynFuncDataCacheList[i].predCount[j] -= 1;
216+ }
217+ }
218+ ctrl.PredCountPingPongRestore(dyntask.get());
219+ for (int i = 0; i < 2; ++i) {
220+ for (uint32_t j = 0; j < opCounts[i]; ++j) {
221+ EXPECT_EQ(dyntask->dynFuncDataCacheList[i].predCountPong[j], initVals[i][j]);
222+ }
223+ }
224+ };
225+ 
226+ runLaunch(); // consumes slice 1
227+ runLaunch(); // consumes slice 0 (cleans record-time dirt)
228+ runLaunch(); // consumes slice 1 again (previous dirt cleaned)
229+ runLaunch(); // consumes slice 0 again
230+ 
231+ // The live pointers must never fall back to scattered state: they always alias slice[0].
232+ for (int i = 0; i < 2; ++i) {
233+ EXPECT_EQ(dyntask->dynFuncDataCacheList[i].predCount, dyntask->dynFuncDataCacheList[i].predCountPing);
234+ }
235+}
236+ 
237+TEST(CtrlFlowCacheDrcoUt, ReadyQueuePingPongRoundTrip)
238+{
239+ DeviceWorkspaceAllocator workspace;
240+ auto dyntask = std::make_unique<DynDeviceTask>(workspace);
241+ dyntask->devTask.coreFunctionCnt = 8;
242+ 
243+ std::array<std::array<uint32_t, 16>, READY_QUEUE_SIZE> elemBuf{};
244+ std::array<std::unique_ptr<ReadyCoreFunctionQueue>, READY_QUEUE_SIZE> queue{};
245+ SetupReadyQueues(dyntask.get(), elemBuf, queue);
246+ 
247+ const int aivIdx = DynDeviceTask::GetReadyQueueIndexByCoreType(CoreType::AIV);
248+ const int aicIdx = DynDeviceTask::GetReadyQueueIndexByCoreType(CoreType::AIC);
249+ std::array<std::array<uint32_t, 16>, READY_QUEUE_SIZE> initial{};
250+ std::array<uint32_t, READY_QUEUE_SIZE> initSize{};
251+ initSize[aivIdx] = 2;
252+ initSize[aicIdx] = 1;
253+ initial[aivIdx][0] = MakeTaskID(0, 1);
254+ initial[aivIdx][1] = MakeTaskID(0, 2);
255+ initial[aicIdx][0] = MakeTaskID(1, 1);
256+ for (size_t i = 0; i < READY_QUEUE_SIZE; ++i) {
257+ for (uint32_t j = 0; j < initSize[i]; ++j) {
258+ queue[i]->UnsafeEnqueue(initial[i][j]);
259+ }
260+ }
261+ 
262+ std::vector<uint8_t> cacheBuf;
263+ DevControlFlowCache ctrl;
264+ SetupCtrlCache(ctrl, cacheBuf);
265+ 
266+ ctrl.ReadyQueueDataBackup(dyntask.get());
267+ ASSERT_NE(dyntask->readyQueueBackup, nullptr);
268+ 
269+ // Record state: pingElem = the buffers the record launch will dirty; pongElem = clean
270+ // copies initialized from the snapshot for the first cached launch.
271+ std::array<uint32_t*, READY_QUEUE_SIZE> snapData{};
272+ for (size_t i = 0; i < READY_QUEUE_SIZE; ++i) {
273+ EXPECT_EQ(dyntask->readyQueueBackup->pingElem[i], elemBuf[i].data());
274+ EXPECT_NE(dyntask->readyQueueBackup->pongElem[i], elemBuf[i].data());
275+ snapData[i] = dyntask->readyQueueBackup->queueList[i].Data();
276+ for (uint32_t j = 0; j < initSize[i]; ++j) {
277+ EXPECT_EQ(dyntask->readyQueueBackup->pongElem[i][j], initial[i][j]);
278+ }
279+ }
280+ 
281+ // Simulate back-to-back launches with the shadow-restore protocol: restore rebinds elem_ to
282+ // the clean buffer (no memcpy), sche consumes from the head and LIFO-tail re-appends
283+ // (dirtying the buffer), the post-push shadow refreshes the OTHER buffer. Every launch must
284+ // consume the initial snapshot; the AICPU queue stays empty (Size 0) to cover that path.
285+ std::array<uint32_t*, READY_QUEUE_SIZE> lastData{};
286+ for (size_t i = 0; i < READY_QUEUE_SIZE; ++i) {
287+ lastData[i] = queue[i]->Data();
288+ }
289+ auto runLaunch = [&]() {
290+ ctrl.ReadyQueueDataPingPongSwap(dyntask.get(), 4);
291+ for (size_t i = 0; i < READY_QUEUE_SIZE; ++i) {
292+ EXPECT_NE(queue[i]->Data(), lastData[i]);
293+ lastData[i] = queue[i]->Data();
294+ 
295+ ASSERT_EQ(queue[i]->Size(), initSize[i]);
296+ uint32_t k = 0;
297+ for (const uint32_t* it = queue[i]->begin(); it != queue[i]->end(); ++it, ++k) {
298+ EXPECT_EQ(*it, initial[i][k]);
299+ }
300+ }
301+ // sche-side dirtying: head dequeue plus LIFO tail pull and re-append.
302+ for (size_t i = 0; i < READY_QUEUE_SIZE; ++i) {
303+ if (initSize[i] == 0) {
304+ continue;
305+ }
306+ uint32_t out[4];
307+ (void)queue[i]->Dequeue(1);
308+ (void)queue[i]->DequeueTail(1, out);
309+ queue[i]->UnsafeEnqueue(MakeTaskID(7, 7));
310+ }
311+ ctrl.ReadyQueueDataPingPongRestore(dyntask.get());
312+ // The buffer the NEXT launch consumes must be clean again.
313+ for (size_t i = 0; i < READY_QUEUE_SIZE; ++i) {
314+ const uint32_t* inactive = (queue[i]->Data() == dyntask->readyQueueBackup->pingElem[i]) ?
315+ dyntask->readyQueueBackup->pongElem[i] :
316+ dyntask->readyQueueBackup->pingElem[i];
317+ for (uint32_t j = 0; j < initSize[i]; ++j) {
318+ EXPECT_EQ(inactive[j], initial[i][j]);
319+ }
320+ }
321+ };
322+ 
323+ runLaunch(); // consumes pongElem (initialized at backup)
324+ runLaunch(); // consumes pingElem (cleaned by launch 1's shadow)
325+ runLaunch(); // consumes pongElem again (previous dirt cleaned)
326+ runLaunch(); // consumes pingElem again
327+ 
328+ // The snapshot buffers stay immutable across all rounds.
329+ for (size_t i = 0; i < READY_QUEUE_SIZE; ++i) {
330+ EXPECT_EQ(dyntask->readyQueueBackup->queueList[i].Data(), snapData[i]);
331+ for (uint32_t j = 0; j < initSize[i]; ++j) {
332+ EXPECT_EQ(snapData[i][j], initial[i][j]);
333+ }
334+ }
335+}
336+ 
154TEST(CtrlFlowCacheDrcoUt, ReadyQueueDataBackupRestore_WithDrco)337TEST(CtrlFlowCacheDrcoUt, ReadyQueueDataBackupRestore_WithDrco)
155{338{
156 DeviceWorkspaceAllocator workspace;339 DeviceWorkspaceAllocator workspace;
@@ -181,7 +364,7 @@ TEST(CtrlFlowCacheDrcoUt, ReadyQueueDataBackupRestore_WithDrco)
181 ctrl.ReadyQueueDataBackup(dyntask.get());364 ctrl.ReadyQueueDataBackup(dyntask.get());
182 ASSERT_NE(dyntask->readyQueueBackup, nullptr);365 ASSERT_NE(dyntask->readyQueueBackup, nullptr);
183 366 
184- ctrl.ReadyQueueDataRestore(dyntask.get(), 4);367+ ctrl.ReadyQueueDataPingPongSwap(dyntask.get(), 4);
185 SUCCEED();368 SUCCEED();
186 369 
187 EXPECT_EQ(dyntask->devTask.coreFunctionCnt, 8U);370 EXPECT_EQ(dyntask->devTask.coreFunctionCnt, 8U);
@@ -224,7 +407,7 @@ TEST(CtrlFlowCacheDrcoUt, DieReadyQueueDataBackupRestore_WithDrco)
224 ctrl.DieReadyQueueDataBackup(dyntask.get());407 ctrl.DieReadyQueueDataBackup(dyntask.get());
225 ASSERT_NE(dyntask->dieReadyQueueBackup, nullptr);408 ASSERT_NE(dyntask->dieReadyQueueBackup, nullptr);
226 409 
227- ctrl.DieReadyQueueDataRestore(dyntask.get(), 4);410+ ctrl.DieReadyQueuePingPongSwap(dyntask.get(), 4);
228 SUCCEED();411 SUCCEED();
229 412 
230 EXPECT_EQ(drco.root.perCorePendingQueueArray[0]->size, 1U);413 EXPECT_EQ(drco.root.perCorePendingQueueArray[0]->size, 1U);