Line data Source code
1 : /*
2 : * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
3 : * Description: ccu representation implementation file
4 : * Author: sunzhepeng
5 : * Create: 2024-06-17
6 : */
7 :
8 : #include "ccu_rep_v1.h"
9 : #include "ccu_ins_generator_v1.h"
10 : #include "string_util.h"
11 : #include "exception_util.h"
12 : #include "ccu_api_exception.h"
13 : #include "ccu_kernel.h"
14 :
15 : namespace hcomm {
16 : namespace CcuRep {
17 :
18 171 : void CcuRepAdd::SetCommonInfo()
19 : {
20 171 : type = CcuRepType::ADD;
21 171 : instrCount = insGenPtr->GetInstrCount(type);
22 171 : }
23 :
24 4 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Address &addrC, const Address &addrA, const Variable &varB)
25 4 : : insGenPtr(insGenPtr), subType(AddSubType::ADDR_PLUS_VAR_TO_ADDR), addrA(addrA), addrC(addrC), varB(varB)
26 : {
27 4 : SetCommonInfo();
28 4 : supportCcuV1 = true;
29 4 : }
30 :
31 6 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Address &addrC, const Address &addrA, const Address &addrB)
32 6 : : insGenPtr(insGenPtr), subType(AddSubType::ADDR_PLUS_ADDR_TO_ADDR), addrA(addrA), addrB(addrB), addrC(addrC)
33 : {
34 6 : SetCommonInfo();
35 6 : supportCcuV1 = true;
36 6 : }
37 :
38 103 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Variable &varC, const Variable &varA, const Variable &varB)
39 103 : : insGenPtr(insGenPtr), subType(AddSubType::VAR_PLUS_VAR_TO_VAR), varA(varA), varB(varB), varC(varC)
40 : {
41 103 : SetCommonInfo();
42 103 : supportCcuV1 = true;
43 103 : }
44 :
45 45 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Address &addrA, const Variable &offset)
46 45 : : insGenPtr(insGenPtr), subType(AddSubType::SELF_ADD_ADDRESS), addrA(addrA), varB(offset)
47 : {
48 45 : SetCommonInfo();
49 45 : supportCcuV1 = true;
50 45 : }
51 :
52 4 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Variable &varA, const Variable &offset)
53 4 : : insGenPtr(insGenPtr), subType(AddSubType::SELF_ADD_VARIABLE), varA(varA), varB(offset)
54 : {
55 4 : SetCommonInfo();
56 4 : supportCcuV1 = true;
57 4 : }
58 :
59 1 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Variable &varA, const uint16_t immedB)
60 1 : : insGenPtr(insGenPtr), subType(AddSubType::SELF_ADD_IMMED_VARIABLE), varA(varA), immedB(immedB)
61 : {
62 1 : SetCommonInfo();
63 1 : supportCcuV1 = false;
64 1 : }
65 :
66 1 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Variable &varC, const Variable &varA, const uint16_t immedB)
67 1 : : insGenPtr(insGenPtr), subType(AddSubType::VAR_PLUS_IMMED_TO_VAR), varA(varA), varC(varC), immedB(immedB)
68 : {
69 1 : SetCommonInfo();
70 1 : supportCcuV1 = false;
71 1 : }
72 :
73 3 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Address &addrC, const Address &addrA, const uint16_t immedB)
74 3 : : insGenPtr(insGenPtr), subType(AddSubType::ADDR_PLUS_IMMED_TO_ADDR), addrA(addrA), addrC(addrC), immedB(immedB)
75 : {
76 3 : SetCommonInfo();
77 3 : supportCcuV1 = false;
78 3 : }
79 :
80 0 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Address &addrA, const uint16_t immedB)
81 0 : : insGenPtr(insGenPtr), subType(AddSubType::SELF_ADD_IMMED_ADDRESS), addrA(addrA), immedB(immedB)
82 : {
83 0 : SetCommonInfo();
84 0 : supportCcuV1 = false;
85 0 : }
86 :
87 1 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Address &addrC, const Variable &varA, const Variable &varB)
88 1 : : insGenPtr(insGenPtr), subType(AddSubType::VAR_PLUS_VAR_TO_ADDR), addrC(addrC), varA(varA), varB(varB)
89 : {
90 1 : SetCommonInfo();
91 1 : supportCcuV1 = false;
92 1 : }
93 :
94 1 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Address &addrC, const Variable &varA, const uint16_t immedB)
95 1 : : insGenPtr(insGenPtr), subType(AddSubType::VAR_PLUS_IMMED_TO_ADDR), addrC(addrC), varA(varA), immedB(immedB)
96 : {
97 1 : SetCommonInfo();
98 1 : supportCcuV1 = false;
99 1 : }
100 :
101 1 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Variable &varC, const Address &addrA, const Address &addrB)
102 1 : : insGenPtr(insGenPtr), subType(AddSubType::ADDR_PLUS_ADDR_TO_VAR), addrA(addrA), addrB(addrB), varC(varC)
103 : {
104 1 : SetCommonInfo();
105 1 : supportCcuV1 = false;
106 1 : }
107 :
108 1 : CcuRepAdd::CcuRepAdd(CcuInsGeneratorBase* insGenPtr, const Variable &varC, const Address &addrA, const uint16_t immedB)
109 1 : : insGenPtr(insGenPtr), subType(AddSubType::ADDR_PLUS_IMMED_TO_VAR), addrA(addrA), varC(varC), immedB(immedB)
110 : {
111 1 : SetCommonInfo();
112 1 : supportCcuV1 = false;
113 1 : }
114 :
115 146 : void CcuRepAdd::ValidateInsGenPtrForAdd()
116 : {
117 146 : CcuInsGeneratorV1* tmpPtrV1 = dynamic_cast<CcuInsGeneratorV1 *>(insGenPtr);
118 146 : CHK_PRT_THROW((tmpPtrV1 && !supportCcuV1),
119 : HCCL_ERROR("[CcuRepAdd][%s]Cannot translate CcuRepAdd for A5 when supportCcuV1 is false", __func__),
120 : Hccl::CcuApiException, "tmpPtrV1 does not match supportCcuV1");
121 146 : }
122 :
123 146 : bool CcuRepAdd::Translate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
124 : {
125 146 : ValidateInsGenPtrForAdd();
126 146 : Hccl::CHECK_NULLPTR(instr, "[CcuRepAdd::Translate] instr is nullptr!");
127 146 : this->instrId = instrId;
128 146 : translated = true;
129 :
130 146 : insGenPtr->CcuRepAddTranslate(ccuKernel, instr, this, dep);
131 146 : CHK_PRT_THROW((instrId > UINT16_MAX - instrCount),
132 : HCCL_ERROR("[CcuRepAdd::Translate]uint16 integer overflow occurs, instrId = [%hu], instrCount = [%hu]", instrId, instrCount),
133 : Hccl::InternalException, "integer overflow");
134 146 : instrId += instrCount;
135 146 : return translated;
136 : }
137 :
138 136 : std::string CcuRepAdd::Describe()
139 : {
140 136 : switch (subType) {
141 1 : case AddSubType::ADDR_PLUS_VAR_TO_ADDR: {
142 1 : return Hccl::StringFormat("Address[%u] = Address[%u] + Variable[%u]", addrC.Id(), addrA.Id(), varB.Id());
143 : }
144 2 : case AddSubType::ADDR_PLUS_ADDR_TO_ADDR: {
145 2 : return Hccl::StringFormat("Address[%u] = Address[%u] + Address[%u]", addrC.Id(), addrA.Id(), addrB.Id());
146 : }
147 88 : case AddSubType::VAR_PLUS_VAR_TO_VAR: {
148 88 : return Hccl::StringFormat("Variable[%u] = Variable[%u] + Variable[%u]", varC.Id(), varA.Id(), varB.Id());
149 : }
150 42 : case AddSubType::SELF_ADD_ADDRESS: {
151 42 : return Hccl::StringFormat("Address[%u] += Variable[%u]", addrA.Id(), varB.Id());
152 : }
153 1 : case AddSubType::SELF_ADD_VARIABLE: {
154 1 : return Hccl::StringFormat("Variable[%u] += Variable[%u]", varA.Id(), varB.Id());
155 : }
156 0 : case AddSubType::SELF_ADD_IMMED_VARIABLE: {
157 0 : return Hccl::StringFormat("Variable[%u] += Immed[%u]", varA.Id(), immedB);
158 : }
159 1 : case AddSubType::VAR_PLUS_IMMED_TO_VAR: {
160 1 : return Hccl::StringFormat("Variable[%u] = Variable[%u] + Immed[%u]", varC.Id(), varA.Id(), immedB);
161 : }
162 1 : case AddSubType::ADDR_PLUS_IMMED_TO_ADDR: {
163 1 : return Hccl::StringFormat("Address[%u] += Address[%u] + Immed[%hu]", addrC.Id(), addrA.Id(), immedB);
164 : }
165 0 : case AddSubType::VAR_PLUS_VAR_TO_ADDR: {
166 0 : return Hccl::StringFormat("Address[%u] = Variable[%u] + Variable[%u]", addrC.Id(), varA.Id(), varB.Id());
167 : }
168 0 : case AddSubType::SELF_ADD_IMMED_ADDRESS: {
169 0 : return Hccl::StringFormat("Address[%u] += Immed[%u]", addrA.Id(), immedB);
170 : }
171 0 : case AddSubType::VAR_PLUS_IMMED_TO_ADDR: {
172 0 : return Hccl::StringFormat("Address[%u] += varA[%u] + Immed[%hu]", addrC.Id(), varA.Id(), immedB);
173 : }
174 0 : case AddSubType::ADDR_PLUS_IMMED_TO_VAR: {
175 0 : return Hccl::StringFormat("Variable[%u] = Address[%u] + Immed[%u]", varC.Id(), addrA.Id(), immedB);
176 : }
177 0 : default: {
178 0 : return Hccl::StringFormat("Invalid Add");
179 : }
180 : }
181 : }
182 :
183 76 : Address CcuRepAdd::GetAddrA()
184 : {
185 76 : return addrA;
186 : }
187 :
188 3 : Address CcuRepAdd::GetAddrB()
189 : {
190 3 : return addrB;
191 : }
192 :
193 7 : Address CcuRepAdd::GetAddrC()
194 : {
195 7 : return addrC;
196 : }
197 :
198 98 : Variable CcuRepAdd::GetVarA()
199 : {
200 98 : return varA;
201 : }
202 :
203 138 : Variable CcuRepAdd::GetVarB()
204 : {
205 138 : return varB;
206 : }
207 :
208 96 : Variable CcuRepAdd::GetVarC()
209 : {
210 96 : return varC;
211 : }
212 :
213 5 : uint16_t CcuRepAdd::GetImmedB()
214 : {
215 5 : return immedB;
216 : }
217 :
218 156 : AddSubType CcuRepAdd::GetSubType()
219 : {
220 156 : return subType;
221 : }
222 : }; // namespace CcuRep
223 : }; // namespace hcomm
|