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