LCOV - code coverage report
Current view: top level - legacy/ascend950/unified_platform/ccu/ccu_representation/reps/control - ccu_rep_funcblock.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 88.9 % 90 80
Test Date: 2026-07-28 12:11:00 Functions: 100.0 % 13 13

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 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.h"
      12              : #include "ccu_rep_reference_manager.h"
      13              : #include "ccu_rep_translator.h"
      14              : 
      15              : #include "string_util.h"
      16              : #include "exception_util.h"
      17              : #include "ccu_api_exception.h"
      18              : 
      19              : namespace Hccl {
      20              : namespace CcuRep {
      21              : 
      22            3 : CcuRepFuncBlock::CcuRepFuncBlock(const std::string &label) : CcuRepBlock(label)
      23              : {
      24            3 :     type = CcuRepType::FUNC_BLOCK;
      25            3 : }
      26              : 
      27            7 : std::string CcuRepFuncBlock::Describe()
      28              : {
      29            7 :     return StringFormat("FuncBlock[%s]", GetLabel().c_str());
      30              : }
      31              : 
      32            3 : void CcuRepFuncBlock::SetFuncManager(CcuRepReferenceManager *funcManager)
      33              : {
      34            3 :     this->funcManager = funcManager;
      35            3 : }
      36              : 
      37            3 : void CcuRepFuncBlock::SetCallLayer(uint16_t callLayer)
      38              : {
      39            3 :     if (callLayer != FUNC_CALL_LAYER_INVALID) {
      40            0 :         this->callLayer = callLayer;
      41            0 :         return;
      42              :     }
      43              : 
      44            3 :     uint16_t innerCallLayer = 0;
      45           17 :     for (const auto &rep : GetReps()) {
      46           14 :         if (rep->Type() == CcuRepType::FUNC_CALL) {
      47            0 :             innerCallLayer  = std::static_pointer_cast<CcuRepFuncCall>(rep)->GetCallLayer() + 1;
      48            0 :             this->callLayer = this->callLayer > innerCallLayer ? this->callLayer : innerCallLayer;
      49              :         }
      50              :     }
      51            3 :     if (this->callLayer > FUNC_NEST_MAX - 1) {
      52            0 :         THROW<CcuApiException>("Max Func Call Nest Num is %u", FUNC_NEST_MAX);
      53              :     }
      54              : }
      55              : 
      56            2 : uint16_t CcuRepFuncBlock::GetCallLayer() const
      57              : {
      58            2 :     return callLayer;
      59              : }
      60              : 
      61            3 : void CcuRepFuncBlock::DefineInArg(const Variable &var)
      62              : {
      63            3 :     inArgCount++;
      64            3 :     if (inArgCount > FUNC_ARG_MAX) {
      65            0 :         THROW<CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_ARG_MAX);
      66              :     }
      67            3 :     inArgs.push_back(CcuRepArg(var));
      68            9 :     HCCL_INFO("Define Input Arg: Index[%u], Type[Variable] Id[%u]", inArgs.size(), var.Id());
      69            3 : }
      70              : 
      71            3 : void CcuRepFuncBlock::DefineOutArg(const Variable &var)
      72              : {
      73            3 :     outArgCount++;
      74            3 :     if (outArgCount > FUNC_ARG_MAX) {
      75            0 :         THROW<CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_ARG_MAX);
      76              :     }
      77            3 :     outArgs.push_back(CcuRepArg(var));
      78            9 :     HCCL_INFO("Define Output Arg: Index[%u], Type[Variable] Id[%u]", outArgs.size(), var.Id());
      79            3 : }
      80              : 
      81            1 : void CcuRepFuncBlock::DefineInArg(const std::vector<Variable> &varList)
      82              : {
      83            1 :     inArgCount += varList.size();
      84            1 :     if (inArgCount > FUNC_ARG_MAX) {
      85            0 :         THROW<CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_ARG_MAX);
      86              :     }
      87            1 :     inArgs.push_back(CcuRepArg(varList));
      88            3 :     HCCL_INFO("Define Input Arg: Index[%u], Type[Variable List]: ", inArgs.size());
      89            3 :     for (uint32_t index = 0; index < varList.size(); index++) {
      90            6 :         HCCL_INFO("    Index[%u].Id[%u]", index, varList[index].Id());
      91              :     }
      92            1 : }
      93              : 
      94            1 : void CcuRepFuncBlock::DefineOutArg(const std::vector<Variable> &varList)
      95              : {
      96            1 :     outArgCount += varList.size();
      97            1 :     if (outArgCount > FUNC_ARG_MAX) {
      98            0 :         THROW<CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_ARG_MAX);
      99              :     }
     100            1 :     outArgs.push_back(CcuRepArg(varList));
     101            3 :     HCCL_INFO("Define Output Arg: Index[%u], Type[Variable List]: ", outArgs.size());
     102            3 :     for (uint32_t index = 0; index < varList.size(); index++) {
     103            6 :         HCCL_INFO("    Index[%u].Id[%u]", index, varList[index].Id());
     104              :     }
     105            1 : }
     106              : 
     107            4 : uint16_t CcuRepFuncBlock::InstrCount()
     108              : {
     109            4 :     instrCount = CcuRepBlock::InstrCount() + inArgCount + outArgCount + 2; // FuncBlock需要2外两条指令
     110            4 :     return instrCount;
     111              : }
     112              : 
     113            3 : bool CcuRepFuncBlock::Translate(CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
     114              : {
     115            3 :     if (funcManager == nullptr) {
     116            0 :         THROW<CcuApiException>("funcManager is nullptr");
     117              :     }
     118              : 
     119            3 :     this->instrId = instrId;
     120            3 :     translated    = true;
     121              : 
     122              :     // 函数入口为Nop指令
     123            3 :     LoadImdToXnInstr(instr++, dep.reserveXnId, 0);
     124            3 :     instrId++;
     125              : 
     126            3 :     uint32_t iInArg = 0;
     127            7 :     for (uint32_t i = 0; i < inArgs.size(); i++) {
     128            4 :         if (inArgs[i].type == CcuArgType::VARIABLE) {
     129            3 :             LoadXXInstr(instr++, inArgs[i].var.Id(), funcManager->GetFuncIn()[iInArg++].Id(), dep.reserveXnId);
     130            3 :             instrId++;
     131            1 :         } else if (inArgs[i].type == CcuArgType::VARIABLE_LIST) {
     132            3 :             for (uint32_t j = 0; j < inArgs[i].varList.size(); j++) {
     133            2 :                 LoadXXInstr(instr++, inArgs[i].varList[j].Id(), funcManager->GetFuncIn()[iInArg++].Id(),
     134            2 :                             dep.reserveXnId);
     135            2 :                 instrId++;
     136              :             }
     137              :         }
     138              :     }
     139              : 
     140              :     // 使用空实现的自定义删除器,避免智能指针析构时释放对象
     141              :     auto translator
     142            3 :         = CcuRepTranslator(std::shared_ptr<CcuRepReferenceManager>(funcManager, [](CcuRepReferenceManager *ptr) {}), dep);
     143            3 :     translator.Translate(GetReps(), instr, instrId, [](std::shared_ptr<CcuRepBase> rep) -> bool {
     144           28 :         return true;
     145              :     });
     146              : 
     147            3 :     uint32_t iOutArg = 0;
     148            7 :     for (uint32_t i = 0; i < outArgs.size(); i++) {
     149            4 :         if (outArgs[i].type == CcuArgType::VARIABLE) {
     150            3 :             LoadXXInstr(instr++, funcManager->GetFuncOut()[iOutArg++].Id(), outArgs[i].var.Id(), dep.reserveXnId);
     151            3 :             instrId++;
     152            1 :         } else if (outArgs[i].type == CcuArgType::VARIABLE_LIST) {
     153            3 :             for (uint32_t j = 0; j < outArgs[i].varList.size(); j++) {
     154            2 :                 LoadXXInstr(instr++, funcManager->GetFuncOut()[iOutArg++].Id(), outArgs[i].varList[j].Id(),
     155            2 :                             dep.reserveXnId);
     156            2 :                 instrId++;
     157              :             }
     158              :         }
     159              :     }
     160              : 
     161            3 :     JumpInstr(instr++, funcManager->GetFuncRet(callLayer).Id(), dep.reserveXnId, 1);
     162            3 :     instrId++;
     163              : 
     164            3 :     return translated;
     165            3 : }
     166              : 
     167              : }; // namespace CcuRep
     168              : }; // namespace Hccl
        

Generated by: LCOV version 2.0-1