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_ins_generator_v1.h"
12 : #include "hcomm_c_adpt.h"
13 : #include "ccu_rep_base_v1.h"
14 : #include "../../../../endpoint_pairs/channels/ccu/ccu_urma_channel.h"
15 : #include "ccu_api_exception.h"
16 : #include "ccu_assist_v1.h"
17 : #include "ccu_log.h"
18 :
19 : namespace hcomm {
20 : namespace CcuRep {
21 :
22 : #define UNUSED(x) (void)(x)
23 :
24 : namespace {
25 : template <typename T>
26 0 : void LoadAddrArg(CcuInstr*& instr, const T& dst, const T& src, const TransDep& dep)
27 : {
28 0 : LoadGSAGSAInstr(instr++, dst.addr.Id(), src.addr.Id(), dep.reserveGsaId);
29 0 : LoadXXInstr(instr++, dst.token.Id(), src.token.Id(), dep.reserveXnId);
30 0 : }
31 :
32 : template <typename T>
33 : HcclResult
34 0 : LoadAddrListArg(CcuInstr*& instr, const std::vector<T>& dst, const std::vector<T>& src, const TransDep& dep)
35 : {
36 0 : if (src.size() != dst.size()) {
37 0 : HCCL_ERROR("Mismatched Arg Size: srcSize[%u], dstSize[%u]", src.size(), dst.size());
38 0 : return HCCL_E_PARA;
39 : }
40 0 : for (uint32_t j = 0; j < src.size(); j++) {
41 0 : LoadAddrArg(instr, dst[j], src[j], dep);
42 : }
43 0 : return HcclResult::HCCL_SUCCESS;
44 : }
45 : } // namespace
46 :
47 9 : HcclResult CcuInsGeneratorV1::CcuRepBufLocReadTranslate(
48 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufLocRead* repBufLocRead, const TransDep& dep)
49 : {
50 : UNUSED(ccuKernel);
51 9 : CHK_PTR_NULL(repBufLocRead);
52 54 : TransLocMemToLocMSInstr(
53 9 : instr++, repBufLocRead->GetDst().Id(), repBufLocRead->GetSrc().addr.Id(),
54 18 : repBufLocRead->GetSrc().token.Id(), repBufLocRead->GetLen().Id(), dep.reserveChannalId[0],
55 18 : repBufLocRead->GetSem().Id(), repBufLocRead->GetMask(), 0, 0, 1, 1);
56 :
57 9 : return HcclResult::HCCL_SUCCESS;
58 : }
59 :
60 7 : HcclResult CcuInsGeneratorV1::CcuRepBufLocWriteTranslate(
61 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufLocWrite* repBufLocWrite, const TransDep& dep)
62 : {
63 : UNUSED(ccuKernel);
64 7 : CHK_PTR_NULL(repBufLocWrite);
65 42 : TransLocMSToLocMemInstr(
66 7 : instr++, repBufLocWrite->GetDst().addr.Id(), repBufLocWrite->GetDst().token.Id(),
67 14 : repBufLocWrite->GetSrc().Id(), repBufLocWrite->GetLen().Id(), dep.reserveChannalId[0],
68 14 : repBufLocWrite->GetSem().Id(), repBufLocWrite->GetMask(), 0, 0, 1, 1);
69 :
70 7 : return HcclResult::HCCL_SUCCESS;
71 : }
72 :
73 1 : HcclResult CcuInsGeneratorV1::CcuRepBufReadTranslate(
74 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufRead* repBufRead, const TransDep& dep)
75 : {
76 : UNUSED(ccuKernel);
77 : UNUSED(dep);
78 1 : CHK_PTR_NULL(repBufRead);
79 1 : void* channelPtr{nullptr};
80 1 : CHK_PRT_RET(
81 : static_cast<HcclResult>(HcommChannelGet(repBufRead->GetChannel(), &channelPtr)) != HcclResult::HCCL_SUCCESS,
82 : HCCL_ERROR("failed to get ccu channel, type[%d]", repBufRead->Type()), HCCL_E_INTERNAL);
83 :
84 1 : auto* channelImpl = dynamic_cast<CcuUrmaChannel*>(static_cast<Channel*>(channelPtr));
85 1 : CHK_PTR_NULL(channelImpl);
86 :
87 7 : TransRmtMemToLocMSInstr(
88 1 : instr++, repBufRead->GetDst().Id(), repBufRead->GetSrc().addr.Id(), repBufRead->GetSrc().token.Id(),
89 2 : repBufRead->GetLen().Id(), channelImpl->GetChannelId(), repBufRead->GetSem().Id(), repBufRead->GetMask(), 0,
90 : 0, 1, 1);
91 :
92 1 : return HcclResult::HCCL_SUCCESS;
93 : }
94 :
95 3 : HcclResult CcuInsGeneratorV1::CcuRepWriteTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepWrite* repWrite)
96 : {
97 : UNUSED(ccuKernel);
98 3 : CHK_PTR_NULL(repWrite);
99 3 : void* channelPtr{nullptr};
100 3 : CHK_PRT_RET(
101 : static_cast<HcclResult>(HcommChannelGet(repWrite->GetChannel(), &channelPtr)) != HcclResult::HCCL_SUCCESS,
102 : HCCL_ERROR("failed to get ccu channel, type[%d]", repWrite->Type()), HCCL_E_INTERNAL);
103 :
104 3 : auto* channelImpl = dynamic_cast<CcuUrmaChannel*>(static_cast<Channel*>(channelPtr));
105 3 : CHK_PTR_NULL(channelImpl);
106 33 : TransLocMemToRmtMemInstr(
107 3 : instr++, repWrite->GetRem().addr.Id(), repWrite->GetRem().token.Id(), repWrite->GetLoc().addr.Id(),
108 6 : repWrite->GetLoc().token.Id(), repWrite->GetLen().Id(), channelImpl->GetChannelId(),
109 6 : repWrite->GetDataType(), repWrite->GetOpType(), repWrite->GetSem().Id(), repWrite->GetMask(), 0, 0, 1, 1,
110 3 : repWrite->GetReduceFlag());
111 :
112 3 : return HcclResult::HCCL_SUCCESS;
113 : }
114 :
115 3 : HcclResult CcuInsGeneratorV1::CcuRepReadTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRead* repRead)
116 : {
117 : UNUSED(ccuKernel);
118 3 : CHK_PTR_NULL(repRead);
119 3 : void* channelPtr{nullptr};
120 3 : CHK_PRT_RET(
121 : static_cast<HcclResult>(HcommChannelGet(repRead->GetChannel(), &channelPtr)) != HcclResult::HCCL_SUCCESS,
122 : HCCL_ERROR("failed to get ccu channel, type[%d]", repRead->Type()), HCCL_E_INTERNAL);
123 :
124 3 : auto* channelImpl = dynamic_cast<CcuUrmaChannel*>(static_cast<Channel*>(channelPtr));
125 3 : CHK_PTR_NULL(channelImpl);
126 33 : TransRmtMemToLocMemInstr(
127 3 : instr++, repRead->GetLoc().addr.Id(), repRead->GetLoc().token.Id(), repRead->GetRem().addr.Id(),
128 6 : repRead->GetRem().token.Id(), repRead->GetLen().Id(), channelImpl->GetChannelId(), repRead->GetDataType(),
129 6 : repRead->GetOpType(), repRead->GetSem().Id(), repRead->GetMask(), 0, 0, 1, 1, repRead->GetReduceFlag());
130 :
131 3 : return HcclResult::HCCL_SUCCESS;
132 : }
133 :
134 0 : HcclResult CcuInsGeneratorV1::CcuRepRemMemTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRemMem* repRemMem)
135 : {
136 : UNUSED(ccuKernel);
137 0 : CHK_PTR_NULL(repRemMem);
138 0 : void* channelPtr{nullptr};
139 0 : CHK_PRT_RET(
140 : static_cast<HcclResult>(HcommChannelGet(repRemMem->GetChannel(), &channelPtr)) != HcclResult::HCCL_SUCCESS,
141 : HCCL_ERROR("failed to get ccu channel, type[%d]", repRemMem->Type()), HCCL_E_INTERNAL);
142 :
143 0 : auto* channelImpl = dynamic_cast<CcuUrmaChannel*>(static_cast<Channel*>(channelPtr));
144 0 : uint32_t size{0}, tokenId{0}, tokenValue{0};
145 0 : uint64_t addr{0};
146 0 : CHK_PTR_NULL(channelImpl);
147 0 : CHK_PRT_RET(
148 : channelImpl->GetRmtBuffer(addr, size, tokenId, tokenValue) != HcclResult::HCCL_SUCCESS,
149 : HCCL_ERROR(
150 : "[CcuRepRemMem][%s] failed to get remote buffer, channelHandle[0x%llx].", __func__,
151 : repRemMem->GetChannel()),
152 : HCCL_E_UNAVAIL); // 当前认为channel只持有一个buffer
153 :
154 0 : auto tokenInfo = GetToken(tokenId, tokenValue, 1);
155 :
156 0 : LoadImdToGSAInstr(instr++, repRemMem->GetRem().addr.Id(), addr);
157 0 : LoadImdToXnInstr(instr++, repRemMem->GetRem().token.Id(), tokenInfo, CCU_LOAD_TO_XN_SEC_INFO);
158 :
159 0 : return HcclResult::HCCL_SUCCESS;
160 : }
161 :
162 3 : HcclResult CcuInsGeneratorV1::CcuRepLocCpyTranslate(
163 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepLocCpy* ccuRepLocCpy, const TransDep& dep)
164 : {
165 : UNUSED(ccuKernel);
166 3 : CHK_PTR_NULL(ccuRepLocCpy);
167 3 : if (ccuRepLocCpy->GetReduceFlag() == 0) {
168 14 : TransLocMemToLocMemInstr(
169 2 : instr++, ccuRepLocCpy->GetDst().addr.Id(), ccuRepLocCpy->GetDst().token.Id(),
170 4 : ccuRepLocCpy->GetSrc().addr.Id(), ccuRepLocCpy->GetSrc().token.Id(), ccuRepLocCpy->GetLen().Id(),
171 4 : dep.reserveChannalId[0], ccuRepLocCpy->GetSem().Id(), ccuRepLocCpy->GetMask(), 0, 0, 1, 1);
172 : } else {
173 : // 这个翻译需要验证
174 10 : TransLocMemToRmtMemInstr(
175 1 : instr++, ccuRepLocCpy->GetDst().addr.Id(), ccuRepLocCpy->GetDst().token.Id(),
176 2 : ccuRepLocCpy->GetSrc().addr.Id(), ccuRepLocCpy->GetSrc().token.Id(), ccuRepLocCpy->GetLen().Id(),
177 1 : dep.reserveChannalId[0], ccuRepLocCpy->GetDataType(), ccuRepLocCpy->GetOpType(),
178 2 : ccuRepLocCpy->GetSem().Id(), ccuRepLocCpy->GetMask(), 0, 0, 1, 1, ccuRepLocCpy->GetReduceFlag());
179 : }
180 :
181 3 : return HcclResult::HCCL_SUCCESS;
182 : }
183 :
184 1 : HcclResult CcuInsGeneratorV1::CcuRepBufWriteTranslate(
185 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufWrite* ccuRepBufWrite, const TransDep& dep)
186 : {
187 : UNUSED(ccuKernel);
188 : UNUSED(dep);
189 1 : CHK_PTR_NULL(ccuRepBufWrite);
190 1 : void* channelPtr{nullptr};
191 1 : CHK_PRT_RET(
192 : static_cast<HcclResult>(HcommChannelGet(ccuRepBufWrite->GetChannel(), &channelPtr))
193 : != HcclResult::HCCL_SUCCESS,
194 : HCCL_ERROR("failed to get ccu channel, type[%d]", ccuRepBufWrite->Type()), HCCL_E_INTERNAL);
195 :
196 1 : auto* channelImpl = dynamic_cast<CcuUrmaChannel*>(static_cast<Channel*>(channelPtr));
197 1 : CHK_PTR_NULL(channelImpl);
198 7 : TransLocMSToRmtMemInstr(
199 1 : instr++, ccuRepBufWrite->GetDst().addr.Id(), ccuRepBufWrite->GetDst().token.Id(),
200 2 : ccuRepBufWrite->GetSrc().Id(), ccuRepBufWrite->GetLen().Id(), channelImpl->GetChannelId(),
201 2 : ccuRepBufWrite->GetSem().Id(), ccuRepBufWrite->GetMask(), 0, 0, 1, 1);
202 :
203 1 : return HcclResult::HCCL_SUCCESS;
204 : }
205 :
206 6 : HcclResult CcuInsGeneratorV1::CcuRepBufReduceTranslate(
207 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufReduce* ccuRepBufReduce)
208 : {
209 : UNUSED(ccuKernel);
210 6 : CHK_PTR_NULL(ccuRepBufReduce);
211 6 : if (ccuRepBufReduce->GetCount() < CCU_REDUCE_MIN_MS) {
212 1 : HCCL_ERROR("count[%u] must be at least %u", ccuRepBufReduce->GetCount(), CCU_REDUCE_MIN_MS);
213 1 : return HCCL_E_PARA;
214 : }
215 5 : if (ccuRepBufReduce->GetCount() > CCU_REDUCE_MAX_MS || ccuRepBufReduce->GetMem().size() > CCU_REDUCE_MAX_MS) {
216 0 : HCCL_ERROR(
217 : "count[%u] and mem size[%zu] must less than %u", ccuRepBufReduce->GetCount(),
218 : ccuRepBufReduce->GetMem().size(), CCU_REDUCE_MAX_MS);
219 0 : return HCCL_E_PARA;
220 : }
221 :
222 : // 这里需要注意,在数据格式膨胀的情况下,需要传入用来存放输出的MSId
223 : // 特别是2P场景,输入MS的数目为2,但是在8bit进,32bit出的场景,输出MS的数目为4
224 : // 传入的MS中已经包含了需要使用的输入输出的最大量,因此,这里应该直接去MS的size
225 5 : auto mem = ccuRepBufReduce->GetMem();
226 5 : uint16_t msId[CCU_REDUCE_MAX_MS] = {0};
227 21 : for (uint16_t i = 0; i < mem.size(); i++) {
228 16 : msId[i] = mem[i].Id();
229 : }
230 :
231 5 : if (ccuRepBufReduce->GetOpType() == CCU_REDUCE_SUM) {
232 3 : if (ccuRepBufReduce->GetOutputDataType() == 1) { // 1是fp16
233 18 : AddInstr(
234 3 : instr++, msId, ccuRepBufReduce->GetCount(), ccuRepBufReduce->GetOutputDataType(),
235 6 : ccuRepBufReduce->GetDataType(), ccuRepBufReduce->GetSem().Id(), ccuRepBufReduce->GetMask(), 0, 0, 1,
236 6 : ccuRepBufReduce->GetXnIdLength().Id());
237 0 : } else if (ccuRepBufReduce->GetOutputDataType() == 2) { // 2是bf16
238 0 : AddInstr(
239 0 : instr++, msId, ccuRepBufReduce->GetCount(), ccuRepBufReduce->GetOutputDataType(),
240 0 : ccuRepBufReduce->GetDataType(), ccuRepBufReduce->GetSem().Id(), ccuRepBufReduce->GetMask(), 0, 0, 1,
241 0 : ccuRepBufReduce->GetXnIdLength().Id());
242 : } else {
243 0 : AddInstr(
244 0 : instr++, msId, ccuRepBufReduce->GetCount(), 0, ccuRepBufReduce->GetDataType(),
245 0 : ccuRepBufReduce->GetSem().Id(), ccuRepBufReduce->GetMask(), 0, 0, 1,
246 0 : ccuRepBufReduce->GetXnIdLength().Id());
247 : }
248 2 : } else if (ccuRepBufReduce->GetOpType() == CCU_REDUCE_MAX) {
249 5 : MaxInstr(
250 1 : instr++, msId, ccuRepBufReduce->GetCount(), ccuRepBufReduce->GetDataType(),
251 2 : ccuRepBufReduce->GetSem().Id(), ccuRepBufReduce->GetMask(), 0, 0, 1,
252 2 : ccuRepBufReduce->GetXnIdLength().Id());
253 1 : } else if (ccuRepBufReduce->GetOpType() == CCU_REDUCE_MIN) {
254 5 : MinInstr(
255 1 : instr++, msId, ccuRepBufReduce->GetCount(), ccuRepBufReduce->GetDataType(),
256 2 : ccuRepBufReduce->GetSem().Id(), ccuRepBufReduce->GetMask(), 0, 0, 1,
257 2 : ccuRepBufReduce->GetXnIdLength().Id());
258 : }
259 :
260 5 : return HcclResult::HCCL_SUCCESS;
261 5 : }
262 :
263 1097 : uint32_t CcuInsGeneratorV1::GetInstrCount(CcuRepType repType)
264 : {
265 1097 : if (repTypeInstrCount.find(repType) == repTypeInstrCount.end()) {
266 4 : Hccl::THROW<Hccl::CcuApiException>("[%s] Unsupported repType[%d]", __func__, repType);
267 : }
268 1093 : return repTypeInstrCount[repType];
269 : }
270 :
271 10 : HcclResult CcuInsGeneratorV1::CcuRepLocRecordEventTranslate(
272 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepLocRecordEvent* ccuRepLocRecordEvent)
273 : {
274 : UNUSED(ccuKernel);
275 10 : CHK_PTR_NULL(ccuRepLocRecordEvent);
276 10 : SetCKEInstr(instr++, ccuRepLocRecordEvent->GetEvent().Id(), ccuRepLocRecordEvent->GetMask(), 0, 0, 1);
277 :
278 10 : return HcclResult::HCCL_SUCCESS;
279 : }
280 :
281 32 : HcclResult CcuInsGeneratorV1::CcuRepLocWaitEventTranslate(
282 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepLocWaitEvent* ccuRepLocWaitEvent)
283 : {
284 : UNUSED(ccuKernel);
285 32 : CHK_PTR_NULL(ccuRepLocWaitEvent);
286 : // SetCKEInstr支持硬件profiling功能
287 32 : if (ccuRepLocWaitEvent->GetIsProfiling()) {
288 21 : SetCKEInstr(instr++, 0, 0, ccuRepLocWaitEvent->GetEvent().Id(), ccuRepLocWaitEvent->GetMask(), 1);
289 : } else {
290 11 : ClearCKEInstr(instr++, 0, 0, ccuRepLocWaitEvent->GetEvent().Id(), ccuRepLocWaitEvent->GetMask(), 1);
291 : }
292 :
293 32 : return HcclResult::HCCL_SUCCESS;
294 : }
295 :
296 2 : HcclResult CcuInsGeneratorV1::CcuRepLocWaitNotifyTranslate(
297 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepLocWaitNotify* ccuRepLocWaitNotify)
298 : {
299 : UNUSED(ccuKernel);
300 2 : CHK_PTR_NULL(ccuRepLocWaitNotify);
301 : // SetCKEInstr支持硬件profiling功能
302 2 : if (ccuRepLocWaitNotify->GetIsProfiling()) {
303 1 : SetCKEInstr(instr++, 0, 0, ccuRepLocWaitNotify->GetNotify().Id(), ccuRepLocWaitNotify->GetMask(), 1);
304 : } else {
305 1 : ClearCKEInstr(instr++, 0, 0, ccuRepLocWaitNotify->GetNotify().Id(), ccuRepLocWaitNotify->GetMask(), 1);
306 : }
307 :
308 2 : return HcclResult::HCCL_SUCCESS;
309 : }
310 :
311 4 : HcclResult CcuInsGeneratorV1::CcuRepRemWaitSemTranslate(
312 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRemWaitSem* cuRepRemWaitSem)
313 : {
314 : UNUSED(ccuKernel);
315 4 : CHK_PTR_NULL(cuRepRemWaitSem);
316 4 : void* channelPtr{nullptr};
317 4 : CHK_PRT_RET(
318 : static_cast<HcclResult>(HcommChannelGet(cuRepRemWaitSem->GetChannel(), &channelPtr))
319 : != HcclResult::HCCL_SUCCESS,
320 : HCCL_ERROR("failed to get ccu channel, type[%d]", cuRepRemWaitSem->Type()), HCCL_E_INTERNAL);
321 :
322 4 : auto* channelImpl = dynamic_cast<CcuUrmaChannel*>(static_cast<Channel*>(channelPtr));
323 4 : CHK_PTR_NULL(channelImpl);
324 4 : uint32_t locCkeId{0};
325 4 : CHK_PRT_RET(
326 : channelImpl->GetLocCkeByIndex(cuRepRemWaitSem->GetSemIndex(), locCkeId) != HcclResult::HCCL_SUCCESS,
327 : HCCL_ERROR(
328 : "[CcuRepRemWaitSem][%s] failed to get loc cke id, channelHandle[0x%llx], semIndex[%u].", __func__,
329 : cuRepRemWaitSem->GetChannel(), cuRepRemWaitSem->GetSemIndex()),
330 : HCCL_E_UNAVAIL);
331 :
332 : // 需要profiling的使用SetCKEInstr, 否则使用ClearCKEInstr
333 4 : if (cuRepRemWaitSem->GetIsProfiling()) {
334 4 : SetCKEInstr(instr++, 0, 0, locCkeId, cuRepRemWaitSem->GetMask(), 1);
335 : } else {
336 0 : ClearCKEInstr(instr++, 0, 0, locCkeId, cuRepRemWaitSem->GetMask(), 1);
337 : }
338 :
339 4 : return HcclResult::HCCL_SUCCESS;
340 : }
341 :
342 5 : HcclResult CcuInsGeneratorV1::CcuRepRemPostVarTranslate(
343 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRemPostVar* ccuRepRemPostVar)
344 : {
345 : UNUSED(ccuKernel);
346 5 : CHK_PTR_NULL(ccuRepRemPostVar);
347 5 : void* channelPtr{nullptr};
348 5 : CHK_PRT_RET(
349 : static_cast<HcclResult>(HcommChannelGet(ccuRepRemPostVar->GetChannel(), &channelPtr))
350 : != HcclResult::HCCL_SUCCESS,
351 : HCCL_ERROR("failed to get ccu channel, type[%d]", ccuRepRemPostVar->Type()), HCCL_E_INTERNAL);
352 :
353 5 : auto* channelImpl = dynamic_cast<CcuUrmaChannel*>(static_cast<Channel*>(channelPtr));
354 5 : CHK_PTR_NULL(channelImpl);
355 5 : uint32_t rmtXnId{0};
356 5 : CHK_PRT_RET(
357 : channelImpl->GetRmtXnByIndex(ccuRepRemPostVar->GetParamIndex(), rmtXnId) != HcclResult::HCCL_SUCCESS,
358 : HCCL_ERROR(
359 : "[CcuRepRemPostSem][%s] failed to get remote xn id, channelHandle[0x%llx].", __func__,
360 : ccuRepRemPostVar->GetChannel()),
361 : HCCL_E_UNAVAIL);
362 :
363 5 : uint32_t rmtCkeId{0};
364 5 : CHK_PRT_RET(
365 : channelImpl->GetRmtCkeByIndex(ccuRepRemPostVar->GetSemIndex(), rmtCkeId) != HcclResult::HCCL_SUCCESS,
366 : HCCL_ERROR(
367 : "[CcuRepRemPostSem][%s] failed to get remote cke id, channelHandle[0x%llx].", __func__,
368 : ccuRepRemPostVar->GetChannel()),
369 : HCCL_E_UNAVAIL);
370 :
371 15 : SyncXnInstr(
372 5 : instr++, rmtXnId, ccuRepRemPostVar->GetParam().Id(), channelImpl->GetChannelId(), rmtCkeId,
373 5 : ccuRepRemPostVar->GetMask(), 0, 0, 0, 0, 1);
374 :
375 5 : return HcclResult::HCCL_SUCCESS;
376 : }
377 :
378 2 : HcclResult CcuInsGeneratorV1::CcuRepRemPostSemTranslate(
379 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRemPostSem* ccuRepRemPostSem, const TransDep& dep)
380 : {
381 : UNUSED(ccuKernel);
382 2 : CHK_PTR_NULL(ccuRepRemPostSem);
383 2 : void* channelPtr{nullptr};
384 2 : CHK_PRT_RET(
385 : static_cast<HcclResult>(HcommChannelGet(ccuRepRemPostSem->GetChannel(), &channelPtr))
386 : != HcclResult::HCCL_SUCCESS,
387 : HCCL_ERROR("failed to get ccu channel, type[%d]", ccuRepRemPostSem->Type()), HCCL_E_INTERNAL);
388 :
389 2 : auto* channelImpl = dynamic_cast<CcuUrmaChannel*>(static_cast<Channel*>(channelPtr));
390 2 : CHK_PTR_NULL(channelImpl);
391 2 : uint32_t rmtCkeId{0};
392 2 : CHK_PRT_RET(
393 : channelImpl->GetRmtCkeByIndex(ccuRepRemPostSem->GetSemIndex(), rmtCkeId) != HcclResult::HCCL_SUCCESS,
394 : HCCL_ERROR(
395 : "[CcuRepRemPostSem][%s] failed to get remote cke id, channelHandle[0x%llx].", __func__,
396 : ccuRepRemPostSem->GetChannel()),
397 : HCCL_E_UNAVAIL);
398 :
399 4 : SyncCKEInstr(
400 2 : instr++, rmtCkeId, dep.reserveCkeId, ccuRepRemPostSem->GetMask(), channelImpl->GetChannelId(), 0, 0, 0, 0,
401 : 1);
402 :
403 2 : return HcclResult::HCCL_SUCCESS;
404 : }
405 :
406 2 : HcclResult CcuInsGeneratorV1::CcuRepRecordSharedNotifyTranslate(
407 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRecordSharedNotify* ccuRepRecordSharedNotify, const TransDep& dep)
408 : {
409 : UNUSED(ccuKernel);
410 2 : CHK_PTR_NULL(ccuRepRecordSharedNotify);
411 : // 非本die时利用环回访问
412 2 : if (ccuRepRecordSharedNotify->GetNotify().DieId() != dep.dieId) {
413 3 : SyncCKEInstr(
414 1 : instr++, ccuRepRecordSharedNotify->GetNotify().Id(), dep.reserveCkeId,
415 1 : ccuRepRecordSharedNotify->GetMask(), dep.reserveChannalId[1], 0, 0, 0, 0, 1);
416 : } else {
417 2 : SetCKEInstr(
418 1 : instr++, ccuRepRecordSharedNotify->GetNotify().Id(), ccuRepRecordSharedNotify->GetMask(), 0, 0, 1);
419 : }
420 :
421 2 : return HcclResult::HCCL_SUCCESS;
422 : }
423 :
424 94 : HcclResult CcuInsGeneratorV1::CcuRepAddTranslate(
425 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepAdd* ccuRepAdd, [[maybe_unused]] const TransDep& dep)
426 : {
427 : UNUSED(ccuKernel);
428 94 : CHK_PTR_NULL(ccuRepAdd);
429 94 : switch (ccuRepAdd->GetSubType()) {
430 1 : case AddSubType::ADDR_PLUS_VAR_TO_ADDR: {
431 3 : LoadGSAXnInstr(
432 1 : instr++, ccuRepAdd->GetAddrC().Id(), ccuRepAdd->GetAddrA().Id(), ccuRepAdd->GetVarB().Id());
433 1 : break;
434 : }
435 2 : case AddSubType::ADDR_PLUS_ADDR_TO_ADDR: {
436 6 : LoadGSAGSAInstr(
437 2 : instr++, ccuRepAdd->GetAddrC().Id(), ccuRepAdd->GetAddrA().Id(), ccuRepAdd->GetAddrB().Id());
438 2 : break;
439 : }
440 63 : case AddSubType::VAR_PLUS_VAR_TO_VAR: {
441 63 : LoadXXInstr(instr++, ccuRepAdd->GetVarC().Id(), ccuRepAdd->GetVarA().Id(), ccuRepAdd->GetVarB().Id());
442 63 : break;
443 : }
444 27 : case AddSubType::SELF_ADD_ADDRESS: {
445 81 : LoadGSAXnInstr(
446 27 : instr++, ccuRepAdd->GetAddrA().Id(), ccuRepAdd->GetAddrA().Id(), ccuRepAdd->GetVarB().Id());
447 27 : break;
448 : }
449 1 : case AddSubType::SELF_ADD_VARIABLE: {
450 1 : LoadXXInstr(instr++, ccuRepAdd->GetVarA().Id(), ccuRepAdd->GetVarA().Id(), ccuRepAdd->GetVarB().Id());
451 1 : break;
452 : }
453 0 : default: {
454 0 : HCCL_ERROR("Invalid Add, subType[%d]", static_cast<int>(ccuRepAdd->GetSubType()));
455 0 : return HCCL_E_PARA;
456 : }
457 : }
458 :
459 94 : return HcclResult::HCCL_SUCCESS;
460 : }
461 :
462 328 : HcclResult CcuInsGeneratorV1::CcuRepAssignTranslate(
463 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepAssign* ccuRepAssign, const TransDep& dep)
464 : {
465 : UNUSED(ccuKernel);
466 328 : CHK_PTR_NULL(ccuRepAssign);
467 328 : switch (ccuRepAssign->GetSubType()) {
468 166 : case AssignSubType::IMD_TO_VARIABLE: {
469 166 : LoadImdToXnInstr(instr++, ccuRepAssign->GetVarA().Id(), ccuRepAssign->GetImmed());
470 166 : break;
471 : }
472 9 : case AssignSubType::IMD_TO_ADDR: {
473 9 : LoadImdToGSAInstr(instr++, ccuRepAssign->GetAddrA().Id(), ccuRepAssign->GetImmed());
474 9 : break;
475 : }
476 10 : case AssignSubType::VAR_TO_ADDR: {
477 10 : LoadGSAXnInstr(instr++, ccuRepAssign->GetAddrA().Id(), dep.reserveGsaId, ccuRepAssign->GetVarA().Id());
478 10 : break;
479 : }
480 23 : case AssignSubType::ADDR_TO_ADDR: {
481 69 : LoadGSAGSAInstr(
482 23 : instr++, ccuRepAssign->GetAddrB().Id(), ccuRepAssign->GetAddrA().Id(), dep.reserveGsaId);
483 23 : break;
484 : }
485 120 : case AssignSubType::VAR_TO_VAR: {
486 120 : LoadXXInstr(instr++, ccuRepAssign->GetVarB().Id(), ccuRepAssign->GetVarA().Id(), dep.reserveXnId);
487 120 : break;
488 : }
489 0 : default: {
490 0 : HCCL_ERROR("Invalid Assign, subType[%d]", static_cast<int>(ccuRepAssign->GetSubType()));
491 0 : return HCCL_E_PARA;
492 : }
493 : }
494 :
495 328 : return HcclResult::HCCL_SUCCESS;
496 : }
497 :
498 0 : HcclResult CcuInsGeneratorV1::CcuRepMulTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepMul* ccuRepMul)
499 : {
500 : UNUSED(ccuKernel);
501 : UNUSED(instr);
502 : UNUSED(ccuRepMul);
503 0 : return HCCL_E_NOT_SUPPORT;
504 : }
505 :
506 0 : HcclResult CcuInsGeneratorV1::CcuRepSubTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepSub* ccuRepSub)
507 : {
508 : UNUSED(ccuKernel);
509 : UNUSED(instr);
510 : UNUSED(ccuRepSub);
511 0 : return HCCL_E_NOT_SUPPORT;
512 : }
513 :
514 0 : HcclResult CcuInsGeneratorV1::CcuRepAndTranslate(
515 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepAnd* ccuRepAnd, const TransDep& dep)
516 : {
517 : UNUSED(ccuKernel);
518 : UNUSED(instr);
519 : UNUSED(ccuRepAnd);
520 : UNUSED(dep);
521 0 : return HCCL_E_NOT_SUPPORT;
522 : }
523 :
524 0 : HcclResult CcuInsGeneratorV1::CcuRepNotTranslate(
525 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepNot* ccuRepNot, const TransDep& dep)
526 : {
527 : UNUSED(ccuKernel);
528 : UNUSED(instr);
529 : UNUSED(ccuRepNot);
530 : UNUSED(dep);
531 0 : return HCCL_E_NOT_SUPPORT;
532 : }
533 :
534 0 : HcclResult CcuInsGeneratorV1::CcuRepOrTranslate(
535 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepOr* ccuRepOr, const TransDep& dep)
536 : {
537 : UNUSED(ccuKernel);
538 : UNUSED(instr);
539 : UNUSED(ccuRepOr);
540 : UNUSED(dep);
541 0 : return HCCL_E_NOT_SUPPORT;
542 : }
543 :
544 0 : HcclResult CcuInsGeneratorV1::CcuRepXorTranslate(
545 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepXor* ccuRepXor, const TransDep& dep)
546 : {
547 : UNUSED(ccuKernel);
548 : UNUSED(instr);
549 : UNUSED(ccuRepXor);
550 : UNUSED(dep);
551 0 : return HCCL_E_NOT_SUPPORT;
552 : }
553 :
554 0 : HcclResult CcuInsGeneratorV1::CcuRepShLTranslate(
555 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepShL* ccuRepShL, const TransDep& dep)
556 : {
557 : UNUSED(ccuKernel);
558 : UNUSED(instr);
559 : UNUSED(ccuRepShL);
560 : UNUSED(dep);
561 0 : return HCCL_E_NOT_SUPPORT;
562 : }
563 :
564 0 : HcclResult CcuInsGeneratorV1::CcuRepShRTranslate(
565 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepShR* ccuRepShR, const TransDep& dep)
566 : {
567 : UNUSED(ccuKernel);
568 : UNUSED(instr);
569 : UNUSED(ccuRepShR);
570 : UNUSED(dep);
571 0 : return HCCL_E_NOT_SUPPORT;
572 : }
573 :
574 8 : HcclResult CcuInsGeneratorV1::CcuRepFuncBlockTranslate(
575 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepFuncBlock* funcBlockPtr,
576 : const TransDep& dep, uint32_t step)
577 : {
578 : UNUSED(ccuKernel);
579 8 : CHK_PTR_NULL(funcBlockPtr);
580 8 : std::vector<CcuRepArg>& outArgs = funcBlockPtr->GetOutArgs();
581 8 : CcuRepReferenceManager* funcManager = funcBlockPtr->GetFuncManager();
582 8 : CHK_PTR_NULL(funcManager);
583 8 : if (step == 0) {
584 : // 函数入口为nop
585 4 : LoadImdToXnInstr(instr++, dep.reserveXnId, 0); // 向记录常量0的Xn再次赋值0作为nop操作
586 4 : curInstrId++;
587 4 : } else if (step == 1) {
588 : // 处理输出的参数
589 4 : uint32_t iOutArg = 0;
590 4 : for (uint32_t i = 0; i < outArgs.size(); i++) {
591 0 : if (outArgs[i].type == CcuArgType::VARIABLE) {
592 0 : LoadXXInstr(
593 0 : instr++, funcManager->GetFuncOut()[iOutArg++].Id(), outArgs[i].var.Id(), dep.reserveXnId);
594 0 : curInstrId++;
595 0 : } else if (outArgs[i].type == CcuArgType::VARIABLE_LIST) {
596 0 : for (uint32_t j = 0; j < outArgs[i].varList.size(); j++) {
597 0 : LoadXXInstr(
598 0 : instr++, funcManager->GetFuncOut()[iOutArg++].Id(), outArgs[i].varList[j].Id(),
599 0 : dep.reserveXnId);
600 0 : curInstrId++;
601 : }
602 : }
603 : }
604 :
605 : // 返回调用处
606 4 : JumpInstr(instr++, funcManager->GetFuncRet(funcBlockPtr->GetCallLayer()).Id(), dep.reserveXnId, 1);
607 4 : curInstrId++;
608 : } else {
609 0 : HCCL_ERROR("Unsupported step[%d] for CcuRepFuncBlockTranslate", step);
610 0 : return HCCL_E_PARA;
611 : }
612 8 : return HcclResult::HCCL_SUCCESS;
613 : }
614 :
615 5 : void CcuInsGeneratorV1::LoadFuncCallInArgs(
616 : CcuInstr* instr, std::vector<CcuRepArg>& inArgs, std::vector<Variable>& formalIns, uint16_t reserveXnId)
617 : {
618 5 : uint32_t idx = 0;
619 12 : for (uint32_t i = 0; i < inArgs.size(); i++) {
620 7 : if (inArgs[i].type == CcuArgType::VARIABLE) {
621 7 : LoadXXInstr(instr + idx, formalIns[idx].Id(), inArgs[i].var.Id(), reserveXnId);
622 7 : idx++;
623 0 : } else if (inArgs[i].type == CcuArgType::VARIABLE_LIST) {
624 0 : for (uint32_t j = 0; j < inArgs[i].varList.size(); j++) {
625 0 : LoadXXInstr(instr + idx, formalIns[idx].Id(), inArgs[i].varList[j].Id(), reserveXnId);
626 0 : idx++;
627 : }
628 : }
629 : }
630 5 : }
631 :
632 5 : void CcuInsGeneratorV1::LoadFuncCallOutArgs(
633 : CcuInstr* instr, uint32_t offset, std::vector<CcuRepArg>& outArgs, CcuRepReferenceManager* funcManager,
634 : uint16_t reserveXnId)
635 : {
636 5 : uint32_t idx = 0;
637 5 : for (uint32_t i = 0; i < outArgs.size(); i++) {
638 0 : if (outArgs[i].type == CcuArgType::VARIABLE) {
639 0 : LoadXXInstr(
640 0 : instr + offset + idx, outArgs[i].var.Id(), funcManager->GetFuncOut()[idx].Id(), reserveXnId);
641 0 : idx++;
642 0 : } else if (outArgs[i].type == CcuArgType::VARIABLE_LIST) {
643 0 : for (uint32_t j = 0; j < outArgs[i].varList.size(); j++) {
644 0 : LoadXXInstr(
645 0 : instr + offset + idx, outArgs[i].varList[j].Id(), funcManager->GetFuncOut()[idx].Id(),
646 : reserveXnId);
647 0 : idx++;
648 : }
649 : }
650 : }
651 5 : }
652 :
653 5 : HcclResult CcuInsGeneratorV1::CcuRepFuncCallTranslate(
654 : CcuKernel* ccuKernel, CcuInstr*& curInstr, uint16_t& curInstrId, CcuRepFuncCall* funcCallPtr,
655 : const TransDep& dep)
656 : {
657 : UNUSED(ccuKernel);
658 : (void)curInstrId;
659 : (void)curInstr;
660 :
661 5 : FuncCallContext ctx;
662 5 : CHK_RET(PrepareFuncCallContext(funcCallPtr, ctx));
663 :
664 5 : std::vector<CcuRepArg>& outArgs = funcCallPtr->GetOutArgs();
665 5 : std::vector<CcuRepArg>& inArgs = funcCallPtr->GetInArgs();
666 5 : uint32_t inArgCount = ctx.inArgCount;
667 5 : CcuInstr* instr = ctx.instr;
668 5 : CcuRepReferenceManager* funcManager = ctx.funcManager;
669 5 : std::vector<Variable>& formalIns = ctx.formalIns;
670 5 : std::shared_ptr<CcuRepFuncBlock>& funcBlock = ctx.funcBlock;
671 5 : LoadFuncCallInArgs(instr, inArgs, formalIns, dep.reserveXnId);
672 :
673 5 : uint32_t locId = 0;
674 5 : if (funcBlock != nullptr) {
675 5 : LoadImdToXnInstr(instr + inArgCount + locId++, funcManager->GetFuncCall().Id(), funcBlock->StartInstrId());
676 : } else {
677 0 : LoadXXInstr(
678 0 : instr + inArgCount + locId++, funcManager->GetFuncCall().Id(), funcCallPtr->GetFuncAddrVar().Id(),
679 0 : dep.reserveXnId);
680 : }
681 :
682 10 : LoadImdToXnInstr(
683 5 : instr + inArgCount + locId++, funcManager->GetFuncRet(funcCallPtr->GetCallLayer()).Id(),
684 5 : funcCallPtr->StartInstrId() + inArgCount + 3); // 需要指向函数返回位置,为输入指令Id + 3
685 5 : JumpInstr(instr + inArgCount + locId++, funcManager->GetFuncCall().Id(), dep.reserveXnId, 1);
686 5 : LoadImdToXnInstr(instr + inArgCount + locId++, dep.reserveXnId, 0);
687 :
688 5 : uint32_t extraInstrNum = GetInstrCount(funcCallPtr->Type());
689 5 : LoadFuncCallOutArgs(instr, inArgCount + extraInstrNum, outArgs, funcManager, dep.reserveXnId);
690 5 : return HcclResult::HCCL_SUCCESS;
691 5 : }
692 :
693 22 : HcclResult CcuInsGeneratorV1::CcuRepJumpTranslate(
694 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJump* jumpPtr, const TransDep& dep)
695 : {
696 : UNUSED(ccuKernel);
697 : (void)instr;
698 : (void)curInstrId;
699 :
700 : // 翻译直接跳转指令
701 22 : CHK_PTR_NULL(jumpPtr);
702 22 : std::shared_ptr<CcuRepJumpLabel> jumpLabel = jumpPtr->GetJumpLabel();
703 22 : CHK_PTR_NULL(jumpLabel);
704 22 : LoadImdToXnInstr(jumpPtr->GetInstr() + 0, jumpPtr->GetTargetInstrId().Id(), jumpLabel->StartInstrId());
705 22 : JumpInstr(jumpPtr->GetInstr() + 1, jumpPtr->GetTargetInstrId().Id(), dep.reserveXnId, 1);
706 :
707 22 : return HcclResult::HCCL_SUCCESS;
708 22 : }
709 :
710 77 : HcclResult CcuInsGeneratorV1::CcuRepJumpNETranslate(
711 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpNE* jumpNEPtr,
712 : [[maybe_unused]] const TransDep& dep)
713 : {
714 : UNUSED(ccuKernel);
715 : UNUSED(instr);
716 : UNUSED(curInstrId);
717 77 : CHK_PTR_NULL(jumpNEPtr);
718 77 : std::shared_ptr<CcuRepJumpLabel> jumpLabel = jumpNEPtr->GetJumpLabel();
719 77 : CHK_PTR_NULL(jumpLabel);
720 77 : LoadImdToXnInstr(jumpNEPtr->GetInstr() + 0, jumpNEPtr->GetTargetInstrId().Id(), jumpLabel->StartInstrId());
721 154 : JumpInstr(
722 77 : jumpNEPtr->GetInstr() + 1, jumpNEPtr->GetTargetInstrId().Id(), jumpNEPtr->GetCondition().Id(),
723 : jumpNEPtr->GetExpectedNum());
724 :
725 77 : return HcclResult::HCCL_SUCCESS;
726 77 : }
727 :
728 10 : HcclResult CcuInsGeneratorV1::CcuRepJumpEQTranslate(
729 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpEQ* jumpEQPtr, const TransDep& dep)
730 : {
731 : UNUSED(ccuKernel);
732 : UNUSED(instr);
733 : UNUSED(curInstrId);
734 :
735 10 : CHK_PTR_NULL(jumpEQPtr);
736 10 : std::shared_ptr<CcuRepJumpLabel> jumpLabel = jumpEQPtr->GetJumpLabel();
737 10 : CHK_PTR_NULL(jumpLabel);
738 10 : uint32_t localInstrIndex = 0;
739 10 : CcuInstr* startInstr = jumpEQPtr->GetInstr();
740 10 : Variable& targetInstrId = jumpEQPtr->GetTargetInstrId();
741 20 : LoadImdToXnInstr(
742 10 : startInstr + localInstrIndex++, targetInstrId.Id(),
743 10 : jumpEQPtr->StartInstrId() + 4); // 需要指向NOP位置,为输入指令Id + 4
744 10 : JumpInstr(
745 10 : startInstr + localInstrIndex++, targetInstrId.Id(), jumpEQPtr->GetCondition().Id(),
746 : jumpEQPtr->GetExpectedNum());
747 10 : LoadImdToXnInstr(startInstr + localInstrIndex++, targetInstrId.Id(), jumpLabel->StartInstrId());
748 10 : JumpInstr(startInstr + localInstrIndex++, targetInstrId.Id(), dep.reserveXnId, 1);
749 10 : LoadImdToXnInstr(startInstr + localInstrIndex++, dep.reserveXnId, 0);
750 :
751 10 : return HcclResult::HCCL_SUCCESS;
752 10 : }
753 :
754 0 : HcclResult CcuInsGeneratorV1::CcuRepJumpLETranslate(
755 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpLE* jumpLEPtr,
756 : [[maybe_unused]] const TransDep& dep)
757 : {
758 : UNUSED(ccuKernel);
759 : (void)instr;
760 : (void)curInstrId;
761 0 : CHK_PTR_NULL(jumpLEPtr);
762 0 : HCCL_ERROR("Unsupported Jump type for CcuV1: %s", jumpLEPtr->Describe().c_str());
763 0 : return HCCL_E_NOT_SUPPORT;
764 : }
765 :
766 0 : HcclResult CcuInsGeneratorV1::CcuRepJumpGETranslate(
767 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpGE* jumpGEPtr,
768 : [[maybe_unused]] const TransDep& dep)
769 : {
770 : UNUSED(ccuKernel);
771 : (void)instr;
772 : (void)curInstrId;
773 0 : CHK_PTR_NULL(jumpGEPtr);
774 0 : HCCL_ERROR("Unsupported Jump type for CcuV1: %s", jumpGEPtr->Describe().c_str());
775 0 : return HCCL_E_NOT_SUPPORT;
776 : }
777 :
778 0 : HcclResult CcuInsGeneratorV1::CcuRepJumpGTTranslate(
779 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpGT* jumpGTPtr,
780 : [[maybe_unused]] const TransDep& dep)
781 : {
782 : UNUSED(ccuKernel);
783 : (void)instr;
784 : (void)curInstrId;
785 0 : CHK_PTR_NULL(jumpGTPtr);
786 0 : HCCL_ERROR("Unsupported Jump type for CcuV1: %s", jumpGTPtr->Describe().c_str());
787 0 : return HCCL_E_NOT_SUPPORT;
788 : }
789 :
790 0 : HcclResult CcuInsGeneratorV1::CcuRepJumpLTTranslate(
791 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpLT* jumpLTPtr,
792 : [[maybe_unused]] const TransDep& dep)
793 : {
794 : UNUSED(ccuKernel);
795 : (void)instr;
796 : (void)curInstrId;
797 0 : CHK_PTR_NULL(jumpLTPtr);
798 0 : HCCL_ERROR("Unsupported Jump type for CcuV1: %s", jumpLTPtr->Describe().c_str());
799 0 : return HCCL_E_NOT_SUPPORT;
800 : }
801 :
802 0 : HcclResult CcuInsGeneratorV1::CcuRepLoopTranslate(
803 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoop* loopPtr)
804 : {
805 : UNUSED(ccuKernel);
806 : UNUSED(curInstrId);
807 0 : CHK_PTR_NULL(loopPtr);
808 0 : auto loopBlock = loopPtr->GetLoopBlock();
809 0 : CHK_PTR_NULL(loopBlock);
810 :
811 0 : LoopInstr(
812 0 : instr++, loopBlock->StartInstrId(), loopBlock->StartInstrId() + loopBlock->InstrCount() - 1,
813 0 : loopPtr->GetLoopParam()->Id());
814 0 : return HcclResult::HCCL_SUCCESS;
815 : }
816 :
817 0 : HcclResult CcuInsGeneratorV1::LoadLoopCallArg(
818 : CcuInstr*& instr, const CcuRepArg& inArg, const CcuRepArg& blkArg, const TransDep& dep)
819 : {
820 0 : switch (inArg.type) {
821 0 : case CcuArgType::VARIABLE:
822 0 : LoadXXInstr(instr++, blkArg.var.Id(), inArg.var.Id(), dep.reserveXnId);
823 0 : break;
824 0 : case CcuArgType::VARIABLE_LIST:
825 0 : if (inArg.varList.size() != blkArg.varList.size()) {
826 0 : HCCL_ERROR(
827 : "Mismatched Arg Size, inArg.varList.size[%zu], blkArg.varList.size[%zu]", inArg.varList.size(),
828 : blkArg.varList.size());
829 0 : return HCCL_E_PARA;
830 : }
831 0 : for (uint32_t j = 0; j < inArg.varList.size(); j++) {
832 0 : LoadXXInstr(instr++, blkArg.varList[j].Id(), inArg.varList[j].Id(), dep.reserveXnId);
833 : }
834 0 : break;
835 0 : case CcuArgType::MEMORY:
836 0 : LoadAddrArg(instr, blkArg.mem, inArg.mem, dep);
837 0 : break;
838 0 : case CcuArgType::LOCAL_ADDR:
839 0 : LoadAddrArg(instr, blkArg.localAddr, inArg.localAddr, dep);
840 0 : break;
841 0 : case CcuArgType::REMOTE_ADDR:
842 0 : LoadAddrArg(instr, blkArg.remoteAddr, inArg.remoteAddr, dep);
843 0 : break;
844 0 : case CcuArgType::MEMORY_LIST:
845 0 : CHK_RET(LoadAddrListArg(instr, blkArg.memList, inArg.memList, dep));
846 0 : break;
847 0 : case CcuArgType::LOCAL_ADDR_LIST:
848 0 : CHK_RET(LoadAddrListArg(instr, blkArg.localAddrList, inArg.localAddrList, dep));
849 0 : break;
850 0 : case CcuArgType::REMOTE_ADDR_LIST:
851 0 : CHK_RET(LoadAddrListArg(instr, blkArg.remoteAddrList, inArg.remoteAddrList, dep));
852 0 : break;
853 0 : default:
854 0 : HCCL_ERROR("Mismatched Arg Type, inArg.type[%d]", static_cast<int>(inArg.type));
855 0 : return HCCL_E_PARA;
856 : }
857 0 : return HcclResult::HCCL_SUCCESS;
858 : }
859 :
860 0 : HcclResult CcuInsGeneratorV1::CcuRepLoopCallTranslate(
861 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoopCall* loopCallPtr, const TransDep& dep)
862 : {
863 : UNUSED(ccuKernel);
864 : UNUSED(curInstrId);
865 0 : CHK_PTR_NULL(loopCallPtr);
866 0 : auto loopBlock = loopCallPtr->GetLoopBlock();
867 0 : CHK_PTR_NULL(loopBlock);
868 0 : std::vector<CcuRepArg>& inArgs = loopCallPtr->GetInArgs();
869 :
870 0 : for (uint32_t i = 0; i < inArgs.size(); i++) {
871 0 : const CcuRepArg& blkArg = loopBlock->GetArg(i);
872 0 : const CcuRepArg& inArg = inArgs[i];
873 0 : if (inArg.type != blkArg.type) {
874 0 : HCCL_ERROR(
875 : "Mismatched Arg Type, inArg.type[%d], blkArg.type[%d]", static_cast<int>(inArg.type),
876 : static_cast<int>(blkArg.type));
877 0 : return HCCL_E_PARA;
878 : }
879 0 : CHK_RET(LoadLoopCallArg(instr, inArg, blkArg, dep));
880 : }
881 :
882 0 : return HcclResult::HCCL_SUCCESS;
883 0 : }
884 :
885 0 : HcclResult CcuInsGeneratorV1::CcuRepSetLoopTranslate(
886 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepSetLoop* setLoopPtr)
887 : {
888 : UNUSED(ccuKernel);
889 : (void)curInstrId;
890 0 : CHK_PTR_NULL(setLoopPtr);
891 0 : LoadImdToXnInstr(instr++, setLoopPtr->loopParam.Id(), GetLoopParam(setLoopPtr->executor.Id(), 0, 0));
892 0 : LoadXXInstr(instr++, setLoopPtr->loopParam.Id(), setLoopPtr->loopParam.Id(), setLoopPtr->var.Id());
893 0 : return HcclResult::HCCL_SUCCESS;
894 : }
895 :
896 3 : HcclResult CcuInsGeneratorV1::CcuRepLoadTranslate(
897 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoad* loadPtr, const TransDep& dep)
898 : {
899 : UNUSED(ccuKernel);
900 : (void)curInstrId;
901 3 : CHK_PTR_NULL(loadPtr);
902 3 : uint64_t varAddr = dep.xnBaseAddr[dep.dieId] + CCU_RESOURCE_XN_PER_SIZE * loadPtr->GetVar().Id();
903 :
904 3 : LoadImdToGSAInstr(instr++, dep.commGsa[0], varAddr);
905 3 : LoadImdToGSAInstr(instr++, dep.commGsa[1], loadPtr->GetAddr());
906 3 : LoadImdToXnInstr(instr++, dep.commXn[0], dep.ccuResSpaceTokenInfo, CCU_LOAD_TO_XN_SEC_INFO);
907 3 : LoadImdToXnInstr(instr++, dep.commXn[1], dep.memTokenInfo, CCU_LOAD_TO_XN_SEC_INFO);
908 3 : LoadImdToXnInstr(instr++, dep.commXn[2], CCU_RESOURCE_XN_PER_SIZE * loadPtr->GetNum());
909 3 : TransLocMemToLocMemInstr(
910 3 : instr++, dep.commGsa[0], dep.commXn[0], dep.commGsa[1], dep.commXn[1], dep.commXn[2],
911 3 : dep.reserveChannalId[0], dep.commSignal, loadPtr->GetMask(), 0, 0, 1, 1);
912 3 : SetCKEInstr(instr++, 0, 0, dep.commSignal, loadPtr->GetMask(), 1);
913 :
914 3 : return HcclResult::HCCL_SUCCESS;
915 : }
916 :
917 3 : HcclResult CcuInsGeneratorV1::CcuRepLoadVarTranslate(
918 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoadVar* loadVarPtr, const TransDep& dep)
919 : {
920 : UNUSED(ccuKernel);
921 : (void)curInstrId;
922 3 : CHK_PTR_NULL(loadVarPtr);
923 3 : uint64_t varAddr = dep.xnBaseAddr[dep.dieId] + CCU_RESOURCE_XN_PER_SIZE * loadVarPtr->GetVar().Id();
924 3 : LoadImdToGSAInstr(instr++, dep.commGsa[0], varAddr);
925 3 : LoadGSAXnInstr(instr++, dep.commGsa[1], dep.reserveGsaId, loadVarPtr->GetSrc().Id());
926 3 : LoadImdToXnInstr(instr++, dep.commXn[0], dep.ccuResSpaceTokenInfo, CCU_LOAD_TO_XN_SEC_INFO);
927 3 : LoadImdToXnInstr(instr++, dep.commXn[1], dep.memTokenInfo, CCU_LOAD_TO_XN_SEC_INFO);
928 3 : LoadImdToXnInstr(instr++, dep.commXn[2], CCU_RESOURCE_XN_PER_SIZE * loadVarPtr->GetNum());
929 3 : TransLocMemToLocMemInstr(
930 3 : instr++, dep.commGsa[0], dep.commXn[0], dep.commGsa[1], dep.commXn[1], dep.commXn[2],
931 3 : dep.reserveChannalId[0], dep.commSignal, loadVarPtr->GetMask(), 0, 0, 1, 1);
932 3 : SetCKEInstr(instr++, 0, 0, dep.commSignal, loadVarPtr->GetMask(), 1);
933 :
934 3 : return HcclResult::HCCL_SUCCESS;
935 : }
936 :
937 34 : HcclResult CcuInsGeneratorV1::CcuRepLoadArgTranslate(
938 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoadArg* loadArgPtr, const TransDep& dep)
939 : {
940 : UNUSED(ccuKernel);
941 : (void)curInstrId;
942 34 : CHK_PTR_NULL(loadArgPtr);
943 34 : if (dep.isFuncBlock) {
944 : // Xn(var) = Xn(loadXnId) + 0
945 1 : LoadXXInstr(instr++, loadArgPtr->GetVar().Id(), dep.loadXnId, dep.reserveXnId);
946 : } else {
947 33 : LoadSqeArgsToXnInstr(instr++, loadArgPtr->GetVar().Id(), loadArgPtr->GetArgId());
948 : }
949 :
950 34 : return HcclResult::HCCL_SUCCESS;
951 : }
952 :
953 115 : HcclResult CcuInsGeneratorV1::CcuRepNopTranslate(
954 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepNop* nopPtr, const TransDep& dep)
955 : {
956 : UNUSED(ccuKernel);
957 : (void)curInstrId;
958 : (void)nopPtr;
959 115 : LoadImdToXnInstr(instr++, dep.reserveXnId, 0);
960 :
961 115 : return HcclResult::HCCL_SUCCESS;
962 : }
963 :
964 3 : HcclResult CcuInsGeneratorV1::CcuRepStoreTranslate(
965 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepStore* storePtr, const TransDep& dep)
966 : {
967 : UNUSED(ccuKernel);
968 : (void)curInstrId;
969 3 : CHK_PTR_NULL(storePtr);
970 3 : uint64_t varAddr = dep.xnBaseAddr[dep.dieId] + CCU_RESOURCE_XN_PER_SIZE * storePtr->GetVar().Id();
971 :
972 3 : LoadImdToGSAInstr(instr++, dep.commGsa[0], storePtr->GetAddr());
973 3 : LoadImdToGSAInstr(instr++, dep.commGsa[1], varAddr);
974 3 : LoadImdToXnInstr(instr++, dep.commXn[0], dep.memTokenInfo, CCU_LOAD_TO_XN_SEC_INFO);
975 3 : LoadImdToXnInstr(instr++, dep.commXn[1], dep.ccuResSpaceTokenInfo, CCU_LOAD_TO_XN_SEC_INFO);
976 3 : LoadImdToXnInstr(instr++, dep.commXn[2], CCU_RESOURCE_XN_PER_SIZE * storePtr->GetNum());
977 3 : TransLocMemToLocMemInstr(
978 3 : instr++, dep.commGsa[0], dep.commXn[0], dep.commGsa[1], dep.commXn[1], dep.commXn[2],
979 3 : dep.reserveChannalId[0], dep.commSignal, storePtr->GetMask(), 0, 0, 1, 1);
980 3 : SetCKEInstr(instr++, 0, 0, dep.commSignal, storePtr->GetMask(), 1);
981 :
982 3 : return HcclResult::HCCL_SUCCESS;
983 : }
984 :
985 3 : HcclResult CcuInsGeneratorV1::CcuRepStoreVarTranslate(
986 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepStoreVar* storeVarPtr, const TransDep& dep)
987 : {
988 : UNUSED(ccuKernel);
989 : (void)curInstrId;
990 3 : CHK_PTR_NULL(storeVarPtr);
991 3 : uint64_t varAddr = dep.xnBaseAddr[dep.dieId] + CCU_RESOURCE_XN_PER_SIZE * storeVarPtr->GetVar().Id();
992 :
993 3 : LoadImdToGSAInstr(instr++, dep.commGsa[0], varAddr);
994 3 : LoadGSAXnInstr(instr++, dep.commGsa[1], dep.reserveGsaId, storeVarPtr->GetDst().Id());
995 3 : LoadImdToXnInstr(instr++, dep.commXn[0], dep.memTokenInfo, CCU_LOAD_TO_XN_SEC_INFO);
996 3 : LoadImdToXnInstr(instr++, dep.commXn[1], dep.ccuResSpaceTokenInfo, CCU_LOAD_TO_XN_SEC_INFO);
997 3 : LoadImdToXnInstr(instr++, dep.commXn[2], CCU_RESOURCE_XN_PER_SIZE * storeVarPtr->GetNum());
998 3 : TransLocMemToLocMemInstr(
999 3 : instr++, dep.commGsa[1], dep.commXn[0], dep.commGsa[0], dep.commXn[1], dep.commXn[2],
1000 3 : dep.reserveChannalId[0], dep.commSignal, storeVarPtr->GetMask(), 0, 0, 1, 1);
1001 3 : SetCKEInstr(instr++, 0, 0, dep.commSignal, storeVarPtr->GetMask(), 1);
1002 :
1003 3 : return HcclResult::HCCL_SUCCESS;
1004 : }
1005 :
1006 20 : HcclResult CcuInsGeneratorV1::CcuRepLoopGroupBundleTranslate(
1007 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoopGroupBundle* bundlePtr,
1008 : const TransDep& dep)
1009 : {
1010 : UNUSED(ccuKernel);
1011 20 : CHK_PTR_NULL(bundlePtr);
1012 20 : const auto& loops = bundlePtr->GetLoops();
1013 :
1014 58 : for (const auto& loop : loops) {
1015 38 : if (loop.layout == CcuRepLoopGroupBundle::Layout::Config) {
1016 19 : uint64_t lpImm = GetLoopParam(loop.executor.Id(), loop.config.addrOffset, loop.config.iterNum);
1017 19 : LoadImdToXnInstr(instr++, loop.loopParamVar.Id(), lpImm);
1018 19 : curInstrId++;
1019 : } else {
1020 19 : uint64_t ctxImm = static_cast<uint64_t>(loop.executor.Id()) << 45; // 左移45位到对应字段然后相加
1021 19 : LoadImdToXnInstr(instr++, dep.reserveXnId, ctxImm);
1022 19 : curInstrId++;
1023 19 : LoadXXInstr(instr++, loop.loopParamVar.Id(), loop.loopParamVar.Id(), dep.reserveXnId);
1024 19 : curInstrId++;
1025 : }
1026 : }
1027 :
1028 20 : if (bundlePtr->GetLayout() == CcuRepLoopGroupBundle::Layout::Config) {
1029 10 : uint64_t parallelImm = GetParallelParam(
1030 10 : bundlePtr->GetConfig().cloneNum, bundlePtr->GetRepeatLoopIdx(), bundlePtr->GetTotalLoopNum());
1031 10 : LoadImdToXnInstr(instr++, bundlePtr->GetParallelVar().Id(), parallelImm);
1032 10 : curInstrId++;
1033 :
1034 40 : uint64_t offsetImm = ::hcomm::CcuRep::GetOffsetParam(
1035 10 : bundlePtr->GetConfig().addrOffset, bundlePtr->GetConfig().ccuBufferOffset,
1036 10 : bundlePtr->GetConfig().eventOffset);
1037 10 : LoadImdToXnInstr(instr++, bundlePtr->GetOffsetParam().Id(), offsetImm);
1038 10 : curInstrId++;
1039 : }
1040 :
1041 40 : LoopGroupInstr(
1042 20 : instr++, curInstrId + 3, bundlePtr->GetParallelVar().Id(), bundlePtr->GetOffsetParam().Id(),
1043 : 0); // 向后3条为loop指令
1044 20 : curInstrId++;
1045 :
1046 20 : uint16_t loopCount = static_cast<uint16_t>(loops.size());
1047 20 : uint16_t jumpTargetInstrId = curInstrId + 2 + loopCount + 1; // 跳转目标为向后2条+loop指令条数+额外1条
1048 20 : LoadImdToXnInstr(instr++, dep.reserveXnId, jumpTargetInstrId);
1049 20 : curInstrId++;
1050 20 : JumpInstr(instr++, dep.reserveXnId, dep.reserveXnId, 1);
1051 20 : curInstrId++;
1052 :
1053 58 : for (const auto& loop : loops) {
1054 38 : const auto& block = loop.repLoopBlock;
1055 38 : CHK_PTR_NULL(block);
1056 114 : LoopInstr(
1057 38 : instr++, block->StartInstrId(), block->StartInstrId() + block->InstrCount() - 1,
1058 38 : loop.loopParamVar.Id());
1059 38 : curInstrId++;
1060 : }
1061 :
1062 20 : LoadImdToXnInstr(instr++, dep.reserveXnId, 0);
1063 20 : curInstrId++;
1064 :
1065 20 : return HcclResult::HCCL_SUCCESS;
1066 : }
1067 :
1068 55 : uint16_t CcuInsGeneratorV1::CcuRepLoopGroupBundleInstrCount(CcuRepLoopGroupBundle* bundlePtr)
1069 : {
1070 55 : if (bundlePtr == nullptr) {
1071 0 : Hccl::THROW<Hccl::CcuApiException>("[%s] bundlePtr is nullptr", __func__);
1072 : }
1073 55 : const auto& loops = bundlePtr->GetLoops();
1074 55 : const uint16_t loopCount = static_cast<uint16_t>(loops.size());
1075 55 : uint16_t varBasedLoopCount = 0;
1076 164 : for (const auto& loop : loops) {
1077 109 : if (loop.layout != CcuRepLoopGroupBundle::Layout::Config) {
1078 57 : varBasedLoopCount++;
1079 : }
1080 : }
1081 : // 每 loop:config 1 条载入 / var 2 条;config bundle 额外 2 条(parallel+offset);+loopgroup1 +跳过2 +每loop
1082 : // Loop1 +收尾1
1083 55 : const uint16_t groupOffset = (loopCount - varBasedLoopCount) + (varBasedLoopCount * 2)
1084 55 : + (bundlePtr->GetLayout() == CcuRepLoopGroupBundle::Layout::Config ? 2 : 0);
1085 55 : return groupOffset + 1 + 2 + loopCount + 1; // 跳过2条
1086 : }
1087 :
1088 : } // namespace CcuRep
1089 : } // namespace hcomm
|