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_generater_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 33 : void CcuRepMul::SetCommonInfo()
21 : {
22 33 : type = CcuRepType::MUL;
23 33 : instrCount = 1;
24 33 : }
25 :
26 5 : CcuRepMul::CcuRepMul(CcuInsGeneraterBase* insGenPtr, const Variable &varC, const Variable &varA, const Variable &varB)
27 5 : : insGenPtr(insGenPtr), subType(MulSubType::VAR_MUL_VAR_TO_VAR), varA(varA), varB(varB), varC(varC)
28 : {
29 5 : SetCommonInfo();
30 5 : }
31 :
32 3 : CcuRepMul::CcuRepMul(CcuInsGeneraterBase* insGenPtr, const Variable &varC, const Variable &varA, uint16_t immedB)
33 3 : : insGenPtr(insGenPtr), subType(MulSubType::VAR_MUL_IMMED_TO_VAR), varA(varA), varC(varC), immedB(immedB)
34 : {
35 3 : SetCommonInfo();
36 3 : }
37 :
38 3 : CcuRepMul::CcuRepMul(CcuInsGeneraterBase* insGenPtr, const Variable &varA, const Variable &varB)
39 3 : : insGenPtr(insGenPtr), subType(MulSubType::SELF_MUL_VAR_VARIABLE), varA(varA), varB(varB)
40 : {
41 3 : SetCommonInfo();
42 3 : }
43 :
44 3 : CcuRepMul::CcuRepMul(CcuInsGeneraterBase* insGenPtr, const Variable &varA, uint16_t immedB)
45 3 : : insGenPtr(insGenPtr), subType(MulSubType::SELF_MUL_IMMED_VARIABLE), varA(varA), immedB(immedB)
46 : {
47 3 : SetCommonInfo();
48 3 : }
49 :
50 3 : CcuRepMul::CcuRepMul(CcuInsGeneraterBase* insGenPtr, const Address &addrC, const Variable &varA, const Variable &varB)
51 3 : : insGenPtr(insGenPtr), subType(MulSubType::VAR_MUL_VAR_TO_ADDR), varA(varA), varB(varB), addrC(addrC)
52 : {
53 3 : SetCommonInfo();
54 3 : }
55 :
56 3 : CcuRepMul::CcuRepMul(CcuInsGeneraterBase* insGenPtr, const Address &addrC, const Variable &varA, const Address &addrB)
57 3 : : insGenPtr(insGenPtr), subType(MulSubType::VAR_MUL_ADDR_TO_ADDR), varA(varA), addrB(addrB), addrC(addrC)
58 : {
59 3 : SetCommonInfo();
60 3 : }
61 :
62 3 : CcuRepMul::CcuRepMul(CcuInsGeneraterBase* insGenPtr, const Address &addrA, const Variable &varB)
63 3 : : insGenPtr(insGenPtr), subType(MulSubType::SELF_MUL_VAR_ADDRESS), varB(varB), addrA(addrA)
64 : {
65 3 : SetCommonInfo();
66 3 : }
67 :
68 3 : CcuRepMul::CcuRepMul(CcuInsGeneraterBase* insGenPtr, const Address &addrC, const Address &addrA, const uint16_t immedB)
69 3 : : insGenPtr(insGenPtr), subType(MulSubType::ADDR_MUL_IMMED_TO_ADDR), addrA(addrA), addrC(addrC), immedB(immedB)
70 : {
71 3 : SetCommonInfo();
72 3 : }
73 :
74 2 : CcuRepMul::CcuRepMul(CcuInsGeneraterBase* insGenPtr, const Address &addrC, const Variable &varA, const uint16_t immedB)
75 2 : : insGenPtr(insGenPtr), subType(MulSubType::VAR_MUL_IMMED_TO_ADDR), varA(varA), addrC(addrC), immedB(immedB)
76 : {
77 2 : SetCommonInfo();
78 2 : }
79 :
80 2 : CcuRepMul::CcuRepMul(CcuInsGeneraterBase* insGenPtr, const Address &addrA, const uint16_t immedB)
81 2 : : insGenPtr(insGenPtr), subType(MulSubType::SELF_MUL_IMMED_ADDRESS), addrA(addrA), immedB(immedB)
82 : {
83 2 : SetCommonInfo();
84 2 : }
85 :
86 3 : CcuRepMul::CcuRepMul(CcuInsGeneraterBase* insGenPtr, const Variable &varC, const Address &addrA, const uint16_t immedB)
87 3 : : insGenPtr(insGenPtr), subType(MulSubType::ADDR_MUL_IMMED_TO_VAR), varC(varC), addrA(addrA), immedB(immedB)
88 : {
89 3 : SetCommonInfo();
90 3 : }
91 :
92 4 : void CcuRepMul::ValidateInsGenPtrForMul()
93 : {
94 4 : CcuInsGeneraterV1* tmpPtrV1 = dynamic_cast<CcuInsGeneraterV1 *>(insGenPtr);
95 12 : CHK_PRT_THROW((tmpPtrV1 && !supportCcuV1),
96 : HCCL_ERROR("[CcuRepMul][%s]Cannot translate CcuRepMul for A5 when supportCcuV1 is false", __func__),
97 : Hccl::CcuApiException, "tmpPtrV1 does not match supportCcuV1");
98 0 : }
99 :
100 4 : bool CcuRepMul::Translate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &instrId, const TransDep &dep)
101 : {
102 4 : ValidateInsGenPtrForMul();
103 0 : Hccl::CHECK_NULLPTR(instr, "[CcuRepMul::Translate] instr is nullptr!");
104 0 : this->instrId = instrId;
105 0 : translated = true;
106 :
107 0 : insGenPtr->CcuRepMulTranslate(ccuKernel, instr, this);
108 :
109 0 : CHK_PRT_THROW((instrId > UINT16_MAX - instrCount),
110 : HCCL_ERROR("[CcuRepMul::Translate]uint16 integer overflow occurs, instrId = [%hu], instrCount = [%hu]", instrId, instrCount),
111 : Hccl::InternalException, "integer overflow");
112 0 : instrId += instrCount;
113 :
114 0 : return translated;
115 : }
116 :
117 11 : std::string CcuRepMul::Describe()
118 : {
119 11 : switch (subType) {
120 1 : case MulSubType::VAR_MUL_VAR_TO_VAR: {
121 1 : return Hccl::StringFormat("Variable[%u] = Variable[%u] * Variable[%u]", varC.Id(), varA.Id(), varB.Id());
122 : }
123 1 : case MulSubType::VAR_MUL_IMMED_TO_VAR: {
124 1 : return Hccl::StringFormat("Variable[%u] = Variable[%u] * Immed[%u]", varC.Id(), varA.Id(), immedB);
125 : }
126 1 : case MulSubType::SELF_MUL_VAR_VARIABLE: {
127 1 : return Hccl::StringFormat("Variable[%u] *= Variable[%u]", varA.Id(), varB.Id());
128 : }
129 1 : case MulSubType::SELF_MUL_IMMED_VARIABLE: {
130 1 : return Hccl::StringFormat("Variable[%u] *= Immed[%u]", varA.Id(), immedB);
131 : }
132 1 : case MulSubType::VAR_MUL_VAR_TO_ADDR: {
133 1 : return Hccl::StringFormat("Address[%u] = Variable[%u] * Variable[%u]", addrC.Id(), varA.Id(), varB.Id());
134 : }
135 1 : case MulSubType::VAR_MUL_ADDR_TO_ADDR: {
136 1 : return Hccl::StringFormat("Address[%u] = Variable[%u] * Address[%u]", addrC.Id(), varA.Id(), addrB.Id());
137 : }
138 1 : case MulSubType::VAR_MUL_IMMED_TO_ADDR: {
139 1 : return Hccl::StringFormat("address[%u] = Variable[%u] * Immed[%u]", addrC.Id(), varA.Id(), immedB);
140 : }
141 1 : case MulSubType::ADDR_MUL_IMMED_TO_ADDR: {
142 1 : return Hccl::StringFormat("address[%u] = address[%u] * Immed[%u]", addrC.Id(), addrA.Id(), immedB);
143 : }
144 1 : case MulSubType::SELF_MUL_VAR_ADDRESS: {
145 1 : return Hccl::StringFormat("address[%u] *= Variable[%u]", addrA.Id(), varB.Id());
146 : }
147 1 : case MulSubType::SELF_MUL_IMMED_ADDRESS: {
148 1 : return Hccl::StringFormat("address[%u] *= Immed[%u]", addrA.Id(), immedB);
149 : }
150 1 : case MulSubType::ADDR_MUL_IMMED_TO_VAR: {
151 1 : return Hccl::StringFormat("Variable[%u] = address[%u] * Immed[%u]", varC.Id(), addrA.Id(), immedB);
152 : }
153 0 : default: {
154 0 : return Hccl::StringFormat("Invalid Mul");
155 : }
156 : }
157 : }
158 :
159 1 : Address CcuRepMul::GetAddrA()
160 : {
161 1 : return addrA;
162 : }
163 :
164 1 : Address CcuRepMul::GetAddrB()
165 : {
166 1 : return addrB;
167 : }
168 :
169 2 : Address CcuRepMul::GetAddrC()
170 : {
171 2 : return addrC;
172 : }
173 :
174 2 : Variable CcuRepMul::GetVarA()
175 : {
176 2 : return varA;
177 : }
178 :
179 1 : Variable CcuRepMul::GetVarB()
180 : {
181 1 : return varB;
182 : }
183 :
184 1 : Variable CcuRepMul::GetVarC()
185 : {
186 1 : return varC;
187 : }
188 :
189 1 : uint16_t CcuRepMul::GetImmedB()
190 : {
191 1 : return immedB;
192 : }
193 :
194 11 : MulSubType CcuRepMul::GetSubType()
195 : {
196 11 : return subType;
197 : }
198 : }; // namespace CcuRep
199 : }; // namespace hcomm
|