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 67 : CcuRepReferenceManager::CcuRepReferenceManager(uint8_t deiId) : dieId(deiId)
19 : {
20 67 : funcInVar.resize(FUNC_ARG_MAX);
21 67 : funcOutVar.resize(FUNC_ARG_MAX);
22 67 : funcCallVar.resize(1 + FUNC_NEST_MAX + 1); // FUNC_NEST_MAX个xn存放返回地址,1个xn存放block的起始地址和1个xn存放函数地址调用时返回地址
23 67 : }
24 :
25 49 : CcuResReq CcuRepReferenceManager::GetResReq(uint8_t reqDieId)
26 : {
27 49 : CcuResReq resReq;
28 49 : resReq.xnReq[reqDieId] = FUNC_ARG_MAX + FUNC_ARG_MAX + 1 + FUNC_NEST_MAX + 1;
29 49 : return resReq;
30 : }
31 :
32 53 : void CcuRepReferenceManager::GetRes(CcuRepResource &res)
33 : {
34 53 : res.variable[dieId].insert(res.variable[dieId].end(), funcInVar.begin(), funcInVar.end());
35 53 : res.variable[dieId].insert(res.variable[dieId].end(), funcOutVar.begin(), funcOutVar.end());
36 53 : res.variable[dieId].insert(res.variable[dieId].end(), funcCallVar.begin(), funcCallVar.end());
37 53 : }
38 :
39 36 : bool CcuRepReferenceManager::CheckValid(const std::string &label)
40 : {
41 36 : return (referenceMap.find(label) != referenceMap.end());
42 : }
43 :
44 18 : bool CcuRepReferenceManager::CheckUnique(const std::string &label)
45 : {
46 18 : return (referenceMap.find(label) == referenceMap.end());
47 : }
48 :
49 36 : std::shared_ptr<CcuRepBlock> CcuRepReferenceManager::GetRefBlock(const std::string &label)
50 : {
51 36 : if (!CheckValid(label)) {
52 1 : THROW<CcuApiException>("Invalid Reference: %s", label.c_str());
53 : }
54 35 : return referenceMap[label];
55 : }
56 :
57 18 : void CcuRepReferenceManager::SetRefBlock(const std::string &label, std::shared_ptr<CcuRepBlock> refBlock)
58 : {
59 18 : if (!CheckUnique(label)) {
60 1 : THROW<CcuApiException>("Duplicate Definition: %s", label.c_str());
61 : }
62 17 : referenceMap[label] = refBlock;
63 17 : }
64 :
65 0 : uint16_t CcuRepReferenceManager::GetFuncAddr(const std::string &label)
66 : {
67 0 : if (!CheckValid(label)) {
68 0 : THROW<CcuApiException>("Invalid Reference: %s", label.c_str());
69 : }
70 :
71 0 : if (referenceMap[label]->Type() != CcuRepType::FUNC_BLOCK) {
72 0 : THROW<CcuApiException>("Invalid Type, %s Must be FuncBlock", label.c_str());
73 : }
74 :
75 0 : return referenceMap[label]->StartInstrId();
76 : }
77 :
78 8 : const Variable &CcuRepReferenceManager::GetFuncCall()
79 : {
80 8 : return funcCallVar[0];
81 : }
82 :
83 7 : const Variable &CcuRepReferenceManager::GetFuncRet(uint16_t callLayer)
84 : {
85 7 : if (callLayer > FUNC_NEST_MAX) {
86 0 : THROW<CcuApiException>("Max Func Call Nest Num is %u, callLayer = %u", FUNC_NEST_MAX, callLayer);
87 : }
88 7 : return funcCallVar[callLayer + 1];
89 : }
90 :
91 175 : const std::vector<Variable> &CcuRepReferenceManager::GetFuncIn()
92 : {
93 175 : return funcInVar;
94 : }
95 :
96 7 : const std::vector<Variable> &CcuRepReferenceManager::GetFuncOut()
97 : {
98 7 : return funcOutVar;
99 : }
100 :
101 0 : void CcuRepReferenceManager::Dump() const
102 : {
103 0 : for (const auto &kv : referenceMap) {
104 0 : HCCL_INFO("refBlock[%s]:", kv.first.c_str());
105 : }
106 0 : }
107 :
108 64 : void CcuRepReferenceManager::ClearRepReference()
109 : {
110 64 : referenceMap.clear();
111 64 : }
112 :
113 : }; // namespace CcuRep
114 : }; // namespace Hccl
|