Line data Source code
1 : /*
2 : * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
3 : * Description: ccu representation implementation file
4 : * Author: sunzhepeng
5 : * Create: 2024-06-17
6 : */
7 :
8 : #include "ccu_rep_v1.h"
9 : #include "exception_util.h"
10 : #include "ccu_api_exception.h"
11 : #include "ccu_ins_generater_base.h"
12 : #include "ccu_ins_generater_v1.h"
13 :
14 : namespace hcomm {
15 : namespace CcuRep {
16 :
17 : using namespace Hccl;
18 :
19 : // jump基类
20 8 : CcuRepJumpBase::CcuRepJumpBase(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId):
21 8 : insGeneratorPtr_(insGenPtr), label(label), targetInstrId(targetInstrId)
22 : {
23 8 : }
24 :
25 27 : CcuRepJumpBase::CcuRepJumpBase(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId,
26 27 : const Variable& expectedVar, const Variable& condition):
27 27 : insGeneratorPtr_(insGenPtr), label(label), targetInstrId(targetInstrId), expectedVar(expectedVar), condition(condition)
28 : {
29 27 : }
30 :
31 29 : void CcuRepJumpBase::Reference(std::shared_ptr<CcuRepJumpLabel> refRep)
32 : {
33 29 : jumpLabel = refRep;
34 29 : }
35 :
36 41 : void CcuRepJumpBase::ValidateInsGeneratorForJump()
37 : {
38 41 : CcuInsGeneraterV1* tmpPtrV1 = dynamic_cast<CcuInsGeneraterV1*>(insGeneratorPtr_);
39 41 : if (tmpPtrV1 && !supportCcuV1) {
40 : // 当右值只传入var没有立即数时,无法在A5上翻译
41 0 : Hccl::THROW<Hccl::CcuApiException>("Cannot translate %s for A5 when supportCcuV1 is false!",
42 0 : this->Describe().c_str());
43 : }
44 41 : }
45 :
46 49 : CcuResult CcuRepJumpBase::InitInstr(CcuInstr *&instr, uint16_t &instrId)
47 : {
48 49 : CCU_CHK_PTR_NULL(instr);
49 49 : if (this->instr == nullptr) {
50 29 : this->instrId = instrId;
51 29 : this->instr = instr;
52 29 : instr += instrCount;
53 29 : instrId += instrCount;
54 : }
55 49 : return CcuResult::CCU_SUCCESS;
56 : }
57 :
58 : // direct jump
59 8 : CcuRepJump::CcuRepJump(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId) :
60 8 : CcuRepJumpBase(insGenPtr, label, targetInstrId)
61 : {
62 8 : type = CcuRepType::JUMP;
63 8 : instrCount = insGeneratorPtr_->GetInstrCount(type);
64 8 : }
65 :
66 8 : bool CcuRepJump::Translate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
67 : {
68 8 : if (InitInstr(instr, instrId) != CcuResult::CCU_SUCCESS) {
69 0 : Hccl::THROW<Hccl::CcuApiException>("instr is empty!");
70 : }
71 :
72 8 : if (jumpLabel->Translated()) {
73 6 : CHK_RET_THROW(Hccl::CcuApiException,
74 : Hccl::StringFormat("[CcuRepJump][%s] failed to translate repJump for instrId[%u] ", __func__, instrId),
75 : insGeneratorPtr_->CcuRepJumpTranslate(ccuKernel, instr, instrId, this, dep));
76 6 : translated = true;
77 : }
78 :
79 8 : return translated;
80 : }
81 :
82 5 : std::string CcuRepJump::Describe()
83 : {
84 5 : return Hccl::StringFormat("Jump To Label[%s]", label.c_str());
85 : }
86 :
87 : // jumpNE
88 15 : CcuRepJumpNE::CcuRepJumpNE(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId,
89 15 : const Variable &expectedVar, const Variable &condition, uint64_t expected)
90 15 : : CcuRepJumpBase(insGenPtr, label, targetInstrId, expectedVar, condition)
91 : {
92 15 : this->expected = expected;
93 15 : type = CcuRepType::JUMP_NE;
94 15 : instrCount = insGeneratorPtr_->GetInstrCount(type);
95 15 : comp2Immed = true; // A6翻译时插入一条加载立即数指令,A5无关
96 15 : supportCcuV1 = true;
97 15 : }
98 :
99 0 : CcuRepJumpNE::CcuRepJumpNE(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId,
100 0 : const Variable &condition, const Variable &expectedVar)
101 0 : : CcuRepJumpBase(insGenPtr, label, targetInstrId, expectedVar, condition)
102 : {
103 : // 仅用于A6翻译
104 0 : type = CcuRepType::JUMP_NE;
105 0 : instrCount = 2; // 2条指令,暂直接填充指令数,insGenerator中未记录这种使用方式对应的指令数
106 0 : comp2Immed = false;
107 0 : supportCcuV1 = false;
108 0 : }
109 :
110 22 : bool CcuRepJumpNE::Translate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
111 : {
112 22 : ValidateInsGeneratorForJump();
113 :
114 22 : if (InitInstr(instr, instrId) != CcuResult::CCU_SUCCESS) {
115 0 : Hccl::THROW<Hccl::CcuApiException>("instr is empty!");
116 : }
117 :
118 22 : if (jumpLabel->Translated()) {
119 13 : CHK_RET_THROW(Hccl::CcuApiException,
120 : Hccl::StringFormat("[CcuRepJumpNE][%s] failed to translate repJumpNE for instrId[%u] ", __func__, instrId),
121 : insGeneratorPtr_->CcuRepJumpNETranslate(ccuKernel, instr, instrId, this, dep));
122 13 : translated = true;
123 : }
124 :
125 22 : return translated;
126 : }
127 :
128 13 : std::string CcuRepJumpNE::Describe()
129 : {
130 26 : return Hccl::StringFormat("Jump To Label[%s], When Condition[%u] Not equal to Expected[%lu]", label.c_str(), condition.Id(),
131 13 : expected);
132 : }
133 :
134 : // jumpEQ
135 12 : CcuRepJumpEQ::CcuRepJumpEQ(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId,
136 12 : const Variable &expectedVar, const Variable &condition, uint64_t expected)
137 12 : : CcuRepJumpBase(insGenPtr, label, targetInstrId, expectedVar, condition)
138 : {
139 12 : this->expected = expected;
140 12 : type = CcuRepType::JUMP_EQ;
141 12 : instrCount = insGeneratorPtr_->GetInstrCount(type);
142 12 : comp2Immed = true; // A6翻译时插入一条加载立即数指令,A5无关
143 12 : supportCcuV1 = true;
144 12 : }
145 :
146 0 : CcuRepJumpEQ::CcuRepJumpEQ(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId,
147 0 : const Variable &condition, const Variable &expectedVar)
148 0 : : CcuRepJumpBase(insGenPtr, label, targetInstrId, expectedVar, condition)
149 : {
150 0 : type = CcuRepType::JUMP_EQ;
151 0 : instrCount = 2; // 2条指令,暂直接填充指令数,insGenerator中未记录这种使用方式对应的指令数
152 0 : comp2Immed = false;
153 0 : supportCcuV1 = false;
154 0 : }
155 :
156 19 : bool CcuRepJumpEQ::Translate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
157 : {
158 19 : ValidateInsGeneratorForJump();
159 19 : if (InitInstr(instr, instrId) != CcuResult::CCU_SUCCESS) {
160 0 : Hccl::THROW<Hccl::CcuApiException>("instr is empty!");
161 : }
162 :
163 19 : if (jumpLabel->Translated()) {
164 10 : CHK_RET_THROW(Hccl::CcuApiException,
165 : Hccl::StringFormat("[CcuRepJumpEQ][%s] failed to translate repJumpEQ for instrId[%u] ", __func__, instrId),
166 : insGeneratorPtr_->CcuRepJumpEQTranslate(ccuKernel, instr, instrId, this, dep));
167 10 : translated = true;
168 : }
169 :
170 19 : return translated;
171 : }
172 :
173 10 : std::string CcuRepJumpEQ::Describe()
174 : {
175 20 : return Hccl::StringFormat("Jump To Label[%s], When Condition[%u] Be equal to Expected[%lu]", label.c_str(), condition.Id(),
176 10 : expected);
177 : }
178 :
179 : // jumpLE
180 0 : CcuRepJumpLE::CcuRepJumpLE(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId,
181 0 : const Variable &condition, const Variable &expectedVar)
182 0 : : CcuRepJumpBase(insGenPtr, label, targetInstrId, expectedVar, condition)
183 : {
184 0 : type = CcuRepType::JUMP_LE;
185 0 : instrCount = 2; // 2条指令
186 0 : comp2Immed = false;
187 0 : supportCcuV1 = false;
188 0 : }
189 :
190 0 : CcuRepJumpLE::CcuRepJumpLE(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId,
191 0 : const Variable &expectedVar, const Variable &condition, uint64_t expected)
192 0 : : CcuRepJumpBase(insGenPtr, label, targetInstrId, expectedVar, condition)
193 : {
194 0 : this->expected = expected;
195 0 : type = CcuRepType::JUMP_LE;
196 0 : instrCount = 3; // 3条指令
197 0 : comp2Immed = true;
198 0 : supportCcuV1 = false;
199 0 : }
200 :
201 0 : bool CcuRepJumpLE::Translate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
202 : {
203 0 : ValidateInsGeneratorForJump();
204 :
205 0 : if (InitInstr(instr, instrId) != CcuResult::CCU_SUCCESS) {
206 0 : Hccl::THROW<Hccl::CcuApiException>("instr is empty!");
207 : }
208 :
209 0 : if (jumpLabel->Translated()) {
210 0 : CHK_RET_THROW(Hccl::CcuApiException,
211 : Hccl::StringFormat("[CcuRepJumpLE][%s] failed to translate repJumpLE for instrId[%u] ", __func__, instrId),
212 : insGeneratorPtr_->CcuRepJumpLETranslate(ccuKernel, instr, instrId, this, dep));
213 0 : translated = true;
214 : }
215 :
216 0 : return translated;
217 : }
218 :
219 0 : std::string CcuRepJumpLE::Describe()
220 : {
221 0 : return Hccl::StringFormat("Jump To Label[%s], When Condition[%u] <= Expected[%lu]", label.c_str(), condition.Id(),
222 0 : expected);
223 : }
224 :
225 : // jumpGE
226 0 : CcuRepJumpGE::CcuRepJumpGE(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId,
227 0 : const Variable &condition, const Variable &expectedVar)
228 0 : : CcuRepJumpBase(insGenPtr, label, targetInstrId, expectedVar, condition)
229 : {
230 0 : type = CcuRepType::JUMP_GE;
231 0 : instrCount = 2; // 2条指令
232 0 : comp2Immed = false;
233 0 : supportCcuV1 = false;
234 0 : }
235 :
236 0 : CcuRepJumpGE::CcuRepJumpGE(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId,
237 0 : const Variable &expectedVar, const Variable &condition, uint64_t expected)
238 0 : : CcuRepJumpBase(insGenPtr, label, targetInstrId, expectedVar, condition)
239 : {
240 0 : this->expected = expected;
241 0 : type = CcuRepType::JUMP_GE;
242 0 : instrCount = 3; // 3条指令
243 0 : comp2Immed = true;
244 0 : supportCcuV1 = false;
245 0 : }
246 :
247 0 : bool CcuRepJumpGE::Translate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
248 : {
249 0 : ValidateInsGeneratorForJump();
250 :
251 0 : if (InitInstr(instr, instrId) != CcuResult::CCU_SUCCESS) {
252 0 : Hccl::THROW<Hccl::CcuApiException>("instr is empty!");
253 : }
254 :
255 0 : if (jumpLabel->Translated()) {
256 0 : CHK_RET_THROW(Hccl::CcuApiException,
257 : Hccl::StringFormat("[CcuRepJumpGE][%s] failed to translate repJumpGE for instrId[%u] ", __func__, instrId),
258 : insGeneratorPtr_->CcuRepJumpGETranslate(ccuKernel, instr, instrId, this, dep));
259 :
260 0 : translated = true;
261 : }
262 :
263 0 : return translated;
264 : }
265 :
266 0 : std::string CcuRepJumpGE::Describe()
267 : {
268 0 : return Hccl::StringFormat("Jump To Label[%s], When Condition[%u] >= Expected[%lu]", label.c_str(), condition.Id(),
269 0 : expected);
270 : }
271 :
272 : // jumpGT
273 0 : CcuRepJumpGT::CcuRepJumpGT(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId,
274 0 : const Variable &condition, const Variable &expectedVar)
275 0 : : CcuRepJumpBase(insGenPtr, label, targetInstrId, expectedVar, condition)
276 : {
277 0 : type = CcuRepType::JUMP_GT;
278 0 : instrCount = 2; // 2条指令
279 0 : comp2Immed = false;
280 0 : supportCcuV1 = false;
281 0 : }
282 :
283 0 : CcuRepJumpGT::CcuRepJumpGT(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId,
284 0 : const Variable &expectedVar, const Variable &condition, uint64_t expected)
285 0 : : CcuRepJumpBase(insGenPtr, label, targetInstrId, expectedVar, condition)
286 : {
287 0 : this->expected = expected;
288 0 : type = CcuRepType::JUMP_GT;
289 0 : instrCount = 3; // 3条指令
290 0 : comp2Immed = true;
291 0 : supportCcuV1 = false;
292 0 : }
293 :
294 0 : bool CcuRepJumpGT::Translate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
295 : {
296 0 : ValidateInsGeneratorForJump();
297 0 : if (InitInstr(instr, instrId) != CcuResult::CCU_SUCCESS) {
298 0 : Hccl::THROW<Hccl::CcuApiException>("instr is empty!");
299 : }
300 0 : if (jumpLabel->Translated()) {
301 0 : CHK_RET_THROW(Hccl::CcuApiException,
302 : Hccl::StringFormat("[CcuRepJumpGT][%s] failed to translate repJumpGT for instrId[%u] ", __func__, instrId),
303 : insGeneratorPtr_->CcuRepJumpGTTranslate(ccuKernel, instr, instrId, this, dep));
304 :
305 0 : translated = true;
306 : }
307 :
308 0 : return translated;
309 : }
310 :
311 0 : std::string CcuRepJumpGT::Describe()
312 : {
313 0 : return Hccl::StringFormat("Jump To Label[%s], When Condition[%u] > Expected[%lu]", label.c_str(), condition.Id(),
314 0 : expected);
315 : }
316 :
317 : // jumpLT
318 0 : CcuRepJumpLT::CcuRepJumpLT(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId,
319 0 : const Variable &condition, const Variable &expectedVar)
320 0 : : CcuRepJumpBase(insGenPtr, label, targetInstrId, expectedVar, condition)
321 : {
322 0 : type = CcuRepType::JUMP_LT;
323 0 : instrCount = 2; // 2条指令
324 0 : comp2Immed = false;
325 0 : supportCcuV1 = false;
326 0 : }
327 :
328 0 : CcuRepJumpLT::CcuRepJumpLT(CcuInsGeneraterBase* insGenPtr, const std::string &label, const Variable &targetInstrId,
329 0 : const Variable &expectedVar, const Variable &condition, uint64_t expected)
330 0 : : CcuRepJumpBase(insGenPtr, label, targetInstrId, expectedVar, condition)
331 : {
332 0 : this->expected = expected;
333 0 : type = CcuRepType::JUMP_LT;
334 0 : instrCount = 3; // 3条指令
335 0 : comp2Immed = true;
336 0 : supportCcuV1 = false;
337 0 : }
338 :
339 0 : bool CcuRepJumpLT::Translate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
340 : {
341 0 : ValidateInsGeneratorForJump();
342 0 : if (InitInstr(instr, instrId) != CcuResult::CCU_SUCCESS) {
343 0 : Hccl::THROW<Hccl::CcuApiException>("instr is empty!");
344 : }
345 :
346 0 : if (jumpLabel->Translated()) {
347 0 : CHK_RET_THROW(Hccl::CcuApiException,
348 : Hccl::StringFormat("[CcuRepJumpLT][%s] failed to translate repJumpLT for instrId[%u] ", __func__, instrId),
349 : insGeneratorPtr_->CcuRepJumpLTTranslate(ccuKernel, instr, instrId, this, dep));
350 :
351 0 : translated = true;
352 : }
353 :
354 0 : return translated;
355 : }
356 :
357 0 : std::string CcuRepJumpLT::Describe()
358 : {
359 0 : return Hccl::StringFormat("Jump To Label[%s], When Condition[%u] < Expected[%lu]", label.c_str(), condition.Id(),
360 0 : expected);
361 : }
362 : }; // namespace CcuRep
363 : }; // namespace hcomm
|