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.h"
12 : #include "ccu_rep_reference_manager.h"
13 :
14 : #include "string_util.h"
15 : #include "exception_util.h"
16 : #include "ccu_api_exception.h"
17 :
18 : namespace Hccl {
19 : namespace CcuRep {
20 :
21 2 : CcuRepFuncCall::CcuRepFuncCall(const std::string &label) : label(label)
22 : {
23 2 : type = CcuRepType::FUNC_CALL;
24 2 : }
25 :
26 6 : CcuRepFuncCall::CcuRepFuncCall(const Variable &funcAddrVar) : label(""), funcAddrVar(funcAddrVar)
27 : {
28 2 : type = CcuRepType::FUNC_CALL;
29 2 : }
30 :
31 4 : const std::string &CcuRepFuncCall::GetLabel() const
32 : {
33 4 : return label;
34 : }
35 :
36 2 : void CcuRepFuncCall::Reference(std::shared_ptr<CcuRepFuncBlock> refRep)
37 : {
38 2 : funcBlock = refRep;
39 2 : }
40 :
41 4 : void CcuRepFuncCall::SetFuncManager(CcuRepReferenceManager *funcManager)
42 : {
43 4 : this->funcManager = funcManager;
44 4 : }
45 :
46 66 : void CcuRepFuncCall::SetInArg(const Variable &var)
47 : {
48 66 : inArgCount++;
49 66 : if (inArgCount > FUNC_IN_MAX) {
50 0 : HCCL_ERROR("[%s]CcuFunc Max ArgCount = %u, Current ArgCount = %u", __func__, FUNC_IN_MAX, inArgCount);
51 0 : THROW<CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_IN_MAX);
52 : }
53 66 : inArgs.push_back(CcuRepArg(var));
54 198 : HCCL_INFO("[%s]Define Input Arg: Index[%u], Type[Variable] Id[%u]", __func__, inArgs.size(), var.Id());
55 66 : }
56 :
57 2 : void CcuRepFuncCall::SetOutArg(const Variable &var)
58 : {
59 2 : outArgCount++;
60 2 : if (outArgCount > FUNC_OUT_MAX) {
61 0 : THROW<CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_OUT_MAX);
62 : }
63 2 : outArgs.push_back(CcuRepArg(var));
64 2 : }
65 :
66 0 : void CcuRepFuncCall::SetInArg(const std::vector<Variable> &varList)
67 : {
68 0 : inArgCount += varList.size();
69 0 : if (inArgCount > FUNC_IN_MAX) {
70 0 : HCCL_ERROR("[%s]CcuFunc Max ArgCount = %u, Current ArgCount = %u", __func__, FUNC_IN_MAX, inArgCount);
71 0 : THROW<CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_IN_MAX);
72 : }
73 0 : inArgs.push_back(CcuRepArg(varList));
74 0 : HCCL_INFO("[%s]Define Input Arg: Index[%u], Type[Variable List]: ", __func__, inArgs.size());
75 0 : }
76 :
77 0 : void CcuRepFuncCall::SetOutArg(const std::vector<Variable> &varList)
78 : {
79 0 : outArgCount += varList.size();
80 0 : if (outArgCount > FUNC_OUT_MAX) {
81 0 : THROW<CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_OUT_MAX);
82 : }
83 0 : outArgs.push_back(CcuRepArg(varList));
84 0 : }
85 :
86 12 : uint16_t CcuRepFuncCall::InstrCount()
87 : {
88 12 : instrCount = inArgCount + outArgCount + 4; // funcCall除去入参和出参的处理外,需要额外4条指令
89 12 : return instrCount;
90 : }
91 :
92 4 : bool CcuRepFuncCall::Translate(CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
93 : {
94 4 : if (funcManager == nullptr) {
95 0 : THROW<CcuApiException>("funcManager is nullptr");
96 : }
97 : // 未实现, FuncCall和FuncBlock中的args个数校验
98 4 : uint32_t extraInstrNum = 4; // funcCall除去入参和出参的处理外,需要额外4条指令
99 4 : if (this->instr == nullptr) {
100 4 : this->instrId = instrId;
101 4 : this->instr = instr;
102 4 : instr += InstrCount();
103 4 : instrId += InstrCount();
104 : }
105 :
106 4 : if (funcBlock != nullptr && !funcBlock->Translated()) {
107 0 : return translated;
108 : }
109 :
110 4 : translated = true;
111 :
112 4 : uint32_t iInArg = 0;
113 70 : for (uint32_t i = 0; i < inArgs.size(); i++) {
114 66 : if (inArgs[i].type == CcuArgType::VARIABLE) {
115 66 : LoadXXInstr(this->instr + iInArg, funcManager->GetFuncIn()[iInArg].Id(), inArgs[i].var.Id(),
116 66 : dep.reserveXnId);
117 66 : iInArg++;
118 0 : } else if (inArgs[i].type == CcuArgType::VARIABLE_LIST) {
119 0 : for (uint32_t j = 0; j < inArgs[i].varList.size(); j++) {
120 0 : LoadXXInstr(this->instr + iInArg, funcManager->GetFuncIn()[iInArg].Id(), inArgs[i].varList[j].Id(),
121 0 : dep.reserveXnId);
122 0 : iInArg++;
123 : }
124 : }
125 : }
126 :
127 4 : uint32_t locId = 0;
128 4 : if (funcBlock != nullptr) {
129 2 : LoadImdToXnInstr(this->instr + inArgCount + locId++, funcManager->GetFuncCall().Id(),
130 2 : funcBlock->StartInstrId());
131 : } else {
132 2 : LoadXXInstr(this->instr + inArgCount + locId++, funcManager->GetFuncCall().Id(), funcAddrVar.Id(),
133 2 : dep.reserveXnId);
134 : }
135 :
136 4 : LoadImdToXnInstr(this->instr + inArgCount + locId++, funcManager->GetFuncRet(GetCallLayer()).Id(),
137 4 : this->instrId + inArgCount + 3); // 需要指向函数返回位置,为输入指令Id + 3
138 4 : JumpInstr(this->instr + inArgCount + locId++, funcManager->GetFuncCall().Id(), dep.reserveXnId, 1);
139 4 : LoadImdToXnInstr(this->instr + inArgCount + locId++, dep.reserveXnId, 0);
140 :
141 4 : uint32_t iOutArg = 0;
142 6 : for (uint32_t i = 0; i < outArgs.size(); i++) {
143 2 : if (outArgs[i].type == CcuArgType::VARIABLE) {
144 2 : LoadXXInstr(this->instr + inArgCount + extraInstrNum + iOutArg, outArgs[i].var.Id(),
145 2 : funcManager->GetFuncOut()[iOutArg].Id(), dep.reserveXnId);
146 2 : iOutArg++;
147 0 : } else if (outArgs[i].type == CcuArgType::VARIABLE_LIST) {
148 0 : for (uint32_t j = 0; j < outArgs[i].varList.size(); j++) {
149 0 : LoadXXInstr(this->instr + inArgCount + extraInstrNum + iOutArg, outArgs[i].varList[j].Id(),
150 0 : funcManager->GetFuncOut()[iOutArg].Id(), dep.reserveXnId);
151 0 : iOutArg++;
152 : }
153 : }
154 : }
155 :
156 4 : return translated;
157 : }
158 :
159 4 : std::string CcuRepFuncCall::Describe()
160 : {
161 4 : return StringFormat("FuncCall[%s]", label.c_str());
162 : }
163 :
164 4 : int32_t CcuRepFuncCall::GetCallLayer()
165 : {
166 4 : return funcBlock == nullptr ? FUNC_NEST_MAX : funcBlock->GetCallLayer();
167 : }
168 :
169 : }; // namespace CcuRep
170 : }; // namespace Hccl
|