* Copyright (c) 2025-2026 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
* \file kernel_bundle_dev_cache.cpp
* \brief Implementation of the per-bundle control-flow-cache device buffer singleton.
*/
#include "machine/runtime/bundle/kernel_bundle_dev_cache.h"
#include "machine/runtime/memory_utils/device_memory_utils.h"
#include "tilefwk/pypto_fwk_log.h"
namespace npu::tile_fwk::bundle {
KernelBundleDevCache& KernelBundleDevCache::Instance()
{
static KernelBundleDevCache instance;
return instance;
}
uint8_t* KernelBundleDevCache::GetOrCopy(uint64_t key, const dynamic::CtrlFlowCacheBlob& hostCtrlCache)
{
if (hostCtrlCache.empty()) {
return nullptr;
}
std::lock_guard<std::mutex> lk(mutex_);
auto it = cache_.find(key);
if (it != cache_.end()) {
return it->second;
}
dynamic::DeviceMemoryUtils devMem(true);
uint8_t* dev = devMem.CopyToDev(const_cast<uint8_t*>(hostCtrlCache.data()), hostCtrlCache.size(), nullptr);
cache_[key] = dev;
MACHINE_LOGI("[kernel-bundle] dev-cache: copied ctrl-flow cache key=%#lx size=%zuB (cached, reused hereafter)", key,
hostCtrlCache.size());
MACHINE_LOGI("#ctrl.cache.align: site=KernelBundleDevCache.GetOrCopy base=%p size=%zu align64=%lu %s",
static_cast<void*>(dev), hostCtrlCache.size(),
static_cast<unsigned long>(reinterpret_cast<uintptr_t>(dev) % npu::tile_fwk::DUPPED_STITCH_NODE_ALIGN),
reinterpret_cast<uintptr_t>(dev) % npu::tile_fwk::DUPPED_STITCH_NODE_ALIGN == 0 ? "OK" : "MISALIGNED");
return dev;
}
uint8_t* KernelBundleDevCache::GetOrAllocCellMatch(uint64_t key, uint64_t bytes)
{
if (bytes == 0) {
return nullptr;
}
std::lock_guard<std::mutex> lk(mutex_);
auto it = cellMatchCache_.find(key);
if (it != cellMatchCache_.end() && it->second.addr != nullptr && it->second.capacity >= bytes) {
return it->second.addr;
}
dynamic::DeviceMemoryUtils devMem(true);
if (it != cellMatchCache_.end() && it->second.addr != nullptr) {
devMem.Free(it->second.addr);
}
uint8_t* dev = devMem.AllocDev(static_cast<size_t>(bytes), nullptr);
cellMatchCache_[key] = CellMatchBuf{dev, bytes};
MACHINE_LOGI("[kernel-bundle] dev-cache: alloc cell-match pool key=%#lx size=%luB (cached, reused hereafter)", key,
static_cast<unsigned long>(bytes));
return dev;
}
KernelBundleDevCache::~KernelBundleDevCache()
{
cache_.clear();
cellMatchCache_.clear();
}
}