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 { return label; }
28 :
29 11 : void CcuRepLoop::Reference(std::shared_ptr<CcuRepLoopBlock> refRep) { loopBlock = refRep; }
30 :
31 51 : std::shared_ptr<CcuRepBase> CcuRepLoop::SetLoopParam(Executor executor, Variable var)
32 : {
33 51 : return std::make_shared<CcuRepSetLoop>(loopParam, executor, var);
34 : }
35 :
36 9 : bool CcuRepLoop::Translate(CcuInstr*& instr, uint16_t& instrId, [[maybe_unused]] const TransDep& dep)
37 : {
38 9 : this->instrId = instrId;
39 9 : translated = true;
40 :
41 9 : Hccl::CHECK_NULLPTR(loopBlock, "[CcuRepLoop::Translate] LoopBlock is nullptr!");
42 :
43 9 : if (!loopBlock->Translated()) {
44 0 : THROW<CcuApiException>("Reference To Invalid LoopBlock");
45 : }
46 :
47 9 : uint16_t startInstrId = loopBlock->StartInstrId();
48 9 : uint16_t loopInstrCount = loopBlock->InstrCount();
49 9 : if (loopInstrCount == 0) {
50 0 : HCCL_ERROR(
51 : "[CcuRepLoop][Translate] loopInstrCount[%u] is 0, which causes underflow in endInstrId calculation.",
52 : loopInstrCount);
53 0 : return false;
54 : }
55 9 : if (startInstrId > USHRT_MAX - loopInstrCount) {
56 0 : HCCL_ERROR(
57 : "[CcuRepLoop][Translate] startInstrId[%u] + loopInstrCount[%u] exceeds the maximum value of unsigned "
58 : "short int.",
59 : startInstrId, loopInstrCount);
60 0 : return false;
61 : }
62 9 : if (instrId > USHRT_MAX - instrCount) {
63 0 : HCCL_ERROR(
64 : "[CcuRepLoop][Translate] instrId[%u] + instrCount[%u] exceeds the maximum value of unsigned short int.",
65 : instrId, instrCount);
66 0 : return false;
67 : }
68 9 : uint16_t endInstrId = startInstrId + loopInstrCount - 1;
69 :
70 9 : LoopInstr(instr++, startInstrId, endInstrId, loopParam.Id());
71 :
72 9 : instrId += instrCount;
73 :
74 9 : return translated;
75 : }
76 :
77 12 : std::string CcuRepLoop::Describe() { return StringFormat("Loop reference to [%s]", label.c_str()); }
78 :
79 : }; // namespace CcuRep
80 : }; // namespace Hccl
|