* 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_pack.h
* \brief Pack-side entry points for the kernel bundle (.pyptokb).
*
* Lives in tile_fwk_runtime, NOT in libtile_fwk_bundle.so: the pack hooks are called from the compile/emulation and
* launch paths inside the runtime, whereas the load/query/launch side is the standalone ABI library. Keeping the
* two halves in separate targets is what breaks the otherwise circular dependency between them.
*/
#pragma once
#include <cstddef>
#include <cstdint>
#include <mutex>
#include <unordered_set>
#include <vector>
namespace npu::tile_fwk {
struct DyndevFunctionAttribute;
namespace bundle {
std::vector<uint8_t> SerializeWorkspaceSymbols(const DyndevFunctionAttribute& dynAttr);
void MaybePackKernelBundle(const DyndevFunctionAttribute& dynAttr, const std::vector<uint8_t>& ctrlFlowCache);
class KernelBundlePackHook {
public:
static KernelBundlePackHook& Instance();
void StashCtrlFlowCache(const DyndevFunctionAttribute* dynAttr, const uint8_t* cache, size_t size);
void MaybePack(const DyndevFunctionAttribute* dynAttr);
KernelBundlePackHook(const KernelBundlePackHook&) = delete;
KernelBundlePackHook& operator=(const KernelBundlePackHook&) = delete;
private:
KernelBundlePackHook() = default;
~KernelBundlePackHook() = default;
std::mutex mutex_;
const DyndevFunctionAttribute* stashedFor_{nullptr};
std::vector<uint8_t> stashedCache_;
std::unordered_set<const DyndevFunctionAttribute*> packed_;
};
}
}