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 <climits>
13 :
14 : #include "string_util.h"
15 : #include "exception_util.h"
16 : #include "ccu_api_exception.h"
17 :
18 : namespace Hccl {
19 : namespace CcuRep {
20 :
21 53 : CcuRepLoop::CcuRepLoop(const std::string &label, const Variable &loopParam) : label(label), loopParam(loopParam)
22 : {
23 53 : type = CcuRepType::LOOP;
24 53 : instrCount = 1; // loop翻译需要1条指令
25 53 : }
26 :
27 10 : const std::string &CcuRepLoop::GetLabel() const
28 : {
29 10 : return label;
30 : }
31 :
32 11 : void CcuRepLoop::Reference(std::shared_ptr<CcuRepLoopBlock> refRep)
33 : {
34 11 : loopBlock = refRep;
35 11 : }
36 :
37 51 : std::shared_ptr<CcuRepBase> CcuRepLoop::SetLoopParam(Executor executor, Variable var)
38 : {
39 51 : return std::make_shared<CcuRepSetLoop>(loopParam, executor, var);
40 : }
41 :
42 9 : bool CcuRepLoop::Translate(CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
43 : {
44 9 : this->instrId = instrId;
45 9 : translated = true;
46 :
47 9 : Hccl::CHECK_NULLPTR(loopBlock, "[CcuRepLoop::Translate] LoopBlock is nullptr!");
48 :
49 9 : if (!loopBlock->Translated()) {
50 0 : THROW<CcuApiException>("Reference To Invalid LoopBlock");
51 : }
52 :
53 9 : uint16_t startInstrId = loopBlock->StartInstrId();
54 9 : uint16_t loopInstrCount = loopBlock->InstrCount();
55 9 : if (loopInstrCount == 0) {
56 0 : HCCL_ERROR("[CcuRepLoop][Translate] loopInstrCount[%u] is 0, which causes underflow in endInstrId calculation.",
57 : loopInstrCount);
58 0 : return false;
59 : }
60 9 : if (startInstrId > USHRT_MAX - loopInstrCount) {
61 0 : HCCL_ERROR("[CcuRepLoop][Translate] startInstrId[%u] + loopInstrCount[%u] exceeds the maximum value of unsigned short int.",
62 : startInstrId, loopInstrCount);
63 0 : return false;
64 : }
65 9 : if (instrId > USHRT_MAX - instrCount) {
66 0 : HCCL_ERROR("[CcuRepLoop][Translate] instrId[%u] + instrCount[%u] exceeds the maximum value of unsigned short int.",
67 : instrId, instrCount);
68 0 : return false;
69 : }
70 9 : uint16_t endInstrId = startInstrId + loopInstrCount - 1;
71 :
72 9 : LoopInstr(instr++, startInstrId, endInstrId, loopParam.Id());
73 :
74 9 : instrId += instrCount;
75 :
76 9 : return translated;
77 : }
78 :
79 12 : std::string CcuRepLoop::Describe()
80 : {
81 12 : return StringFormat("Loop reference to [%s]", label.c_str());
82 : }
83 :
84 : }; // namespace CcuRep
85 : }; // namespace Hccl
|