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 : #ifndef CCU_INS_GENERATOR_BASE
12 : #define CCU_INS_GENERATOR_BASE
13 :
14 : #include "ccu_rep_base_v1.h"
15 : #include "ccu_datatype_v1.h"
16 : #include "ccu_microcode_v1.h"
17 : #include "ccu_rep_v1.h"
18 : #include "ccu_kernel.h"
19 : #include "ccu_log.h"
20 :
21 : namespace hcomm {
22 : namespace CcuRep {
23 :
24 : class CcuInsGeneratorBase {
25 : public:
26 374 : CcuInsGeneratorBase() {}
27 :
28 : // 虚析构函数,确保派生类对象正确析构
29 374 : virtual ~CcuInsGeneratorBase() = default;
30 : // data
31 : virtual HcclResult CcuRepBufLocReadTranslate(
32 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufLocRead* repBufLocRead, const TransDep& dep)
33 : = 0;
34 : virtual HcclResult CcuRepBufLocWriteTranslate(
35 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufLocWrite* repBufLocWrite, const TransDep& dep)
36 : = 0;
37 : virtual HcclResult
38 : CcuRepBufReadTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufRead* repBufRead, const TransDep& dep)
39 : = 0;
40 : virtual HcclResult
41 : CcuRepBufReduceTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufReduce* ccuRepBufReduce)
42 : = 0;
43 : virtual HcclResult CcuRepBufWriteTranslate(
44 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepBufWrite* ccuRepBufWrite, const TransDep& dep)
45 : = 0;
46 : virtual HcclResult
47 : CcuRepLocCpyTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepLocCpy* ccuRepLocCpy, const TransDep& dep)
48 : = 0;
49 : virtual HcclResult CcuRepReadTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRead* repRemMem) = 0;
50 : virtual HcclResult CcuRepRemMemTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRemMem* repRemMem) = 0;
51 : virtual HcclResult CcuRepWriteTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepWrite* repWrite) = 0;
52 :
53 : // sync
54 : virtual HcclResult CcuRepLocRecordEventTranslate(
55 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepLocRecordEvent* ccuRepLocRecordEvent)
56 : = 0;
57 : virtual HcclResult
58 : CcuRepLocWaitEventTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepLocWaitEvent* ccuRepLocWaitEvent)
59 : = 0;
60 : virtual HcclResult
61 : CcuRepLocWaitNotifyTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepLocWaitNotify* ccuRepLocWaitNotify)
62 : = 0;
63 : virtual HcclResult CcuRepRecordSharedNotifyTranslate(
64 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRecordSharedNotify* ccuRepRecordSharedNotify,
65 : const TransDep& dep)
66 : = 0;
67 : virtual HcclResult
68 : CcuRepRemWaitSemTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRemWaitSem* cuRepRemWaitSem)
69 : = 0;
70 : virtual HcclResult
71 : CcuRepRemPostVarTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRemPostVar* ccuRepRemPostVar)
72 : = 0;
73 : virtual HcclResult CcuRepRemPostSemTranslate(
74 : CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepRemPostSem* ccuRepRemPostSem, const TransDep& dep)
75 : = 0;
76 : // logical
77 : virtual HcclResult
78 : CcuRepAndTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepAnd* ccuRepAnd, const TransDep& dep)
79 : = 0;
80 : virtual HcclResult
81 : CcuRepNotTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepNot* ccuRepNot, const TransDep& dep)
82 : = 0;
83 : virtual HcclResult
84 : CcuRepOrTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepOr* ccuRepOr, const TransDep& dep)
85 : = 0;
86 : virtual HcclResult
87 : CcuRepXorTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepXor* ccuRepXor, const TransDep& dep)
88 : = 0;
89 :
90 : // shift
91 : virtual HcclResult
92 : CcuRepShLTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepShL* ccuRepShL, const TransDep& dep)
93 : = 0;
94 : virtual HcclResult
95 : CcuRepShRTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepShR* ccuRepShR, const TransDep& dep)
96 : = 0;
97 :
98 : // arithmetic
99 : virtual HcclResult
100 : CcuRepAddTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepAdd* ccuRepAdd, const TransDep& dep)
101 : = 0;
102 : ;
103 : virtual HcclResult
104 : CcuRepAssignTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepAssign* ccuRepAssign, const TransDep& dep)
105 : = 0;
106 : virtual HcclResult CcuRepMulTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepMul* ccuRepMul) = 0;
107 : virtual HcclResult CcuRepSubTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, CcuRepSub* ccuRepSub) = 0;
108 :
109 : // control
110 : virtual HcclResult CcuRepFuncBlockTranslate(
111 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepFuncBlock* funcBlockPtr,
112 : const TransDep& dep, uint32_t step)
113 : = 0;
114 : virtual HcclResult CcuRepFuncCallTranslate(
115 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepFuncCall* funcCallPtr,
116 : const TransDep& dep)
117 : = 0;
118 : virtual HcclResult CcuRepJumpTranslate(
119 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJump* jumpPtr, const TransDep& dep)
120 : = 0;
121 : virtual HcclResult CcuRepJumpNETranslate(
122 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpNE* jumpNEPtr, const TransDep& dep)
123 : = 0;
124 : virtual HcclResult CcuRepJumpEQTranslate(
125 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpEQ* jumpEQPtr, const TransDep& dep)
126 : = 0;
127 : virtual HcclResult CcuRepJumpLETranslate(
128 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpLE* jumpLEPtr, const TransDep& dep)
129 : = 0;
130 : virtual HcclResult CcuRepJumpGETranslate(
131 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpGE* jumpGEPtr, const TransDep& dep)
132 : = 0;
133 : virtual HcclResult CcuRepJumpGTTranslate(
134 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpGT* jumpGTPtr, const TransDep& dep)
135 : = 0;
136 : virtual HcclResult CcuRepJumpLTTranslate(
137 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepJumpLT* jumpLTPtr, const TransDep& dep)
138 : = 0;
139 :
140 : // loop
141 : virtual HcclResult
142 : CcuRepLoopTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoop* loopPtr)
143 : = 0;
144 : virtual HcclResult CcuRepLoopCallTranslate(
145 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoopCall* loopCallPtr,
146 : const TransDep& dep)
147 : = 0;
148 : virtual HcclResult
149 : CcuRepSetLoopTranslate(CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepSetLoop* setLoopPtr)
150 : = 0;
151 : virtual HcclResult CcuRepLoopGroupBundleTranslate(
152 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoopGroupBundle* bundlePtr,
153 : const TransDep& dep)
154 : = 0;
155 : virtual uint16_t CcuRepLoopGroupBundleInstrCount(CcuRepLoopGroupBundle* bundlePtr) = 0;
156 :
157 : // common
158 : virtual HcclResult CcuRepLoadTranslate(
159 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoad* loadPtr, const TransDep& dep)
160 : = 0;
161 : virtual HcclResult CcuRepLoadVarTranslate(
162 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoadVar* loadVarPtr,
163 : const TransDep& dep)
164 : = 0;
165 : virtual HcclResult CcuRepLoadArgTranslate(
166 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoadArg* loadArgPtr,
167 : const TransDep& dep)
168 : = 0;
169 : virtual HcclResult CcuRepNopTranslate(
170 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepNop* nopPtr, const TransDep& dep)
171 : = 0;
172 : virtual HcclResult CcuRepStoreTranslate(
173 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepStore* storePtr, const TransDep& dep)
174 : = 0;
175 : virtual HcclResult CcuRepStoreVarTranslate(
176 : CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& curInstrId, CcuRepStoreVar* storeVarPtr,
177 : const TransDep& dep)
178 : = 0;
179 :
180 : virtual uint32_t GetInstrCount(CcuRepType repType) = 0;
181 :
182 825 : virtual HcclResult PrepareConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel)
183 : {
184 : // A5使用基类空实现;A6需要根据repType做不同处理
185 : (void)repPtr;
186 : (void)dep;
187 : (void)ccuKernel;
188 825 : return HcclResult::HCCL_SUCCESS;
189 : }
190 :
191 : protected:
192 : struct FuncCallContext {
193 : uint32_t inArgCount{0};
194 : CcuRepReferenceManager* funcManager{nullptr};
195 : std::shared_ptr<CcuRepFuncBlock> funcBlock;
196 : CcuInstr* instr{nullptr};
197 : std::vector<Variable> formalIns;
198 : };
199 :
200 5 : HcclResult PrepareFuncCallContext(CcuRepFuncCall* funcCallPtr, FuncCallContext& ctx)
201 : {
202 5 : CHK_PTR_NULL(funcCallPtr);
203 5 : ctx.inArgCount = funcCallPtr->GetInArgCount();
204 5 : ctx.funcManager = funcCallPtr->GetFuncManager();
205 5 : CHK_PTR_NULL(ctx.funcManager);
206 5 : ctx.funcBlock = funcCallPtr->GetFuncBlock();
207 5 : CHK_PTR_NULL(ctx.funcBlock);
208 5 : ctx.instr = funcCallPtr->GetInstr();
209 5 : CHK_PTR_NULL(ctx.instr);
210 5 : ctx.formalIns = ctx.funcBlock->GetInArgVars();
211 5 : if (static_cast<uint32_t>(ctx.formalIns.size()) != ctx.inArgCount) {
212 0 : HCCL_ERROR(
213 : "FuncCall arg count mismatch: caller = %u, callee formal = %u", ctx.inArgCount,
214 : static_cast<uint32_t>(ctx.formalIns.size()));
215 0 : return HCCL_E_PARA;
216 : }
217 5 : return HcclResult::HCCL_SUCCESS;
218 : }
219 : };
220 : } // namespace CcuRep
221 : } // namespace hcomm
222 :
223 : #endif
|