Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "ccu_rep_reference_manager.h"
12 : #include "exception_util.h"
13 : #include "ccu_api_exception.h"
14 :
15 : namespace Hccl {
16 : namespace CcuRep {
17 :
18 64 : CcuRepReferenceManager::CcuRepReferenceManager(uint8_t deiId) : dieId(deiId)
19 : {
20 64 : funcInVar.resize(FUNC_IN_MAX);
21 64 : funcOutVar.resize(FUNC_OUT_MAX);
22 64 : funcCallVar.resize(
23 : 1 + FUNC_NEST_MAX
24 : + 1); // FUNC_NEST_MAX个xn存放返回地址,1个xn存放block的起始地址和1个xn存放函数地址调用时返回地址
25 64 : }
26 :
27 49 : CcuResReq CcuRepReferenceManager::GetResReq(uint8_t reqDieId)
28 : {
29 49 : CcuResReq resReq;
30 49 : resReq.xnReq[reqDieId] = FUNC_IN_MAX + FUNC_OUT_MAX + 1 + FUNC_NEST_MAX + 1;
31 49 : return resReq;
32 : }
33 :
34 53 : void CcuRepReferenceManager::GetRes(CcuRepResource& res)
35 : {
36 53 : res.variable[dieId].insert(res.variable[dieId].end(), funcInVar.begin(), funcInVar.end());
37 53 : res.variable[dieId].insert(res.variable[dieId].end(), funcOutVar.begin(), funcOutVar.end());
38 53 : res.variable[dieId].insert(res.variable[dieId].end(), funcCallVar.begin(), funcCallVar.end());
39 53 : }
40 :
41 24 : bool CcuRepReferenceManager::CheckValid(const std::string& label)
42 : {
43 24 : return (referenceMap.find(label) != referenceMap.end());
44 : }
45 :
46 13 : bool CcuRepReferenceManager::CheckUnique(const std::string& label)
47 : {
48 13 : return (referenceMap.find(label) == referenceMap.end());
49 : }
50 :
51 24 : std::shared_ptr<CcuRepBlock> CcuRepReferenceManager::GetRefBlock(const std::string& label)
52 : {
53 24 : if (!CheckValid(label)) {
54 1 : THROW<CcuApiException>("Invalid Reference: %s", label.c_str());
55 : }
56 23 : return referenceMap[label];
57 : }
58 :
59 13 : void CcuRepReferenceManager::SetRefBlock(const std::string& label, std::shared_ptr<CcuRepBlock> refBlock)
60 : {
61 13 : if (!CheckUnique(label)) {
62 1 : THROW<CcuApiException>("Duplicate Definition: %s", label.c_str());
63 : }
64 12 : referenceMap[label] = refBlock;
65 12 : }
66 :
67 0 : uint16_t CcuRepReferenceManager::GetFuncAddr(const std::string& label)
68 : {
69 0 : if (!CheckValid(label)) {
70 0 : THROW<CcuApiException>("Invalid Reference: %s", label.c_str());
71 : }
72 :
73 0 : if (referenceMap[label]->Type() != CcuRepType::FUNC_BLOCK) {
74 0 : THROW<CcuApiException>("Invalid Type, %s Must be FuncBlock", label.c_str());
75 : }
76 :
77 0 : return referenceMap[label]->StartInstrId();
78 : }
79 :
80 8 : const Variable& CcuRepReferenceManager::GetFuncCall() { return funcCallVar[0]; }
81 :
82 6 : const Variable& CcuRepReferenceManager::GetFuncRet(uint16_t callLayer)
83 : {
84 6 : if (callLayer > FUNC_NEST_MAX) {
85 0 : THROW<CcuApiException>("Max Func Call Nest Num is %u, callLayer = %u", FUNC_NEST_MAX, callLayer);
86 : }
87 6 : return funcCallVar[callLayer + 1];
88 : }
89 :
90 159 : const std::vector<Variable>& CcuRepReferenceManager::GetFuncIn() { return funcInVar; }
91 :
92 4 : const std::vector<Variable>& CcuRepReferenceManager::GetFuncOut() { return funcOutVar; }
93 :
94 0 : void CcuRepReferenceManager::Dump() const
95 : {
96 0 : for (const auto& kv : referenceMap) {
97 0 : HCCL_INFO("refBlock[%s]:", kv.first.c_str());
98 : }
99 0 : }
100 :
101 64 : void CcuRepReferenceManager::ClearRepReference() { referenceMap.clear(); }
102 :
103 : }; // namespace CcuRep
104 : }; // namespace Hccl
|