Line data Source code
1 : /**
2 : * Copyright (c) 2025 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 HCOMM_CCU_KERNEL_H
12 : #define HCOMM_CCU_KERNEL_H
13 :
14 : #include <cstdint>
15 : #include <functional>
16 : #include <array>
17 : #include <memory>
18 : #include <string>
19 : #include <vector>
20 : #include <unordered_map>
21 : #include <unordered_set>
22 :
23 : #include "ccu_task_arg_v1.h"
24 : #include "ccu_task_param_v1.h"
25 :
26 : #include "ccu_kernel_resource.h"
27 : #include "ccu_instr_info_v1.h"
28 : #include "ccu_rep_context_v1.h"
29 :
30 : #include "ccu_funccall_v1.h"
31 : #include "ccu_loopcall_v1.h"
32 :
33 : #include "hccl_types.h"
34 : #include "hcomm_primitives.h"
35 : #include "ccu_datatype_v1.h"
36 : #include "ccu_interface_assist_v1.h"
37 :
38 : #include "ccu_res_repo.h"
39 : #include "ccu_dev_mgr_imp.h" // CcuVersion
40 :
41 : // 暂时引用方便算法开发
42 : #include "ccu_repeat_v1.h"
43 : #include "ccu_condition_v1.h"
44 : #include "ccu_rep_funcblock_v1.h"
45 : #include "ccu_loopblock_v1.h"
46 : #include "ccu_loopcall_v1.h"
47 : #include "ccu_assist_pub.h"
48 :
49 : #ifndef CCU_PROFILING // 和hccl仓兼容性使用
50 : #define CCU_PROFILING
51 : #endif
52 : #include "ccu_types.h"
53 :
54 : namespace hcomm {
55 :
56 : struct GroupInfo {
57 : uint16_t loopParamId;
58 : uint16_t parallelParamId;
59 : uint16_t residualId;
60 : };
61 :
62 : struct GroupOpConfig {
63 : uint32_t msInterleave;
64 : uint32_t loopCount;
65 : uint64_t memSlice;
66 : };
67 :
68 : // Kernel 查询信息结构体,由 CcuKernel::GetCcuKernelInfo 在锁内填充。
69 : // 后续新增查询字段时只需在此结构体中添加,无需新增接口。
70 : struct CcuKernelInfo {
71 : uint32_t maxTaskArgsNum{0}; // taskArgs数组所需的元素个数,注册期间由LoadArg的argId推算: max(argId)+1,空则为0
72 : };
73 :
74 : class CcuKernel : public CcuRep::CcuRepContext {
75 : public:
76 3735 : CcuKernel() = default;
77 : ~CcuKernel() override;
78 :
79 : HcclResult SetupProfilingInfo(const char* kernelFuncName);
80 : HcclResult ApplyDieFromChannels();
81 : HcclResult ValidateAndApplyDie(uint32_t targetDieId);
82 :
83 : CcuResReq GetResourceRequest();
84 : CcuResRepository& GetResRepository();
85 : CcuRepResource& GetResource();
86 : CcuSharedResource& GetExportedRes();
87 : CcuSharedResource& GetImportedRes();
88 :
89 : void SetResRepository(const CcuResRepository& resRepo);
90 : void SetInstrId(uint32_t instrId);
91 : uint32_t GetInstrId() const;
92 : uint32_t GetInstrCount();
93 : void SetCcuInstrInfo(const CcuRep::CcuInstrInfo& instrInfo);
94 :
95 : CcuResult GeneTaskParams(const uint64_t* taskArgs, uint32_t argsNum, std::vector<CcuTaskParam>& taskParams);
96 :
97 : void SetInsGenerater(CcuRep::CcuInsGeneratorBase* insGeneratorBase);
98 70 : void SetCcuVersion(CcuVersion version) { ccuVersion_ = version; }
99 : // 该友元函数用于在context类外创建Variable并被context内的资源管理器管理
100 : friend CcuRep::Variable CcuRep::CreateVariable(CcuRep::CcuRepContext* context);
101 :
102 : HcclResult AddProfilingInfo(
103 : const ChannelHandle* channels, uint32_t channelNum, HcclDataType dataType, HcclDataType outputDataType,
104 : HcclReduceOp opType, const std::string& opName);
105 :
106 : HcclResult AddCcuProfiling(
107 : GroupInfo groupInfo, const std::vector<ChannelHandle> channelHandle, HcclDataType dataType,
108 : HcclDataType outputDataType, HcclReduceOp opType, const std::string& opName);
109 : HcclResult AddCcuProfiling(
110 : const ChannelHandle* channels, uint32_t channelNum, HcclDataType dataType, HcclDataType outputDataType,
111 : HcclReduceOp opType, const std::string& opName);
112 : HcclResult
113 : GetCcuProfilingInfo(const uint64_t* taskArgs, uint32_t argSize, std::vector<CcuProfilingInfo>& allCcuProfilingInfo);
114 :
115 : const std::vector<CcuProfilingInfo>& GetAllCcuProfilingInfo() { return allCcuProfilingInfos_; };
116 :
117 : // process const values
118 147 : std::unordered_map<uint64_t, CcuRep::Variable>& GetConstValue2VarMap() { return constValue2VarMap; }
119 : HcclResult Add2ConstValue2VarMap(std::vector<uint64_t>& values);
120 :
121 8 : const std::unordered_set<ChannelHandle>& GetChannels() { return channels_; }
122 :
123 : public:
124 : // Alloc 相关接口
125 : CcuResult VariableAlloc(CcuVariableHandle* varHandle);
126 : CcuResult AddressAlloc(CcuAddressHandle* addrHandle);
127 : CcuResult EventAlloc(CcuEventHandle* eventHandle);
128 : CcuResult BufferAlloc(CcuBufferHandle* bufHandle);
129 : CcuResult
130 : LocalAddrAlloc(CcuLocalAddrHandle* localAddrHandle, CcuAddressHandle* addrHandle, CcuVariableHandle* tokenHandle);
131 : CcuResult RemoteAddrAlloc(
132 : CcuRemoteAddrHandle* remoteAddrHandle, CcuAddressHandle* addrHandle, CcuVariableHandle* tokenHandle);
133 : CcuResult BlockVariableAlloc(CcuVariableHandle* varHandles, uint32_t count);
134 : CcuResult BlockEventAlloc(CcuEventHandle* eventHandles, uint32_t count);
135 : CcuResult BlockBufferAlloc(CcuBufferHandle* bufHandles, uint32_t count);
136 : CcuResult VariableCreateByChannel(ChannelHandle channel, uint32_t varIndex, CcuVariableHandle* varHandle);
137 : CcuResult VariableCreateByAcquire(CcuVariableHandle acqHandle, uint32_t index, CcuVariableHandle* varHandle);
138 : CcuResult EventCreateByAcquire(CcuEventHandle acqHandle, uint32_t index, CcuEventHandle* eventHandle);
139 :
140 : // 参数加载类 相关接口
141 : CcuResult LoadArg(CcuVariableHandle varHandle, uint32_t argId);
142 : // 在锁内填充 Kernel 查询信息;当前包含 maxTaskArgsNum。
143 : CcuResult GetCcuKernelInfo(CcuKernelInfo& info) const;
144 : CcuResult LoadVar(uint64_t addr, CcuVariableHandle varHandle, uint32_t num);
145 : CcuResult CcuLoadVarFromVarAddr(CcuVariableHandle addrHandle, CcuVariableHandle varHandle, uint32_t num);
146 : CcuResult StoreVar(uint64_t addr, CcuVariableHandle varHandle, uint32_t num);
147 : CcuResult CcuStoreVarToVarAddr(CcuVariableHandle addrHandle, CcuVariableHandle varHandle, uint32_t num);
148 :
149 : // Event信号同步类 相关接口
150 : // mask 由调用方独立传入(与 Event 句柄解耦),不再设独立的 SetEventMask 接口。
151 : CcuResult EventRecord(CcuEventHandle eventHandle, uint32_t mask);
152 : CcuResult EventWait(CcuEventHandle eventHandle, uint32_t mask);
153 : CcuResult NotifyRecord(const ChannelHandle channel, uint32_t remoteNotifyIdx, uint32_t mask);
154 : CcuResult NotifyWait(const ChannelHandle channel, uint32_t localNotifyIdx, uint32_t mask);
155 : CcuResult WriteVariableWithNotify(
156 : const ChannelHandle channel, CcuVariableHandle varHandle, uint32_t remoteVarIdx, uint32_t remoteNotifyIdx,
157 : uint32_t mask);
158 : // 本地(同 device 内跨 core)通知同步:用 notifyTag 字符串作为对端标识,
159 : // 由调用方约定生产者/消费者使用相同的 tag 字符串完成配对。
160 : // 与 NotifyRecord/Wait(用 ChannelHandle 标识跨 rank 通道)的对偶。
161 : // 必须 public:C API ccu_primitives_impl.cc 直接调用。
162 : CcuResult LocalNotifyRecord(const char* notifyTag, const uint32_t mask);
163 : CcuResult LocalNotifyWait(const char* notifyTag, const uint32_t mask);
164 : // 本地数据拷贝 相关接口
165 : CcuResult LocalCopyMemToBuffer(
166 : CcuBufferHandle dstHandle, CcuLocalAddrHandle srcHandle, CcuVariableHandle lenHandle,
167 : CcuEventHandle eventHandle, uint32_t mask);
168 : CcuResult LocalCopyBufferToMem(
169 : CcuLocalAddrHandle dstHandle, CcuBufferHandle srcHandle, CcuVariableHandle lenHandle,
170 : CcuEventHandle eventHandle, uint32_t mask);
171 : CcuResult LocalCopyMemToMem(
172 : CcuLocalAddrHandle dstHandle, CcuLocalAddrHandle srcHandle, CcuVariableHandle lenHandle,
173 : CcuEventHandle eventHandle, uint32_t mask);
174 :
175 : // 本地reduce 相关接口
176 : CcuResult LocalMemReduce(
177 : CcuLocalAddrHandle dstHandle, CcuLocalAddrHandle srcHandle, CcuVariableHandle lenHandle, HcclDataType dataType,
178 : HcclReduceOp opType, CcuEventHandle eventHandle, uint32_t mask);
179 : CcuResult LocalBufferReduce(
180 : CcuBufferHandle* bufHandles, uint32_t count, HcclDataType dataType, HcclDataType outputDataType,
181 : HcclReduceOp opType, CcuVariableHandle lenHandle, CcuEventHandle eventHandle, uint32_t mask);
182 :
183 : // 运算重载 相关接口
184 : CcuResult VariableAssignImm(CcuVariableHandle var, uint64_t immediate);
185 : CcuResult VariableAssignVar(CcuVariableHandle var, CcuVariableHandle varA);
186 : CcuResult
187 : VariableAddVarToVar(CcuVariableHandle varHandle, CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
188 : CcuResult
189 : VariableSubVarToVar(CcuVariableHandle varHandle, CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
190 : CcuResult
191 : VariableMulVarToVar(CcuVariableHandle varHandle, CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
192 : CcuResult VariableAddImmToVar(CcuVariableHandle varHandle, CcuVariableHandle varAHandle, uint16_t immediate);
193 : CcuResult VariableSubImmToVar(CcuVariableHandle varHandle, CcuVariableHandle varAHandle, uint16_t immediate);
194 : CcuResult VariableMulImmToVar(CcuVariableHandle varHandle, CcuVariableHandle varAHandle, uint16_t immediate);
195 : CcuResult
196 : VariableAndVarToVar(CcuVariableHandle varHandle, CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
197 : CcuResult
198 : VariableOrVarToVar(CcuVariableHandle varHandle, CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
199 : CcuResult
200 : VariableXorVarToVar(CcuVariableHandle varHandle, CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
201 : CcuResult VariableNotVar(CcuVariableHandle varHandle, CcuVariableHandle varAHandle);
202 : CcuResult
203 : VariableShlVarToVar(CcuVariableHandle varHandle, CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
204 : CcuResult
205 : VariableShrVarToVar(CcuVariableHandle varHandle, CcuVariableHandle varAHandle, CcuVariableHandle varBHandle);
206 : CcuResult AddressAssignImm(CcuAddressHandle addr, uint64_t immediate);
207 : CcuResult AddressAssignVar(CcuAddressHandle addrHandle, CcuVariableHandle varHandle);
208 : CcuResult AddressAssignAddr(CcuAddressHandle dstAddrHandle, CcuAddressHandle srcAddrHandle);
209 : CcuResult AddressAddVarToAddr(CcuAddressHandle resAddr, CcuAddressHandle lhsAddr, CcuVariableHandle rhsVar);
210 : CcuResult
211 : AddressAddAddrToAddr(CcuAddressHandle resAddrHandle, CcuAddressHandle addrAHandle, CcuAddressHandle addrBHandle);
212 : CcuResult AddressAddAssignVar(CcuAddressHandle addr, CcuVariableHandle var);
213 : CcuResult AddressAddAssignAddr(CcuAddressHandle addrHandle, CcuAddressHandle otherHandle);
214 : CcuResult AddressAddImmToAddr(CcuAddressHandle resAddr, CcuAddressHandle addrA, uint16_t imm);
215 :
216 : // 远端数据传输操作
217 :
218 : CcuResult ReadMemToMem(
219 : ChannelHandle channel, CcuLocalAddrHandle localHandle, CcuRemoteAddrHandle remoteHandle,
220 : CcuVariableHandle lenHandle, CcuEventHandle eventHandle, uint32_t mask);
221 : CcuResult ReadMemToBuffer(
222 : ChannelHandle channel, CcuBufferHandle localHandle, CcuRemoteAddrHandle remoteHandle,
223 : CcuVariableHandle lenHandle, CcuEventHandle eventHandle, uint32_t mask);
224 : CcuResult ReadMemToMemReduce(
225 : ChannelHandle channel, CcuLocalAddrHandle localHandle, CcuRemoteAddrHandle remoteHandle,
226 : CcuVariableHandle lenHandle, HcclDataType dataType, HcclReduceOp opType, CcuEventHandle eventHandle,
227 : uint32_t mask);
228 : CcuResult WriteMemToMem(
229 : ChannelHandle channel, CcuRemoteAddrHandle remoteHandle, CcuLocalAddrHandle localHandle,
230 : CcuVariableHandle lenHandle, CcuEventHandle eventHandle, uint32_t mask);
231 : CcuResult WriteBufferToMem(
232 : ChannelHandle channel, CcuRemoteAddrHandle remoteHandle, CcuBufferHandle localHandle,
233 : CcuVariableHandle lenHandle, CcuEventHandle eventHandle, uint32_t mask);
234 : CcuResult WriteMemToMemReduce(
235 : ChannelHandle channel, CcuRemoteAddrHandle remoteHandle, CcuLocalAddrHandle localHandle,
236 : CcuVariableHandle lenHandle, HcclDataType dataType, HcclReduceOp opType, CcuEventHandle eventHandle,
237 : uint32_t mask);
238 :
239 : CcuResult IfBegin(CcuVariableHandle varHandle, uint64_t immediate, CcuConditionType condType, const char* label);
240 : CcuResult
241 : IfBeginVar(CcuVariableHandle lhsHandle, CcuVariableHandle rhsHandle, CcuConditionType condType, const char* label);
242 : CcuResult IfElse(const char* label);
243 : CcuResult IfEnd(const char* label);
244 :
245 : void IfLabelStackPush(const char* label);
246 : void IfLabelStackMarkBodyDone();
247 : const char* IfLabelStackPopForElse();
248 : bool IfLabelStackTopIsClosable();
249 : const char* IfLabelStackPop();
250 :
251 : void DoWhileLabelStackPush(const char* label);
252 : const char* DoWhileLabelStackPopForWhile();
253 :
254 : void FlushClosablePendingIfs();
255 : void Append(std::shared_ptr<CcuRep::CcuRepBase> rep) override;
256 : CcuResult WhileBegin(CcuVariableHandle varHandle, uint64_t immediate, CcuConditionType condType, const char* label);
257 : CcuResult WhileBeginVar(
258 : CcuVariableHandle lhsHandle, CcuVariableHandle rhsHandle, CcuConditionType condType, const char* label);
259 : CcuResult WhileEnd(const char* label);
260 : CcuResult DoWhileBegin(const char* label);
261 : CcuResult DoWhileEnd(CcuVariableHandle varHandle, uint64_t immediate, CcuConditionType condType, const char* label);
262 : CcuResult DoWhileEndVar(
263 : CcuVariableHandle lhsHandle, CcuVariableHandle rhsHandle, CcuConditionType condType, const char* label);
264 :
265 : CcuResult LoopCreate(CcuLoop* loop);
266 : CcuResult LoopBodyEnter(CcuLoop loop);
267 : CcuResult LoopBodyExit(CcuLoop loop);
268 : // 建组/加环统一以版本化 cfg 为准;旧 config 的转换在 C ABI 适配层完成。
269 : CcuResult LoopGroupCreate(CcuLoopGroup* group, uint32_t maxLoopNum, const CcuLoopGroupCfg* cfg);
270 : CcuResult LoopGroupCreateFromVar(
271 : CcuLoopGroup* group, uint32_t maxLoopNum, CcuVariableHandle parallelVarHandle,
272 : CcuVariableHandle offsetVarHandle);
273 : CcuResult LoopGroupCreateFromVarV2(
274 : CcuLoopGroup* group, uint32_t maxLoopNum, CcuVariableHandle parallelVarV2, CcuVariableHandle offsetVarV2,
275 : CcuVariableHandle varOffsetVar);
276 : CcuResult LoopGroupAddLoop(CcuLoopGroup group, CcuLoop loop, const CcuLoopCfg* cfg);
277 : CcuResult LoopGroupAddLoopFromVar(CcuLoopGroup group, CcuLoop loop, CcuVariableHandle loopParamVar);
278 : CcuResult LoopGroupAddLoopFromVarV2(
279 : CcuLoopGroup group, CcuLoop loop, CcuVariableHandle iterNumVar, CcuVariableHandle addrOffsetVar,
280 : CcuVariableHandle ctxIdVar);
281 :
282 : CcuResult FuncBlockLookup(const void* funcPtr, uint64_t* outHandle);
283 : CcuResult FuncBlockBegin(const void* funcPtr, uint64_t* outHandle);
284 : CcuResult FuncBlockEnd(uint64_t handle);
285 : CcuResult FuncDefineInArg(uint64_t handle, CcuVariableHandle formal);
286 : CcuResult FuncCall(uint64_t handle, const CcuVariableHandle* inArgs, uint32_t numIn);
287 :
288 : private:
289 : CcuResult GetVariableByHandle(CcuVariableHandle varHandle, CcuRep::Variable** variable);
290 : CcuResult GetEventByHandle(CcuEventHandle eventHandle, CcuRep::CompletedEvent** event);
291 : CcuResult LatchBodyError(CcuResult err);
292 : // 按需扩容 res_.blockExecutor[0]:不足 maxLoopNum 时补足,足够则不动;
293 : // 由 LoopGroupCreate / LoopGroupCreateFromVar 在 LoopGroup 创建时调用。
294 : CcuResult EnsureLoopEnginePool(uint32_t maxLoopNum);
295 :
296 : CcuResult ValidateTaskArgs(const uint64_t* taskArgs, uint32_t argsNum) const;
297 : void FillTaskParam(
298 : CcuTaskParam& param, uint32_t index, uint32_t seqNum, const uint64_t* taskArgs, uint32_t argsNum) const;
299 :
300 : CcuResult ResolveBufRemoteLenEvent(
301 : CcuBufferHandle bufHandle, CcuRemoteAddrHandle remoteHandle, CcuVariableHandle lenHandle,
302 : CcuEventHandle eventHandle, CcuRep::CcuBuf** buf, CcuRep::RemoteAddr** remote, CcuRep::Variable** len,
303 : CcuRep::CompletedEvent** event);
304 :
305 : CcuResult ResolveLocalRemoteLenEvent(
306 : CcuLocalAddrHandle localHandle, CcuRemoteAddrHandle remoteHandle, CcuVariableHandle lenHandle,
307 : CcuEventHandle eventHandle, CcuRep::LocalAddr** local, CcuRep::RemoteAddr** remote, CcuRep::Variable** len,
308 : CcuRep::CompletedEvent** event);
309 :
310 : CcuResult ResolveRemoteLocalLenEvent(
311 : CcuRemoteAddrHandle remoteHandle, CcuLocalAddrHandle localHandle, CcuVariableHandle lenHandle,
312 : CcuEventHandle eventHandle, CcuRep::RemoteAddr** remote, CcuRep::LocalAddr** local, CcuRep::Variable** len,
313 : CcuRep::CompletedEvent** event);
314 :
315 : // 校验从 varHandle 起的 num 个 Variable 句柄对应的内部变量 Id 连续递增,
316 : // 用于 LoadVar/StoreVar 等接口对“连续变量块”的前置校验。
317 : CcuResult CheckContinuousVariables(
318 : CcuVariableHandle varHandle, uint32_t num, const CcuRep::Variable& baseVar, const char* tag);
319 :
320 : // GetCcuProfilingInfo 的子步骤:处理 sqe & waitcke 类型的 profiling 信息,结果直接 push 到 allCcuProfilingInfos_
321 : // 中。
322 : HcclResult CollectSqeAndWaitCkeProfilingInfo();
323 : // GetCcuProfilingInfo 的子步骤:根据 LoopGroup 的 profiling 缓存构建
324 : // varId -> argIndex 与 varId -> varId 两个查找表,供 LoopGroup 段查询入参使用。
325 : HcclResult BuildLoopGroupVarIdMaps(
326 : std::unordered_map<uint16_t, uint32_t>& varId2ArgIndexMap,
327 : std::unordered_map<uint16_t, uint16_t>& varId2VarIdMap);
328 : // GetCcuProfilingInfo 的子步骤:处理 LoopGroup 的 profiling 信息,结果 push 到 allCcuProfilingInfos_ 中。
329 : HcclResult CollectLoopGroupProfilingInfo(
330 : const uint64_t* taskArgs, uint32_t argSize, const std::unordered_map<uint16_t, uint32_t>& varId2ArgIndexMap,
331 : const std::unordered_map<uint16_t, uint16_t>& varId2VarIdMap);
332 :
333 : CcuRep::Variable CreateJumpTargetVar();
334 :
335 : struct IfLabelEntry {
336 : const char* label{nullptr};
337 : bool bodyDone{false};
338 : };
339 : struct DoWhileLabelEntry {
340 : const char* label{nullptr};
341 : std::shared_ptr<CcuRep::CcuRepBlock> snapshotBlock{nullptr};
342 : size_t snapshotRepCount{0};
343 : };
344 : std::vector<IfLabelEntry> iflabelStack_;
345 : std::vector<DoWhileLabelEntry> doWhileLabelStack_;
346 : bool isFlushing_ = false;
347 :
348 : struct PendingIfContext {
349 : std::shared_ptr<CcuRep::CcuRepJumpLabel> elseLabel;
350 : std::shared_ptr<CcuRep::CcuRepJumpLabel> endLabel;
351 : bool hasElse{false};
352 : };
353 :
354 : struct PendingWhileContext {
355 : std::shared_ptr<CcuRep::CcuRepJumpLabel> beginLabel;
356 : std::shared_ptr<CcuRep::CcuRepJumpLabel> endLabel;
357 : CcuVariableHandle varHandle;
358 : uint64_t immediate;
359 : CcuConditionType condType;
360 : };
361 :
362 : struct PendingDoWhileContext {
363 : std::shared_ptr<CcuRep::CcuRepJumpLabel> beginLabel;
364 : };
365 :
366 : std::unordered_map<CcuVariableHandle, CcuRep::Variable> ccuVarMap_{};
367 :
368 : std::unordered_map<std::string, PendingIfContext> pendingIfCtx_{};
369 : std::unordered_map<std::string, PendingWhileContext> pendingWhileCtx_{};
370 : std::unordered_map<std::string, PendingDoWhileContext> pendingDoWhileCtx_{};
371 :
372 : std::unordered_map<CcuEventHandle, CcuRep::CompletedEvent> ccuEventMap_{};
373 :
374 : CcuResult GetBufferByHandle(CcuBufferHandle bufferHandle, CcuRep::CcuBuf** buffer);
375 : std::unordered_map<CcuBufferHandle, CcuRep::CcuBuf> ccuBufferMap_{};
376 :
377 : CcuResult GetAddressByHandle(CcuAddressHandle addrHandle, CcuRep::Address** address);
378 : std::unordered_map<CcuAddressHandle, CcuRep::Address> ccuAddrMap_{};
379 :
380 : CcuResult GetLocalAddrByHandle(CcuLocalAddrHandle handle, CcuRep::LocalAddr** localAddr);
381 : std::unordered_map<CcuLocalAddrHandle, CcuRep::LocalAddr> ccuLocalAddrMap_{};
382 :
383 : CcuResult GetRemoteAddrByHandle(CcuRemoteAddrHandle handle, CcuRep::RemoteAddr** remoteAddr);
384 : std::unordered_map<CcuRemoteAddrHandle, CcuRep::RemoteAddr> ccuRemoteAddrMap_{};
385 :
386 : std::unordered_set<uint32_t> loadArgUsedSet_{};
387 :
388 : protected:
389 : // 使用channel中的Variable
390 : HcclResult CreateVariable(const ChannelHandle channel, uint32_t varIndex, CcuRep::Variable* var);
391 : CcuRep::Variable CreateVariable();
392 : CcuRep::Variable CreateExpectVar();
393 : CcuRep::Variable CreateContinuousVariable();
394 : CcuRep::LocalAddr CreateLocalAddr();
395 : CcuRep::RemoteAddr CreateRemoteAddr();
396 : CcuRep::RemoteAddr GetRemoteAddr(const ChannelHandle channel, const uint32_t index);
397 : CcuRep::LocalNotify CreateLocalNotify();
398 : CcuRep::CompletedEvent CreateCompletedEvent();
399 : CcuRep::CcuBuf CreateCcuBuf();
400 : CcuRep::Executor CreateExecutor();
401 :
402 : HcclResult CreateBlockCcuBuf(const uint32_t count, CcuRep::CcuBuf* ccuBufs);
403 : HcclResult CreateBlockExecutor(const uint32_t count, CcuRep::Executor* ccuExes);
404 : HcclResult CreateBlockCompletedEvent(const uint32_t count, CcuRep::CompletedEvent* ccuEvents);
405 :
406 : // 内部 *Nb / RecordEvent / WaitEvent 系列:mask 由调用方独立传入,
407 : // 不再从 CompletedEvent 上读取。
408 : HcclResult RecordEvent(CcuRep::CompletedEvent event, uint32_t mask);
409 : HcclResult WaitEvent(CcuRep::CompletedEvent event, uint32_t mask);
410 :
411 : // 数据操作
412 : HcclResult WriteNb(
413 : const ChannelHandle channel, const CcuRep::RemoteAddr& rem, const CcuRep::LocalAddr& loc,
414 : const CcuRep::Variable& len, CcuRep::CompletedEvent event, uint32_t mask);
415 : HcclResult WriteNb(
416 : const ChannelHandle channel, const CcuRep::RemoteAddr& rem, const CcuRep::CcuBuf& loc,
417 : const CcuRep::Variable& len, CcuRep::CompletedEvent event, uint32_t mask);
418 :
419 : HcclResult ReadNb(
420 : const ChannelHandle channel, const CcuRep::LocalAddr& loc, const CcuRep::RemoteAddr& rem,
421 : const CcuRep::Variable& len, CcuRep::CompletedEvent event, uint32_t mask);
422 : HcclResult ReadNb(
423 : const ChannelHandle channel, const CcuRep::CcuBuf& loc, const CcuRep::RemoteAddr& rem,
424 : const CcuRep::Variable& len, CcuRep::CompletedEvent event, uint32_t mask);
425 :
426 : HcclResult WriteReduceNb(
427 : const ChannelHandle channel, const CcuRep::RemoteAddr& rem, const CcuRep::LocalAddr& loc,
428 : const CcuRep::Variable& len, HcclDataType dataType, HcclReduceOp opType, CcuRep::CompletedEvent event,
429 : uint32_t mask);
430 : HcclResult ReadReduceNb(
431 : const ChannelHandle channel, const CcuRep::LocalAddr& loc, const CcuRep::RemoteAddr& rem,
432 : const CcuRep::Variable& len, HcclDataType dataType, HcclReduceOp opType, CcuRep::CompletedEvent event,
433 : uint32_t mask);
434 :
435 : HcclResult LocalCopyNb(
436 : const CcuRep::LocalAddr& dst, const CcuRep::LocalAddr& src, const CcuRep::Variable& len,
437 : CcuRep::CompletedEvent event, uint32_t mask); // dst和src是否都是local
438 : HcclResult LocalCopyNb(
439 : const CcuRep::CcuBuf& dst, const CcuRep::LocalAddr& src, const CcuRep::Variable& len,
440 : CcuRep::CompletedEvent event, uint32_t mask);
441 : HcclResult LocalCopyNb(
442 : const CcuRep::LocalAddr& dst, const CcuRep::CcuBuf& src, const CcuRep::Variable& len,
443 : CcuRep::CompletedEvent event, uint32_t mask);
444 :
445 : HcclResult LocalReduceNb(
446 : const CcuRep::LocalAddr& dst, const CcuRep::LocalAddr& src, const CcuRep::Variable& len, HcclDataType dataType,
447 : HcclReduceOp opType, CcuRep::CompletedEvent event, uint32_t mask);
448 : HcclResult LocalReduceNb(
449 : const CcuRep::CcuBuf* bufs, uint32_t count, HcclDataType dataType, HcclDataType outputDataType,
450 : HcclReduceOp opType, const CcuRep::Variable& len, CcuRep::CompletedEvent event, uint32_t mask);
451 :
452 : // 参数操作
453 : void Load(const CcuRep::Variable& var);
454 :
455 : // Variable src中存放内存地址,从地址中加载数据到Variable var中
456 : void LoadVariable(const CcuRep::Variable& src, const CcuRep::Variable& var);
457 :
458 : void StoreVariable(const CcuRep::Variable& var, uint64_t addr);
459 : // 控制逻辑
460 : // 宏定义IF、WHILE
461 : CcuRep::FuncCall Func(const std::string& label);
462 : CcuRep::FuncCall Func(const CcuRep::Variable& funcAddr);
463 : CcuRep::LoopCall Loop(const std::string& label);
464 :
465 : private:
466 : CcuRep::Address CreateAddress();
467 : CcuRep::LocalAddr CreateLocalAddr(const CcuRep::Variable& token);
468 :
469 : protected:
470 : GroupOpConfig moConfig_{0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFFFFFFFFFF};
471 :
472 : private:
473 : template <typename T>
474 : T CreateResAssist(std::array<std::vector<T>, CCU_MAX_IODIE_NUM>& resRecord);
475 : template <typename T>
476 : std::vector<T> CreateBlockResAssist(const uint32_t count, std::array<std::vector<T>, CCU_MAX_IODIE_NUM>& resRecord);
477 :
478 : private:
479 : CcuRepResource res_{};
480 : CcuResRepository resRepo_{};
481 :
482 : std::unordered_set<ChannelHandle> channels_{};
483 :
484 : CcuRep::CcuInstrInfo instrInfo_{};
485 :
486 : uint32_t loadArgIndex_{0};
487 :
488 : CcuVersion ccuVersion_{CcuVersion::CCU_INVALID};
489 :
490 : // A5(CCU_V1) 专用:CCU_IF,CCU_ELSE 跳转共用一个 Xn;当前仅CCU_IF和CCU_ELSE复用,While和DoWhile暂未复用
491 : // A5 上 CCU_IF和ELSE的targetVar 仅用于LoadImdToXn + 紧接 Jump,生命周期不跨越其他指令,可安全复用。
492 : // A6不复用,保持每次独立分配的语义。
493 : std::unique_ptr<CcuRep::Variable> sharedJumpTargetVar_;
494 :
495 : CcuSharedResource exportedRes_{};
496 : CcuSharedResource importedRes_{};
497 : std::vector<GroupInfo> groupOpSizeInfo_;
498 : std::vector<CcuProfilingInfo> allCcuProfilingInfos_;
499 :
500 : // 记录每个kernel所需常量,适用于A6场景
501 : std::unordered_map<uint64_t, CcuRep::Variable> constValue2VarMap;
502 :
503 : struct LoopDescriptor {
504 : std::string label;
505 : std::shared_ptr<CcuRep::CcuRepLoopBlock> repLoopBlock;
506 : std::shared_ptr<CcuRep::CcuRepBlock> prevActiveBlock;
507 : bool bodyDefined{false};
508 : };
509 :
510 : struct VersionV2LoopRecord {
511 : CcuRep::Variable iterNumVar;
512 : CcuRep::Variable addrOffsetVar;
513 : CcuRep::Variable ctxIdVar;
514 : };
515 :
516 : struct LoopGroupDescriptor {
517 : CcuLoopGroupCfg config;
518 : uint64_t totalLoopNum{0};
519 : uint32_t loopCount{0};
520 : CcuRep::Variable parallelVar;
521 : CcuRep::Variable offsetVar;
522 : CcuRep::Variable xnOffsetVar;
523 : std::shared_ptr<CcuRep::CcuRepBase> bundleRep;
524 : bool isVarBased{false};
525 : bool isVersionV2{false};
526 : std::vector<VersionV2LoopRecord> versionV2Loops;
527 : };
528 :
529 : struct FuncDescriptor {
530 : const void* funcPtr{nullptr};
531 : std::string label;
532 : std::shared_ptr<CcuRep::CcuRepFuncBlock> repFuncBlock;
533 : std::shared_ptr<CcuRep::CcuRepBlock> prevActiveBlock;
534 : bool bodyDefined{false};
535 : };
536 :
537 : std::unordered_map<CcuLoop, LoopDescriptor> loopMap_;
538 : std::unordered_map<CcuLoopGroup, LoopGroupDescriptor> loopGroupMap_;
539 : CcuResult LookupLoopGroupAndLoop(
540 : CcuLoopGroup group, CcuLoop loop, const char* fnName, const char* createFnName, LoopGroupDescriptor*& grpDesc,
541 : LoopDescriptor*& loopDesc, uint32_t& loopIdx);
542 : uint32_t loopHandleCounter_{0};
543 : uint32_t loopGroupHandleCounter_{0};
544 : uint32_t loopBodyDepth_{0};
545 :
546 : std::unordered_map<uint64_t, FuncDescriptor> funcMap_;
547 : std::unordered_map<const void*, uint64_t> funcInstanceMap_;
548 : uint64_t funcHandleCounter_{0};
549 : bool inFuncBody_{false};
550 : // loop/func body 内首个非法错误的粘性闩,退出 body 时上抛(void body 无法直接回传)。
551 : CcuResult bodyError_{CcuResult::CCU_SUCCESS};
552 :
553 : std::unordered_map<CcuLoopExecutors, std::vector<CcuRep::Executor>> loopEnginePools_;
554 : uint32_t loopEnginePoolCounter_{0};
555 :
556 : std::string name_{};
557 : };
558 :
559 : } // namespace hcomm
560 :
561 : #endif // HCOMM_CCU_KERNEL_H
|