Merged
perf(machine): Host ctrl-flow / stitch hot-path opts for 9.2.0 #7305
perf(machine): Host ctrl-flow / stitch hot-path opts for 9.2.0 #7305
Merged
LordOfMysteriesAdd on 27 days ago
Total: 9 files changed+116-24
@@ -197,8 +197,10 @@ static uint32_t UpdateSlotsImpl(DeviceWorkspaceAllocator* workspace, DeviceExecu
197 slot.stitchOutcastIdx = static_cast<uint32_t>(entry.outcastIdx);197 slot.stitchOutcastIdx = static_cast<uint32_t>(entry.outcastIdx);
198 DEV_IF_NONDEVICE198 DEV_IF_NONDEVICE
199 {199 {
200- topo_dump::DumpProducerCellAccess(devTaskId, entry.slotIdx, devNextIdx, *devRootSrc, outcast, slot,200+ if (unlikely(topo_dump::DumpEnabled())) {
201- expressionList);201+ topo_dump::DumpProducerCellAccess(devTaskId, entry.slotIdx, devNextIdx, *devRootSrc, outcast, slot,
202+ expressionList);
203+ }
202 }204 }
203 uint32_t cellMatchTagId = CellMatchBuildTagId(slot.slotAllocIterId, cellMatchTagSeq);205 uint32_t cellMatchTagId = CellMatchBuildTagId(slot.slotAllocIterId, cellMatchTagSeq);
204 uint32_t errCode = 0;206 uint32_t errCode = 0;
@@ -114,9 +114,11 @@ void PartialUpdateStitchOneConsumerList(DevAscendFunctionDupped& nextDup, DevAsc
114 }114 }
115 DEV_IF_NONDEVICE115 DEV_IF_NONDEVICE
116 {116 {
117- topo_dump::DumpConsumerCellAccess(static_cast<uint32_t>(devTaskId), slotIdx,117+ if (unlikely(topo_dump::DumpEnabled())) {
118- static_cast<uint32_t>(devNextIdx), *nextSrc, consumer, cellMatchTableDesc,118+ topo_dump::DumpConsumerCellAccess(static_cast<uint32_t>(devTaskId), slotIdx,
119- expressionList);119+ static_cast<uint32_t>(devNextIdx), *nextSrc, consumer,
120+ cellMatchTableDesc, expressionList);
121+ }
120 }122 }
121 123 
122 CellMatchStitchEnhance(consumerOffset, consumerValidShape, cellMatchTableDesc,124 CellMatchStitchEnhance(consumerOffset, consumerValidShape, cellMatchTableDesc,
@@ -148,6 +150,9 @@ void DeviceStitchContext::CheckStitch(DevAscendFunctionDupped* stitchedList, int
148{150{
149 DEV_IF_NONDEVICE151 DEV_IF_NONDEVICE
150 {152 {
153+ if (likely(!topo_dump::DumpEnabled())) {
154+ return;
155+ }
151 uint32_t dynPredCount = 0;156 uint32_t dynPredCount = 0;
152 uint32_t dynSuccCount = 0;157 uint32_t dynSuccCount = 0;
153 for (int k = 0; k <= size; k++) {158 for (int k = 0; k <= size; k++) {
@@ -322,6 +327,9 @@ static void ValidateAndDumpStitchEdge(const DevAscendFunctionDupped& producerDup
322 size_t consumerIdx, size_t consumerOperationIdx,327 size_t consumerIdx, size_t consumerOperationIdx,
323 DeviceStitchContext::StitchKind debugStitchKind, int debugSlotIdx)328 DeviceStitchContext::StitchKind debugStitchKind, int debugSlotIdx)
324{329{
330+ if (likely(!topo_dump::DumpEnabled())) {
331+ return;
332+ }
325 if (producerOperationIdx >= producerDup.GetSource()->GetOperationSize()) {333 if (producerOperationIdx >= producerDup.GetSource()->GetOperationSize()) {
326 DEV_ERROR(ProgEncodeErr::STITCH_HANDLE_INDEX_OUT_OF_RANGE,334 DEV_ERROR(ProgEncodeErr::STITCH_HANDLE_INDEX_OUT_OF_RANGE,
327 "#ctrl.task.pre.stitch.handle: producerOperationIdx %zu exceeds the size of GetOperation %zu",335 "#ctrl.task.pre.stitch.handle: producerOperationIdx %zu exceeds the size of GetOperation %zu",
@@ -377,8 +385,10 @@ void DeviceStitchContext::HandleOneStitch(DevAscendFunctionDupped& producerDup,
377 385 
378 DEV_IF_NONDEVICE386 DEV_IF_NONDEVICE
379 {387 {
380- ValidateAndDumpStitchEdge(producerDup, consumerDup, producerOperationIdx, consumerIdx, consumerOperationIdx,388+ if (unlikely(topo_dump::DumpEnabled())) {
381- debugStitchKind, debugSlotIdx);389+ ValidateAndDumpStitchEdge(producerDup, consumerDup, producerOperationIdx, consumerIdx, consumerOperationIdx,
390+ debugStitchKind, debugSlotIdx);
391+ }
382 }392 }
383}393}
384 394 
@@ -454,9 +464,11 @@ uint64_t DeviceStitchContext::FullCoverDefaultUpdateStitch(DevAscendFunctionDupp
454 consumer.operationIdx);464 consumer.operationIdx);
455 DEV_IF_NONDEVICE465 DEV_IF_NONDEVICE
456 {466 {
457- topo_dump::DumpConsumerCellAccess(static_cast<uint32_t>(devTaskId), slotIdx,467+ if (unlikely(topo_dump::DumpEnabled())) {
458- static_cast<uint32_t>(devNextIdx), *nextSrc, consumer, cellMatchTableDesc,468+ topo_dump::DumpConsumerCellAccess(static_cast<uint32_t>(devTaskId), slotIdx,
459- expressionList);469+ static_cast<uint32_t>(devNextIdx), *nextSrc, consumer,
470+ cellMatchTableDesc, expressionList);
471+ }
460 }472 }
461 CellMatchHandle<HandleCellMatchFull>(fullCoverOffset, fullCoverValidShape, cellMatchTableDesc,473 CellMatchHandle<HandleCellMatchFull>(fullCoverOffset, fullCoverValidShape, cellMatchTableDesc,
462 fullUpdateTableData, &matchCount, &prevDup, &nextDup, devNextIdx,474 fullUpdateTableData, &matchCount, &prevDup, &nextDup, devNextIdx,
@@ -27,6 +27,13 @@
27namespace npu::tile_fwk::dynamic::topo_dump {27namespace npu::tile_fwk::dynamic::topo_dump {
28 28 
29#ifndef __DEVICE__29#ifndef __DEVICE__
30+ 
31+bool DumpEnabled()
32+{
33+ static const bool cached = (config::GetDebugOption<int64_t>(CFG_RUNTIME_DBEUG_MODE) == CFG_RUNTIME_DEBUG_VERIFY);
34+ return cached;
35+}
36+ 
30namespace {37namespace {
31 38 
32void OpenIfPathChanged(std::ofstream& ofs, std::string& lastPath, const std::string& path, const char* header)39void OpenIfPathChanged(std::ofstream& ofs, std::string& lastPath, const std::string& path, const char* header)
@@ -44,12 +51,6 @@ void OpenIfPathChanged(std::ofstream& ofs, std::string& lastPath, const std::str
44 }51 }
45}52}
46 53 
47-inline bool DumpEnabled()
48-{
49- static const bool cached = (config::GetDebugOption<int64_t>(CFG_RUNTIME_DBEUG_MODE) == CFG_RUNTIME_DEBUG_VERIFY);
50- return cached;
51-}
52- 
53struct CellMatchHandleCollect {54struct CellMatchHandleCollect {
54 static inline uint32_t Process(int index, const DevCellMatchTableDesc& desc, std::vector<int>* cellIdxListOut)55 static inline uint32_t Process(int index, const DevCellMatchTableDesc& desc, std::vector<int>* cellIdxListOut)
55 {56 {
@@ -167,6 +168,8 @@ void DumpStitchEdge(const DevAscendFunctionDupped& producerDup, const DevAscendF
167 168 
168#else // __DEVICE__169#else // __DEVICE__
169 170 
171+bool DumpEnabled() { return false; }
172+ 
170void DumpProducerCellAccess(uint32_t, int, uint32_t, DevAscendFunction&, DevAscendFunctionOutcast&,173void DumpProducerCellAccess(uint32_t, int, uint32_t, DevAscendFunction&, DevAscendFunctionOutcast&,
171 const DeviceExecuteSlot&, const uint64_t*)174 const DeviceExecuteSlot&, const uint64_t*)
172{}175{}
@@ -23,6 +23,10 @@
23 23 
24namespace npu::tile_fwk::dynamic::topo_dump {24namespace npu::tile_fwk::dynamic::topo_dump {
25 25 
26+/// Host-only stitch verify/dump gate. True when runtime_debug_mode == VERIFY.
27+/// Default false so stitch hot path skips CheckStitch / topo dump without a rebuild.
28+bool DumpEnabled();
29+ 
26void DumpProducerCellAccess(uint32_t devTaskId, int slotIdx, uint32_t devNextIdx, DevAscendFunction& devRootSrc,30void DumpProducerCellAccess(uint32_t devTaskId, int slotIdx, uint32_t devNextIdx, DevAscendFunction& devRootSrc,
27 DevAscendFunctionOutcast& outcast, const DeviceExecuteSlot& slot,31 DevAscendFunctionOutcast& outcast, const DeviceExecuteSlot& slot,
28 const uint64_t* expressionList);32 const uint64_t* expressionList);
@@ -164,11 +164,17 @@ DevControlFlowCache* EmulationLauncher::CreateHostCtrlFlowCache(DevAscendProgram
164 uintdevptr_t initOffset = reinterpret_cast<uintdevptr_t>(encodeCtrlCache.data);164 uintdevptr_t initOffset = reinterpret_cast<uintdevptr_t>(encodeCtrlCache.data);
165 InitHostCtrlFlowCacheLayout(encodeCtrlCache, devProg, dyndevAttr, initOffset);165 InitHostCtrlFlowCacheLayout(encodeCtrlCache, devProg, dyndevAttr, initOffset);
166 uint32_t ctrlCacheAllocSize = encodeCtrlCache.GetSize();166 uint32_t ctrlCacheAllocSize = encodeCtrlCache.GetSize();
167- DevControlFlowCache* hostCtrlFlowCache = reinterpret_cast<DevControlFlowCache*>(167+ // cacheData 是容量上限(默认 100MB),录制只写 used 部分。只清 cacheData 之前的元数据前缀;
168- memUtils.AllocZero(ctrlCacheAllocSize, nullptr));168+ // payload 在 AllocateCache 按块清零,避免整块 memset。
169- if (hostCtrlFlowCache == nullptr) {169+ // 9.2.0 EmulationMemoryUtils::AllocDev 无 align 参数,此处不做 64B aligned_alloc。
170+ uint8_t* rawCache = memUtils.AllocDev(ctrlCacheAllocSize, nullptr);
171+ if (rawCache == nullptr) {
170 return nullptr;172 return nullptr;
171 }173 }
174+ const uint64_t headerBytes = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(encodeCtrlCache.cacheData.Data()) -
175+ reinterpret_cast<uintptr_t>(&encodeCtrlCache));
176+ (void)memset_s(rawCache, headerBytes, 0, headerBytes);
177+ DevControlFlowCache* hostCtrlFlowCache = reinterpret_cast<DevControlFlowCache*>(rawCache);
172 hostCtrlFlowCache->allCacheSize = ctrlCacheAllocSize;178 hostCtrlFlowCache->allCacheSize = ctrlCacheAllocSize;
173 initOffset = reinterpret_cast<uintdevptr_t>(hostCtrlFlowCache->data);179 initOffset = reinterpret_cast<uintdevptr_t>(hostCtrlFlowCache->data);
174 InitHostCtrlFlowCacheLayout(*hostCtrlFlowCache, devProg, dyndevAttr, initOffset);180 InitHostCtrlFlowCacheLayout(*hostCtrlFlowCache, devProg, dyndevAttr, initOffset);
@@ -208,9 +208,18 @@ inline void DeviceLogSplitDebug(const char* func, const char* format, Args... ar
208#define DEV_IF_DEBUG if (true)208#define DEV_IF_DEBUG if (true)
209#define DEV_IF_INFO if (true)209#define DEV_IF_INFO if (true)
210 210 
211-#define DEV_VERBOSE_DEBUG_SPLIT(fmt, args...) PYPTO_SIM_LOG(DLOG_DEBUG, MACHINE, fmt, ##args)211+#define DEV_VERBOSE_DEBUG_SPLIT(fmt, args...) \
212-#define DEV_VERBOSE_DEBUG(fmt, args...) PYPTO_SIM_LOG(DLOG_DEBUG, MACHINE, fmt, ##args)212+ do { \
213-#define DEV_VERBOSE_INFO(fmt, args...) PYPTO_SIM_LOG(DLOG_INFO, MACHINE, fmt, ##args)213+ DEV_IF_VERBOSE_LOG { PYPTO_SIM_LOG(DLOG_DEBUG, MACHINE, fmt, ##args); } \
214+ } while (0)
215+#define DEV_VERBOSE_DEBUG(fmt, args...) \
216+ do { \
217+ DEV_IF_VERBOSE_LOG { PYPTO_SIM_LOG(DLOG_DEBUG, MACHINE, fmt, ##args); } \
218+ } while (0)
219+#define DEV_VERBOSE_INFO(fmt, args...) \
220+ do { \
221+ DEV_IF_VERBOSE_LOG { PYPTO_SIM_LOG(DLOG_INFO, MACHINE, fmt, ##args); } \
222+ } while (0)
214#define DEV_DEBUG_SPLIT(fmt, args...) PYPTO_SIM_LOG(DLOG_DEBUG, MACHINE, fmt, ##args)223#define DEV_DEBUG_SPLIT(fmt, args...) PYPTO_SIM_LOG(DLOG_DEBUG, MACHINE, fmt, ##args)
215#define DEV_DEBUG(fmt, args...) PYPTO_SIM_LOG(DLOG_DEBUG, MACHINE, fmt, ##args)224#define DEV_DEBUG(fmt, args...) PYPTO_SIM_LOG(DLOG_DEBUG, MACHINE, fmt, ##args)
216#define DEV_INFO(fmt, args...) PYPTO_SIM_LOG(DLOG_INFO, MACHINE, fmt, ##args)225#define DEV_INFO(fmt, args...) PYPTO_SIM_LOG(DLOG_INFO, MACHINE, fmt, ##args)
@@ -17,6 +17,7 @@
17 17 
18#include <cinttypes>18#include <cinttypes>
19#include <type_traits>19#include <type_traits>
20+#include "securec.h"
20#include "machine/utils/dynamic/dev_encode_types.h"21#include "machine/utils/dynamic/dev_encode_types.h"
21#include "machine/utils/dynamic/dev_encode_function.h"22#include "machine/utils/dynamic/dev_encode_function.h"
22#include "machine/utils/dynamic/dev_encode_function_dupped_data.h"23#include "machine/utils/dynamic/dev_encode_function_dupped_data.h"
@@ -293,8 +294,12 @@ struct DevControlFlowCache {
293 void* result = nullptr;294 void* result = nullptr;
294 if (cacheDataOffset + size < cacheData.size()) {295 if (cacheDataOffset + size < cacheData.size()) {
295 result = &cacheData[cacheDataOffset];296 result = &cacheData[cacheDataOffset];
296- /* make cache 8 byte aligned */297+ /* make cache CFGCACHE_ALIGN aligned */
297- cacheDataOffset += (size + CFGCACHE_ALIGN - 1) / CFGCACHE_ALIGN * CFGCACHE_ALIGN;298+ const uint64_t reserved = (size + CFGCACHE_ALIGN - 1) / CFGCACHE_ALIGN * CFGCACHE_ALIGN;
299+ // Payload is not pre-zeroed. Callers leave holes null and expect a zero slab.
300+ const uint64_t zeroBytes = (cacheDataOffset + reserved <= cacheData.size()) ? reserved : size;
301+ (void)memset_s(result, zeroBytes, 0, zeroBytes);
302+ cacheDataOffset += reserved;
298 DEV_VERBOSE_DEBUG("cacheDataOffset is: %lu", cacheDataOffset);303 DEV_VERBOSE_DEBUG("cacheDataOffset is: %lu", cacheDataOffset);
299 } else {304 } else {
300 DEV_DEBUG("[ctrl.cache.record] Recording is stopped, requestSize=%lu, "305 DEV_DEBUG("[ctrl.cache.record] Recording is stopped, requestSize=%lu, "
@@ -726,3 +726,23 @@ TEST(CtrlFlowCacheRelocUt, RelocIncastOutcastTask_NoTable_FallsBackToStructural)
726 EXPECT_EQ(duppedData->GetOutcastAddress(1).GetAddressValue(), RelocTableFixture::kCommAddr);726 EXPECT_EQ(duppedData->GetOutcastAddress(1).GetAddressValue(), RelocTableFixture::kCommAddr);
727 EXPECT_EQ(dyntask->dynFuncDataList->startArgs, &args);727 EXPECT_EQ(dyntask->dynFuncDataList->startArgs, &args);
728}728}
729+ 
730+// Payload is no longer pre-zeroed by CreateHostCtrlFlowCache; AllocateCache must clear each slab
731+// (including 64B alignment padding) so holes stay null for ready-queue / mix-task backup.
732+TEST(CtrlFlowCacheAllocateUt, AllocateCache_ZerosPoisonedSlabAndPadding)
733+{
734+ DevControlFlowCache ctrl;
735+ std::vector<uint8_t> cacheBuf(4096, 0xAB);
736+ ctrl.cacheData = DevRelocVector<uint8_t>(static_cast<int>(cacheBuf.size()), cacheBuf.data());
737+ ctrl.cacheDataOffset = 0;
738+ 
739+ constexpr uint64_t kReq = 40; // not 64-aligned → reserved padding must also be cleared
740+ void* p = ctrl.AllocateCache(kReq);
741+ ASSERT_NE(p, nullptr);
742+ const uint64_t reserved = (kReq + CFGCACHE_ALIGN - 1) / CFGCACHE_ALIGN * CFGCACHE_ALIGN;
743+ auto* bytes = static_cast<uint8_t*>(p);
744+ for (uint64_t i = 0; i < reserved; ++i) {
745+ EXPECT_EQ(bytes[i], 0) << "offset " << i;
746+ }
747+ EXPECT_EQ(ctrl.cacheDataOffset, reserved);
748+}
@@ -17,7 +17,9 @@
17 */17 */
18 18 
19#include <gtest/gtest.h>19#include <gtest/gtest.h>
20+#include <cstring>
20#include "test_machine_common.h"21#include "test_machine_common.h"
22+#include "machine/utils/dynamic/dev_encode_program_ctrlflow_cache.h"
21 23 
22struct EmulationLauncherTest : UnitTestBase {24struct EmulationLauncherTest : UnitTestBase {
23protected:25protected:
@@ -131,6 +133,35 @@ TEST_F(EmulationLauncherTest, CreateHostCtrlFlowCache_DirectCall)
131 EXPECT_NE(cache, nullptr);133 EXPECT_NE(cache, nullptr);
132}134}
133 135 
136+// CreateHostCtrlFlowCache only zeros the metadata prefix; AllocateCache must still return a zero slab
137+// even if the unused payload was poisoned (simulates AllocDev without full memset).
138+TEST_F(EmulationLauncherTest, CreateHostCtrlFlowCache_AllocateCacheZerosPoisonedPayload)
139+{
140+ auto* func = GetFunc();
141+ ASSERT_NE(func, nullptr);
142+ 
143+ EmulationMemoryUtils memUtils;
144+ DevAscendProgram* devProg = DeviceLauncher::GetDevProg(func);
145+ ASSERT_NE(devProg, nullptr);
146+ 
147+ DevControlFlowCache* cache = EmulationLauncher::CreateHostCtrlFlowCache(devProg, func, memUtils);
148+ ASSERT_NE(cache, nullptr);
149+ EXPECT_EQ(cache->deviceTaskCount, 0U);
150+ EXPECT_FALSE(cache->isRecording);
151+ EXPECT_EQ(cache->cacheDataOffset, 0U);
152+ ASSERT_GE(cache->cacheData.size(), 256U);
153+ 
154+ std::memset(cache->cacheData.Data(), 0xCD, 256);
155+ constexpr uint64_t kReq = 48;
156+ void* slab = cache->AllocateCache(kReq);
157+ ASSERT_NE(slab, nullptr);
158+ const uint64_t reserved = (kReq + CFGCACHE_ALIGN - 1) / CFGCACHE_ALIGN * CFGCACHE_ALIGN;
159+ auto* bytes = static_cast<uint8_t*>(slab);
160+ for (uint64_t i = 0; i < reserved; ++i) {
161+ EXPECT_EQ(bytes[i], 0) << "offset " << i;
162+ }
163+}
164+ 
134// Covers: EmulationBuildControlFlowCache standalone with real IO data165// Covers: EmulationBuildControlFlowCache standalone with real IO data
135TEST_F(EmulationLauncherTest, EmulationBuildControlFlowCache_WithRealIO)166TEST_F(EmulationLauncherTest, EmulationBuildControlFlowCache_WithRealIO)
136{167{