LCOV - code coverage report
Current view: top level - src/api_check - kernel_data_copy_slice_check.cpp (source / functions) Coverage Total Hit
Test: coverage.info_filtered Lines: 88.0 % 83 73
Test Date: 2026-07-27 14:41:20 Functions: 100.0 % 6 6

            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              : /*!
      12              :  * \file kernel_data_copy_slice_check.cpp
      13              :  * \brief
      14              :  */
      15              : 
      16              : #include "kernel_check_params.h"
      17              : #include "kernel_data_copy_slice_check.h"
      18              : 
      19              : namespace AscendC {
      20              : namespace check {
      21           44 : bool TikcppDataCopySliceCheck::CheckSliceInfoParamters(const std::string& errMsg)
      22              : {
      23           80 :     for (uint32_t i = 0; i < param_.dimValue; i++) {
      24           64 :         if (param_.dstSliceInfo[i].burstLen != param_.srcSliceInfo[i].burstLen) {
      25            4 :             CHECK_LOG_ERROR(
      26              :                 "%s, "
      27              :                 "dstSliceInfo[%u]'s busrt len (%u) should be equal to SliceInfo[%u]'s busrt len (%u)",
      28              :                 errMsg.c_str(), i, param_.dstSliceInfo[i].burstLen, i, param_.srcSliceInfo[i].burstLen);
      29            4 :             return false;
      30              :         }
      31              : 
      32           60 :         if (i > 0 && param_.dstSliceInfo[i].burstLen != 1) {
      33            4 :             CHECK_LOG_ERROR(
      34              :                 "%s, "
      35              :                 "dim[%u]'s dstSliceInfo's busrt len should be equal to 1, but get (%u)",
      36              :                 errMsg.c_str(), i, param_.dstSliceInfo[i].burstLen);
      37            4 :             return false;
      38              :         }
      39              : 
      40           56 :         if (param_.dstSliceInfo[i].startIndex >= param_.dstSliceInfo[i].endIndex) {
      41            8 :             CHECK_LOG_ERROR(
      42              :                 "%s, "
      43              :                 "dstSliceInfo[%u]'s start index (%u) should be lee than dstSliceInfo[%u]'s end index (%u)",
      44              :                 errMsg.c_str(), i, param_.dstSliceInfo[i].startIndex, i, param_.dstSliceInfo[i].endIndex);
      45            8 :             return false;
      46              :         }
      47              : 
      48           48 :         if (param_.dstSliceInfo[i].endIndex >= param_.dstShape[i]) {
      49            4 :             CHECK_LOG_ERROR(
      50              :                 "%s, "
      51              :                 "dstSliceInfo[%u]'s end index (%u) should be less than dstSliceInfo[%u]'s shape (%u)",
      52              :                 errMsg.c_str(), i, param_.dstSliceInfo[i].endIndex, i, param_.dstShape[i]);
      53            4 :             return false;
      54              :         }
      55              : 
      56           44 :         if (param_.srcSliceInfo[i].startIndex >= param_.srcSliceInfo[i].endIndex) {
      57            4 :             CHECK_LOG_ERROR(
      58              :                 "%s, "
      59              :                 "srcSliceInfo[%u]'s start index (%u) should be lee than srcSliceInfo[%u]'s end index (%u)",
      60              :                 errMsg.c_str(), i, param_.srcSliceInfo[i].startIndex, i, param_.srcSliceInfo[i].endIndex);
      61            4 :             return false;
      62              :         }
      63              : 
      64           40 :         if (param_.srcSliceInfo[i].endIndex >= param_.srcShape[i]) {
      65            4 :             CHECK_LOG_ERROR(
      66              :                 "%s, "
      67              :                 "srcSliceInfo[%u]'s end index (%u) should be less than dst tensor dim[%u]'s shape (%u)",
      68              :                 errMsg.c_str(), i, param_.srcSliceInfo[i].endIndex, i, param_.srcShape[i]);
      69            4 :             return false;
      70              :         }
      71              :     }
      72           16 :     return true;
      73              : }
      74              : 
      75           88 : uint32_t TikcppDataCopySliceCheck::DataCopyGetTotalInstrsNum(const SliceInfo sliceInfo[], const uint32_t shapeIn[])
      76              : {
      77           88 :     uint32_t sliceSize = 1;
      78           88 :     uint32_t currentCount = 0;
      79           88 :     uint32_t totalInstrsNum = 0;
      80          264 :     for (uint32_t i = 0; i < param_.dimValue; i++) {
      81          176 :         if (i == 0) {
      82           88 :             totalInstrsNum = 1;
      83              :         } else {
      84           88 :             sliceSize = sliceSize * shapeIn[i - 1];
      85           88 :             currentCount =
      86           88 :                 (sliceInfo[i].endIndex - sliceInfo[i].startIndex + 1 + sliceInfo[i].stride) / (1 + sliceInfo[i].stride);
      87           88 :             totalInstrsNum = totalInstrsNum * currentCount;
      88              :         }
      89              :     }
      90           88 :     return totalInstrsNum;
      91              : }
      92              : 
      93           16 : bool TikcppDataCopySliceCheck::CheckDataCopyIntrsParamters(const std::string& errMsg)
      94              : {
      95           16 :     uint64_t oneStrideLen = 0;
      96              : 
      97           16 :     uint32_t oneSliceLen =
      98           16 :         param_.srcSliceInfo[0].burstLen * static_cast<uint32_t>(PlatFormParams::UB_BLOCK_SIZE) / param_.srcDtypeBytes +
      99           16 :         param_.srcSliceInfo[0].stride;
     100           16 :     uint32_t totalLen =
     101           16 :         param_.srcSliceInfo[0].endIndex - param_.srcSliceInfo[0].startIndex + 1 + param_.srcSliceInfo[0].stride;
     102           16 :     if (totalLen % oneSliceLen != 0) {
     103            8 :         CHECK_LOG_ERROR(
     104              :             "%s, "
     105              :             "construct datacopy intrs paramters block count failed, please check srcSliceInfo[0].burstLen(%u), "
     106              :             "srcSliceInfo[0].startIndex(%u), srcSliceInfo[0].endIndex(%u), srcSliceInfo[0].stride(%u)",
     107              :             errMsg.c_str(), param_.srcSliceInfo[0].burstLen, param_.srcSliceInfo[0].startIndex,
     108              :             param_.srcSliceInfo[0].endIndex, param_.srcSliceInfo[0].stride);
     109            8 :         return false;
     110              :     }
     111              : 
     112            8 :     if (param_.isGM2UB) {
     113            8 :         oneStrideLen = static_cast<uint64_t>(param_.dstSliceInfo[0].stride * param_.dstDtypeBytes);
     114            8 :         if (oneStrideLen % static_cast<uint64_t>(PlatFormParams::UB_BLOCK_SIZE) != 0) {
     115            4 :             CHECK_LOG_ERROR(
     116              :                 "%s, "
     117              :                 "one dst stride len (%lu) should be multiple of block size (%lu), "
     118              :                 "dstSliceInfo[0].stride(%u), dstDtypeBytes(%u)",
     119              :                 errMsg.c_str(), oneStrideLen, static_cast<uint64_t>(PlatFormParams::UB_BLOCK_SIZE),
     120              :                 param_.dstSliceInfo[0].stride, param_.dstDtypeBytes);
     121            4 :             return false;
     122              :         }
     123              :     } else {
     124            0 :         oneStrideLen = static_cast<uint64_t>(param_.srcSliceInfo[0].stride * param_.srcDtypeBytes);
     125            0 :         if (oneStrideLen % static_cast<uint64_t>(PlatFormParams::UB_BLOCK_SIZE) != 0) {
     126            0 :             CHECK_LOG_ERROR(
     127              :                 "%s, "
     128              :                 "one src stride len (%lu) should be multiple of block size (%lu), "
     129              :                 "srcSliceInfo[0].stride(%u), srcDtypeBytes(%u)",
     130              :                 errMsg.c_str(), oneStrideLen, static_cast<uint64_t>(PlatFormParams::UB_BLOCK_SIZE),
     131              :                 param_.srcSliceInfo[0].stride, param_.srcDtypeBytes);
     132            0 :             return false;
     133              :         }
     134              :     }
     135            4 :     return true;
     136              : }
     137              : 
     138           44 : bool TikcppDataCopySliceCheck::CheckDataCopyInstrsNum(const std::string& errMsg)
     139              : {
     140           44 :     uint32_t srcIntrsNum = DataCopyGetTotalInstrsNum(param_.srcSliceInfo, param_.srcShape);
     141           44 :     uint32_t dstIntrsNum = DataCopyGetTotalInstrsNum(param_.dstSliceInfo, param_.dstShape);
     142           44 :     if (srcIntrsNum != dstIntrsNum) {
     143            0 :         CHECK_LOG_ERROR(
     144              :             "%s, "
     145              :             "srcIntrsNum(%u) != dstIntrsNum(%u)",
     146              :             errMsg.c_str(), srcIntrsNum, dstIntrsNum);
     147            0 :         return false;
     148              :     }
     149           44 :     if (srcIntrsNum > MAX_SLICE_SIZE) {
     150            0 :         CHECK_LOG_ERROR(
     151              :             "%s, "
     152              :             "srcIntrsNum(%u) is bigger than MAX_SLICE_SIZE(%u), please reset the sliceInfo parameter",
     153              :             errMsg.c_str(), srcIntrsNum, MAX_SLICE_SIZE);
     154            0 :         return false;
     155              :     }
     156           44 :     return true;
     157              : }
     158              : 
     159           52 : bool TikcppDataCopySliceCheck::CheckAllHighLevel()
     160              : {
     161           52 :     const std::string supportPos = "VECIN/VECOUT/VECCALC";
     162           52 :     if (param_.isGM2UB) {
     163          132 :         ASCENDC_CHECK(CheckTensorScope(param_.logicPos, static_cast<uint8_t>(HardWareIndex::UB), "dst", supportPos));
     164              :     } else {
     165           24 :         ASCENDC_CHECK(CheckTensorScope(param_.logicPos, static_cast<uint8_t>(HardWareIndex::UB), "src", supportPos));
     166              :     }
     167           88 :     ASCENDC_CHECK(CheckDataCopyInstrsNum("check total number of instructions mov or mov_align failed"));
     168          132 :     ASCENDC_CHECK(CheckBufferSizeOverFlow(
     169              :         param_.sizeNum, GlobalParams::Instance().bufferSizeMap.at(param_.pos), "check ub tensor buffersize failed"));
     170           88 :     ASCENDC_CHECK(CheckSliceInfoParamters("check sliceInfo paramters"));
     171           32 :     ASCENDC_CHECK(CheckDataCopyIntrsParamters("check data copy instr paramters"));
     172            4 :     ASCENDC_CHECK(CheckAddrAlign());
     173            4 :     return true;
     174           52 : }
     175              : 
     176            4 : bool TikcppDataCopySliceCheck::CheckAddrAlign()
     177              : {
     178              :     // gm is 1 Byte aligned, so do not need to check aligned
     179              :     uint64_t ubBaseAddr =
     180            4 :         static_cast<uint64_t>(reinterpret_cast<uintptr_t>(ConstDefiner::Instance().GetHardwareBaseAddr(Hardware::UB)));
     181              : 
     182            4 :     if (param_.isGM2UB) {
     183            4 :         uint64_t dstAbsPos = param_.dstAddr - ubBaseAddr;
     184            4 :         ASCENDC_CHECK_AND_LOG(((dstAbsPos % alignBytes_) == 0), {
     185              :             CHECK_LOG_ERROR(
     186              :                 "instr %s dst addr is %lu, which should be %u B Aligned", apiName.c_str(), dstAbsPos, alignBytes_);
     187              :         });
     188              :     } else {
     189            0 :         uint64_t srcAbsPos = param_.srcAddr - ubBaseAddr;
     190            0 :         ASCENDC_CHECK_AND_LOG(((srcAbsPos % alignBytes_) == 0), {
     191              :             CHECK_LOG_ERROR(
     192              :                 "instr %s src addr is %lu, which should be %u B Aligned", apiName.c_str(), srcAbsPos, alignBytes_);
     193              :         });
     194              :     }
     195            4 :     return true;
     196              : }
     197              : } // namespace check
     198              : } // namespace AscendC
        

Generated by: LCOV version 2.0-1