* 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.h
* \brief Process-wide cache of per-bundle control-flow-cache DEVICE buffers.
*
* The packed base-0 control-flow cache is copied host->device once per bundle (keyed by LoadedBundle::bundleKey,
* a content digest -- NOT hashKey, which is equal across shape variants of one op) and reused across launches.
* Cached device buffers live for the whole process; DevMemoryPool releases them at teardown.
*/
#pragma once
#include <cstdint>
#include <mutex>
#include <unordered_map>
#include "machine/runtime/launcher/ctrl_flow_cache_manager.h"
namespace npu::tile_fwk::bundle {
class KernelBundleDevCache {
public:
static KernelBundleDevCache& Instance();
uint8_t* GetOrCopy(uint64_t key, const dynamic::CtrlFlowCacheBlob& hostCtrlCache);
uint8_t* GetOrAllocCellMatch(uint64_t key, uint64_t bytes);
KernelBundleDevCache(const KernelBundleDevCache&) = delete;
KernelBundleDevCache& operator=(const KernelBundleDevCache&) = delete;
private:
KernelBundleDevCache() = default;
~KernelBundleDevCache();
struct CellMatchBuf {
uint8_t* addr{nullptr};
uint64_t capacity{0};
};
std::mutex mutex_;
std::unordered_map<uint64_t, uint8_t*> cache_;
std::unordered_map<uint64_t, CellMatchBuf> cellMatchCache_;
};
}