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