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