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_rep_reference_manager_v1.h"
13 :
14 : #include "string_util.h"
15 : #include "exception_util.h"
16 : #include "ccu_api_exception.h"
17 :
18 : #include "ccu_ins_generator_base.h"
19 : #include "ccu_kernel.h"
20 :
21 : namespace hcomm {
22 : namespace CcuRep {
23 :
24 : using namespace Hccl;
25 :
26 16 : CcuRepFuncCall::CcuRepFuncCall(CcuInsGeneratorBase* insGenPtr, const std::string& label)
27 16 : : insGeneratorPtr_(insGenPtr),
28 16 : label(label)
29 : {
30 16 : type = CcuRepType::FUNC_CALL;
31 16 : instrCount = 0;
32 16 : }
33 :
34 2 : CcuRepFuncCall::CcuRepFuncCall(CcuInsGeneratorBase* insGenPtr, const Variable& funcAddrVar)
35 2 : : insGeneratorPtr_(insGenPtr),
36 2 : label(""),
37 4 : funcAddrVar(funcAddrVar)
38 : {
39 2 : type = CcuRepType::FUNC_CALL;
40 2 : }
41 :
42 6 : const std::string& CcuRepFuncCall::GetLabel() const { return label; }
43 :
44 6 : void CcuRepFuncCall::Reference(std::shared_ptr<CcuRepFuncBlock> refRep) { funcBlock = refRep; }
45 :
46 6 : void CcuRepFuncCall::SetFuncManager(CcuRepReferenceManager* funcManager) { this->funcManager = funcManager; }
47 :
48 9 : void CcuRepFuncCall::SetInArg(const Variable& var)
49 : {
50 9 : inArgCount++;
51 9 : inArgs.push_back(CcuRepArg(var));
52 9 : }
53 :
54 2 : void CcuRepFuncCall::SetOutArg(const Variable& var)
55 : {
56 2 : outArgCount++;
57 2 : if (outArgCount > FUNC_ARG_MAX) {
58 0 : Hccl::THROW<Hccl::CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_ARG_MAX);
59 : }
60 2 : outArgs.push_back(CcuRepArg(var));
61 2 : }
62 :
63 1 : void CcuRepFuncCall::SetInArg(const std::vector<Variable>& varList)
64 : {
65 1 : inArgCount += varList.size();
66 1 : inArgs.push_back(CcuRepArg(varList));
67 1 : }
68 :
69 1 : void CcuRepFuncCall::SetOutArg(const std::vector<Variable>& varList)
70 : {
71 1 : outArgCount += varList.size();
72 1 : if (outArgCount > FUNC_ARG_MAX) {
73 0 : Hccl::THROW<Hccl::CcuApiException>("CcuFunc Max ArgCount = %u", FUNC_ARG_MAX);
74 : }
75 1 : outArgs.push_back(CcuRepArg(varList));
76 1 : }
77 :
78 26 : uint16_t CcuRepFuncCall::InstrCount()
79 : {
80 52 : instrCount = inArgCount + outArgCount
81 26 : + insGeneratorPtr_->GetInstrCount(type); // funcCall除去入参和出参的处理外,需要额外4条指令
82 26 : return instrCount;
83 : }
84 :
85 5 : bool CcuRepFuncCall::Translate(CcuKernel* ccuKernel, CcuInstr*& instr, uint16_t& instrId, const TransDep& dep)
86 : {
87 5 : if (funcManager == nullptr) {
88 0 : Hccl::THROW<Hccl::CcuApiException>("funcManager is nullptr");
89 : }
90 : // 未实现, FuncCall和FuncBlock中的args个数校验
91 :
92 5 : if (this->instr == nullptr) {
93 5 : this->instrId = instrId;
94 5 : this->instr = instr;
95 5 : instr += InstrCount();
96 5 : instrId += InstrCount();
97 : }
98 :
99 5 : if (funcBlock != nullptr && !funcBlock->Translated()) {
100 0 : return translated;
101 : }
102 :
103 5 : translated = true;
104 :
105 5 : CHK_PRT_THROW(
106 : insGeneratorPtr_->CcuRepFuncCallTranslate(ccuKernel, instr, instrId, this, dep) != HcclResult::HCCL_SUCCESS,
107 : HCCL_ERROR("[CcuRepFuncCall][Translate] failed to translate for instrId[%u]", instrId),
108 : Hccl::CcuApiException, "CcuRepFuncCall translate failed");
109 :
110 5 : return translated;
111 : }
112 :
113 6 : std::string CcuRepFuncCall::Describe() { return Hccl::StringFormat("FuncCall[%s]", label.c_str()); }
114 :
115 5 : int32_t CcuRepFuncCall::GetCallLayer() { return funcBlock == nullptr ? FUNC_NEST_MAX : funcBlock->GetCallLayer(); }
116 :
117 : }; // namespace CcuRep
118 : }; // namespace hcomm
|