Line data Source code
1 : /**
2 : * Copyright (c) 2026 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_v1.h"
12 : #include "ccu_ins_generator_v1.h"
13 : #include "string_util.h"
14 : #include "exception_util.h"
15 : #include "ccu_api_exception.h"
16 : #include "ccu_kernel.h"
17 :
18 : namespace hcomm {
19 : namespace CcuRep {
20 :
21 181 : void CcuRepAdd::SetCommonInfo()
22 : {
23 181 : type = CcuRepType::ADD;
24 181 : instrCount = insGenPtr->GetInstrCount(type);
25 181 : }
26 :
27 4 : CcuRepAdd::CcuRepAdd(
28 4 : CcuInsGeneratorBase* insGenPtr, const Address& addrC, const Address& addrA, const Variable& varB)
29 4 : : insGenPtr(insGenPtr),
30 4 : subType(AddSubType::ADDR_PLUS_VAR_TO_ADDR),
31 4 : addrA(addrA),
32 4 : addrC(addrC),
33 8 : varB(varB)
34 : {
35 4 : SetCommonInfo();
36 4 : supportCcuV1 = true;
37 4 : }
38 :
39 6 : CcuRepAdd::CcuRepAdd(
40 6 : CcuInsGeneratorBase* insGenPtr, const Address& addrC, const Address& addrA, const Address& addrB)
41 6 : : insGenPtr(insGenPtr),
42 6 : subType(AddSubType::ADDR_PLUS_ADDR_TO_ADDR),
43 6 : addrA(addrA),
44 6 : addrB(addrB),
45 12 : addrC(addrC)
46 : {
47 6 : SetCommonInfo();
48 6 : supportCcuV1 = true;
49 6 : }
50 :
51 113 : CcuRepAdd::CcuRepAdd(
52 113 : CcuInsGeneratorBase* insGenPtr, const Variable& varC, const Variable& varA, const Variable& varB)
53 113 : : insGenPtr(insGenPtr),
54 113 : subType(AddSubType::VAR_PLUS_VAR_TO_VAR),
55 113 : varA(varA),
56 113 : varB(varB),
57 226 : varC(varC)
58 : {
59 113 : SetCommonInfo();
60 113 : supportCcuV1 = true;
61 113 : }
62 :
63 45 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Address& addrA, const Variable& offset)
64 45 : : insGenPtr(insGenPtr),
65 45 : subType(AddSubType::SELF_ADD_ADDRESS),
66 45 : addrA(addrA),
67 45 : varB(offset)
68 : {
69 45 : SetCommonInfo();
70 45 : supportCcuV1 = true;
71 45 : }
72 :
73 4 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Variable& varA, const Variable& offset)
74 4 : : insGenPtr(insGenPtr),
75 4 : subType(AddSubType::SELF_ADD_VARIABLE),
76 4 : varA(varA),
77 8 : varB(offset)
78 : {
79 4 : SetCommonInfo();
80 4 : supportCcuV1 = true;
81 4 : }
82 :
83 1 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Variable& varA, const uint16_t immedB)
84 1 : : insGenPtr(insGenPtr),
85 1 : subType(AddSubType::SELF_ADD_IMMED_VARIABLE),
86 1 : varA(varA),
87 2 : immedB(immedB)
88 : {
89 1 : SetCommonInfo();
90 1 : supportCcuV1 = false;
91 1 : }
92 :
93 1 : CcuRepAdd::CcuRepAdd(
94 1 : CcuInsGeneratorBase* insGenPtr, const Variable& varC, const Variable& varA, const uint16_t immedB)
95 1 : : insGenPtr(insGenPtr),
96 1 : subType(AddSubType::VAR_PLUS_IMMED_TO_VAR),
97 1 : varA(varA),
98 1 : varC(varC),
99 2 : immedB(immedB)
100 : {
101 1 : SetCommonInfo();
102 1 : supportCcuV1 = false;
103 1 : }
104 :
105 3 : CcuRepAdd::CcuRepAdd(
106 3 : CcuInsGeneratorBase* insGenPtr, const Address& addrC, const Address& addrA, const uint16_t immedB)
107 3 : : insGenPtr(insGenPtr),
108 3 : subType(AddSubType::ADDR_PLUS_IMMED_TO_ADDR),
109 3 : addrA(addrA),
110 3 : addrC(addrC),
111 6 : immedB(immedB)
112 : {
113 3 : SetCommonInfo();
114 3 : supportCcuV1 = false;
115 3 : }
116 :
117 0 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Address& addrA, const uint16_t immedB)
118 0 : : insGenPtr(insGenPtr),
119 0 : subType(AddSubType::SELF_ADD_IMMED_ADDRESS),
120 0 : addrA(addrA),
121 0 : immedB(immedB)
122 : {
123 0 : SetCommonInfo();
124 0 : supportCcuV1 = false;
125 0 : }
126 :
127 1 : CcuRepAdd::CcuRepAdd(
128 1 : CcuInsGeneratorBase* insGenPtr, const Address& addrC, const Variable& varA, const Variable& varB)
129 1 : : insGenPtr(insGenPtr),
130 1 : subType(AddSubType::VAR_PLUS_VAR_TO_ADDR),
131 1 : addrC(addrC),
132 1 : varA(varA),
133 2 : varB(varB)
134 : {
135 1 : SetCommonInfo();
136 1 : supportCcuV1 = false;
137 1 : }
138 :
139 1 : CcuRepAdd::CcuRepAdd(
140 1 : CcuInsGeneratorBase* insGenPtr, const Address& addrC, const Variable& varA, const uint16_t immedB)
141 1 : : insGenPtr(insGenPtr),
142 1 : subType(AddSubType::VAR_PLUS_IMMED_TO_ADDR),
143 1 : addrC(addrC),
144 1 : varA(varA),
145 2 : immedB(immedB)
146 : {
147 1 : SetCommonInfo();
148 1 : supportCcuV1 = false;
149 1 : }
150 :
151 1 : CcuRepAdd::CcuRepAdd(
152 1 : CcuInsGeneratorBase* insGenPtr, const Variable& varC, const Address& addrA, const Address& addrB)
153 1 : : insGenPtr(insGenPtr),
154 1 : subType(AddSubType::ADDR_PLUS_ADDR_TO_VAR),
155 1 : addrA(addrA),
156 1 : addrB(addrB),
157 2 : varC(varC)
158 : {
159 1 : SetCommonInfo();
160 1 : supportCcuV1 = false;
161 1 : }
162 :
163 1 : CcuRepAdd::CcuRepAdd(
164 1 : CcuInsGeneratorBase* insGenPtr, const Variable& varC, const Address& addrA, const uint16_t immedB)
165 1 : : insGenPtr(insGenPtr),
166 1 : subType(AddSubType::ADDR_PLUS_IMMED_TO_VAR),
167 1 : addrA(addrA),
168 1 : varC(varC),
169 1 : immedB(immedB)
170 : {
171 1 : SetCommonInfo();
172 1 : supportCcuV1 = false;
173 1 : }
174 :
175 156 : void CcuRepAdd::ValidateInsGenPtrForAdd()
176 : {
177 156 : CcuInsGeneratorV1* tmpPtrV1 = dynamic_cast<CcuInsGeneratorV1*>(insGenPtr);
178 156 : CHK_PRT_THROW(
179 : (tmpPtrV1 && !supportCcuV1),
180 : HCCL_ERROR("[CcuRepAdd][%s]Cannot translate CcuRepAdd for A5 when supportCcuV1 is false", __func__),
181 : Hccl::CcuApiException, "tmpPtrV1 does not match supportCcuV1");
182 156 : }
183 :
184 156 : bool CcuRepAdd::Translate(CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& instrId, const TransDep& dep)
185 : {
186 156 : ValidateInsGenPtrForAdd();
187 156 : Hccl::CHECK_NULLPTR(instr, "[CcuRepAdd::Translate] instr is nullptr!");
188 156 : this->instrId = instrId;
189 156 : translated = true;
190 :
191 156 : CHK_PRT_THROW(
192 : insGenPtr->CcuRepAddTranslate(ccuKernel, instr, this, dep) != HcclResult::HCCL_SUCCESS,
193 : HCCL_ERROR("[CcuRepAdd][Translate] failed to translate for instrId[%u]", instrId), Hccl::CcuApiException,
194 : "CcuRepAdd translate failed");
195 156 : CHK_PRT_THROW(
196 : (instrId > UINT16_MAX - instrCount),
197 : HCCL_ERROR(
198 : "[CcuRepAdd::Translate]uint16 integer overflow occurs, instrId = [%hu], instrCount = [%hu]", instrId,
199 : instrCount),
200 : Hccl::InternalException, "integer overflow");
201 156 : instrId += instrCount;
202 156 : return translated;
203 : }
204 :
205 146 : std::string CcuRepAdd::Describe()
206 : {
207 146 : switch (subType) {
208 1 : case AddSubType::ADDR_PLUS_VAR_TO_ADDR: {
209 : return Hccl::StringFormat(
210 1 : "Address[%u] = Address[%u] + Variable[%u]", addrC.Id(), addrA.Id(), varB.Id());
211 : }
212 2 : case AddSubType::ADDR_PLUS_ADDR_TO_ADDR: {
213 : return Hccl::StringFormat(
214 2 : "Address[%u] = Address[%u] + Address[%u]", addrC.Id(), addrA.Id(), addrB.Id());
215 : }
216 98 : case AddSubType::VAR_PLUS_VAR_TO_VAR: {
217 : return Hccl::StringFormat(
218 98 : "Variable[%u] = Variable[%u] + Variable[%u]", varC.Id(), varA.Id(), varB.Id());
219 : }
220 42 : case AddSubType::SELF_ADD_ADDRESS: {
221 42 : return Hccl::StringFormat("Address[%u] += Variable[%u]", addrA.Id(), varB.Id());
222 : }
223 1 : case AddSubType::SELF_ADD_VARIABLE: {
224 1 : return Hccl::StringFormat("Variable[%u] += Variable[%u]", varA.Id(), varB.Id());
225 : }
226 0 : case AddSubType::SELF_ADD_IMMED_VARIABLE: {
227 0 : return Hccl::StringFormat("Variable[%u] += Immed[%u]", varA.Id(), immedB);
228 : }
229 1 : case AddSubType::VAR_PLUS_IMMED_TO_VAR: {
230 1 : return Hccl::StringFormat("Variable[%u] = Variable[%u] + Immed[%u]", varC.Id(), varA.Id(), immedB);
231 : }
232 1 : case AddSubType::ADDR_PLUS_IMMED_TO_ADDR: {
233 1 : return Hccl::StringFormat("Address[%u] += Address[%u] + Immed[%hu]", addrC.Id(), addrA.Id(), immedB);
234 : }
235 0 : case AddSubType::VAR_PLUS_VAR_TO_ADDR: {
236 : return Hccl::StringFormat(
237 0 : "Address[%u] = Variable[%u] + Variable[%u]", addrC.Id(), varA.Id(), varB.Id());
238 : }
239 0 : case AddSubType::SELF_ADD_IMMED_ADDRESS: {
240 0 : return Hccl::StringFormat("Address[%u] += Immed[%u]", addrA.Id(), immedB);
241 : }
242 0 : case AddSubType::VAR_PLUS_IMMED_TO_ADDR: {
243 0 : return Hccl::StringFormat("Address[%u] += varA[%u] + Immed[%hu]", addrC.Id(), varA.Id(), immedB);
244 : }
245 0 : case AddSubType::ADDR_PLUS_IMMED_TO_VAR: {
246 0 : return Hccl::StringFormat("Variable[%u] = Address[%u] + Immed[%u]", varC.Id(), addrA.Id(), immedB);
247 : }
248 0 : default: {
249 0 : return Hccl::StringFormat("Invalid Add");
250 : }
251 : }
252 : }
253 :
254 76 : Address CcuRepAdd::GetAddrA() { return addrA; }
255 :
256 3 : Address CcuRepAdd::GetAddrB() { return addrB; }
257 :
258 7 : Address CcuRepAdd::GetAddrC() { return addrC; }
259 :
260 108 : Variable CcuRepAdd::GetVarA() { return varA; }
261 :
262 148 : Variable CcuRepAdd::GetVarB() { return varB; }
263 :
264 106 : Variable CcuRepAdd::GetVarC() { return varC; }
265 :
266 5 : uint16_t CcuRepAdd::GetImmedB() { return immedB; }
267 :
268 166 : AddSubType CcuRepAdd::GetSubType() { return subType; }
269 : }; // namespace CcuRep
270 : }; // namespace hcomm
|