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