LCOV - code coverage report
Current view: top level - base_comm/resources/ccu/ccu_representation/reps/arithmetic - ccu_rep_sub.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 97.7 % 130 127
Test Date: 2026-08-18 17:47:01 Functions: 95.5 % 22 21

            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 "ccu_ins_generator_v1.h"
      13              : #include "string_util.h"
      14              : #include "exception_util.h"
      15              : #include "ccu_api_exception.h"
      16              : #include "ccu_kernel.h"
      17              : 
      18              : namespace hcomm {
      19              : namespace CcuRep {
      20              : 
      21           35 :     void CcuRepSub::SetCommonInfo()
      22              :     {
      23           35 :         type = CcuRepType::SUB;
      24           35 :         instrCount = insGenPtr->GetInstrCount(type);
      25           35 :     }
      26              : 
      27            7 :     CcuRepSub::CcuRepSub(
      28            7 :         CcuInsGeneratorBase* insGenPtr, const Variable& varC, const Variable& varA, const Variable& varB)
      29            7 :         : insGenPtr(insGenPtr),
      30            7 :           subType(MinusSubType::VAR_MINUS_VAR_TO_VAR),
      31            7 :           varA(varA),
      32            7 :           varB(varB),
      33           14 :           varC(varC)
      34              :     {
      35            7 :         SetCommonInfo();
      36            7 :     }
      37              : 
      38            5 :     CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Variable& varC, const Variable& varA, uint16_t immedB)
      39            5 :         : insGenPtr(insGenPtr),
      40            5 :           subType(MinusSubType::VAR_MINUS_IMMED_TO_VAR),
      41            5 :           varA(varA),
      42            5 :           varC(varC),
      43           10 :           immedB(immedB)
      44              :     {
      45            5 :         SetCommonInfo();
      46            5 :     }
      47              : 
      48            3 :     CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Variable& varA, const Variable& varB)
      49            3 :         : insGenPtr(insGenPtr),
      50            3 :           subType(MinusSubType::SELF_SUB_VAR_VARIABLE),
      51            3 :           varA(varA),
      52            3 :           varB(varB)
      53              :     {
      54            3 :         SetCommonInfo();
      55            3 :     }
      56              : 
      57            3 :     CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Variable& varA, uint16_t immedB)
      58            3 :         : insGenPtr(insGenPtr),
      59            3 :           subType(MinusSubType::SELF_SUB_IMMED_VARIABLE),
      60            3 :           varA(varA),
      61            3 :           immedB(immedB)
      62              :     {
      63            3 :         SetCommonInfo();
      64            3 :     }
      65              : 
      66            4 :     CcuRepSub::CcuRepSub(
      67            4 :         CcuInsGeneratorBase* insGenPtr, const Address& addrC, const Address& addrA, const Variable& varB)
      68            4 :         : insGenPtr(insGenPtr),
      69            4 :           subType(MinusSubType::ADDR_MINUS_VAR_TO_ADDR),
      70            4 :           varB(varB),
      71            4 :           addrA(addrA),
      72           12 :           addrC(addrC)
      73              :     {
      74            4 :         SetCommonInfo();
      75            4 :     }
      76              : 
      77            2 :     CcuRepSub::CcuRepSub(
      78            2 :         CcuInsGeneratorBase* insGenPtr, const Address& addrC, const Address& addrA, const uint16_t immedB)
      79            2 :         : insGenPtr(insGenPtr),
      80            2 :           subType(MinusSubType::ADDR_MINUS_IMMED_TO_ADDR),
      81            2 :           addrA(addrA),
      82            2 :           addrC(addrC),
      83            4 :           immedB(immedB)
      84              :     {
      85            2 :         SetCommonInfo();
      86            2 :     }
      87              : 
      88            3 :     CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Address& addrA, const Variable& varB)
      89            3 :         : insGenPtr(insGenPtr),
      90            3 :           subType(MinusSubType::SELF_SUB_VAR_ADDRESS),
      91            3 :           varB(varB),
      92            6 :           addrA(addrA)
      93              :     {
      94            3 :         SetCommonInfo();
      95            3 :     }
      96              : 
      97            3 :     CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Address& addrA, const uint16_t immedB)
      98            3 :         : insGenPtr(insGenPtr),
      99            3 :           subType(MinusSubType::SELF_SUB_IMMED_ADDRESS),
     100            3 :           addrA(addrA),
     101            6 :           immedB(immedB)
     102              :     {
     103            3 :         SetCommonInfo();
     104            3 :     }
     105              : 
     106            2 :     CcuRepSub::CcuRepSub(
     107            2 :         CcuInsGeneratorBase* insGenPtr, const Address& addrC, const Variable& varA, const uint16_t immedB)
     108            2 :         : insGenPtr(insGenPtr),
     109            2 :           subType(MinusSubType::VAR_MINUS_IMMED_TO_ADDR),
     110            2 :           varA(varA),
     111            2 :           addrC(addrC),
     112            2 :           immedB(immedB)
     113              :     {
     114            2 :         SetCommonInfo();
     115            2 :     }
     116            3 :     CcuRepSub::CcuRepSub(
     117            3 :         CcuInsGeneratorBase* insGenPtr, const Variable& varC, const Address& addrA, const uint16_t immedB)
     118            3 :         : insGenPtr(insGenPtr),
     119            3 :           subType(MinusSubType::ADDR_MINUS_IMMED_TO_VAR),
     120            3 :           varC(varC),
     121            3 :           addrA(addrA),
     122            6 :           immedB(immedB)
     123              :     {
     124            3 :         SetCommonInfo();
     125            3 :     }
     126              : 
     127            8 :     void CcuRepSub::ValidateInsGenPtrForSub()
     128              :     {
     129            8 :         CcuInsGeneratorV1* tmpPtrV1 = dynamic_cast<CcuInsGeneratorV1*>(insGenPtr);
     130            8 :         CHK_PRT_THROW(
     131              :             (tmpPtrV1 && !supportCcuV1),
     132              :             HCCL_ERROR("[CcuRepSub][%s]Cannot translate CcuRepSub for A5 when supportCcuV1 is false", __func__),
     133              :             Hccl::CcuApiException, "tmpPtrV1 does not match supportCcuV1");
     134            8 :     }
     135              : 
     136            8 :     bool CcuRepSub::Translate(
     137              :         CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& instrId, [[maybe_unused]] const TransDep& dep)
     138              :     {
     139            8 :         ValidateInsGenPtrForSub();
     140            8 :         Hccl::CHECK_NULLPTR(instr, "[CcuRepSub::Translate] instr is nullptr!");
     141            8 :         this->instrId = instrId;
     142            8 :         translated = true;
     143              : 
     144            8 :         CHK_PRT_THROW(
     145              :             insGenPtr->CcuRepSubTranslate(ccuKernel, instr, this) != HcclResult::HCCL_SUCCESS,
     146              :             HCCL_ERROR("[CcuRepSub][Translate] failed to translate for instrId[%u]", instrId), Hccl::CcuApiException,
     147              :             "CcuRepSub translate failed");
     148              : 
     149            8 :         CHK_PRT_THROW(
     150              :             (instrId > UINT16_MAX - instrCount),
     151              :             HCCL_ERROR(
     152              :                 "[CcuRepSub::Translate]uint16 integer overflow occurs, instrId = [%hu], instrCount = [%hu]", instrId,
     153              :                 instrCount),
     154              :             Hccl::InternalException, "integer overflow");
     155            8 :         instrId += instrCount;
     156              : 
     157            8 :         return translated;
     158              :     }
     159              : 
     160           14 :     std::string CcuRepSub::Describe()
     161              :     {
     162           14 :         switch (subType) {
     163            3 :             case MinusSubType::VAR_MINUS_VAR_TO_VAR: {
     164              :                 return Hccl::StringFormat(
     165            3 :                     "Variable[%u] = Variable[%u] - Variable[%u]", varC.Id(), varA.Id(), varB.Id());
     166              :             }
     167            3 :             case MinusSubType::VAR_MINUS_IMMED_TO_VAR: {
     168            3 :                 return Hccl::StringFormat("Variable[%u] = Variable[%u] - Immed[%u]", varC.Id(), varA.Id(), immedB);
     169              :             }
     170            1 :             case MinusSubType::SELF_SUB_VAR_VARIABLE: {
     171            1 :                 return Hccl::StringFormat("Variable[%u] -= Variable[%u]", varA.Id(), varB.Id());
     172              :             }
     173            1 :             case MinusSubType::SELF_SUB_IMMED_VARIABLE: {
     174            1 :                 return Hccl::StringFormat("Variable[%u] -= Immed[%u]", varA.Id(), immedB);
     175              :             }
     176            1 :             case MinusSubType::ADDR_MINUS_VAR_TO_ADDR: {
     177              :                 return Hccl::StringFormat(
     178            1 :                     "address[%u] = address[%u] - Variable[%u]", addrC.Id(), addrA.Id(), varB.Id());
     179              :             }
     180            1 :             case MinusSubType::ADDR_MINUS_IMMED_TO_ADDR: {
     181            1 :                 return Hccl::StringFormat("address[%u] = address[%u] - Immed[%u]", addrC.Id(), addrA.Id(), immedB);
     182              :             }
     183            1 :             case MinusSubType::SELF_SUB_VAR_ADDRESS: {
     184            1 :                 return Hccl::StringFormat("address[%u] -= Variable[%u]", addrA.Id(), varB.Id());
     185              :             }
     186            1 :             case MinusSubType::SELF_SUB_IMMED_ADDRESS: {
     187            1 :                 return Hccl::StringFormat("address[%u] -= Immed[%u]", addrA.Id(), immedB);
     188              :             }
     189            1 :             case MinusSubType::VAR_MINUS_IMMED_TO_ADDR: {
     190            1 :                 return Hccl::StringFormat("address[%u] = Variable[%u] - Immed[%u]", addrC.Id(), varA.Id(), immedB);
     191              :             }
     192            1 :             case MinusSubType::ADDR_MINUS_IMMED_TO_VAR: {
     193            1 :                 return Hccl::StringFormat("Variable[%u] = address[%u] - Immed[%u]", varC.Id(), addrA.Id(), immedB);
     194              :             }
     195            0 :             default: {
     196            0 :                 return Hccl::StringFormat("Invalid Sub");
     197              :             }
     198              :         }
     199              :     }
     200              : 
     201            5 :     Address CcuRepSub::GetAddrA() { return addrA; }
     202              : 
     203            0 :     Address CcuRepSub::GetAddrB() { return addrB; }
     204              : 
     205            2 :     Address CcuRepSub::GetAddrC() { return addrC; }
     206              : 
     207            6 :     Variable CcuRepSub::GetVarA() { return varA; }
     208              : 
     209            7 :     Variable CcuRepSub::GetVarB() { return varB; }
     210              : 
     211            7 :     Variable CcuRepSub::GetVarC() { return varC; }
     212              : 
     213            4 :     uint16_t CcuRepSub::GetImmedB() { return immedB; }
     214              : 
     215           18 :     MinusSubType CcuRepSub::GetSubType() { return subType; }
     216              : }; // namespace CcuRep
     217              : }; // namespace hcomm
        

Generated by: LCOV version 2.0-1