Line data Source code
1 : /**
2 : * Copyright (c) 2026 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_v1.h"
12 : #include "ccu_ins_generator_v1.h"
13 : #include "string_util.h"
14 : #include "exception_util.h"
15 : #include "ccu_api_exception.h"
16 : #include "ccu_kernel.h"
17 :
18 : namespace hcomm {
19 : namespace CcuRep {
20 :
21 35 : void CcuRepSub::SetCommonInfo()
22 : {
23 35 : type = CcuRepType::SUB;
24 35 : instrCount = insGenPtr->GetInstrCount(type);
25 35 : }
26 :
27 7 : CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Variable &varC, const Variable &varA, const Variable &varB)
28 7 : : insGenPtr(insGenPtr), subType(MinusSubType::VAR_MINUS_VAR_TO_VAR), varA(varA), varB(varB), varC(varC)
29 : {
30 7 : SetCommonInfo();
31 7 : }
32 :
33 5 : CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Variable &varC, const Variable &varA, uint16_t immedB)
34 5 : : insGenPtr(insGenPtr), subType(MinusSubType::VAR_MINUS_IMMED_TO_VAR), varA(varA), varC(varC), immedB(immedB)
35 : {
36 5 : SetCommonInfo();
37 5 : }
38 :
39 3 : CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Variable &varA, const Variable &varB)
40 3 : : insGenPtr(insGenPtr), subType(MinusSubType::SELF_SUB_VAR_VARIABLE), varA(varA), varB(varB)
41 : {
42 3 : SetCommonInfo();
43 3 : }
44 :
45 3 : CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Variable &varA, uint16_t immedB)
46 3 : : insGenPtr(insGenPtr), subType(MinusSubType::SELF_SUB_IMMED_VARIABLE), varA(varA), immedB(immedB)
47 : {
48 3 : SetCommonInfo();
49 3 : }
50 :
51 4 : CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Address &addrC, const Address &addrA, const Variable &varB)
52 4 : : insGenPtr(insGenPtr), subType(MinusSubType::ADDR_MINUS_VAR_TO_ADDR), varB(varB), addrA(addrA), addrC(addrC)
53 : {
54 4 : SetCommonInfo();
55 4 : }
56 :
57 2 : CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Address &addrC, const Address &addrA, const uint16_t immedB)
58 2 : : insGenPtr(insGenPtr), subType(MinusSubType::ADDR_MINUS_IMMED_TO_ADDR), addrA(addrA), addrC(addrC), immedB(immedB)
59 : {
60 2 : SetCommonInfo();
61 2 : }
62 :
63 3 : CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Address &addrA, const Variable &varB)
64 3 : : insGenPtr(insGenPtr), subType(MinusSubType::SELF_SUB_VAR_ADDRESS), varB(varB), addrA(addrA)
65 : {
66 3 : SetCommonInfo();
67 3 : }
68 :
69 3 : CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Address &addrA, const uint16_t immedB)
70 3 : : insGenPtr(insGenPtr), subType(MinusSubType::SELF_SUB_IMMED_ADDRESS), addrA(addrA), immedB(immedB)
71 : {
72 3 : SetCommonInfo();
73 3 : }
74 :
75 2 : CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Address &addrC, const Variable &varA, const uint16_t immedB)
76 2 : : insGenPtr(insGenPtr), subType(MinusSubType::VAR_MINUS_IMMED_TO_ADDR), varA(varA), addrC(addrC), immedB(immedB)
77 : {
78 2 : SetCommonInfo();
79 2 : }
80 3 : CcuRepSub::CcuRepSub(CcuInsGeneratorBase* insGenPtr, const Variable &varC, const Address &addrA, const uint16_t immedB)
81 3 : : insGenPtr(insGenPtr), subType(MinusSubType::ADDR_MINUS_IMMED_TO_VAR), varC(varC), addrA(addrA), immedB(immedB)
82 : {
83 3 : SetCommonInfo();
84 3 : }
85 :
86 8 : void CcuRepSub::ValidateInsGenPtrForSub()
87 : {
88 8 : CcuInsGeneratorV1* tmpPtrV1 = dynamic_cast<CcuInsGeneratorV1 *>(insGenPtr);
89 8 : CHK_PRT_THROW((tmpPtrV1 && !supportCcuV1),
90 : HCCL_ERROR("[CcuRepSub][%s]Cannot translate CcuRepSub for A5 when supportCcuV1 is false", __func__),
91 : Hccl::CcuApiException, "tmpPtrV1 does not match supportCcuV1");
92 8 : }
93 :
94 8 : bool CcuRepSub::Translate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
95 : {
96 8 : ValidateInsGenPtrForSub();
97 8 : Hccl::CHECK_NULLPTR(instr, "[CcuRepSub::Translate] instr is nullptr!");
98 8 : this->instrId = instrId;
99 8 : translated = true;
100 :
101 8 : insGenPtr->CcuRepSubTranslate(ccuKernel, instr, this);
102 :
103 8 : CHK_PRT_THROW((instrId > UINT16_MAX - instrCount),
104 : HCCL_ERROR("[CcuRepSub::Translate]uint16 integer overflow occurs, instrId = [%hu], instrCount = [%hu]", instrId, instrCount),
105 : Hccl::InternalException, "integer overflow");
106 8 : instrId += instrCount;
107 :
108 8 : return translated;
109 : }
110 :
111 14 : std::string CcuRepSub::Describe()
112 : {
113 14 : switch (subType) {
114 3 : case MinusSubType::VAR_MINUS_VAR_TO_VAR: {
115 3 : return Hccl::StringFormat("Variable[%u] = Variable[%u] - Variable[%u]", varC.Id(), varA.Id(), varB.Id());
116 : }
117 3 : case MinusSubType::VAR_MINUS_IMMED_TO_VAR: {
118 3 : return Hccl::StringFormat("Variable[%u] = Variable[%u] - Immed[%u]", varC.Id(), varA.Id(), immedB);
119 : }
120 1 : case MinusSubType::SELF_SUB_VAR_VARIABLE: {
121 1 : return Hccl::StringFormat("Variable[%u] -= Variable[%u]", varA.Id(), varB.Id());
122 : }
123 1 : case MinusSubType::SELF_SUB_IMMED_VARIABLE: {
124 1 : return Hccl::StringFormat("Variable[%u] -= Immed[%u]", varA.Id(), immedB);
125 : }
126 1 : case MinusSubType::ADDR_MINUS_VAR_TO_ADDR: {
127 1 : return Hccl::StringFormat("address[%u] = address[%u] - Variable[%u]", addrC.Id(), addrA.Id(), varB.Id());
128 : }
129 1 : case MinusSubType::ADDR_MINUS_IMMED_TO_ADDR: {
130 1 : return Hccl::StringFormat("address[%u] = address[%u] - Immed[%u]", addrC.Id(), addrA.Id(), immedB);
131 : }
132 1 : case MinusSubType::SELF_SUB_VAR_ADDRESS: {
133 1 : return Hccl::StringFormat("address[%u] -= Variable[%u]", addrA.Id(), varB.Id());
134 : }
135 1 : case MinusSubType::SELF_SUB_IMMED_ADDRESS: {
136 1 : return Hccl::StringFormat("address[%u] -= Immed[%u]", addrA.Id(), immedB);
137 : }
138 1 : case MinusSubType::VAR_MINUS_IMMED_TO_ADDR: {
139 1 : return Hccl::StringFormat("address[%u] = Variable[%u] - Immed[%u]", addrC.Id(), varA.Id(), immedB);
140 : }
141 1 : case MinusSubType::ADDR_MINUS_IMMED_TO_VAR: {
142 1 : return Hccl::StringFormat("Variable[%u] = address[%u] - Immed[%u]", varC.Id(), addrA.Id(), immedB);
143 : }
144 0 : default: {
145 0 : return Hccl::StringFormat("Invalid Sub");
146 : }
147 : }
148 : }
149 :
150 5 : Address CcuRepSub::GetAddrA()
151 : {
152 5 : return addrA;
153 : }
154 :
155 0 : Address CcuRepSub::GetAddrB()
156 : {
157 0 : return addrB;
158 : }
159 :
160 2 : Address CcuRepSub::GetAddrC()
161 : {
162 2 : return addrC;
163 : }
164 :
165 6 : Variable CcuRepSub::GetVarA()
166 : {
167 6 : return varA;
168 : }
169 :
170 7 : Variable CcuRepSub::GetVarB()
171 : {
172 7 : return varB;
173 : }
174 :
175 7 : Variable CcuRepSub::GetVarC()
176 : {
177 7 : return varC;
178 : }
179 :
180 4 : uint16_t CcuRepSub::GetImmedB()
181 : {
182 4 : return immedB;
183 : }
184 :
185 18 : MinusSubType CcuRepSub::GetSubType()
186 : {
187 18 : return subType;
188 : }
189 : }; // namespace CcuRep
190 : }; // namespace hcomm
|