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 :
13 : #include "string_util.h"
14 : #include "exception_util.h"
15 : #include "ccu_api_exception.h"
16 :
17 : namespace Hccl {
18 : namespace CcuRep {
19 :
20 61 : CcuRepLoopCall::CcuRepLoopCall(const std::string &label) : label(label)
21 : {
22 61 : type = CcuRepType::LOOP_CALL;
23 61 : }
24 :
25 19 : const std::string &CcuRepLoopCall::GetLabel() const
26 : {
27 19 : return label;
28 : }
29 :
30 18 : void CcuRepLoopCall::Reference(std::shared_ptr<CcuRepLoopBlock> refRep)
31 : {
32 18 : loopBlock = refRep;
33 18 : }
34 :
35 63 : void CcuRepLoopCall::SetInArg(const Variable &var)
36 : {
37 63 : inArgCount++;
38 63 : inArgInstrCount++;
39 63 : inArgs.push_back(CcuRepArg(var));
40 63 : }
41 :
42 0 : void CcuRepLoopCall::SetInArg(const std::vector<Variable> &varList)
43 : {
44 0 : inArgCount += varList.size();
45 0 : inArgInstrCount += varList.size();
46 0 : inArgs.push_back(CcuRepArg(varList));
47 0 : }
48 :
49 63 : void CcuRepLoopCall::SetInArg(const Memory &mem)
50 : {
51 63 : inArgCount++;
52 63 : inArgInstrCount += 2; // 传递Memory需要2条指令
53 63 : inArgs.push_back(CcuRepArg(mem));
54 63 : }
55 :
56 51 : void CcuRepLoopCall::SetInArg(const std::vector<Memory> &memList)
57 : {
58 51 : inArgCount += memList.size();
59 51 : inArgInstrCount += memList.size() * 2; // 传递Memory需要2条指令
60 51 : inArgs.push_back(CcuRepArg(memList));
61 51 : }
62 :
63 54 : uint16_t CcuRepLoopCall::InstrCount()
64 : {
65 54 : instrCount = inArgInstrCount;
66 54 : return instrCount;
67 : }
68 :
69 18 : bool CcuRepLoopCall::Translate(CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
70 : {
71 18 : this->instrId = instrId;
72 18 : translated = true;
73 :
74 18 : Hccl::CHECK_NULLPTR(loopBlock, "[CcuRepLoopCall::Translate] LoopBlock is nullptr!");
75 :
76 18 : if (!loopBlock->Translated()) {
77 0 : THROW<CcuApiException>("Reference To Invalid LoopBlock");
78 : }
79 :
80 66 : for (uint32_t i = 0; i < inArgs.size(); i++) {
81 48 : if (inArgs[i].type == CcuArgType::VARIABLE && loopBlock->GetArg(i).type == CcuArgType::VARIABLE) {
82 18 : LoadXXInstr(instr++, loopBlock->GetArg(i).var.Id(), inArgs[i].var.Id(), dep.reserveXnId);
83 30 : } else if (inArgs[i].type == CcuArgType::VARIABLE_LIST
84 30 : && loopBlock->GetArg(i).type == CcuArgType::VARIABLE_LIST) {
85 0 : if (inArgs[i].varList.size() != loopBlock->GetArg(i).varList.size()) {
86 0 : THROW<CcuApiException>("Mismatched Arg Size");
87 : }
88 0 : for (uint32_t j = 0; j < inArgs[i].varList.size(); j++) {
89 0 : LoadXXInstr(instr++, loopBlock->GetArg(i).varList[j].Id(), inArgs[i].varList[j].Id(), dep.reserveXnId);
90 : }
91 30 : } else if (inArgs[i].type == CcuArgType::MEMORY && loopBlock->GetArg(i).type == CcuArgType::MEMORY) {
92 21 : LoadGSAGSAInstr(instr++, loopBlock->GetArg(i).mem.addr.Id(), inArgs[i].mem.addr.Id(), dep.reserveGsaId);
93 21 : LoadXXInstr(instr++, loopBlock->GetArg(i).mem.token.Id(), inArgs[i].mem.token.Id(), dep.reserveXnId);
94 9 : } else if (inArgs[i].type == CcuArgType::MEMORY_LIST && loopBlock->GetArg(i).type == CcuArgType::MEMORY_LIST) {
95 9 : if (inArgs[i].memList.size() != loopBlock->GetArg(i).memList.size()) {
96 0 : THROW<CcuApiException>("Mismatched Arg Size");
97 : }
98 81 : for (uint32_t j = 0; j < inArgs[i].memList.size(); j++) {
99 72 : LoadGSAGSAInstr(instr++, loopBlock->GetArg(i).memList[j].addr.Id(), inArgs[i].memList[j].addr.Id(),
100 72 : dep.reserveGsaId);
101 72 : LoadXXInstr(instr++, loopBlock->GetArg(i).memList[j].token.Id(), inArgs[i].memList[j].token.Id(),
102 72 : dep.reserveXnId);
103 : }
104 : } else {
105 0 : THROW<CcuApiException>("Mismatched Arg Type");
106 : }
107 : }
108 :
109 18 : instrId += InstrCount();
110 :
111 18 : return translated;
112 : }
113 :
114 27 : std::string CcuRepLoopCall::Describe()
115 : {
116 27 : return StringFormat("LoopCall[%s]", label.c_str());
117 : }
118 :
119 : }; // namespace CcuRep
120 : }; // namespace Hccl
|