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 : #ifndef CCU_INS_GENERATOR_V2
11 : #define CCU_INS_GENERATOR_V2
12 :
13 : #include "ccu_ins_generator_base.h"
14 :
15 : namespace hcomm {
16 :
17 : namespace CcuRep {
18 :
19 : class CcuInsGeneratorV2 : public CcuInsGeneratorBase {
20 : public:
21 201 : CcuInsGeneratorV2() {}
22 :
23 : // 虚析构函数,确保派生类对象正确析构
24 67 : virtual ~CcuInsGeneratorV2() = default;
25 :
26 : // data
27 : HcclResult CcuRepBufLocReadTranslate(CcuKernel* ccuKernel,
28 : CcuInstr *&instr, CcuRepBufLocRead* repBufLocRead, const TransDep &dep) override;
29 : HcclResult CcuRepBufLocWriteTranslate(CcuKernel* ccuKernel,
30 : CcuInstr *&instr, CcuRepBufLocWrite* repBufLocWrite, const TransDep &dep) override;
31 : HcclResult CcuRepBufReadTranslate(CcuKernel* ccuKernel,
32 : CcuInstr *&instr, CcuRepBufRead* repBufRead, const TransDep &dep) override;
33 : HcclResult CcuRepBufReduceTranslate(CcuKernel* ccuKernel, CcuInstr *&instr,
34 : CcuRepBufReduce* ccuRepBufReduce) override;
35 : HcclResult CcuRepBufWriteTranslate(CcuKernel* ccuKernel,
36 : CcuInstr *&instr, CcuRepBufWrite* ccuRepBufWrite, const TransDep &dep) override;
37 : HcclResult CcuRepLocCpyTranslate(CcuKernel* ccuKernel,
38 : CcuInstr *&instr, CcuRepLocCpy* ccuRepLocCpy, const TransDep &dep) override;
39 : HcclResult CcuRepReadTranslate(CcuKernel* ccuKernel, CcuInstr *&instr, CcuRepRead* repRemMem) override;
40 : HcclResult CcuRepRemMemTranslate(CcuKernel* ccuKernel, CcuInstr *&instr, CcuRepRemMem* repRemMem) override;
41 : HcclResult CcuRepWriteTranslate(CcuKernel* ccuKernel, CcuInstr *&instr, CcuRepWrite* repWrite) override;
42 :
43 : // sync
44 : HcclResult CcuRepLocRecordEventTranslate(CcuKernel* ccuKernel, CcuInstr *&instr,
45 : CcuRepLocRecordEvent* ccuRepLocRecordEvent) override;
46 : HcclResult CcuRepLocWaitEventTranslate(CcuKernel* ccuKernel, CcuInstr *&instr,
47 : CcuRepLocWaitEvent* ccuRepLocWaitEvent) override;
48 : HcclResult CcuRepLocWaitNotifyTranslate(CcuKernel* ccuKernel, CcuInstr *&instr,
49 : CcuRepLocWaitNotify* ccuRepLocWaitNotify) override;
50 : HcclResult CcuRepRecordSharedNotifyTranslate(CcuKernel* ccuKernel,
51 : CcuInstr *&instr, CcuRepRecordSharedNotify* ccuRepRecordSharedNotify, const TransDep &dep) override;
52 : HcclResult CcuRepRemWaitSemTranslate(CcuKernel* ccuKernel, CcuInstr *&instr,
53 : CcuRepRemWaitSem* cuRepRemWaitSem) override;
54 : HcclResult CcuRepRemPostVarTranslate(CcuKernel* ccuKernel, CcuInstr *&instr,
55 : CcuRepRemPostVar* ccuRepRemPostVar) override;
56 : HcclResult CcuRepRemPostSemTranslate(CcuKernel* ccuKernel,
57 : CcuInstr *&instr, CcuRepRemPostSem* ccuRepRemPostSem, const TransDep &dep) override;
58 :
59 : // logical
60 : HcclResult CcuRepAndTranslate(CcuKernel* ccuKernel,
61 : CcuInstr *&instr, CcuRepAnd* ccuRepAnd, const TransDep &dep) override;
62 : HcclResult CcuRepNotTranslate(CcuKernel* ccuKernel,
63 : CcuInstr *&instr, CcuRepNot* ccuRepNot, const TransDep &dep) override;
64 : HcclResult CcuRepOrTranslate(CcuKernel* ccuKernel,
65 : CcuInstr *&instr, CcuRepOr* ccuRepOr, const TransDep &dep) override;
66 : HcclResult CcuRepXorTranslate(CcuKernel* ccuKernel,
67 : CcuInstr *&instr, CcuRepXor* ccuRepXor, const TransDep &dep) override;
68 :
69 : // shift
70 : HcclResult CcuRepShLTranslate(CcuKernel* ccuKernel,
71 : CcuInstr *&instr, CcuRepShL* ccuRepShL, const TransDep &dep) override;
72 : HcclResult CcuRepShRTranslate(CcuKernel* ccuKernel,
73 : CcuInstr *&instr, CcuRepShR* ccuRepShR, const TransDep &dep) override;
74 :
75 : // arithmetic
76 : HcclResult CcuRepAddTranslate(CcuKernel* ccuKernel,
77 : CcuInstr *&instr, CcuRepAdd* ccuRepAdd, const TransDep &dep) override;
78 : HcclResult CcuRepAssignTranslate(CcuKernel* ccuKernel,
79 : CcuInstr *&instr, CcuRepAssign* ccuRepAssign, const TransDep &dep) override;
80 : HcclResult CcuRepMulTranslate(CcuKernel* ccuKernel, CcuInstr *&instr, CcuRepMul* ccuRepMul) override;
81 : HcclResult CcuRepSubTranslate(CcuKernel* ccuKernel, CcuInstr *&instr, CcuRepSub* ccuRepSub) override;
82 :
83 : // control
84 : HcclResult CcuRepFuncBlockTranslate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &instrId, CcuRepFuncBlock* funcBlockPtr,
85 : const TransDep &dep, uint32_t step) override;
86 : HcclResult CcuRepFuncCallTranslate(CcuKernel* ccuKernel,
87 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepFuncCall* funcCallPtr, const TransDep &dep) override;
88 : HcclResult CcuRepJumpTranslate(CcuKernel* ccuKernel,
89 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepJump* jumpPtr, const TransDep &dep) override;
90 : HcclResult CcuRepJumpNETranslate(CcuKernel* ccuKernel,
91 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepJumpNE* jumpNEPtr, const TransDep &dep) override;
92 : HcclResult CcuRepJumpEQTranslate(CcuKernel* ccuKernel,
93 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepJumpEQ* jumpEQPtr, const TransDep &dep) override;
94 : HcclResult CcuRepJumpLETranslate(CcuKernel* ccuKernel,
95 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepJumpLE* jumpLEPtr, const TransDep &dep) override;
96 : HcclResult CcuRepJumpGETranslate(CcuKernel* ccuKernel,
97 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepJumpGE* jumpGEPtr, const TransDep &dep) override;
98 : HcclResult CcuRepJumpGTTranslate(CcuKernel* ccuKernel,
99 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepJumpGT* jumpGTPtr, const TransDep &dep) override;
100 : HcclResult CcuRepJumpLTTranslate(CcuKernel* ccuKernel,
101 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepJumpLT* jumpLTPtr, const TransDep &dep) override;
102 :
103 : // loop
104 : HcclResult CcuRepLoopTranslate(CcuKernel* ccuKernel, CcuInstr *&instr,
105 : uint16_t &curInstrId, CcuRepLoop* loopPtr) override;
106 : HcclResult CcuRepLoopCallTranslate(CcuKernel* ccuKernel,
107 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepLoopCall* loopCallPtr, const TransDep &dep) override;
108 : HcclResult CcuRepSetLoopTranslate(CcuKernel* ccuKernel, CcuInstr *&instr,
109 : uint16_t &curInstrId, CcuRepSetLoop* setLoopPtr) override;
110 : HcclResult CcuRepLoopGroupBundleTranslate(CcuKernel* ccuKernel, CcuInstr *&instr, uint16_t &curInstrId,
111 : CcuRepLoopGroupBundle* bundlePtr, const TransDep &dep) override;
112 : uint16_t CcuRepLoopGroupBundleInstrCount(CcuRepLoopGroupBundle* bundlePtr) override;
113 :
114 : // common
115 : HcclResult CcuRepLoadTranslate(CcuKernel* ccuKernel,
116 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepLoad* loadPtr, const TransDep &dep) override;
117 : HcclResult CcuRepLoadVarTranslate(CcuKernel* ccuKernel,
118 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepLoadVar* loadVarPtr, const TransDep &dep) override;
119 : HcclResult CcuRepLoadArgTranslate(CcuKernel* ccuKernel,
120 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepLoadArg* loadArgPtr, const TransDep &dep) override;
121 : HcclResult CcuRepNopTranslate(CcuKernel* ccuKernel,
122 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepNop* nopPtr, const TransDep &dep) override;
123 : HcclResult CcuRepStoreTranslate(CcuKernel* ccuKernel,
124 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepStore* storePtr, const TransDep &dep) override;
125 : HcclResult CcuRepStoreVarTranslate(CcuKernel* ccuKernel,
126 : CcuInstr *&instr, uint16_t &curInstrId, CcuRepStoreVar* storeVarPtr, const TransDep &dep) override;
127 :
128 : uint32_t GetInstrCount(CcuRepType repType) override;
129 :
130 : HcclResult PrepareConstValue(CcuRepBase* repPtr, const TransDep &dep, CcuKernel *ccuKernel) override;
131 :
132 : private:
133 : HcclResult CcuRepJumpTranslateV2Base(CcuInstr *&curInstr, uint16_t &curInstrId, CcuRepJumpBase* jumpBasePtr,
134 : uint64_t expected, const Variable &condition, const Variable &expectedVar,
135 : ConditionType condType);
136 :
137 : void LoadFuncCallInArgs(CcuInstr* instr, std::vector<CcuRepArg>& inArgs, std::vector<Variable>& formalIns,
138 : uint16_t reserveXnId);
139 : void LoadFuncCallOutArgs(CcuInstr* instr, uint32_t offset, std::vector<CcuRepArg>& outArgs,
140 : CcuRepReferenceManager* funcManager, uint16_t reserveXnId);
141 : HcclResult LoadLoopCallArg(CcuInstr*& instr, const CcuRepArg& inArg, const CcuRepArg& blkArg);
142 : HcclResult PrepareLoadConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel);
143 : HcclResult PrepareLoadVarConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel);
144 : HcclResult PrepareStoreConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel);
145 : HcclResult PrepareStoreVarConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel);
146 : HcclResult PrepareRemPostSemConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel);
147 : HcclResult PrepareRemPostVarConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel);
148 : HcclResult PrepareWriteConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel);
149 : HcclResult PrepareReadConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel);
150 : HcclResult PrepareBufWriteConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel);
151 : HcclResult PrepareBufReadConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel);
152 : HcclResult PrepareLocCpyConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel);
153 : HcclResult PrepareRecordSharedNotifyConstValue(CcuRepBase* repPtr, const TransDep& dep, CcuKernel* ccuKernel);
154 : HcclResult LoopConfigTranslate(CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoopGroupBundle* bundlePtr,
155 : const TransDep& dep);
156 : HcclResult LoopGroupConfigTranslate(CcuInstr*& instr, uint16_t& curInstrId, CcuRepLoopGroupBundle* bundlePtr,
157 : const TransDep& dep, bool isConfig, bool isCompat, uint16_t& loopGroupConfigId);
158 :
159 : static constexpr uint32_t V2_FUNC_BLOCK_INSTR_NUM = 11; // FuncBlock: RelJmp(9)+Jump(1)+Nop(1)
160 : static constexpr uint32_t V2_FUNC_CALL_INSTR_NUM = 13; // FuncCall: FuncBlock(11)+Call(2)
161 :
162 : std::unordered_map<CcuRepType, uint32_t> repTypeInstrCount = {
163 : {CcuRepType::READ, 1},
164 : {CcuRepType::WRITE, 1},
165 : {CcuRepType::REM_MEM, 2},
166 : {CcuRepType::BUF_READ, 1},
167 : {CcuRepType::LOCAL_CPY, 1},
168 : {CcuRepType::LOCAL_REDUCE, 1},
169 : {CcuRepType::BUF_WRITE, 1},
170 : {CcuRepType::BUF_REDUCE, 1},
171 : {CcuRepType::BUF_LOC_READ, 1},
172 : {CcuRepType::BUF_LOC_WRITE, 1},
173 :
174 : {CcuRepType::ASSIGN, 1},
175 : {CcuRepType::ADD, 1},
176 : {CcuRepType::MUL, 1},
177 : {CcuRepType::SUB, 1},
178 :
179 : {CcuRepType::LOC_RECORD_EVENT, 1},
180 : {CcuRepType::LOC_WAIT_EVENT, 1},
181 : {CcuRepType::LOC_WAIT_NOTIFY, 1},
182 : {CcuRepType::RECORD_SHARED_NOTIFY, 1},
183 : {CcuRepType::REM_POST_SEM, 1},
184 : {CcuRepType::REM_POST_VAR, 1},
185 : {CcuRepType::REM_WAIT_SEM, 1},
186 :
187 : {CcuRepType::FUNC_BLOCK, V2_FUNC_BLOCK_INSTR_NUM},
188 : {CcuRepType::FUNC_CALL, V2_FUNC_CALL_INSTR_NUM},
189 : {CcuRepType::JUMP, 2},
190 : {CcuRepType::JUMP_NE, 3},
191 : {CcuRepType::JUMP_EQ, 3},
192 : {CcuRepType::LOOP, 1},
193 : {CcuRepType::LOOPGROUP, 1},
194 : {CcuRepType::SET_LOOP, 1},
195 :
196 : {CcuRepType::LOAD, 3},
197 : {CcuRepType::LOAD_VAR, 3},
198 : {CcuRepType::LOAD_ARG, 1},
199 : {CcuRepType::STORE, 3},
200 : {CcuRepType::STORE_VAR, 3},
201 :
202 : {CcuRepType::WRITE_WITH_ARRIVE_NOTIFY, 1},
203 : {CcuRepType::CLEAR_ALL_ARRIVE_NOTIFY, 1},
204 : {CcuRepType::RECORD_EXPECT_COUNT, 1},
205 : {CcuRepType::WAIT_ALL_PEERS_ARRIVE_NOTIFY, 1},
206 :
207 : {CcuRepType::AND, 1},
208 : {CcuRepType::NOT, 1},
209 : {CcuRepType::OR, 1},
210 : {CcuRepType::XOR, 1},
211 : {CcuRepType::SHL, 1},
212 : {CcuRepType::SHR, 1},
213 :
214 : {CcuRepType::NOP, 1}
215 : };
216 : };
217 :
218 : uint32_t GetRelativeInstrId(uint32_t currentInstrId, uint32_t targetInstrId);
219 :
220 : }
221 : }
222 :
223 : #endif
|