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 : #include "ccu_rep.h"
12 :
13 : #include "string_util.h"
14 : #include "exception_util.h"
15 : #include "ccu_api_exception.h"
16 :
17 : namespace Hccl {
18 : namespace CcuRep {
19 :
20 182 : CcuRepAssign::CcuRepAssign(const Variable& varA, uint64_t immediate)
21 182 : : subType(AssignSubType::IMD_TO_VARIABLE),
22 182 : immediate(immediate),
23 182 : varA(varA)
24 : {
25 182 : type = CcuRepType::ASSIGN;
26 182 : instrCount = 1;
27 182 : }
28 :
29 1 : CcuRepAssign::CcuRepAssign(const Address& addrA, uint64_t immediate)
30 1 : : subType(AssignSubType::IMD_TO_ADDR),
31 1 : immediate(immediate),
32 1 : addrA(addrA)
33 : {
34 1 : type = CcuRepType::ASSIGN;
35 1 : instrCount = 1;
36 1 : }
37 :
38 153 : CcuRepAssign::CcuRepAssign(const Address& addrA, const Variable& varA)
39 153 : : subType(AssignSubType::VAR_TO_ADDR),
40 153 : immediate(0),
41 153 : varA(varA),
42 153 : addrA(addrA)
43 : {
44 153 : type = CcuRepType::ASSIGN;
45 153 : instrCount = 1;
46 153 : }
47 :
48 8 : CcuRepAssign::CcuRepAssign(const Address& addrB, const Address& addrA)
49 8 : : subType(AssignSubType::ADDR_TO_ADDR),
50 8 : immediate(0),
51 8 : addrA(addrA),
52 16 : addrB(addrB)
53 : {
54 8 : type = CcuRepType::ASSIGN;
55 8 : instrCount = 1;
56 8 : }
57 :
58 231 : CcuRepAssign::CcuRepAssign(const Variable& varB, const Variable& varA)
59 231 : : subType(AssignSubType::VAR_TO_VAR),
60 231 : immediate(0),
61 231 : varA(varA),
62 231 : varB(varB)
63 : {
64 231 : type = CcuRepType::ASSIGN;
65 231 : instrCount = 1;
66 231 : }
67 :
68 152 : bool CcuRepAssign::Translate(CcuInstr*& instr, uint16_t& instrId, const TransDep& dep)
69 : {
70 152 : CHECK_NULLPTR(instr, "[CcuRepAssign::Translate] instr is nullptr!");
71 152 : this->instrId = instrId;
72 152 : translated = true;
73 :
74 152 : switch (subType) {
75 67 : case AssignSubType::IMD_TO_VARIABLE: {
76 67 : LoadImdToXnInstr(instr++, varA.Id(), immediate);
77 67 : break;
78 : }
79 1 : case AssignSubType::IMD_TO_ADDR: {
80 1 : LoadImdToGSAInstr(instr++, addrA.Id(), immediate);
81 1 : break;
82 : }
83 27 : case AssignSubType::VAR_TO_ADDR: {
84 27 : LoadGSAXnInstr(instr++, addrA.Id(), dep.reserveGsaId, varA.Id());
85 27 : break;
86 : }
87 8 : case AssignSubType::ADDR_TO_ADDR: {
88 8 : LoadGSAGSAInstr(instr++, addrB.Id(), addrA.Id(), dep.reserveGsaId);
89 8 : break;
90 : }
91 49 : case AssignSubType::VAR_TO_VAR: {
92 49 : LoadXXInstr(instr++, varB.Id(), varA.Id(), dep.reserveXnId);
93 49 : break;
94 : }
95 0 : default: {
96 0 : THROW<CcuApiException>("Invalid Assign");
97 : }
98 : }
99 :
100 152 : CHK_PRT_THROW(
101 : (instrId > UINT16_MAX - instrCount),
102 : HCCL_ERROR(
103 : "[CcuRepAssign::Translate]uint16 integer overflow occurs, instrId = [%hu], instrCount = [%hu]", instrId,
104 : instrCount),
105 : InternalException, "integer overflow");
106 152 : instrId += instrCount;
107 :
108 152 : return translated;
109 : }
110 :
111 170 : std::string CcuRepAssign::Describe()
112 : {
113 170 : switch (subType) {
114 72 : case AssignSubType::IMD_TO_VARIABLE: {
115 72 : return StringFormat("Variable[%u] = Value[%lu]", varA.Id(), immediate);
116 : }
117 2 : case AssignSubType::IMD_TO_ADDR: {
118 2 : return StringFormat("Address[%u] = Value[%lu]", addrA.Id(), immediate);
119 : }
120 27 : case AssignSubType::VAR_TO_ADDR: {
121 27 : return StringFormat("Address[%u] = Variable[%u]", addrA.Id(), varA.Id());
122 : }
123 12 : case AssignSubType::ADDR_TO_ADDR: {
124 12 : return StringFormat("Address[%u] = Address[%u]", addrB.Id(), addrA.Id());
125 : }
126 57 : case AssignSubType::VAR_TO_VAR: {
127 57 : return StringFormat("Var[%u] = Var[%u]", varB.Id(), varA.Id());
128 : }
129 0 : default: {
130 0 : return StringFormat("Invalid Assign");
131 : }
132 : }
133 : }
134 :
135 : }; // namespace CcuRep
136 : }; // namespace Hccl
|