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 : #include "ccu_rep_translator.h"
14 :
15 : #include "string_util.h"
16 : #include "exception_util.h"
17 : #include "ccu_api_exception.h"
18 :
19 : namespace Hccl {
20 : namespace CcuRep {
21 :
22 3 : CcuRepFuncBlock::CcuRepFuncBlock(const std::string &label) : CcuRepBlock(label)
23 : {
24 3 : type = CcuRepType::FUNC_BLOCK;
25 3 : }
26 :
27 5 : std::string CcuRepFuncBlock::Describe()
28 : {
29 5 : return StringFormat("FuncBlock[%s]", GetLabel().c_str());
30 : }
31 :
32 2 : void CcuRepFuncBlock::SetFuncManager(CcuRepReferenceManager *funcManager)
33 : {
34 2 : this->funcManager = funcManager;
35 2 : }
36 :
37 3 : void CcuRepFuncBlock::SetCallLayer(uint16_t callLayer)
38 : {
39 3 : if (callLayer != FUNC_CALL_LAYER_INVALID) {
40 0 : this->callLayer = callLayer;
41 0 : return;
42 : }
43 :
44 3 : uint16_t innerCallLayer = 0;
45 17 : for (const auto &rep : GetReps()) {
46 14 : if (rep->Type() == CcuRepType::FUNC_CALL) {
47 0 : innerCallLayer = std::static_pointer_cast<CcuRepFuncCall>(rep)->GetCallLayer() + 1;
48 0 : this->callLayer = this->callLayer > innerCallLayer ? this->callLayer : innerCallLayer;
49 : }
50 : }
51 3 : if (this->callLayer > FUNC_NEST_MAX - 1) {
52 0 : THROW<CcuApiException>("Max Func Call Nest Num is %u", FUNC_NEST_MAX);
53 : }
54 : }
55 :
56 2 : uint16_t CcuRepFuncBlock::GetCallLayer() const
57 : {
58 2 : return callLayer;
59 : }
60 :
61 3 : void CcuRepFuncBlock::DefineInArg(const Variable &var)
62 : {
63 3 : inArgCount++;
64 3 : if (inArgCount > FUNC_IN_MAX) {
65 0 : HCCL_ERROR("[%s]CcuFunc Max ArgCount = %u, Current ArgCount = %u", __func__, FUNC_IN_MAX, inArgCount);
66 0 : THROW<CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_IN_MAX);
67 : }
68 3 : inArgs.push_back(CcuRepArg(var));
69 9 : HCCL_INFO("[%s]Define Input Arg: Index[%u], Type[Variable] Id[%u]", __func__, inArgs.size(), var.Id());
70 3 : }
71 :
72 3 : void CcuRepFuncBlock::DefineOutArg(const Variable &var)
73 : {
74 3 : outArgCount++;
75 3 : if (outArgCount > FUNC_OUT_MAX) {
76 0 : THROW<CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_OUT_MAX);
77 : }
78 3 : outArgs.push_back(CcuRepArg(var));
79 9 : HCCL_INFO("Define Output Arg: Index[%u], Type[Variable] Id[%u]", outArgs.size(), var.Id());
80 3 : }
81 :
82 1 : void CcuRepFuncBlock::DefineInArg(const std::vector<Variable> &varList)
83 : {
84 1 : inArgCount += varList.size();
85 1 : if (inArgCount > FUNC_IN_MAX) {
86 0 : HCCL_ERROR("[%s]CcuFunc Max ArgCount = %u, Current ArgCount = %u", __func__, FUNC_IN_MAX, inArgCount);
87 0 : THROW<CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_IN_MAX);
88 : }
89 1 : inArgs.push_back(CcuRepArg(varList));
90 3 : HCCL_INFO("[%s]Define Input Arg: Index[%u], Type[Variable List]: ", __func__, inArgs.size());
91 3 : for (uint32_t index = 0; index < varList.size(); index++) {
92 6 : HCCL_INFO(" Index[%u].Id[%u]", index, varList[index].Id());
93 : }
94 1 : }
95 :
96 1 : void CcuRepFuncBlock::DefineOutArg(const std::vector<Variable> &varList)
97 : {
98 1 : outArgCount += varList.size();
99 1 : if (outArgCount > FUNC_OUT_MAX) {
100 1 : THROW<CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_OUT_MAX);
101 : }
102 0 : outArgs.push_back(CcuRepArg(varList));
103 0 : HCCL_INFO("Define Output Arg: Index[%u], Type[Variable List]: ", outArgs.size());
104 0 : for (uint32_t index = 0; index < varList.size(); index++) {
105 0 : HCCL_INFO(" Index[%u].Id[%u]", index, varList[index].Id());
106 : }
107 0 : }
108 :
109 2 : uint16_t CcuRepFuncBlock::InstrCount()
110 : {
111 2 : instrCount = CcuRepBlock::InstrCount() + inArgCount + outArgCount + 2; // FuncBlock需要2外两条指令
112 2 : return instrCount;
113 : }
114 :
115 2 : bool CcuRepFuncBlock::Translate(CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
116 : {
117 2 : if (funcManager == nullptr) {
118 0 : THROW<CcuApiException>("funcManager is nullptr");
119 : }
120 :
121 2 : this->instrId = instrId;
122 2 : translated = true;
123 :
124 : // 函数入口为Nop指令
125 2 : LoadImdToXnInstr(instr++, dep.reserveXnId, 0);
126 2 : instrId++;
127 :
128 2 : uint32_t iInArg = 0;
129 4 : for (uint32_t i = 0; i < inArgs.size(); i++) {
130 2 : if (inArgs[i].type == CcuArgType::VARIABLE) {
131 2 : LoadXXInstr(instr++, inArgs[i].var.Id(), funcManager->GetFuncIn()[iInArg++].Id(), dep.reserveXnId);
132 2 : instrId++;
133 0 : } else if (inArgs[i].type == CcuArgType::VARIABLE_LIST) {
134 0 : for (uint32_t j = 0; j < inArgs[i].varList.size(); j++) {
135 0 : LoadXXInstr(instr++, inArgs[i].varList[j].Id(), funcManager->GetFuncIn()[iInArg++].Id(),
136 0 : dep.reserveXnId);
137 0 : instrId++;
138 : }
139 : }
140 : }
141 :
142 : // 使用空实现的自定义删除器,避免智能指针析构时释放对象
143 : auto translator
144 2 : = CcuRepTranslator(std::shared_ptr<CcuRepReferenceManager>(funcManager, [](CcuRepReferenceManager *ptr) {}), dep);
145 2 : translator.Translate(GetReps(), instr, instrId, [](std::shared_ptr<CcuRepBase> rep) -> bool {
146 28 : return true;
147 : });
148 :
149 2 : uint32_t iOutArg = 0;
150 4 : for (uint32_t i = 0; i < outArgs.size(); i++) {
151 2 : if (outArgs[i].type == CcuArgType::VARIABLE) {
152 2 : LoadXXInstr(instr++, funcManager->GetFuncOut()[iOutArg++].Id(), outArgs[i].var.Id(), dep.reserveXnId);
153 2 : instrId++;
154 0 : } else if (outArgs[i].type == CcuArgType::VARIABLE_LIST) {
155 0 : for (uint32_t j = 0; j < outArgs[i].varList.size(); j++) {
156 0 : LoadXXInstr(instr++, funcManager->GetFuncOut()[iOutArg++].Id(), outArgs[i].varList[j].Id(),
157 0 : dep.reserveXnId);
158 0 : instrId++;
159 : }
160 : }
161 : }
162 :
163 2 : JumpInstr(instr++, funcManager->GetFuncRet(callLayer).Id(), dep.reserveXnId, 1);
164 2 : instrId++;
165 :
166 2 : return translated;
167 2 : }
168 :
169 : }; // namespace CcuRep
170 : }; // namespace Hccl
|