LCOV - code coverage report
Current view: top level - base_comm/resources/ccu/ccu_representation/reps/translator - ccu_ins_generator_v2.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 49.0 % 993 487
Test Date: 2026-08-18 17:47:01 Functions: 54.7 % 75 41

            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_v2.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 "hcom_common.h"
      18              : 
      19              : #include <iostream>
      20              : 
      21              : namespace hcomm {
      22              : 
      23              : namespace CcuRep {
      24              : #define UNUSED(x) static_cast<void>(x)
      25              : 
      26              :     namespace {
      27              :         constexpr uint8_t URMA_DMA_OP_READ = 0x6;     // UB URMA WQEBB opcode: read
      28              :         constexpr uint8_t URMA_DMA_OP_WRITE = 0x3;    // UB URMA WQEBB opcode: write
      29              :         constexpr uint32_t REL_JMP_INSTR_NUM = 9;     // RelJmp生成的指令数
      30              :         constexpr uint32_t FUNC_CALL_JMP_OFFSET = 11; // RelJmp+Jump+Nop
      31              :         constexpr uint32_t FUNC_CALL_RET_OFFSET = 12; // FUNC_CALL_JMP_OFFSET + 1
      32              :         // loopParamVar 布局:iterNum[12:0] gsaOffset[44:13] ctxId[52:45]
      33              :         constexpr uint64_t LOOP_ITER_NUM_MASK = 0x1FFFULL;
      34              :         constexpr uint32_t LOOP_GSA_OFFSET_SHIFT = 13;
      35              :         constexpr uint64_t LOOP_GSA_OFFSET_MASK = 0xFFFFFFFFULL;
      36              :         constexpr uint64_t LOOP_FIELD_MASK = 0x7FULL;
      37              : 
      38              :         template <typename T>
      39            0 :         void LoadAddrArg(CcuInstr*& instr, const T& dst, const T& src)
      40              :         {
      41            0 :             CcuV2::Assign(instr++, dst.addr.Id(), src.addr.Id());
      42            0 :             CcuV2::Assign(instr++, dst.token.Id(), src.token.Id());
      43            0 :         }
      44              : 
      45              :         template <typename T>
      46            0 :         HcclResult LoadAddrListArg(CcuInstr*& instr, const std::vector<T>& dst, const std::vector<T>& src)
      47              :         {
      48            0 :             if (src.size() != dst.size()) {
      49            0 :                 HCCL_ERROR("Mismatched Arg Size: srcSize[%u], dstSize[%u]", src.size(), dst.size());
      50            0 :                 return HCCL_E_PARA;
      51              :             }
      52            0 :             for (uint32_t j = 0; j < src.size(); j++) {
      53            0 :                 LoadAddrArg(instr, dst[j], src[j]);
      54              :             }
      55            0 :             return HcclResult::HCCL_SUCCESS;
      56              :         }
      57           12 :         HcclResult GetUrmaChannel(ChannelHandle channelHandle, CcuUrmaChannel*& channelImpl)
      58              :         {
      59           12 :             void* channelPtr{nullptr};
      60           12 :             CHK_RET(static_cast<HcclResult>(HcommChannelGet(channelHandle, &channelPtr)));
      61           12 :             channelImpl = dynamic_cast<CcuUrmaChannel*>(static_cast<Channel*>(channelPtr));
      62           12 :             CHK_PTR_NULL(channelImpl);
      63           12 :             return HcclResult::HCCL_SUCCESS;
      64              :         }
      65              : 
      66            8 :         HcclResult GetRmtToken(CcuUrmaChannel* channelImpl, uint64_t& rmtToken)
      67              :         {
      68            8 :             uint32_t rmtTokenId{0};
      69            8 :             uint32_t rmtTokenValue{0};
      70            8 :             CHK_PTR_NULL(channelImpl);
      71            8 :             CHK_RET(channelImpl->GetRmtCcuBufferTokenInfo(rmtTokenId, rmtTokenValue));
      72            8 :             rmtToken = CcuRep::GetToken(rmtTokenId, rmtTokenValue, 1);
      73            8 :             return HcclResult::HCCL_SUCCESS;
      74              :         }
      75              :     } // namespace
      76              : 
      77          471 :     uint32_t CcuInsGeneratorV2::GetInstrCount(CcuRepType repType)
      78              :     {
      79          471 :         if (repTypeInstrCount.find(repType) == repTypeInstrCount.end()) {
      80            0 :             Hccl::THROW<Hccl::CcuApiException>("[%s] Unsupported repType[%d]", __func__, repType);
      81              :         }
      82          471 :         return repTypeInstrCount[repType];
      83              :     }
      84              : 
      85            4 :     HcclResult CcuInsGeneratorV2::CcuRepBufLocReadTranslate(
      86              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufLocRead* repBufLocRead, const TransDep& dep)
      87              :     {
      88            4 :         CHK_PTR_NULL(repBufLocRead);
      89            4 :         CcuV2::CacheConfig config = {};
      90              : 
      91           24 :         TransLocMemToLocMS(
      92            4 :             instr++, repBufLocRead->GetDst().Id(), repBufLocRead->GetSrc().addr.Id(),
      93            8 :             repBufLocRead->GetSrc().token.Id(), repBufLocRead->GetLen().Id(), dep.reserveXnId,
      94            8 :             repBufLocRead->GetSem().Id(), repBufLocRead->GetMask(), config);
      95              : 
      96            4 :         return HcclResult::HCCL_SUCCESS;
      97              :     }
      98              : 
      99            2 :     HcclResult CcuInsGeneratorV2::CcuRepBufLocWriteTranslate(
     100              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufLocWrite* repBufLocWrite, const TransDep& dep)
     101              :     {
     102            2 :         CHK_PTR_NULL(repBufLocWrite);
     103            2 :         CcuV2::CacheConfig config = {};
     104              : 
     105           12 :         TransLocMSToLocMem(
     106            2 :             instr++, repBufLocWrite->GetDst().addr.Id(), repBufLocWrite->GetDst().token.Id(),
     107            4 :             repBufLocWrite->GetSrc().Id(), repBufLocWrite->GetLen().Id(), dep.reserveXnId,
     108            4 :             repBufLocWrite->GetSem().Id(), repBufLocWrite->GetMask(), config);
     109              : 
     110            2 :         return HcclResult::HCCL_SUCCESS;
     111              :     }
     112              : 
     113            0 :     HcclResult CcuInsGeneratorV2::CcuRepBufReadTranslate(
     114              :         CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufRead* repBufRead, const TransDep& dep)
     115              :     {
     116            0 :         CHK_PTR_NULL(repBufRead);
     117            0 :         CHK_PTR_NULL(ccuKernel);
     118            0 :         CcuUrmaChannel* channelImpl{nullptr};
     119            0 :         CHK_RET(GetUrmaChannel(repBufRead->GetChannel(), channelImpl));
     120              : 
     121            0 :         CcuV2::TransMemNotifyInfo notify = {};
     122            0 :         CcuV2::TransMemReduceInfo reduce = {};
     123            0 :         CcuV2::TransMemConfig config = {};
     124            0 :         config.dmaOpCode = URMA_DMA_OP_READ; // UB URMA WQEBB opcode: read
     125            0 :         config.src_mode = 1;
     126            0 :         config.dst_mode = 0;
     127            0 :         config.msIdMode = 1;
     128            0 :         uint32_t channelId = channelImpl->GetChannelId();
     129            0 :         const auto& constValue2VarMap = ccuKernel->GetConstValue2VarMap();
     130            0 :         TransMem(
     131            0 :             instr++, repBufRead->GetDst().Id(), constValue2VarMap.at(dep.ccuResSpaceTokenInfo).Id(),
     132            0 :             repBufRead->GetSrc().addr.Id(), repBufRead->GetSrc().token.Id(), repBufRead->GetLen().Id(),
     133            0 :             constValue2VarMap.at(channelId).Id(), notify, reduce, config, repBufRead->GetSem().Id(),
     134            0 :             repBufRead->GetMask());
     135              : 
     136            0 :         return HcclResult::HCCL_SUCCESS;
     137              :     }
     138              : 
     139            0 :     HcclResult CcuInsGeneratorV2::CcuRepWriteTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepWrite* repWrite)
     140              :     {
     141            0 :         CHK_PTR_NULL(repWrite);
     142            0 :         CHK_PTR_NULL(ccuKernel);
     143            0 :         CcuUrmaChannel* channelImpl{nullptr};
     144            0 :         CHK_RET(GetUrmaChannel(repWrite->GetChannel(), channelImpl));
     145            0 :         CcuV2::TransMemNotifyInfo notify = {};
     146            0 :         CcuV2::TransMemReduceInfo reduce = {};
     147            0 :         CcuV2::TransMemConfig config = {};
     148            0 :         config.dmaOpCode = URMA_DMA_OP_WRITE; // UB URMA WQEBB opcode: write
     149            0 :         config.src_mode = 0;
     150            0 :         config.dst_mode = 1;
     151              : 
     152            0 :         if (repWrite->GetReduceFlag() == 1) {
     153            0 :             config.udfEnable = 1;
     154            0 :             reduce.udfType = 0;
     155            0 :             reduce.reduceDataType = repWrite->GetDataType();
     156            0 :             reduce.reduceOpCode = repWrite->GetOpType();
     157              :         }
     158            0 :         uint32_t channelId = channelImpl->GetChannelId();
     159            0 :         const auto& constValue2VarMap = ccuKernel->GetConstValue2VarMap();
     160              : 
     161            0 :         uint64_t rmtCcuResToken{};
     162            0 :         CHK_RET(GetRmtToken(channelImpl, rmtCcuResToken));
     163            0 :         notify.xntId = constValue2VarMap.at(rmtCcuResToken).Id();
     164            0 :         TransMem(
     165            0 :             instr++, repWrite->GetRem().addr.Id(), repWrite->GetRem().token.Id(), repWrite->GetLoc().addr.Id(),
     166            0 :             repWrite->GetLoc().token.Id(), repWrite->GetLen().Id(), constValue2VarMap.at(channelId).Id(), notify,
     167            0 :             reduce, config, repWrite->GetSem().Id(), repWrite->GetMask());
     168              : 
     169            0 :         return HcclResult::HCCL_SUCCESS;
     170              :     }
     171              : 
     172            1 :     HcclResult CcuInsGeneratorV2::CcuRepReadTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRead* repRead)
     173              :     {
     174            1 :         CHK_PTR_NULL(repRead);
     175            1 :         CHK_PTR_NULL(ccuKernel);
     176            1 :         CcuUrmaChannel* channelImpl{nullptr};
     177            1 :         CHK_RET(GetUrmaChannel(repRead->GetChannel(), channelImpl));
     178              : 
     179            1 :         CcuV2::TransMemNotifyInfo notify = {};
     180            1 :         CcuV2::TransMemReduceInfo reduce = {};
     181            1 :         CcuV2::TransMemConfig config = {};
     182            1 :         config.dmaOpCode = URMA_DMA_OP_READ; // UB URMA WQEBB opcode: read
     183              : 
     184            1 :         if (repRead->GetReduceFlag() == 1) {
     185            0 :             config.udfEnable = 1;
     186            0 :             reduce.udfType = 0;
     187            0 :             reduce.reduceDataType = repRead->GetDataType();
     188            0 :             reduce.reduceOpCode = repRead->GetOpType();
     189              :         }
     190            1 :         uint32_t channelId = channelImpl->GetChannelId();
     191            1 :         const auto& constValue2VarMap = ccuKernel->GetConstValue2VarMap();
     192            8 :         TransMem(
     193            1 :             instr++, repRead->GetLoc().addr.Id(), repRead->GetLoc().token.Id(), repRead->GetRem().addr.Id(),
     194            2 :             repRead->GetRem().token.Id(), repRead->GetLen().Id(), constValue2VarMap.at(channelId).Id(), notify, reduce,
     195            2 :             config, repRead->GetSem().Id(), repRead->GetMask());
     196              : 
     197            1 :         return HcclResult::HCCL_SUCCESS;
     198              :     }
     199              : 
     200            0 :     HcclResult CcuInsGeneratorV2::CcuRepRemMemTranslate(
     201              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRemMem* repRemMem)
     202              :     {
     203            0 :         CHK_PTR_NULL(repRemMem);
     204            0 :         CcuUrmaChannel* channelImpl{nullptr};
     205            0 :         CHK_RET(GetUrmaChannel(repRemMem->GetChannel(), channelImpl));
     206              : 
     207            0 :         uint64_t addr{0};
     208            0 :         uint32_t size{0}, tokenId{0}, tokenValue{0};
     209            0 :         CHK_PRT_RET(
     210              :             channelImpl->GetRmtBuffer(addr, size, tokenId, tokenValue) != HcclResult::HCCL_SUCCESS,
     211              :             HCCL_ERROR(
     212              :                 "[CcuRepRemMem][%s] failed to get remote buffer, channelHandle[0x%llx].", __func__,
     213              :                 repRemMem->GetChannel()),
     214              :             HCCL_E_INTERNAL); // 当前认为channel只持有一个buffer
     215              : 
     216            0 :         auto tokenInfo = GetToken(tokenId, tokenValue, 1);
     217              : 
     218            0 :         CcuV2::LoadImdToXn(instr++, repRemMem->GetRem().addr.Id(), addr);
     219            0 :         CcuV2::LoadImdToXn(instr++, repRemMem->GetRem().token.Id(), tokenInfo);
     220              : 
     221            0 :         return HcclResult::HCCL_SUCCESS;
     222              :     }
     223              : 
     224            0 :     HcclResult CcuInsGeneratorV2::CcuRepLocCpyTranslate(
     225              :         CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepLocCpy* repLocCpy, const TransDep& dep)
     226              :     {
     227            0 :         CHK_PTR_NULL(repLocCpy);
     228            0 :         if (repLocCpy->GetReduceFlag() == 0 && repLocCpy->GetUseCcuBuffer() == true) {
     229            0 :             CcuV2::CacheConfig cacheConfig{0x0, 0x0};
     230            0 :             CcuV2::TransLocMemToLocMem(
     231            0 :                 instr++, repLocCpy->GetDstAddrId(), repLocCpy->GetDstTokenId(), repLocCpy->GetSrcAddrId(),
     232            0 :                 repLocCpy->GetSrcTokenId(), repLocCpy->GetLenId(), repLocCpy->GetFirstBufId(),
     233            0 :                 repLocCpy->GetUsedBufNum(), repLocCpy->GetSemId(), repLocCpy->GetMask(), cacheConfig, cacheConfig);
     234              :         } else {
     235              :             // 使用旧接口或带规约场景,都走环回
     236            0 :             CcuV2::TransMemNotifyInfo notify = {};
     237            0 :             CcuV2::TransMemReduceInfo reduce = {};
     238            0 :             CcuV2::TransMemConfig config = {};
     239            0 :             config.dmaOpCode = URMA_DMA_OP_WRITE; // UB URMA WQEBB opcode: write
     240            0 :             if (repLocCpy->GetReduceFlag() == 1) {
     241            0 :                 config.udfEnable = 1;
     242            0 :                 reduce.udfType = 0;
     243            0 :                 reduce.reduceDataType = repLocCpy->GetDataType();
     244            0 :                 reduce.reduceOpCode = repLocCpy->GetOpType();
     245              :             }
     246            0 :             const auto& constValue2VarMap = ccuKernel->GetConstValue2VarMap();
     247            0 :             CcuV2::TransMem(
     248            0 :                 instr++, repLocCpy->GetDstAddrId(), repLocCpy->GetDstTokenId(), repLocCpy->GetSrcAddrId(),
     249            0 :                 repLocCpy->GetSrcTokenId(), repLocCpy->GetLenId(), constValue2VarMap.at(dep.reserveChannalId[0]).Id(),
     250            0 :                 notify, reduce, config, repLocCpy->GetSemId(), repLocCpy->GetMask());
     251              :         }
     252              : 
     253            0 :         return HcclResult::HCCL_SUCCESS;
     254              :     }
     255              : 
     256            0 :     HcclResult CcuInsGeneratorV2::CcuRepBufWriteTranslate(
     257              :         CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufWrite* ccuRepBufWrite, const TransDep& dep)
     258              :     {
     259            0 :         CHK_PTR_NULL(ccuRepBufWrite);
     260            0 :         CHK_PTR_NULL(ccuKernel);
     261            0 :         CcuV2::TransMemNotifyInfo notify = {};
     262            0 :         CcuV2::TransMemReduceInfo reduce = {};
     263            0 :         CcuV2::TransMemConfig config = {};
     264            0 :         config.dmaOpCode = URMA_DMA_OP_WRITE; // UB URMA WQEBB opcode: write
     265            0 :         config.src_mode = 0;                  // 不偏移
     266            0 :         config.dst_mode = 1;                  // 偏移 256K
     267            0 :         config.msIdMode = 1;                  // 使用msId模式
     268            0 :         CcuUrmaChannel* channelImpl{nullptr};
     269            0 :         CHK_RET(GetUrmaChannel(ccuRepBufWrite->GetChannel(), channelImpl));
     270              : 
     271            0 :         uint32_t channelId = channelImpl->GetChannelId();
     272            0 :         const auto& constValue2VarMap = ccuKernel->GetConstValue2VarMap();
     273              : 
     274            0 :         uint64_t rmtCcuResToken{};
     275            0 :         CHK_RET(GetRmtToken(channelImpl, rmtCcuResToken));
     276            0 :         notify.xntId = constValue2VarMap.at(rmtCcuResToken).Id();
     277            0 :         HCCL_INFO(
     278              :             "DstTokenXnId[%u], NotifyTokenXnId[%u]", constValue2VarMap.at(dep.ccuResSpaceTokenInfo).Id(), notify.xntId);
     279            0 :         TransMem(
     280            0 :             instr++, ccuRepBufWrite->GetDst().addr.Id(), ccuRepBufWrite->GetDst().token.Id(),
     281            0 :             ccuRepBufWrite->GetSrc().Id(), constValue2VarMap.at(dep.ccuResSpaceTokenInfo).Id(),
     282            0 :             ccuRepBufWrite->GetLen().Id(), constValue2VarMap.at(channelId).Id(), notify, reduce, config,
     283            0 :             ccuRepBufWrite->GetSem().Id(), ccuRepBufWrite->GetMask());
     284              : 
     285            0 :         return HcclResult::HCCL_SUCCESS;
     286              :     }
     287              : 
     288            2 :     HcclResult CcuInsGeneratorV2::CcuRepBufReduceTranslate(
     289              :         CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufReduce* ccuRepBufReduce)
     290              :     {
     291              :         UNUSED(ccuKernel);
     292            2 :         CHK_PTR_NULL(ccuRepBufReduce);
     293            2 :         if (ccuRepBufReduce->GetCount() > CCU_REDUCE_MAX_MS || ccuRepBufReduce->GetMem().size() > CCU_REDUCE_MAX_MS) {
     294            0 :             HCCL_ERROR(
     295              :                 "count[%u] and mem size[%zu] must less than %u", ccuRepBufReduce->GetCount(),
     296              :                 ccuRepBufReduce->GetMem().size(), CCU_REDUCE_MAX_MS);
     297            0 :             return HCCL_E_PARA;
     298              :         }
     299            2 :         if (ccuRepBufReduce->GetCount() < CCU_REDUCE_MIN_MS) {
     300            0 :             HCCL_ERROR("count[%u] must be at least %u", ccuRepBufReduce->GetCount(), CCU_REDUCE_MIN_MS);
     301            0 :             return HCCL_E_PARA;
     302              :         }
     303              : 
     304            2 :         uint16_t msId[CCU_REDUCE_MAX_MS] = {0};
     305            2 :         const auto& mem = ccuRepBufReduce->GetMem();
     306            6 :         for (uint16_t i = 0; i < mem.size(); i++) {
     307            4 :             msId[i] = mem[i].Id();
     308              :         }
     309              : 
     310            2 :         if (ccuRepBufReduce->GetOpType() == CCU_REDUCE_SUM) {
     311            2 :             if (ccuRepBufReduce->GetOutputDataType() == 1) { // 1是fp16
     312           12 :                 CcuV2::ReduceAdd(
     313            2 :                     instr++, msId, ccuRepBufReduce->GetCount(), ccuRepBufReduce->GetOutputDataType(),
     314            4 :                     ccuRepBufReduce->GetDataType(), ccuRepBufReduce->GetSem().Id(), ccuRepBufReduce->GetMask(),
     315            4 :                     ccuRepBufReduce->GetXnIdLength().Id());
     316            0 :             } else if (ccuRepBufReduce->GetOutputDataType() == 2) { // 2是bf16
     317            0 :                 CcuV2::ReduceAdd(
     318            0 :                     instr++, msId, ccuRepBufReduce->GetCount(), ccuRepBufReduce->GetOutputDataType(),
     319            0 :                     ccuRepBufReduce->GetDataType(), ccuRepBufReduce->GetSem().Id(), ccuRepBufReduce->GetMask(),
     320            0 :                     ccuRepBufReduce->GetXnIdLength().Id());
     321              :             } else {
     322            0 :                 CcuV2::ReduceAdd(
     323            0 :                     instr++, msId, ccuRepBufReduce->GetCount(), 0, ccuRepBufReduce->GetDataType(),
     324            0 :                     ccuRepBufReduce->GetSem().Id(), ccuRepBufReduce->GetMask(), ccuRepBufReduce->GetXnIdLength().Id());
     325              :             }
     326            0 :         } else if (ccuRepBufReduce->GetOpType() == CCU_REDUCE_MAX) {
     327            0 :             CcuV2::ReduceMax(
     328            0 :                 instr++, msId, ccuRepBufReduce->GetCount(), ccuRepBufReduce->GetDataType(),
     329            0 :                 ccuRepBufReduce->GetSem().Id(), ccuRepBufReduce->GetMask(), ccuRepBufReduce->GetXnIdLength().Id());
     330            0 :         } else if (ccuRepBufReduce->GetOpType() == CCU_REDUCE_MIN) {
     331            0 :             CcuV2::ReduceMin(
     332            0 :                 instr++, msId, ccuRepBufReduce->GetCount(), ccuRepBufReduce->GetDataType(),
     333            0 :                 ccuRepBufReduce->GetSem().Id(), ccuRepBufReduce->GetMask(), ccuRepBufReduce->GetXnIdLength().Id());
     334              :         } else {
     335            0 :             HCCL_ERROR("[CcuRepBufReduceTranslate] Unsupported opType[%d]", ccuRepBufReduce->GetOpType());
     336            0 :             return HCCL_E_PARA;
     337              :         }
     338              : 
     339            2 :         return HcclResult::HCCL_SUCCESS;
     340              :     }
     341              : 
     342            1 :     HcclResult CcuInsGeneratorV2::CcuRepLocRecordEventTranslate(
     343              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepLocRecordEvent* ccuRepLocRecordEvent)
     344              :     {
     345            1 :         CHK_PTR_NULL(ccuRepLocRecordEvent);
     346            1 :         CcuV2::SetCKE(instr++, ccuRepLocRecordEvent->GetEvent().Id(), ccuRepLocRecordEvent->GetMask(), 0, 0, 1);
     347              : 
     348            1 :         return HcclResult::HCCL_SUCCESS;
     349              :     }
     350              : 
     351            7 :     HcclResult CcuInsGeneratorV2::CcuRepLocWaitEventTranslate(
     352              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepLocWaitEvent* ccuRepLocWaitEvent)
     353              :     {
     354            7 :         CHK_PTR_NULL(ccuRepLocWaitEvent);
     355              :         // 需要profiling的使用SetCKEInstr, 否则使用ClearCKEInstr
     356            7 :         if (ccuRepLocWaitEvent->GetIsProfiling()) {
     357            1 :             CcuV2::SetCKE(instr++, 0, 0, ccuRepLocWaitEvent->GetEvent().Id(), ccuRepLocWaitEvent->GetMask(), 1);
     358              :         } else {
     359            6 :             CcuV2::ClearCKE(instr++, 0, 0, ccuRepLocWaitEvent->GetEvent().Id(), ccuRepLocWaitEvent->GetMask(), 1);
     360              :         }
     361              : 
     362            7 :         return HcclResult::HCCL_SUCCESS;
     363              :     }
     364              : 
     365            0 :     HcclResult CcuInsGeneratorV2::CcuRepLocWaitNotifyTranslate(
     366              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepLocWaitNotify* ccuRepLocWaitNotify)
     367              :     {
     368            0 :         CHK_PTR_NULL(ccuRepLocWaitNotify);
     369            0 :         if (ccuRepLocWaitNotify->GetIsProfiling()) {
     370            0 :             CcuV2::SetCKE(instr++, 0, 0, ccuRepLocWaitNotify->GetNotify().Id(), ccuRepLocWaitNotify->GetMask(), 1);
     371              :         } else {
     372            0 :             CcuV2::ClearCKE(instr++, 0, 0, ccuRepLocWaitNotify->GetNotify().Id(), ccuRepLocWaitNotify->GetMask(), 1);
     373              :         }
     374              : 
     375            0 :         return HcclResult::HCCL_SUCCESS;
     376              :     }
     377              : 
     378            2 :     HcclResult CcuInsGeneratorV2::CcuRepRemWaitSemTranslate(
     379              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRemWaitSem* ccuRepRemWaitSem)
     380              :     {
     381            2 :         CHK_PTR_NULL(ccuRepRemWaitSem);
     382            2 :         CcuUrmaChannel* channelImpl{nullptr};
     383            2 :         CHK_RET(GetUrmaChannel(ccuRepRemWaitSem->GetChannel(), channelImpl));
     384              : 
     385            2 :         uint32_t locCkeId{0};
     386            2 :         CHK_PRT_RET(
     387              :             channelImpl->GetLocCkeByIndex(ccuRepRemWaitSem->GetSemIndex(), locCkeId) != HcclResult::HCCL_SUCCESS,
     388              :             HCCL_ERROR(
     389              :                 "[CcuRepRemWaitSem][%s] failed to get loc cke id, channelHandle[0x%llx], semIndex[%u].", __func__,
     390              :                 ccuRepRemWaitSem->GetChannel(), ccuRepRemWaitSem->GetSemIndex()),
     391              :             HCCL_E_INTERNAL);
     392              : 
     393              :         // 需要profiling的使用SetCKEInstr, 否则使用ClearCKEInstr
     394            2 :         if (ccuRepRemWaitSem->GetIsProfiling()) {
     395            2 :             CcuV2::SetCKE(instr++, 0, 0, locCkeId, ccuRepRemWaitSem->GetMask(), 1);
     396              :         } else {
     397            0 :             CcuV2::ClearCKE(instr++, 0, 0, locCkeId, ccuRepRemWaitSem->GetMask(), 1);
     398              :         }
     399              : 
     400            2 :         return HcclResult::HCCL_SUCCESS;
     401              :     }
     402              : 
     403            3 :     HcclResult CcuInsGeneratorV2::CcuRepRemPostVarTranslate(
     404              :         CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRemPostVar* ccuRepRemPostVar)
     405              :     {
     406            3 :         CHK_PTR_NULL(ccuRepRemPostVar);
     407            3 :         CHK_PTR_NULL(ccuKernel);
     408            3 :         CcuUrmaChannel* channelImpl{nullptr};
     409            3 :         CHK_RET(GetUrmaChannel(ccuRepRemPostVar->GetChannel(), channelImpl));
     410              : 
     411            3 :         uint32_t channelId = channelImpl->GetChannelId();
     412            3 :         uint64_t rmtSignalAddr{0};
     413            3 :         uint64_t rmtVarAddr{0};
     414            3 :         CHK_PRT_RET(
     415              :             channelImpl->GetRmtSignalAddrByIndex(ccuRepRemPostVar->GetSemIndex(), rmtSignalAddr)
     416              :                 != HcclResult::HCCL_SUCCESS,
     417              :             HCCL_ERROR(
     418              :                 "[CcuInsGeneratorV2][%s] failed to get remote signal addr, channelHandle[0x%llx].", __func__,
     419              :                 ccuRepRemPostVar->GetChannel()),
     420              :             HCCL_E_INTERNAL);
     421            3 :         CHK_PRT_RET(
     422              :             channelImpl->GetRmtVarAddrByIndex(ccuRepRemPostVar->GetParamIndex(), rmtVarAddr)
     423              :                 != HcclResult::HCCL_SUCCESS,
     424              :             HCCL_ERROR(
     425              :                 "[CcuInsGeneratorV2][%s] failed to get remote var addr, channelHandle[0x%llx].", __func__,
     426              :                 ccuRepRemPostVar->GetChannel()),
     427              :             HCCL_E_INTERNAL);
     428            3 :         uint64_t rmtToken{};
     429            3 :         CHK_RET(GetRmtToken(channelImpl, rmtToken));
     430              : 
     431            3 :         const auto& constValue2VarMap = ccuKernel->GetConstValue2VarMap();
     432            3 :         CcuV2::TransMemNotifyInfo notify = {};
     433            3 :         notify.xnId = constValue2VarMap.at(rmtSignalAddr).Id();
     434            3 :         notify.xntId = constValue2VarMap.at(rmtToken).Id();
     435            3 :         notify.value = ccuRepRemPostVar->GetMask();
     436              : 
     437           12 :         SyncWtX(
     438            3 :             instr++, constValue2VarMap.at(rmtVarAddr).Id(), constValue2VarMap.at(rmtToken).Id(),
     439            6 :             ccuRepRemPostVar->GetParam().Id(), constValue2VarMap.at(channelId).Id(), notify, 0, 0);
     440              : 
     441            3 :         return HcclResult::HCCL_SUCCESS;
     442              :     }
     443              : 
     444            1 :     HcclResult CcuInsGeneratorV2::CcuRepRemPostSemTranslate(
     445              :         CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRemPostSem* ccuRepRemPostSem,
     446              :         [[maybe_unused]] const TransDep& dep)
     447              :     {
     448            1 :         CHK_PTR_NULL(ccuRepRemPostSem);
     449            1 :         CHK_PTR_NULL(ccuKernel);
     450            1 :         CcuUrmaChannel* channelImpl{nullptr};
     451            1 :         CHK_RET(GetUrmaChannel(ccuRepRemPostSem->GetChannel(), channelImpl));
     452              : 
     453            1 :         uint64_t rmtSignalAddr{0};
     454            1 :         uint32_t channelId = channelImpl->GetChannelId();
     455            1 :         CHK_PRT_RET(
     456              :             channelImpl->GetRmtSignalAddrByIndex(ccuRepRemPostSem->GetSemIndex(), rmtSignalAddr)
     457              :                 != HcclResult::HCCL_SUCCESS,
     458              :             HCCL_ERROR(
     459              :                 "[CcuInsGeneratorV2][%s] failed to get remote signal addr, channelHandle[0x%llx].", __func__,
     460              :                 ccuRepRemPostSem->GetChannel()),
     461              :             HCCL_E_INTERNAL);
     462            1 :         uint64_t rmtToken{};
     463            1 :         CHK_RET(GetRmtToken(channelImpl, rmtToken));
     464              : 
     465            1 :         const auto& constValue2VarMap = ccuKernel->GetConstValue2VarMap();
     466            1 :         CcuV2::TransMemNotifyInfo notify = {};
     467            1 :         notify.xnId = constValue2VarMap.at(rmtSignalAddr).Id();
     468            1 :         notify.xntId = constValue2VarMap.at(rmtToken).Id();
     469            1 :         notify.value = ccuRepRemPostSem->GetMask();
     470              : 
     471            1 :         CcuV2::SyncWtX(instr++, notify, constValue2VarMap.at(channelId).Id(), 0, 0);
     472              : 
     473            1 :         return HcclResult::HCCL_SUCCESS;
     474              :     }
     475              : 
     476              :     constexpr uint16_t CCU_RESOURCE_CKE_MASK_LENGTH = 2;
     477            0 :     HcclResult CcuInsGeneratorV2::CcuRepRecordSharedNotifyTranslate(
     478              :         CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRecordSharedNotify* ccuRepRecordSharedNotify, const TransDep& dep)
     479              :     {
     480            0 :         CHK_PTR_NULL(ccuRepRecordSharedNotify);
     481            0 :         CHK_PTR_NULL(ccuKernel);
     482            0 :         if (ccuRepRecordSharedNotify->GetNotify().DieId() != dep.dieId) {
     483              :             // 计算对端cke地址,对端Xn基地址 + 256K + (ccuRepRecordSharedNotify->GetNotify().Id() * 8)
     484            0 :             uint64_t ckeAddr = dep.xnBaseAddr[ccuRepRecordSharedNotify->GetNotify().DieId()]
     485              :                                + CCU_RESOURCE_XN_V2_RESERVE_SIZE
     486            0 :                                + (ccuRepRecordSharedNotify->GetNotify().Id() * CCU_RESOURCE_CKE_PER_SIZE);
     487            0 :             const auto& constValue2VarMap = ccuKernel->GetConstValue2VarMap();
     488            0 :             CcuV2::CacheConfig cacheConfig{0x0, 0x0};
     489            0 :             CcuV2::StoreXToMem(
     490            0 :                 instr++,
     491            0 :                 constValue2VarMap.at(ckeAddr).Id(),                             // 对端cke xn
     492            0 :                 constValue2VarMap.at(dep.memTokenInfo).Id(),                    // 对端cke xn token
     493            0 :                 constValue2VarMap.at(ccuRepRecordSharedNotify->GetMask()).Id(), // cke mask
     494            0 :                 constValue2VarMap.at(CCU_RESOURCE_CKE_MASK_LENGTH).Id(),        // cke mask,2字节
     495              :                 cacheConfig,
     496            0 :                 dep.commSignal, // store完成后同步的cke
     497              :                 1);             // store完成后同步的cke mask
     498            0 :             CcuV2::SetCKE(instr++, 0, 0, dep.commSignal, 1, 1);
     499              :         } else {
     500            0 :             CcuV2::SetCKE(
     501            0 :                 instr++, ccuRepRecordSharedNotify->GetNotify().Id(), ccuRepRecordSharedNotify->GetMask(), 0, 0, 1);
     502              :         }
     503              : 
     504            0 :         return HcclResult::HCCL_SUCCESS;
     505              :     }
     506              : 
     507           62 :     HcclResult CcuInsGeneratorV2::CcuRepAddTranslate(
     508              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepAdd* ccuRepAdd, const TransDep& dep)
     509              :     {
     510           62 :         HCCL_INFO("Use table-driven translate for add");
     511              :         UNUSED(dep);
     512           62 :         CHK_PTR_NULL(ccuRepAdd);
     513              : 
     514              :         // {isImmed, isSelf, dstVar, srcAVar, srcBAddr} — 按 AddSubType 枚举顺序
     515              :         static constexpr struct {
     516              :             bool isImmed;
     517              :             bool isSelf;
     518              :             bool dstVar;
     519              :             bool srcAVar;
     520              :             bool srcBAddr;
     521              :         } table[] = {
     522              :             {false, false, false, false, false}, // INVALID
     523              :             {false, false, false, false, false}, // ADDR_PLUS_VAR_TO_ADDR
     524              :             {false, false, false, false, true},  // ADDR_PLUS_ADDR_TO_ADDR
     525              :             {false, false, true, true, false},   // VAR_PLUS_VAR_TO_VAR
     526              :             {false, true, false, false, false},  // SELF_ADD_ADDRESS
     527              :             {false, true, true, true, false},    // SELF_ADD_VARIABLE
     528              :             {true, false, true, true, false},    // VAR_PLUS_IMMED_TO_VAR
     529              :             {true, false, false, false, false},  // ADDR_PLUS_IMMED_TO_ADDR
     530              :             {false, false, false, true, false},  // VAR_PLUS_VAR_TO_ADDR
     531              :             {true, true, false, false, false},   // SELF_ADD_IMMED_ADDRESS
     532              :             {true, true, true, true, false},     // SELF_ADD_IMMED_VARIABLE
     533              :             {true, false, false, true, false},   // VAR_PLUS_IMMED_TO_ADDR
     534              :             {true, false, true, false, false},   // ADDR_PLUS_IMMED_TO_VAR
     535              :             {false, false, true, false, true},   // ADDR_PLUS_ADDR_TO_VAR
     536              :         };
     537              : 
     538           62 :         auto idx = static_cast<size_t>(ccuRepAdd->GetSubType());
     539           62 :         if (idx == 0 || idx >= sizeof(table) / sizeof(table[0])) {
     540            0 :             HCCL_ERROR("Invalid AddSubType[%d]", idx);
     541            0 :             return HCCL_E_PARA;
     542              :         }
     543           62 :         const auto& info = table[idx];
     544              : 
     545           62 :         uint16_t srcAId = info.srcAVar ? ccuRepAdd->GetVarA().Id() : ccuRepAdd->GetAddrA().Id();
     546           62 :         uint16_t dstId = info.isSelf ? srcAId : (info.dstVar ? ccuRepAdd->GetVarC().Id() : ccuRepAdd->GetAddrC().Id());
     547              : 
     548           62 :         if (info.isImmed) {
     549            5 :             CcuV2::AddI(instr++, dstId, srcAId, ccuRepAdd->GetImmedB(), 0, 0);
     550              :         } else {
     551           57 :             uint16_t srcBId = info.srcBAddr ? ccuRepAdd->GetAddrB().Id() : ccuRepAdd->GetVarB().Id();
     552           57 :             CcuV2::Add(instr++, dstId, srcAId, srcBId, 0, 0);
     553              :         }
     554              : 
     555           62 :         return HcclResult::HCCL_SUCCESS;
     556              :     }
     557              : 
     558          157 :     HcclResult CcuInsGeneratorV2::CcuRepAssignTranslate(
     559              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepAssign* ccuRepAssign, const TransDep& dep)
     560              :     {
     561              :         UNUSED(dep);
     562          157 :         CHK_PTR_NULL(ccuRepAssign);
     563          157 :         switch (ccuRepAssign->GetSubType()) {
     564           96 :             case AssignSubType::IMD_TO_VARIABLE: {
     565           96 :                 CcuV2::AssignI(instr++, ccuRepAssign->GetVarA().Id(), ccuRepAssign->GetImmed());
     566           96 :                 break;
     567              :             }
     568            2 :             case AssignSubType::IMD_TO_ADDR: {
     569            2 :                 CcuV2::AssignI(instr++, ccuRepAssign->GetAddrA().Id(), ccuRepAssign->GetImmed());
     570            2 :                 break;
     571              :             }
     572            5 :             case AssignSubType::VAR_TO_ADDR: {
     573            5 :                 CcuV2::Assign(instr++, ccuRepAssign->GetAddrA().Id(), ccuRepAssign->GetVarA().Id());
     574            5 :                 break;
     575              :             }
     576           16 :             case AssignSubType::ADDR_TO_ADDR: {
     577           16 :                 CcuV2::Assign(instr++, ccuRepAssign->GetAddrB().Id(), ccuRepAssign->GetAddrA().Id());
     578           16 :                 break;
     579              :             }
     580           38 :             case AssignSubType::VAR_TO_VAR: {
     581           38 :                 CcuV2::Assign(instr++, ccuRepAssign->GetVarB().Id(), ccuRepAssign->GetVarA().Id());
     582           38 :                 break;
     583              :             }
     584            0 :             default: {
     585            0 :                 HCCL_ERROR("Invalid Assign, subType[%d]", static_cast<int>(ccuRepAssign->GetSubType()));
     586            0 :                 return HCCL_E_PARA;
     587              :             }
     588              :         }
     589              : 
     590          157 :         return HcclResult::HCCL_SUCCESS;
     591              :     }
     592              : 
     593              :     HcclResult
     594            8 :     CcuInsGeneratorV2::CcuRepMulTranslate([[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepMul* ccuRepMul)
     595              :     {
     596            8 :         CHK_PTR_NULL(ccuRepMul);
     597              : 
     598              :         // {isImmed, isSelf, dstVar, srcAVar, srcBAddr} — 按 MulSubType 枚举顺序
     599              :         static constexpr struct {
     600              :             bool isImmed;
     601              :             bool isSelf;
     602              :             bool dstVar;
     603              :             bool srcAVar;
     604              :             bool srcBAddr;
     605              :         } table[] = {
     606              :             {false, false, false, false, false}, // INVALID
     607              :             {false, false, true, true, false},   // VAR_MUL_VAR_TO_VAR
     608              :             {true, false, true, true, false},    // VAR_MUL_IMMED_TO_VAR
     609              :             {false, true, true, true, false},    // SELF_MUL_VAR_VARIABLE
     610              :             {true, true, true, true, false},     // SELF_MUL_IMMED_VARIABLE
     611              :             {false, false, false, true, false},  // VAR_MUL_VAR_TO_ADDR
     612              :             {false, false, false, true, true},   // VAR_MUL_ADDR_TO_ADDR
     613              :             {true, false, false, true, false},   // VAR_MUL_IMMED_TO_ADDR
     614              :             {true, false, false, false, false},  // ADDR_MUL_IMMED_TO_ADDR
     615              :             {false, true, false, false, false},  // SELF_MUL_VAR_ADDRESS
     616              :             {true, true, false, false, false},   // SELF_MUL_IMMED_ADDRESS
     617              :             {true, false, true, false, false},   // ADDR_MUL_IMMED_TO_VAR
     618              :         };
     619              : 
     620            8 :         auto idx = static_cast<size_t>(ccuRepMul->GetSubType());
     621            8 :         if (idx == 0 || idx >= sizeof(table) / sizeof(table[0])) {
     622            0 :             HCCL_ERROR("Invalid Mul, subType[%d]", static_cast<int>(ccuRepMul->GetSubType()));
     623            0 :             return HCCL_E_PARA;
     624              :         }
     625            8 :         const auto& info = table[idx];
     626              : 
     627            8 :         uint16_t srcAId = info.srcAVar ? ccuRepMul->GetVarA().Id() : ccuRepMul->GetAddrA().Id();
     628            8 :         uint16_t dstId = info.isSelf ? srcAId : (info.dstVar ? ccuRepMul->GetVarC().Id() : ccuRepMul->GetAddrC().Id());
     629              : 
     630            8 :         if (info.isImmed) {
     631            3 :             CcuV2::MulI(instr++, dstId, srcAId, ccuRepMul->GetImmedB(), 0, 0);
     632              :         } else {
     633            5 :             uint16_t srcBId = info.srcBAddr ? ccuRepMul->GetAddrB().Id() : ccuRepMul->GetVarB().Id();
     634            5 :             CcuV2::Mul(instr++, dstId, srcAId, srcBId, 0, 0);
     635              :         }
     636              : 
     637            8 :         return HcclResult::HCCL_SUCCESS;
     638              :     }
     639              : 
     640              :     HcclResult
     641            8 :     CcuInsGeneratorV2::CcuRepSubTranslate([[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepSub* ccuRepSub)
     642              :     {
     643            8 :         CHK_PTR_NULL(ccuRepSub);
     644              : 
     645              :         // {isImmed, isSelf, dstVar, srcAVar, srcBAddr} — 按 MinusSubType 枚举顺序
     646              :         static constexpr struct {
     647              :             bool isImmed;
     648              :             bool isSelf;
     649              :             bool dstVar;
     650              :             bool srcAVar;
     651              :             bool srcBAddr;
     652              :         } table[] = {
     653              :             {false, false, false, false, false}, // INVALID
     654              :             {false, false, true, true, false},   // VAR_MINUS_VAR_TO_VAR
     655              :             {true, false, true, true, false},    // VAR_MINUS_IMMED_TO_VAR
     656              :             {false, true, true, true, false},    // SELF_SUB_VAR_VARIABLE
     657              :             {true, true, true, true, false},     // SELF_SUB_IMMED_VARIABLE
     658              :             {false, false, false, false, false}, // ADDR_MINUS_VAR_TO_ADDR
     659              :             {true, false, false, false, false},  // ADDR_MINUS_IMMED_TO_ADDR
     660              :             {false, true, false, false, false},  // SELF_SUB_VAR_ADDRESS
     661              :             {true, true, false, false, false},   // SELF_SUB_IMMED_ADDRESS
     662              :             {true, false, false, true, false},   // VAR_MINUS_IMMED_TO_ADDR
     663              :             {true, false, true, false, false},   // ADDR_MINUS_IMMED_TO_VAR
     664              :         };
     665              : 
     666            8 :         auto idx = static_cast<size_t>(ccuRepSub->GetSubType());
     667            8 :         if (idx == 0 || idx >= sizeof(table) / sizeof(table[0])) {
     668            0 :             HCCL_ERROR("Invalid Sub, subType[%d]", static_cast<int>(ccuRepSub->GetSubType()));
     669            0 :             return HCCL_E_PARA;
     670              :         }
     671            8 :         const auto& info = table[idx];
     672              : 
     673            8 :         uint16_t srcAId = info.srcAVar ? ccuRepSub->GetVarA().Id() : ccuRepSub->GetAddrA().Id();
     674            8 :         uint16_t dstId = info.isSelf ? srcAId : (info.dstVar ? ccuRepSub->GetVarC().Id() : ccuRepSub->GetAddrC().Id());
     675              : 
     676            8 :         if (info.isImmed) {
     677            3 :             CcuV2::SubI(instr++, dstId, srcAId, ccuRepSub->GetImmedB(), 0, 0);
     678              :         } else {
     679            5 :             uint16_t srcBId = info.srcBAddr ? ccuRepSub->GetAddrB().Id() : ccuRepSub->GetVarB().Id();
     680            5 :             CcuV2::Sub(instr++, dstId, srcAId, srcBId, 0, 0);
     681              :         }
     682              : 
     683            8 :         return HcclResult::HCCL_SUCCESS;
     684              :     }
     685              : 
     686            2 :     HcclResult CcuInsGeneratorV2::CcuRepAndTranslate(
     687              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepAnd* ccuRepAnd,
     688              :         [[maybe_unused]] const TransDep& dep)
     689              :     {
     690            2 :         CHK_PTR_NULL(ccuRepAnd);
     691            2 :         switch (ccuRepAnd->GetSubType()) {
     692            2 :             case AndSubType::VAR_AND_VAR_TO_VAR: {
     693            6 :                 CcuV2::And(
     694            2 :                     instr++, ccuRepAnd->GetVarC().Id(), ccuRepAnd->GetVarA().Id(), ccuRepAnd->GetVarB().Id(), 0, 0);
     695            2 :                 break;
     696              :             }
     697            0 :             case AndSubType::SELF_AND_VAR_VARIABLE: {
     698            0 :                 CcuV2::And(
     699            0 :                     instr++, ccuRepAnd->GetVarC().Id(), ccuRepAnd->GetVarC().Id(), ccuRepAnd->GetVarB().Id(), 0, 0);
     700            0 :                 break;
     701              :             }
     702            0 :             default: {
     703            0 :                 HCCL_ERROR("Invalid And, subType[%d]", static_cast<int>(ccuRepAnd->GetSubType()));
     704            0 :                 return HCCL_E_PARA;
     705              :             }
     706              :         }
     707            2 :         return HcclResult::HCCL_SUCCESS;
     708              :     }
     709              : 
     710            1 :     HcclResult CcuInsGeneratorV2::CcuRepNotTranslate(
     711              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepNot* ccuRepNot,
     712              :         [[maybe_unused]] const TransDep& dep)
     713              :     {
     714            1 :         CHK_PTR_NULL(ccuRepNot);
     715            1 :         switch (ccuRepNot->GetSubType()) {
     716            1 :             case NotSubType::VAR_EQUALS_NOT_VAR: {
     717            1 :                 CcuV2::Not(instr++, ccuRepNot->GetVarC().Id(), ccuRepNot->GetVarB().Id(), 0, 0);
     718            1 :                 break;
     719              :             }
     720            0 :             default: {
     721            0 :                 HCCL_ERROR("Invalid Not, subType[%d]", static_cast<int>(ccuRepNot->GetSubType()));
     722            0 :                 return HCCL_E_PARA;
     723              :             }
     724              :         }
     725            1 :         return HcclResult::HCCL_SUCCESS;
     726              :     }
     727              : 
     728            2 :     HcclResult CcuInsGeneratorV2::CcuRepOrTranslate(
     729              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepOr* ccuRepOr,
     730              :         [[maybe_unused]] const TransDep& dep)
     731              :     {
     732            2 :         CHK_PTR_NULL(ccuRepOr);
     733            2 :         switch (ccuRepOr->GetSubType()) {
     734            2 :             case OrSubType::VAR_OR_VAR_TO_VAR: {
     735            2 :                 CcuV2::Or(instr++, ccuRepOr->GetVarC().Id(), ccuRepOr->GetVarA().Id(), ccuRepOr->GetVarB().Id(), 0, 0);
     736            2 :                 break;
     737              :             }
     738            0 :             case OrSubType::SELF_OR_VAR_VARIABLE: {
     739            0 :                 CcuV2::Or(instr++, ccuRepOr->GetVarC().Id(), ccuRepOr->GetVarC().Id(), ccuRepOr->GetVarB().Id(), 0, 0);
     740            0 :                 break;
     741              :             }
     742            0 :             default: {
     743            0 :                 HCCL_ERROR("Invalid Or, subType[%d]", static_cast<int>(ccuRepOr->GetSubType()));
     744            0 :                 return HCCL_E_PARA;
     745              :             }
     746              :         }
     747            2 :         return HcclResult::HCCL_SUCCESS;
     748              :     }
     749              : 
     750            2 :     HcclResult CcuInsGeneratorV2::CcuRepXorTranslate(
     751              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepXor* ccuRepXor,
     752              :         [[maybe_unused]] const TransDep& dep)
     753              :     {
     754            2 :         CHK_PTR_NULL(ccuRepXor);
     755            2 :         switch (ccuRepXor->GetSubType()) {
     756            2 :             case XorSubType::VAR_XOR_VAR_TO_VAR: {
     757            6 :                 CcuV2::Xor(
     758            2 :                     instr++, ccuRepXor->GetVarC().Id(), ccuRepXor->GetVarA().Id(), ccuRepXor->GetVarB().Id(), 0, 0);
     759            2 :                 break;
     760              :             }
     761            0 :             case XorSubType::SELF_XOR_VAR_VARIABLE: {
     762            0 :                 CcuV2::Xor(
     763            0 :                     instr++, ccuRepXor->GetVarC().Id(), ccuRepXor->GetVarC().Id(), ccuRepXor->GetVarB().Id(), 0, 0);
     764            0 :                 break;
     765              :             }
     766            0 :             default: {
     767            0 :                 HCCL_ERROR("Invalid Xor, subType[%d]", static_cast<int>(ccuRepXor->GetSubType()));
     768            0 :                 return HCCL_E_PARA;
     769              :             }
     770              :         }
     771            2 :         return HcclResult::HCCL_SUCCESS;
     772              :     }
     773              : 
     774            6 :     HcclResult CcuInsGeneratorV2::CcuRepShLTranslate(
     775              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepShL* ccuRepShL,
     776              :         [[maybe_unused]] const TransDep& dep)
     777              :     {
     778            6 :         CHK_PTR_NULL(ccuRepShL);
     779            6 :         if (ccuRepShL->GetShiftType() == ShiftType::LOGICAL_SHIFT) {
     780            6 :             switch (ccuRepShL->GetShiftSubType()) {
     781            3 :                 case ShiftSubType::VAR_EQUALS_VAR_SHIFT_VAR: {
     782            9 :                     CcuV2::SLL(
     783            3 :                         instr++, ccuRepShL->GetVarD().Id(), ccuRepShL->GetVarN().Id(), ccuRepShL->GetVarM().Id(), 0, 0);
     784            3 :                     break;
     785              :                 }
     786            1 :                 case ShiftSubType::VAR_SHIFT_ASSIGN_VAR: {
     787            3 :                     CcuV2::SLL(
     788            1 :                         instr++, ccuRepShL->GetVarD().Id(), ccuRepShL->GetVarD().Id(), ccuRepShL->GetVarM().Id(), 0, 0);
     789            1 :                     break;
     790              :                 }
     791            1 :                 case ShiftSubType::ADDR_EQUALS_VAR_SHIFT_VAR: {
     792            3 :                     CcuV2::SLL(
     793            1 :                         instr++, ccuRepShL->GetAddressD().Id(), ccuRepShL->GetVarN().Id(), ccuRepShL->GetVarM().Id(), 0,
     794              :                         0);
     795            1 :                     break;
     796              :                 }
     797            1 :                 case ShiftSubType::ADDR_SHIFT_ASSIGN_VAR: {
     798            3 :                     CcuV2::SLL(
     799            1 :                         instr++, ccuRepShL->GetAddressD().Id(), ccuRepShL->GetAddressD().Id(),
     800            2 :                         ccuRepShL->GetVarM().Id(), 0, 0);
     801            1 :                     break;
     802              :                 }
     803            0 :                 default: {
     804            0 :                     HCCL_ERROR("Invalid Shift left, shiftSubType[%d]", static_cast<int>(ccuRepShL->GetShiftSubType()));
     805            0 :                     return HCCL_E_PARA;
     806              :                 }
     807              :             }
     808              :         } else {
     809            0 :             HCCL_ERROR("Invalid Shift left, shiftType[%d]", static_cast<int>(ccuRepShL->GetShiftType()));
     810            0 :             return HCCL_E_PARA;
     811              :         }
     812            6 :         return HcclResult::HCCL_SUCCESS;
     813              :     }
     814              : 
     815            6 :     HcclResult CcuInsGeneratorV2::CcuRepShRTranslate(
     816              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepShR* ccuRepShR,
     817              :         [[maybe_unused]] const TransDep& dep)
     818              :     {
     819            6 :         CHK_PTR_NULL(ccuRepShR);
     820            6 :         if (ccuRepShR->GetShiftType() == ShiftType::LOGICAL_SHIFT) {
     821            6 :             switch (ccuRepShR->GetShiftSubType()) {
     822            3 :                 case ShiftSubType::VAR_EQUALS_VAR_SHIFT_VAR: {
     823            9 :                     CcuV2::SRL(
     824            3 :                         instr++, ccuRepShR->GetVarD().Id(), ccuRepShR->GetVarN().Id(), ccuRepShR->GetVarM().Id(), 0, 0);
     825            3 :                     break;
     826              :                 }
     827            1 :                 case ShiftSubType::VAR_SHIFT_ASSIGN_VAR: {
     828            3 :                     CcuV2::SRL(
     829            1 :                         instr++, ccuRepShR->GetVarD().Id(), ccuRepShR->GetVarD().Id(), ccuRepShR->GetVarM().Id(), 0, 0);
     830            1 :                     break;
     831              :                 }
     832            1 :                 case ShiftSubType::ADDR_EQUALS_VAR_SHIFT_VAR: {
     833            3 :                     CcuV2::SRL(
     834            1 :                         instr++, ccuRepShR->GetAddressD().Id(), ccuRepShR->GetVarN().Id(), ccuRepShR->GetVarM().Id(), 0,
     835              :                         0);
     836            1 :                     break;
     837              :                 }
     838            1 :                 case ShiftSubType::ADDR_SHIFT_ASSIGN_VAR: {
     839            3 :                     CcuV2::SRL(
     840            1 :                         instr++, ccuRepShR->GetAddressD().Id(), ccuRepShR->GetAddressD().Id(),
     841            2 :                         ccuRepShR->GetVarM().Id(), 0, 0);
     842            1 :                     break;
     843              :                 }
     844            0 :                 default: {
     845            0 :                     HCCL_ERROR("Invalid Shift right, shiftSubType[%d]", static_cast<int>(ccuRepShR->GetShiftSubType()));
     846            0 :                     return HCCL_E_PARA;
     847              :                 }
     848              :             }
     849              :         } else {
     850            0 :             HCCL_ERROR("Invalid Shift right, shiftType[%d]", static_cast<int>(ccuRepShR->GetShiftType()));
     851            0 :             return HCCL_E_PARA;
     852              :         }
     853            6 :         return HcclResult::HCCL_SUCCESS;
     854              :     }
     855              : 
     856            0 :     HcclResult CcuInsGeneratorV2::CcuRepFuncBlockTranslate(
     857              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepFuncBlock* funcBlockPtr,
     858              :         const TransDep& dep, uint32_t step)
     859              :     {
     860            0 :         CHK_PTR_NULL(funcBlockPtr);
     861            0 :         std::vector<CcuRepArg>& inArgs = funcBlockPtr->GetInArgs();
     862              :         (void)inArgs;
     863            0 :         std::vector<CcuRepArg>& outArgs = funcBlockPtr->GetOutArgs();
     864            0 :         CcuRepReferenceManager* funcManager = funcBlockPtr->GetFuncManager();
     865            0 :         CHK_PTR_NULL(funcManager);
     866            0 :         uint16_t callLayer = funcBlockPtr->GetCallLayer();
     867            0 :         if (step == 0) {
     868              :             // 函数入口为nop
     869            0 :             CcuV2::Nop(instr++);
     870            0 :             curInstrId++;
     871            0 :         } else if (step == 1) {
     872              :             // 处理输出的参数
     873            0 :             uint32_t iOutArg = 0;
     874            0 :             const auto& funcOut = funcManager->GetFuncOut();
     875            0 :             if (iOutArg >= funcOut.size()) {
     876            0 :                 HCCL_ERROR("[FuncBlock] out arg index %u >= funcOut size %zu", iOutArg, funcOut.size());
     877            0 :                 return HCCL_E_PARA;
     878              :             }
     879            0 :             for (uint32_t i = 0; i < outArgs.size(); i++) {
     880            0 :                 if (outArgs[i].type == CcuArgType::VARIABLE) {
     881            0 :                     CcuV2::Add(instr++, funcOut[iOutArg++].Id(), outArgs[i].var.Id(), dep.reserveXnId);
     882            0 :                     curInstrId++;
     883            0 :                 } else if (outArgs[i].type == CcuArgType::VARIABLE_LIST) {
     884            0 :                     for (uint32_t j = 0; j < outArgs[i].varList.size(); j++) {
     885            0 :                         CcuV2::Add(instr++, funcOut[iOutArg++].Id(), outArgs[i].varList[j].Id(), dep.reserveXnId);
     886            0 :                         curInstrId++;
     887              :                     }
     888              :                 }
     889              :             }
     890              : 
     891              :             // 返回调用处
     892            0 :             uint32_t relJmpInstrNum = REL_JMP_INSTR_NUM; // relJmp需要9条指令
     893            0 :             CcuV2::RelJmp(
     894            0 :                 instr, funcManager->GetFuncRet(callLayer).Id(), curInstrId + relJmpInstrNum, dep.commXn[0],
     895            0 :                 dep.commXn[1]);
     896            0 :             instr += relJmpInstrNum;
     897            0 :             curInstrId += relJmpInstrNum;
     898            0 :             CcuV2::Jump(instr++, funcManager->GetFuncRet(callLayer).Id(), dep.reserveXnId, dep.reserveXnId, 0);
     899            0 :             curInstrId++;
     900              :         } else {
     901            0 :             HCCL_ERROR("Unsupported step[%d] for CcuRepFuncBlockTranslate", step);
     902            0 :             return HCCL_E_PARA;
     903              :         }
     904            0 :         return HcclResult::HCCL_SUCCESS;
     905              :     }
     906              : 
     907            0 :     void CcuInsGeneratorV2::LoadFuncCallInArgs(
     908              :         CcuInstr* instr, std::vector<CcuRepArg>& inArgs, std::vector<Variable>& formalIns, uint16_t reserveXnId)
     909              :     {
     910            0 :         uint32_t idx = 0;
     911            0 :         for (uint32_t i = 0; i < inArgs.size(); i++) {
     912            0 :             if (inArgs[i].type == CcuArgType::VARIABLE) {
     913            0 :                 CcuV2::Add(instr + idx, formalIns[idx].Id(), inArgs[i].var.Id(), reserveXnId);
     914            0 :                 idx++;
     915            0 :             } else if (inArgs[i].type == CcuArgType::VARIABLE_LIST) {
     916            0 :                 for (uint32_t j = 0; j < inArgs[i].varList.size(); j++) {
     917            0 :                     CcuV2::Add(instr + idx, formalIns[idx].Id(), inArgs[i].varList[j].Id(), reserveXnId);
     918            0 :                     idx++;
     919              :                 }
     920              :             }
     921              :         }
     922            0 :     }
     923              : 
     924            0 :     void CcuInsGeneratorV2::LoadFuncCallOutArgs(
     925              :         CcuInstr* instr, uint32_t offset, std::vector<CcuRepArg>& outArgs, CcuRepReferenceManager* funcManager,
     926              :         uint16_t reserveXnId)
     927              :     {
     928            0 :         uint32_t idx = 0;
     929            0 :         for (uint32_t i = 0; i < outArgs.size(); i++) {
     930            0 :             if (outArgs[i].type == CcuArgType::VARIABLE) {
     931            0 :                 CcuV2::Add(instr + offset + idx, outArgs[i].var.Id(), funcManager->GetFuncOut()[idx].Id(), reserveXnId);
     932            0 :                 idx++;
     933            0 :             } else if (outArgs[i].type == CcuArgType::VARIABLE_LIST) {
     934            0 :                 for (uint32_t j = 0; j < outArgs[i].varList.size(); j++) {
     935            0 :                     CcuV2::Add(
     936            0 :                         instr + offset + idx, outArgs[i].varList[j].Id(), funcManager->GetFuncOut()[idx].Id(),
     937              :                         reserveXnId);
     938            0 :                     idx++;
     939              :                 }
     940              :             }
     941              :         }
     942            0 :     }
     943              : 
     944            0 :     HcclResult CcuInsGeneratorV2::CcuRepFuncCallTranslate(
     945              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& curInstr, uint16_t& curInstrId, CcuRepFuncCall* funcCallPtr,
     946              :         const TransDep& dep)
     947              :     {
     948              :         (void)curInstr;
     949              :         (void)curInstrId;
     950              : 
     951            0 :         FuncCallContext ctx;
     952            0 :         CHK_RET(PrepareFuncCallContext(funcCallPtr, ctx));
     953              : 
     954            0 :         std::vector<CcuRepArg>& inArgs = funcCallPtr->GetInArgs();
     955            0 :         std::vector<CcuRepArg>& outArgs = funcCallPtr->GetOutArgs();
     956            0 :         uint32_t inArgCount = ctx.inArgCount;
     957            0 :         CcuRepReferenceManager* funcManager = ctx.funcManager;
     958            0 :         std::shared_ptr<CcuRepFuncBlock>& funcBlock = ctx.funcBlock;
     959            0 :         CcuInstr* instr = ctx.instr;
     960            0 :         std::vector<Variable>& formalIns = ctx.formalIns;
     961            0 :         Variable funcAddrVar = funcCallPtr->GetFuncAddrVar();
     962            0 :         int32_t callLayer = funcCallPtr->GetCallLayer();
     963            0 :         uint16_t instrId = funcCallPtr->StartInstrId();
     964            0 :         LoadFuncCallInArgs(instr, inArgs, formalIns, dep.reserveXnId);
     965              : 
     966            0 :         uint32_t locId = 0;
     967            0 :         if (funcBlock != nullptr) {
     968            0 :             CcuV2::LoadImdToXn(
     969            0 :                 instr + inArgCount + locId++, funcManager->GetFuncCall().Id(), funcBlock->StartInstrId());
     970              :         } else {
     971            0 :             CcuV2::Add(
     972            0 :                 instr + inArgCount + locId++, funcManager->GetFuncCall().Id(), funcAddrVar.Id(), dep.reserveXnId);
     973              :         }
     974            0 :         CcuV2::LoadImdToXn(
     975            0 :             instr + inArgCount + locId++, funcManager->GetFuncRet(callLayer).Id(),
     976            0 :             instrId + inArgCount + FUNC_CALL_RET_OFFSET); // 需要指向函数返回位置
     977            0 :         CcuV2::RelJmp(
     978            0 :             instr + inArgCount + locId, funcManager->GetFuncCall().Id(),
     979            0 :             instrId + inArgCount + FUNC_CALL_JMP_OFFSET, // Jmp的目标指令为其后11条指令
     980            0 :             dep.commXn[0], dep.commXn[1]);
     981            0 :         locId += REL_JMP_INSTR_NUM; // relJmp需要9条指令
     982            0 :         CcuV2::Jump(instr + inArgCount + locId++, funcManager->GetFuncCall().Id(), dep.reserveXnId, dep.reserveXnId, 0);
     983            0 :         CcuV2::Nop(instr + inArgCount + locId++);
     984              : 
     985            0 :         uint32_t extraInstrNum = GetInstrCount(funcCallPtr->Type());
     986            0 :         LoadFuncCallOutArgs(instr, inArgCount + extraInstrNum, outArgs, funcManager, dep.reserveXnId);
     987            0 :         return HcclResult::HCCL_SUCCESS;
     988            0 :     }
     989              : 
     990           49 :     uint32_t GetRelativeInstrId(uint32_t currentInstrId, uint32_t targetInstrId)
     991              :     {
     992              :         static constexpr uint32_t CCU_INSTR_ID_SPACE_SIZE = 0x10000;
     993           49 :         if (targetInstrId > currentInstrId) {
     994           39 :             return targetInstrId - currentInstrId;
     995              :         } else {
     996           10 :             uint32_t diff = currentInstrId - targetInstrId;
     997           10 :             if (diff >= CCU_INSTR_ID_SPACE_SIZE) {
     998            0 :                 HCCL_ERROR("Jump distance %u exceeds instr id space %u", diff, CCU_INSTR_ID_SPACE_SIZE);
     999            0 :                 return 0;
    1000              :             }
    1001           10 :             return CCU_INSTR_ID_SPACE_SIZE - diff;
    1002              :         }
    1003              :     }
    1004              : 
    1005            6 :     HcclResult CcuInsGeneratorV2::CcuRepJumpTranslate(
    1006              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJump* jumpPtr,
    1007              :         const TransDep& dep)
    1008              :     {
    1009              :         (void)instr;
    1010              :         (void)curInstrId;
    1011              :         (void)dep;
    1012              :         // 翻译直接跳转指令
    1013            6 :         CHK_PTR_NULL(jumpPtr);
    1014            6 :         std::shared_ptr<CcuRepJumpLabel> jumpLabel = jumpPtr->GetJumpLabel();
    1015            6 :         CHK_PTR_NULL(jumpLabel);
    1016            6 :         uint16_t instrIdOffset = GetRelativeInstrId(jumpPtr->StartInstrId() + 1, jumpLabel->StartInstrId());
    1017            6 :         CcuV2::LoadImdToXn(jumpPtr->GetInstr() + 0, jumpPtr->GetTargetInstrId().Id(), instrIdOffset, 0, 0);
    1018              :         //  无条件跳转
    1019            6 :         CcuV2::Jump(
    1020            6 :             jumpPtr->GetInstr() + 1, jumpPtr->GetTargetInstrId().Id(), 0, 0, static_cast<int>(ConditionType::DEFAULT));
    1021              : 
    1022            6 :         return HcclResult::HCCL_SUCCESS;
    1023            6 :     }
    1024              : 
    1025           25 :     HcclResult CcuInsGeneratorV2::CcuRepJumpTranslateV2Base(
    1026              :         CcuInstr*& curInstr, uint16_t& curInstrId, CcuRepJumpBase* jumpBasePtr, uint64_t expected,
    1027              :         const Variable& condition, const Variable& expectedVar, ConditionType condType)
    1028              :     {
    1029              :         (void)curInstr;
    1030              :         (void)curInstrId;
    1031           25 :         CHK_PTR_NULL(jumpBasePtr);
    1032           25 :         CcuInstr* instr = jumpBasePtr->GetInstr();
    1033           25 :         uint16_t instrId = jumpBasePtr->StartInstrId();
    1034           25 :         Variable& targetInstrId = jumpBasePtr->GetTargetInstrId();
    1035           25 :         std::shared_ptr<CcuRepJumpLabel> jumpLabel = jumpBasePtr->GetJumpLabel();
    1036           25 :         CHK_PTR_NULL(jumpLabel);
    1037              : 
    1038           25 :         if (jumpBasePtr->IsComparedWithImmd()) {
    1039           20 :             CcuV2::LoadImdToXn(instr + 0, expectedVar.Id(), expected, 0, 0);
    1040           20 :             uint16_t instrIdOffset = GetRelativeInstrId(instrId + 2, jumpLabel->StartInstrId()); // 2: jump指令偏移
    1041           20 :             CcuV2::LoadImdToXn(instr + 1, targetInstrId.Id(), instrIdOffset, 0, 0);
    1042           80 :             CcuV2::Jump(
    1043           20 :                 instr + 2, targetInstrId.Id(), condition.Id(), expectedVar.Id(), // 2: jump指令偏移
    1044              :                 static_cast<int>(condType));
    1045              :         } else {
    1046            5 :             uint16_t instrIdOffset = GetRelativeInstrId(instrId + 1, jumpLabel->StartInstrId());
    1047            5 :             CcuV2::LoadImdToXn(instr + 0, targetInstrId.Id(), instrIdOffset, 0, 0);
    1048            5 :             CcuV2::Jump(instr + 1, targetInstrId.Id(), condition.Id(), expectedVar.Id(), static_cast<int>(condType));
    1049              :         }
    1050              : 
    1051           25 :         return HcclResult::HCCL_SUCCESS;
    1052           25 :     }
    1053              : 
    1054           12 :     HcclResult CcuInsGeneratorV2::CcuRepJumpNETranslate(
    1055              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpNE* jumpNEPtr,
    1056              :         const TransDep& dep)
    1057              :     {
    1058              :         (void)dep;
    1059           12 :         CHK_PTR_NULL(jumpNEPtr);
    1060           12 :         return CcuRepJumpTranslateV2Base(
    1061           12 :             instr, curInstrId, jumpNEPtr, jumpNEPtr->GetExpectedNum(), jumpNEPtr->GetCondition(),
    1062           24 :             jumpNEPtr->GetExpectedVar(), ConditionType::NOT_EQUAL);
    1063              :     }
    1064              : 
    1065            3 :     HcclResult CcuInsGeneratorV2::CcuRepJumpEQTranslate(
    1066              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpEQ* jumpEQPtr,
    1067              :         const TransDep& dep)
    1068              :     {
    1069              :         (void)dep;
    1070            3 :         CHK_PTR_NULL(jumpEQPtr);
    1071            3 :         return CcuRepJumpTranslateV2Base(
    1072            3 :             instr, curInstrId, jumpEQPtr, jumpEQPtr->GetExpectedNum(), jumpEQPtr->GetCondition(),
    1073            6 :             jumpEQPtr->GetExpectedVar(), ConditionType::EQUAL);
    1074              :     }
    1075              : 
    1076            2 :     HcclResult CcuInsGeneratorV2::CcuRepJumpLETranslate(
    1077              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpLE* jumpLEPtr,
    1078              :         const TransDep& dep)
    1079              :     {
    1080              :         (void)dep;
    1081            2 :         CHK_PTR_NULL(jumpLEPtr);
    1082            2 :         return CcuRepJumpTranslateV2Base(
    1083            2 :             instr, curInstrId, jumpLEPtr, jumpLEPtr->GetExpectedNum(), jumpLEPtr->GetCondition(),
    1084            4 :             jumpLEPtr->GetExpectedVar(), ConditionType::LESS_EQUAL);
    1085              :     }
    1086              : 
    1087            4 :     HcclResult CcuInsGeneratorV2::CcuRepJumpGETranslate(
    1088              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpGE* jumpGEPtr,
    1089              :         const TransDep& dep)
    1090              :     {
    1091              :         (void)dep;
    1092            4 :         CHK_PTR_NULL(jumpGEPtr);
    1093            4 :         return CcuRepJumpTranslateV2Base(
    1094            4 :             instr, curInstrId, jumpGEPtr, jumpGEPtr->GetExpectedNum(), jumpGEPtr->GetCondition(),
    1095            8 :             jumpGEPtr->GetExpectedVar(), ConditionType::GREATER_EQUAL);
    1096              :     }
    1097              : 
    1098            1 :     HcclResult CcuInsGeneratorV2::CcuRepJumpGTTranslate(
    1099              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpGT* jumpGTPtr,
    1100              :         const TransDep& dep)
    1101              :     {
    1102              :         (void)dep;
    1103            1 :         CHK_PTR_NULL(jumpGTPtr);
    1104            1 :         return CcuRepJumpTranslateV2Base(
    1105            1 :             instr, curInstrId, jumpGTPtr, jumpGTPtr->GetExpectedNum(), jumpGTPtr->GetCondition(),
    1106            2 :             jumpGTPtr->GetExpectedVar(), ConditionType::GREATER_THAN);
    1107              :     }
    1108              : 
    1109            3 :     HcclResult CcuInsGeneratorV2::CcuRepJumpLTTranslate(
    1110              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpLT* jumpLTPtr,
    1111              :         const TransDep& dep)
    1112              :     {
    1113              :         (void)dep;
    1114            3 :         CHK_PTR_NULL(jumpLTPtr);
    1115            3 :         return CcuRepJumpTranslateV2Base(
    1116            3 :             instr, curInstrId, jumpLTPtr, jumpLTPtr->GetExpectedNum(), jumpLTPtr->GetCondition(),
    1117            6 :             jumpLTPtr->GetExpectedVar(), ConditionType::LESS_THAN);
    1118              :     }
    1119              : 
    1120            0 :     HcclResult CcuInsGeneratorV2::CcuRepLoopTranslate(
    1121              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoop* loopPtr)
    1122              :     {
    1123              :         (void)curInstrId;
    1124            0 :         CHK_PTR_NULL(loopPtr);
    1125            0 :         auto loopBlock = loopPtr->GetLoopBlock();
    1126            0 :         CHK_PTR_NULL(loopBlock);
    1127            0 :         CcuV2::Loop(
    1128            0 :             instr++, loopBlock->StartInstrId(), loopBlock->StartInstrId() + loopBlock->InstrCount() - 1,
    1129            0 :             loopPtr->GetLoopIterNum().Id(), loopPtr->GetLoopGsaOffset().Id(), loopPtr->GetLoopParam()->Id());
    1130            0 :         return HcclResult::HCCL_SUCCESS;
    1131              :     }
    1132              : 
    1133            0 :     HcclResult CcuInsGeneratorV2::LoadLoopCallArg(CcuInstr*& instr, const CcuRepArg& inArg, const CcuRepArg& blkArg)
    1134              :     {
    1135            0 :         switch (inArg.type) {
    1136            0 :             case CcuArgType::VARIABLE:
    1137            0 :                 CcuV2::Assign(instr++, blkArg.var.Id(), inArg.var.Id());
    1138            0 :                 break;
    1139            0 :             case CcuArgType::VARIABLE_LIST:
    1140            0 :                 if (inArg.varList.size() != blkArg.varList.size()) {
    1141            0 :                     HCCL_ERROR(
    1142              :                         "Mismatched Arg Size, inArg.varList.size[%zu], blkArg.varList.size[%zu]", inArg.varList.size(),
    1143              :                         blkArg.varList.size());
    1144            0 :                     return HCCL_E_PARA;
    1145              :                 }
    1146            0 :                 for (uint32_t j = 0; j < inArg.varList.size(); j++) {
    1147            0 :                     CcuV2::Assign(instr++, blkArg.varList[j].Id(), inArg.varList[j].Id());
    1148              :                 }
    1149            0 :                 break;
    1150            0 :             case CcuArgType::MEMORY:
    1151            0 :                 LoadAddrArg(instr, blkArg.mem, inArg.mem);
    1152            0 :                 break;
    1153            0 :             case CcuArgType::LOCAL_ADDR:
    1154            0 :                 LoadAddrArg(instr, blkArg.localAddr, inArg.localAddr);
    1155            0 :                 break;
    1156            0 :             case CcuArgType::REMOTE_ADDR:
    1157            0 :                 LoadAddrArg(instr, blkArg.remoteAddr, inArg.remoteAddr);
    1158            0 :                 break;
    1159            0 :             case CcuArgType::MEMORY_LIST:
    1160            0 :                 CHK_RET(LoadAddrListArg(instr, blkArg.memList, inArg.memList));
    1161            0 :                 break;
    1162            0 :             case CcuArgType::LOCAL_ADDR_LIST:
    1163            0 :                 CHK_RET(LoadAddrListArg(instr, blkArg.localAddrList, inArg.localAddrList));
    1164            0 :                 break;
    1165            0 :             case CcuArgType::REMOTE_ADDR_LIST:
    1166            0 :                 CHK_RET(LoadAddrListArg(instr, blkArg.remoteAddrList, inArg.remoteAddrList));
    1167            0 :                 break;
    1168            0 :             default:
    1169            0 :                 HCCL_ERROR("Mismatched Arg Type, inArg.type[%d]", static_cast<int>(inArg.type));
    1170            0 :                 return HCCL_E_PARA;
    1171              :         }
    1172            0 :         return HcclResult::HCCL_SUCCESS;
    1173              :     }
    1174              : 
    1175            0 :     HcclResult CcuInsGeneratorV2::CcuRepLoopCallTranslate(
    1176              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoopCall* loopCallPtr,
    1177              :         const TransDep& dep)
    1178              :     {
    1179              :         (void)curInstrId;
    1180              :         (void)dep;
    1181            0 :         CHK_PTR_NULL(loopCallPtr);
    1182            0 :         std::vector<CcuRepArg>& inArgs = loopCallPtr->GetInArgs();
    1183            0 :         auto loopBlock = loopCallPtr->GetLoopBlock();
    1184            0 :         CHK_PTR_NULL(loopBlock);
    1185              : 
    1186            0 :         for (uint32_t i = 0; i < inArgs.size(); i++) {
    1187            0 :             const CcuRepArg& inArg = inArgs[i];
    1188            0 :             const CcuRepArg& blkArg = loopBlock->GetArg(i);
    1189            0 :             if (inArg.type != blkArg.type) {
    1190            0 :                 HCCL_ERROR(
    1191              :                     "Mismatched Arg Type, inArg.type[%d], blkArg.type[%d]", static_cast<int>(inArg.type),
    1192              :                     static_cast<int>(blkArg.type));
    1193            0 :                 return HCCL_E_PARA;
    1194              :             }
    1195            0 :             CHK_RET(LoadLoopCallArg(instr, inArg, blkArg));
    1196              :         }
    1197              : 
    1198            0 :         return HcclResult::HCCL_SUCCESS;
    1199            0 :     }
    1200              : 
    1201            0 :     HcclResult CcuInsGeneratorV2::CcuRepSetLoopTranslate(
    1202              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepSetLoop* setLoopPtr)
    1203              :     {
    1204              :         (void)curInstrId;
    1205            0 :         CHK_PTR_NULL(setLoopPtr);
    1206              :         // CCU V121 将executorId赋值给loop指令的Xp,上层计算的loopContextId实际上没有用
    1207            0 :         CcuV2::LoadImdToXn(instr++, setLoopPtr->loopParam.Id(), setLoopPtr->executor.Id());
    1208            0 :         return HcclResult::HCCL_SUCCESS;
    1209              :     }
    1210              : 
    1211            0 :     HcclResult CcuInsGeneratorV2::CcuRepLoadTranslate(
    1212              :         CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoad* loadPtr, const TransDep& dep)
    1213              :     {
    1214              :         (void)curInstrId;
    1215            0 :         CHK_PTR_NULL(loadPtr);
    1216            0 :         CHK_PTR_NULL(ccuKernel);
    1217            0 :         const auto& constValue2VarMap = ccuKernel->GetConstValue2VarMap();
    1218            0 :         CcuV2::CacheConfig cacheConfig{0x0, 0x0};
    1219              : 
    1220              :         // var寄存器的真实id在常量准备阶段(Register)无法获取,故不记录在常量表中,单独赋值处理
    1221            0 :         CcuV2::LoadImdToXn(instr++, dep.commXn[0], loadPtr->GetVar().Id()); // dst xn id
    1222            0 :         CcuV2::LoadXFromMem(
    1223            0 :             instr++, dep.commXn[0], constValue2VarMap.at(loadPtr->GetAddr()).Id(),
    1224            0 :             constValue2VarMap.at(dep.memTokenInfo).Id(),
    1225            0 :             constValue2VarMap.at(CCU_RESOURCE_XN_PER_SIZE * loadPtr->GetNum()).Id(), cacheConfig, dep.commSignal,
    1226            0 :             loadPtr->GetMask());
    1227            0 :         CcuV2::SetCKE(instr++, 0, 0, dep.commSignal, loadPtr->GetMask(), 1);
    1228              : 
    1229            0 :         return HcclResult::HCCL_SUCCESS;
    1230              :     }
    1231              : 
    1232            0 :     HcclResult CcuInsGeneratorV2::CcuRepLoadVarTranslate(
    1233              :         CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoadVar* loadVarPtr, const TransDep& dep)
    1234              :     {
    1235              :         (void)curInstrId;
    1236            0 :         CHK_PTR_NULL(loadVarPtr);
    1237            0 :         CHK_PTR_NULL(ccuKernel);
    1238            0 :         const auto& constValue2VarMap = ccuKernel->GetConstValue2VarMap();
    1239            0 :         CcuV2::CacheConfig cacheConfig{0x0, 0x0};
    1240              : 
    1241              :         // var寄存器的真实id在常量准备阶段(Register)无法获取,故不记录在常量表中,单独赋值处理
    1242            0 :         CcuV2::LoadImdToXn(instr++, dep.commXn[0], loadVarPtr->GetVar().Id()); // dst xn id
    1243            0 :         CcuV2::LoadXFromMem(
    1244            0 :             instr++, dep.commXn[0], loadVarPtr->GetSrc().Id(), constValue2VarMap.at(dep.memTokenInfo).Id(),
    1245            0 :             constValue2VarMap.at(CCU_RESOURCE_XN_PER_SIZE * loadVarPtr->GetNum()).Id(), cacheConfig, dep.commSignal,
    1246            0 :             loadVarPtr->GetMask());
    1247            0 :         CcuV2::SetCKE(instr++, 0, 0, dep.commSignal, loadVarPtr->GetMask(), 1);
    1248            0 :         return HcclResult::HCCL_SUCCESS;
    1249              :     }
    1250              : 
    1251           15 :     HcclResult CcuInsGeneratorV2::CcuRepLoadArgTranslate(
    1252              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoadArg* loadArgPtr,
    1253              :         const TransDep& dep)
    1254              :     {
    1255              :         (void)curInstrId;
    1256           15 :         CHK_PTR_NULL(loadArgPtr);
    1257           15 :         if (dep.isFuncBlock) {
    1258              :             // Xn(var) = Xn(loadXnId) + 0
    1259            0 :             CcuV2::Add(instr++, loadArgPtr->GetVar().Id(), dep.loadXnId, dep.reserveXnId);
    1260              :         } else {
    1261           15 :             CcuV2::LoadSqeArgsToX(instr++, loadArgPtr->GetVar().Id(), loadArgPtr->GetArgId());
    1262              :         }
    1263              : 
    1264           15 :         return HcclResult::HCCL_SUCCESS;
    1265              :     }
    1266              : 
    1267           31 :     HcclResult CcuInsGeneratorV2::CcuRepNopTranslate(
    1268              :         [[maybe_unused]] CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepNop* nopPtr,
    1269              :         const TransDep& dep)
    1270              :     {
    1271              :         (void)curInstrId;
    1272              :         (void)nopPtr;
    1273              :         (void)dep;
    1274           31 :         CcuV2::Nop(instr++);
    1275              : 
    1276           31 :         return HcclResult::HCCL_SUCCESS;
    1277              :     }
    1278              : 
    1279            0 :     HcclResult CcuInsGeneratorV2::CcuRepStoreTranslate(
    1280              :         CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepStore* storePtr, const TransDep& dep)
    1281              :     {
    1282              :         (void)curInstrId;
    1283            0 :         CHK_PTR_NULL(storePtr);
    1284            0 :         CHK_PTR_NULL(ccuKernel);
    1285            0 :         const auto& constValue2VarMap = ccuKernel->GetConstValue2VarMap();
    1286            0 :         CcuV2::CacheConfig cacheConfig{0x0, 0x0};
    1287              : 
    1288              :         // var寄存器的真实id在常量准备阶段(Register)无法获取,故不记录在常量表中,单独赋值处理
    1289            0 :         CcuV2::LoadImdToXn(instr++, dep.commXn[0], storePtr->GetVar().Id()); // src xn id
    1290            0 :         CcuV2::StoreXToMem(
    1291            0 :             instr++, constValue2VarMap.at(storePtr->GetAddr()).Id(), constValue2VarMap.at(dep.memTokenInfo).Id(),
    1292            0 :             dep.commXn[0], constValue2VarMap.at(CCU_RESOURCE_XN_PER_SIZE * storePtr->GetNum()).Id(), cacheConfig,
    1293            0 :             dep.commSignal, storePtr->GetMask());
    1294            0 :         CcuV2::SetCKE(instr++, 0, 0, dep.commSignal, storePtr->GetMask(), 1);
    1295              : 
    1296            0 :         return HcclResult::HCCL_SUCCESS;
    1297              :     }
    1298              : 
    1299            0 :     HcclResult CcuInsGeneratorV2::CcuRepStoreVarTranslate(
    1300              :         CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepStoreVar* storeVarPtr, const TransDep& dep)
    1301              :     {
    1302            0 :         CHK_PTR_NULL(storeVarPtr);
    1303            0 :         CHK_PTR_NULL(ccuKernel);
    1304              :         (void)curInstrId;
    1305            0 :         CcuV2::CacheConfig cacheConfig{0x0, 0x0};
    1306              : 
    1307              :         // var寄存器的真实id在常量准备阶段(Register)无法获取,故不记录在常量表中,单独赋值处理
    1308            0 :         CcuV2::LoadImdToXn(instr++, dep.commXn[0], storeVarPtr->GetVar().Id());
    1309            0 :         const auto& constValue2VarMap = ccuKernel->GetConstValue2VarMap();
    1310            0 :         if (storeVarPtr->GetHscbFlag()) {
    1311            0 :             CcuV2::HSCBStoreXToMem(
    1312            0 :                 instr++, storeVarPtr->GetDst().Id(), dep.commXn[0],
    1313            0 :                 constValue2VarMap.at(CCU_RESOURCE_XN_PER_SIZE * storeVarPtr->GetNum()).Id(), cacheConfig,
    1314            0 :                 dep.commSignal, storeVarPtr->GetMask());
    1315              :         } else {
    1316            0 :             CcuV2::StoreXToMem(
    1317            0 :                 instr++, storeVarPtr->GetDst().Id(), constValue2VarMap.at(dep.memTokenInfo).Id(), dep.commXn[0],
    1318            0 :                 constValue2VarMap.at(CCU_RESOURCE_XN_PER_SIZE * storeVarPtr->GetNum()).Id(), cacheConfig,
    1319            0 :                 dep.commSignal, storeVarPtr->GetMask());
    1320              :         }
    1321            0 :         CcuV2::SetCKE(instr++, 0, 0, dep.commSignal, storeVarPtr->GetMask(), 1);
    1322              : 
    1323            0 :         return HcclResult::HCCL_SUCCESS;
    1324              :     }
    1325              : 
    1326          360 :     HcclResult CcuInsGeneratorV2::PrepareConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel)
    1327              :     {
    1328          360 :         CHK_PTR_NULL(repPtr);
    1329          360 :         CHK_PTR_NULL(ccuKernel);
    1330          360 :         HCCL_INFO("CcuInsGeneratorV2::PrepareConstValue Current RepType[%d]", repPtr->Type());
    1331          360 :         switch (repPtr->Type()) {
    1332            0 :             case CcuRepType::LOAD:
    1333            0 :                 return PrepareLoadConstValue(repPtr, dep, ccuKernel);
    1334            0 :             case CcuRepType::LOAD_VAR:
    1335            0 :                 return PrepareLoadVarConstValue(repPtr, dep, ccuKernel);
    1336            0 :             case CcuRepType::STORE:
    1337            0 :                 return PrepareStoreConstValue(repPtr, dep, ccuKernel);
    1338            0 :             case CcuRepType::STORE_VAR:
    1339            0 :                 return PrepareStoreVarConstValue(repPtr, dep, ccuKernel);
    1340            1 :             case CcuRepType::REM_POST_SEM:
    1341            1 :                 return PrepareRemPostSemConstValue(repPtr, dep, ccuKernel);
    1342            3 :             case CcuRepType::REM_POST_VAR:
    1343            3 :                 return PrepareRemPostVarConstValue(repPtr, dep, ccuKernel);
    1344            0 :             case CcuRepType::WRITE:
    1345            0 :                 return PrepareWriteConstValue(repPtr, dep, ccuKernel);
    1346            1 :             case CcuRepType::READ:
    1347            1 :                 return PrepareReadConstValue(repPtr, dep, ccuKernel);
    1348            0 :             case CcuRepType::BUF_WRITE:
    1349            0 :                 return PrepareBufWriteConstValue(repPtr, dep, ccuKernel);
    1350            0 :             case CcuRepType::BUF_READ:
    1351            0 :                 return PrepareBufReadConstValue(repPtr, dep, ccuKernel);
    1352            0 :             case CcuRepType::LOCAL_CPY:
    1353            0 :                 return PrepareLocCpyConstValue(repPtr, dep, ccuKernel);
    1354            0 :             case CcuRepType::RECORD_SHARED_NOTIFY:
    1355            0 :                 return PrepareRecordSharedNotifyConstValue(repPtr, dep, ccuKernel);
    1356          355 :             default:
    1357          355 :                 break;
    1358              :         }
    1359          355 :         return HcclResult::HCCL_SUCCESS;
    1360              :     }
    1361              : 
    1362            0 :     HcclResult CcuInsGeneratorV2::PrepareLoadConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel)
    1363              :     {
    1364            0 :         CcuRepLoad* loadPtr = dynamic_cast<CcuRepLoad*>(repPtr);
    1365            0 :         CHK_PTR_NULL(loadPtr);
    1366              :         std::vector<uint64_t> values
    1367            0 :             = {loadPtr->GetAddr(), dep.memTokenInfo, CCU_RESOURCE_XN_PER_SIZE * loadPtr->GetNum()};
    1368            0 :         return ccuKernel->Add2ConstValue2VarMap(values);
    1369            0 :     }
    1370              : 
    1371              :     HcclResult
    1372            0 :     CcuInsGeneratorV2::PrepareLoadVarConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel)
    1373              :     {
    1374            0 :         CcuRepLoadVar* loadVarPtr = dynamic_cast<CcuRepLoadVar*>(repPtr);
    1375            0 :         CHK_PTR_NULL(loadVarPtr);
    1376            0 :         std::vector<uint64_t> values = {dep.memTokenInfo, CCU_RESOURCE_XN_PER_SIZE * loadVarPtr->GetNum()};
    1377            0 :         return ccuKernel->Add2ConstValue2VarMap(values);
    1378            0 :     }
    1379              : 
    1380            0 :     HcclResult CcuInsGeneratorV2::PrepareStoreConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel)
    1381              :     {
    1382            0 :         CcuRepStore* storePtr = dynamic_cast<CcuRepStore*>(repPtr);
    1383            0 :         CHK_PTR_NULL(storePtr);
    1384              :         std::vector<uint64_t> values
    1385            0 :             = {storePtr->GetAddr(), dep.memTokenInfo, CCU_RESOURCE_XN_PER_SIZE * storePtr->GetNum()};
    1386            0 :         return ccuKernel->Add2ConstValue2VarMap(values);
    1387            0 :     }
    1388              : 
    1389              :     HcclResult
    1390            0 :     CcuInsGeneratorV2::PrepareStoreVarConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel)
    1391              :     {
    1392            0 :         CcuRepStoreVar* storeVarPtr = dynamic_cast<CcuRepStoreVar*>(repPtr);
    1393            0 :         CHK_PTR_NULL(storeVarPtr);
    1394            0 :         std::vector<uint64_t> values = {CCU_RESOURCE_XN_PER_SIZE * storeVarPtr->GetNum()};
    1395            0 :         if (!storeVarPtr->GetHscbFlag()) {
    1396            0 :             values.push_back(dep.memTokenInfo);
    1397              :         }
    1398            0 :         return ccuKernel->Add2ConstValue2VarMap(values);
    1399            0 :     }
    1400              : 
    1401            1 :     HcclResult CcuInsGeneratorV2::PrepareRemPostSemConstValue(
    1402              :         CcuRepBase* repPtr, [[maybe_unused]] const TransDep& dep, CcuKernel* ccuKernel)
    1403              :     {
    1404            1 :         CcuRepRemPostSem* repRemPostSemPtr = dynamic_cast<CcuRepRemPostSem*>(repPtr);
    1405            1 :         CHK_PTR_NULL(repRemPostSemPtr);
    1406            1 :         CcuUrmaChannel* channelImpl{};
    1407            1 :         CHK_RET(GetUrmaChannel(repRemPostSemPtr->GetChannel(), channelImpl));
    1408            1 :         uint64_t rmtSignalAddr{0};
    1409            1 :         CHK_PRT_RET(
    1410              :             channelImpl->GetRmtSignalAddrByIndex(repRemPostSemPtr->GetSemIndex(), rmtSignalAddr)
    1411              :                 != HcclResult::HCCL_SUCCESS,
    1412              :             HCCL_ERROR(
    1413              :                 "[CcuInsGeneratorV2][%s] failed to get remote signal addr, channelHandle[0x%llx].", __func__,
    1414              :                 repRemPostSemPtr->GetChannel()),
    1415              :             HCCL_E_INTERNAL);
    1416            1 :         uint64_t rmtToken{};
    1417            1 :         CHK_RET(GetRmtToken(channelImpl, rmtToken));
    1418            2 :         std::vector<uint64_t> values = {channelImpl->GetChannelId(), rmtSignalAddr, rmtToken};
    1419            1 :         return ccuKernel->Add2ConstValue2VarMap(values);
    1420            1 :     }
    1421              : 
    1422            3 :     HcclResult CcuInsGeneratorV2::PrepareRemPostVarConstValue(
    1423              :         CcuRepBase* repPtr, [[maybe_unused]] const TransDep& dep, CcuKernel* ccuKernel)
    1424              :     {
    1425            3 :         CcuRepRemPostVar* repRemPostVarPtr = dynamic_cast<CcuRepRemPostVar*>(repPtr);
    1426            3 :         CHK_PTR_NULL(repRemPostVarPtr);
    1427            3 :         CcuUrmaChannel* channelImpl{};
    1428            3 :         CHK_RET(GetUrmaChannel(repRemPostVarPtr->GetChannel(), channelImpl));
    1429            3 :         uint64_t rmtSignalAddr{0};
    1430            3 :         uint64_t rmtVarAddr{0};
    1431            3 :         CHK_PRT_RET(
    1432              :             channelImpl->GetRmtSignalAddrByIndex(repRemPostVarPtr->GetSemIndex(), rmtSignalAddr)
    1433              :                 != HcclResult::HCCL_SUCCESS,
    1434              :             HCCL_ERROR(
    1435              :                 "[CcuInsGeneratorV2][%s] failed to get remote signal addr, channelHandle[0x%llx].", __func__,
    1436              :                 repRemPostVarPtr->GetChannel()),
    1437              :             HCCL_E_INTERNAL);
    1438            3 :         CHK_PRT_RET(
    1439              :             channelImpl->GetRmtVarAddrByIndex(repRemPostVarPtr->GetParamIndex(), rmtVarAddr)
    1440              :                 != HcclResult::HCCL_SUCCESS,
    1441              :             HCCL_ERROR(
    1442              :                 "[CcuInsGeneratorV2][%s] failed to get remote var addr, channelHandle[0x%llx].", __func__,
    1443              :                 repRemPostVarPtr->GetChannel()),
    1444              :             HCCL_E_INTERNAL);
    1445            3 :         uint64_t rmtToken{};
    1446            3 :         CHK_RET(GetRmtToken(channelImpl, rmtToken));
    1447            6 :         std::vector<uint64_t> values = {channelImpl->GetChannelId(), rmtSignalAddr, rmtToken, rmtVarAddr};
    1448            3 :         return ccuKernel->Add2ConstValue2VarMap(values);
    1449            3 :     }
    1450              : 
    1451            0 :     HcclResult CcuInsGeneratorV2::PrepareWriteConstValue(
    1452              :         CcuRepBase* repPtr, [[maybe_unused]] const TransDep& dep, CcuKernel* ccuKernel)
    1453              :     {
    1454            0 :         CcuRepWrite* repWritePtr = dynamic_cast<CcuRepWrite*>(repPtr);
    1455            0 :         CHK_PTR_NULL(repWritePtr);
    1456            0 :         CcuUrmaChannel* channelImpl{};
    1457            0 :         CHK_RET(GetUrmaChannel(repWritePtr->GetChannel(), channelImpl));
    1458            0 :         uint64_t rmtToken{};
    1459            0 :         CHK_RET(GetRmtToken(channelImpl, rmtToken));
    1460            0 :         std::vector<uint64_t> values = {channelImpl->GetChannelId(), rmtToken};
    1461            0 :         return ccuKernel->Add2ConstValue2VarMap(values);
    1462            0 :     }
    1463              : 
    1464            1 :     HcclResult CcuInsGeneratorV2::PrepareReadConstValue(
    1465              :         CcuRepBase* repPtr, [[maybe_unused]] const TransDep& dep, CcuKernel* ccuKernel)
    1466              :     {
    1467            1 :         CcuRepRead* repReadPtr = dynamic_cast<CcuRepRead*>(repPtr);
    1468            1 :         CHK_PTR_NULL(repReadPtr);
    1469            1 :         CcuUrmaChannel* channelImpl{};
    1470            1 :         CHK_RET(GetUrmaChannel(repReadPtr->GetChannel(), channelImpl));
    1471            2 :         std::vector<uint64_t> values = {channelImpl->GetChannelId()};
    1472            1 :         return ccuKernel->Add2ConstValue2VarMap(values);
    1473            1 :     }
    1474              : 
    1475              :     HcclResult
    1476            0 :     CcuInsGeneratorV2::PrepareBufWriteConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel)
    1477              :     {
    1478            0 :         CcuRepBufWrite* repBufWritePtr = dynamic_cast<CcuRepBufWrite*>(repPtr);
    1479            0 :         CHK_PTR_NULL(repBufWritePtr);
    1480            0 :         CcuUrmaChannel* channelImpl{};
    1481            0 :         CHK_RET(GetUrmaChannel(repBufWritePtr->GetChannel(), channelImpl));
    1482            0 :         uint64_t rmtToken{};
    1483            0 :         CHK_RET(GetRmtToken(channelImpl, rmtToken));
    1484            0 :         std::vector<uint64_t> values = {channelImpl->GetChannelId(), rmtToken, dep.ccuResSpaceTokenInfo};
    1485            0 :         return ccuKernel->Add2ConstValue2VarMap(values);
    1486            0 :     }
    1487              : 
    1488              :     HcclResult
    1489            0 :     CcuInsGeneratorV2::PrepareBufReadConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel)
    1490              :     {
    1491            0 :         CcuRepBufRead* repBufReadPtr = dynamic_cast<CcuRepBufRead*>(repPtr);
    1492            0 :         CHK_PTR_NULL(repBufReadPtr);
    1493            0 :         CcuUrmaChannel* channelImpl{};
    1494            0 :         CHK_RET(GetUrmaChannel(repBufReadPtr->GetChannel(), channelImpl));
    1495            0 :         std::vector<uint64_t> values = {channelImpl->GetChannelId(), dep.ccuResSpaceTokenInfo};
    1496            0 :         return ccuKernel->Add2ConstValue2VarMap(values);
    1497            0 :     }
    1498              : 
    1499            0 :     HcclResult CcuInsGeneratorV2::PrepareLocCpyConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel)
    1500              :     {
    1501            0 :         CcuRepLocCpy* repLocCpyPtr = dynamic_cast<CcuRepLocCpy*>(repPtr);
    1502            0 :         CHK_PTR_NULL(repLocCpyPtr);
    1503            0 :         std::vector<uint64_t> values = {dep.reserveChannalId[0]};
    1504            0 :         return ccuKernel->Add2ConstValue2VarMap(values);
    1505            0 :     }
    1506              : 
    1507            0 :     HcclResult CcuInsGeneratorV2::PrepareRecordSharedNotifyConstValue(
    1508              :         CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel)
    1509              :     {
    1510            0 :         CcuRepRecordSharedNotify* sharedNotifyPtr = dynamic_cast<CcuRepRecordSharedNotify*>(repPtr);
    1511            0 :         CHK_PTR_NULL(sharedNotifyPtr);
    1512            0 :         uint64_t ckeAddr = dep.xnBaseAddr[sharedNotifyPtr->GetNotify().DieId()] + CCU_RESOURCE_XN_V2_RESERVE_SIZE
    1513            0 :                            + (sharedNotifyPtr->GetNotify().Id() * CCU_RESOURCE_CKE_PER_SIZE);
    1514              :         std::vector<uint64_t> values
    1515            0 :             = {ckeAddr, dep.memTokenInfo, CCU_RESOURCE_CKE_MASK_LENGTH, sharedNotifyPtr->GetMask()};
    1516            0 :         return ccuKernel->Add2ConstValue2VarMap(values);
    1517            0 :     }
    1518              : 
    1519            9 :     HcclResult CcuInsGeneratorV2::LoopConfigTranslate(
    1520              :         CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoopGroupBundle* bundlePtr, const TransDep& dep)
    1521              :     {
    1522           28 :         for (const auto& loop : bundlePtr->GetLoops()) {
    1523           19 :             CcuV2::LoadImdToXn(instr++, loop.ctxIdVar.Id(), loop.executor.Id());
    1524           19 :             curInstrId++;
    1525           19 :             if (loop.layout == CcuRepLoopGroupBundle::Layout::Config) {
    1526            7 :                 CcuV2::LoadImdToXn(instr++, loop.iterNumVar.Id(), loop.config.iterNum);
    1527            7 :                 curInstrId++;
    1528            7 :                 CcuV2::LoadImdToXn(instr++, loop.addrOffsetVar.Id(), loop.config.addrOffset);
    1529            7 :                 curInstrId++;
    1530           12 :             } else if (loop.layout == CcuRepLoopGroupBundle::Layout::PackedVar) {
    1531              :                 // loopParamVar 布局:iterNum[12:0] gsaOffset[44:13] ctxId[52:45]
    1532            4 :                 CcuV2::LoadImdToXn(instr++, dep.reserveXnId, LOOP_ITER_NUM_MASK);
    1533            4 :                 curInstrId++;
    1534            4 :                 CcuV2::And(instr++, loop.iterNumVar.Id(), loop.loopParamVar.Id(), dep.reserveXnId);
    1535            4 :                 curInstrId++;
    1536            4 :                 CcuV2::LoadImdToXn(instr++, dep.reserveXnId, LOOP_GSA_OFFSET_SHIFT);
    1537            4 :                 curInstrId++;
    1538            4 :                 CcuV2::SRL(instr++, loop.addrOffsetVar.Id(), loop.loopParamVar.Id(), dep.reserveXnId);
    1539            4 :                 curInstrId++;
    1540            4 :                 CcuV2::LoadImdToXn(instr++, dep.reserveXnId, LOOP_GSA_OFFSET_MASK);
    1541            4 :                 curInstrId++;
    1542            4 :                 CcuV2::And(instr++, loop.addrOffsetVar.Id(), loop.addrOffsetVar.Id(), dep.reserveXnId);
    1543            4 :                 curInstrId++;
    1544              :             }
    1545              :         }
    1546            9 :         return HcclResult::HCCL_SUCCESS;
    1547              :     }
    1548              : 
    1549            9 :     HcclResult CcuInsGeneratorV2::LoopGroupConfigTranslate(
    1550              :         CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoopGroupBundle* bundlePtr, const TransDep& dep, bool isConfig,
    1551              :         bool isCompat, uint16_t& loopGroupConfigId)
    1552              :     {
    1553            9 :         loopGroupConfigId = bundlePtr->GetParallelVar().Id();
    1554            9 :         if (isConfig) {
    1555            4 :             const auto& cfg = bundlePtr->GetConfig();
    1556              :             uint64_t parallelImm
    1557            4 :                 = GetParallelParamV2(cfg.cloneNum, bundlePtr->GetRepeatLoopIdx(), bundlePtr->GetTotalLoopNum());
    1558            4 :             CcuV2::LoadImdToXn(instr++, bundlePtr->GetParallelVar().Id(), parallelImm);
    1559            4 :             curInstrId++;
    1560            4 :             uint64_t offsetImm = GetOffsetParam(cfg.addrOffset, cfg.ccuBufferOffset, cfg.eventOffset);
    1561            4 :             CcuV2::LoadImdToXn(instr++, bundlePtr->GetOffsetParam().Id(), offsetImm);
    1562            4 :             curInstrId++;
    1563            4 :             CcuV2::LoadImdToXn(instr++, bundlePtr->GetXnOffsetVar().Id(), cfg.varOffset);
    1564            4 :             curInstrId++;
    1565            5 :         } else if (isCompat) {
    1566            1 :             const uint16_t newXm = bundlePtr->GetNewParallelVar().Id();
    1567            1 :             const uint16_t scratch = bundlePtr->GetScratchVar().Id();
    1568            1 :             const uint16_t src = bundlePtr->GetParallelVar().Id();
    1569              :             struct FieldMap {
    1570              :                 uint16_t srcShift;
    1571              :                 uint16_t dstShift;
    1572              :             };
    1573            1 :             const FieldMap fields[] = {{41, 0}, {48, 10}, {55, 19}};
    1574            4 :             for (size_t i = 0; i < sizeof(fields) / sizeof(fields[0]); i++) {
    1575            3 :                 const uint16_t dst = (i == 0) ? newXm : scratch;
    1576            3 :                 CcuV2::LoadImdToXn(instr++, dep.reserveXnId, fields[i].srcShift);
    1577            3 :                 curInstrId++;
    1578            3 :                 CcuV2::SRL(instr++, dst, src, dep.reserveXnId);
    1579            3 :                 curInstrId++;
    1580            3 :                 CcuV2::LoadImdToXn(instr++, dep.reserveXnId, LOOP_FIELD_MASK);
    1581            3 :                 curInstrId++;
    1582            3 :                 CcuV2::And(instr++, dst, dst, dep.reserveXnId);
    1583            3 :                 curInstrId++;
    1584            3 :                 if (fields[i].dstShift != 0) {
    1585            2 :                     CcuV2::LoadImdToXn(instr++, dep.reserveXnId, fields[i].dstShift);
    1586            2 :                     curInstrId++;
    1587            2 :                     CcuV2::SLL(instr++, dst, dst, dep.reserveXnId);
    1588            2 :                     curInstrId++;
    1589            2 :                     CcuV2::Or(instr++, newXm, newXm, scratch);
    1590            2 :                     curInstrId++;
    1591              :                 }
    1592              :             }
    1593            1 :             CcuV2::LoadImdToXn(instr++, dep.reserveXnId, 0);
    1594            1 :             curInstrId++;
    1595            1 :             loopGroupConfigId = newXm;
    1596              :         }
    1597            9 :         return HcclResult::HCCL_SUCCESS;
    1598              :     }
    1599              : 
    1600            9 :     HcclResult CcuInsGeneratorV2::CcuRepLoopGroupBundleTranslate(
    1601              :         CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoopGroupBundle* bundlePtr,
    1602              :         const TransDep& dep)
    1603              :     {
    1604              :         UNUSED(ccuKernel);
    1605            9 :         CHK_PTR_NULL(bundlePtr);
    1606            9 :         const auto& loops = bundlePtr->GetLoops();
    1607            9 :         const auto layout = bundlePtr->GetLayout();
    1608              :         // 兼容路径:V2 下需插入位重排指令
    1609            9 :         const bool isCompat = (layout == CcuRepLoopGroupBundle::Layout::PackedVar);
    1610            9 :         const bool isConfig = (layout == CcuRepLoopGroupBundle::Layout::Config);
    1611              : 
    1612              :         // 每 loop 寄存器:按各 loop 自身构造方式载入,与 group 的构造方式相互独立
    1613            9 :         CHK_RET(LoopConfigTranslate(instr, curInstrId, bundlePtr, dep));
    1614              : 
    1615              :         // loopGroupConfig:config 按 960 位分布打包立即数;兼容路径把旧 parallelVar 重排到新分布
    1616              :         // 新分布:LoopNum[9:0] RepeatLoopIndex[18:10] ExtendNum[27:19]
    1617              :         // 旧分布:totalLoopNum<<41 repeatLoopIndex<<48 repeatNum<<55
    1618            9 :         uint16_t loopGroupConfigId{};
    1619            9 :         CHK_RET(LoopGroupConfigTranslate(instr, curInstrId, bundlePtr, dep, isConfig, isCompat, loopGroupConfigId));
    1620              : 
    1621              :         // LoopGroup:loops 定义在其后第 3 条起(跳过 2 条无条件跳转)
    1622              :         // xnOffset(Xp):config/version960 有来源,兼容路径无来源填 0
    1623            9 :         const uint16_t xnOffsetId = isCompat ? dep.reserveXnId : bundlePtr->GetXnOffsetVar().Id();
    1624           18 :         CcuV2::LoopGroup(
    1625            9 :             instr++, curInstrId + 3, loopGroupConfigId, bundlePtr->GetOffsetParam().Id(),
    1626              :             xnOffsetId); // 向后3条为loop入口
    1627            9 :         curInstrId++;
    1628              : 
    1629              :         // 无条件跳转,跳过后续 loop 定义
    1630            9 :         const uint16_t loopCount = static_cast<uint16_t>(loops.size());
    1631            9 :         const uint16_t jumpInstrId = curInstrId + 1;
    1632            9 :         const uint16_t targetInstrId = curInstrId + 2 + loopCount; // 跳转目的为loop指令后额外2条
    1633            9 :         CcuV2::LoadImdToXn(instr++, dep.reserveXnId, GetRelativeInstrId(jumpInstrId, targetInstrId));
    1634            9 :         curInstrId++;
    1635            9 :         CcuV2::Jump(instr++, dep.reserveXnId, 0, 0, static_cast<int>(ConditionType::DEFAULT));
    1636            9 :         curInstrId++;
    1637              : 
    1638           28 :         for (const auto& loop : loops) {
    1639           19 :             const auto& block = loop.repLoopBlock;
    1640           19 :             CHK_PTR_NULL(block);
    1641           95 :             CcuV2::Loop(
    1642           19 :                 instr++, block->StartInstrId(), block->StartInstrId() + block->InstrCount() - 1, loop.iterNumVar.Id(),
    1643           19 :                 loop.addrOffsetVar.Id(), loop.ctxIdVar.Id());
    1644           19 :             curInstrId++;
    1645              :         }
    1646              : 
    1647            9 :         CcuV2::LoadImdToXn(instr++, dep.reserveXnId, 0);
    1648            9 :         curInstrId++;
    1649              : 
    1650            9 :         return HcclResult::HCCL_SUCCESS;
    1651              :     }
    1652              : 
    1653           27 :     uint16_t CcuInsGeneratorV2::CcuRepLoopGroupBundleInstrCount(CcuRepLoopGroupBundle* bundlePtr)
    1654              :     {
    1655           27 :         if (bundlePtr == nullptr) {
    1656            0 :             Hccl::THROW<Hccl::CcuApiException>("[%s] bundlePtr is nullptr", __func__);
    1657              :         }
    1658           27 :         uint16_t total = 0;
    1659              :         // 每 loop 按自身构造方式计条数
    1660           84 :         for (const auto& loop : bundlePtr->GetLoops()) {
    1661           57 :             switch (loop.layout) {
    1662           21 :                 case CcuRepLoopGroupBundle::Layout::Config:
    1663           21 :                     total += 4; // 3 条载入(ctxId/iterNum/gsaOffset) + 1 条 Loop
    1664           21 :                     break;
    1665           24 :                 case CcuRepLoopGroupBundle::Layout::VersionV2:
    1666           24 :                     total += 2; // 1 条 ctxId 载入 + 1 条 Loop
    1667           24 :                     break;
    1668           12 :                 case CcuRepLoopGroupBundle::Layout::PackedVar:
    1669              :                 default:
    1670           12 :                     total += 8; // 7 条(ctxId1 + 位重排6) + 1 条 Loop
    1671           12 :                     break;
    1672              :             }
    1673              :         }
    1674              :         // group 头按 group 自身构造方式计条数
    1675           27 :         switch (bundlePtr->GetLayout()) {
    1676           12 :             case CcuRepLoopGroupBundle::Layout::Config:
    1677           12 :                 total += 7; // 打包3(parallel/offset/xnOffset) + loopgroup1 + 跳过2 + 收尾1
    1678           12 :                 break;
    1679           12 :             case CcuRepLoopGroupBundle::Layout::VersionV2:
    1680           12 :                 total += 4; // loopgroup1 + 跳过2 + 收尾1
    1681           12 :                 break;
    1682            3 :             case CcuRepLoopGroupBundle::Layout::PackedVar:
    1683              :             default:
    1684            3 :                 total += 23; // 位重排19 + loopgroup1 + 跳过2 + 收尾1
    1685            3 :                 break;
    1686              :         }
    1687           27 :         return total;
    1688              :     }
    1689              : 
    1690              : } // namespace CcuRep
    1691              : } // namespace hcomm
        

Generated by: LCOV version 2.0-1