LCOV - code coverage report
Current view: top level - base_comm/resources/ccu/ccu_representation/reps/control - ccu_rep_jump.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 84.9 % 205 174
Test Date: 2026-08-18 17:47:01 Functions: 90.6 % 32 29

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

Generated by: LCOV version 2.0-1