/**
 * 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 std::vector<uint8_t>& hostCtrlCache)
{
    if (hostCtrlCache.empty()) {
        return nullptr; // launch path rebuilds the cache at runtime when absent
    }
    std::lock_guard<std::mutex> lk(mutex_);
    auto it = cache_.find(key);
    if (it != cache_.end()) {
        return it->second; // reuse the device buffer copied on a previous launch
    }
    // Huge-page path -> DevMemoryPool, so the destructor free stays consistent with the sentinel-checked teardown.
    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());
    return dev;
}

uint8_t* KernelBundleDevCache::GetOrAllocCellMatch(uint64_t key, uint64_t bytes)
{
    if (bytes == 0) {
        return nullptr; // op has no dynamic cell-match table for this shape
    }
    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; // reuse the buffer allocated on a previous launch
    }
    dynamic::DeviceMemoryUtils devMem(true);
    if (it != cellMatchCache_.end() && it->second.addr != nullptr) {
        devMem.Free(it->second.addr); // a later shape needs a bigger pool: release the smaller buffer first
    }
    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()
{
    // Release every cached device buffer at once. Runs only on a normal process exit; the STs use os._exit(0),
    // which skips static destructors (the OS reclaims device memory on process death).
    dynamic::DeviceMemoryUtils devMem(true);
    for (auto& kv : cache_) {
        devMem.Free(kv.second);
    }
    cache_.clear();
    for (auto& kv : cellMatchCache_) {
        devMem.Free(kv.second.addr);
    }
    cellMatchCache_.clear();
}

} // namespace npu::tile_fwk::bundle