1 /**
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include "libabckit/include/c/abckit.h"
19 #include "libabckit/include/c/metadata_core.h"
20 #include "helpers/helpers.h"
21 #include "helpers/helpers_runtime.h"
22
23 // NOLINTBEGIN(readability-magic-numbers)
24
25 namespace libabckit::test {
26
27 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
28 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
29 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
30 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 static auto g_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32
33 class LibAbcKitIrInstInputsTest : public ::testing::Test {};
34
35 namespace {
36
TransformIrStatic(AbckitGraph * graph)37 void TransformIrStatic(AbckitGraph *graph)
38 {
39 auto constant1 = g_implG->gFindOrCreateConstantI64(graph, 10);
40 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
41 auto constant2 = g_implG->gFindOrCreateConstantI64(graph, 20);
42 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
43
44 AbckitInst *callInst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CALL_STATIC);
45 ASSERT_NE(callInst, nullptr);
46
47 g_implG->iSetInput(callInst, constant1, 0);
48 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
49 g_implG->iSetInput(callInst, constant2, 1);
50 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
51 }
52
TransformIrStatic2(AbckitGraph * graph)53 void TransformIrStatic2(AbckitGraph *graph)
54 {
55 auto constant1 = g_implG->gFindOrCreateConstantI64(graph, 10U);
56 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
57 auto constant2 = g_implG->gFindOrCreateConstantI64(graph, 20U);
58 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
59
60 AbckitInst *callInst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CALL_STATIC);
61 ASSERT_NE(callInst, nullptr);
62
63 g_implG->iSetInputs(callInst, 2U, constant1, constant2);
64 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
65 }
66 } // namespace
67
68 // Test: test-kind=api, api=GraphApiImpl::iSetInput, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstInputsTest,StaticSetInput)69 TEST_F(LibAbcKitIrInstInputsTest, StaticSetInput)
70 {
71 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_inputs/inst_inputs_static.abc",
72 "inst_inputs_static/ETSGLOBAL", "main");
73 EXPECT_TRUE(helpers::Match(output, "1 2\n"));
74
75 helpers::TransformMethod(
76 ABCKIT_ABC_DIR "ut/ir_core/inst_inputs/inst_inputs_static.abc",
77 ABCKIT_ABC_DIR "ut/ir_core/inst_inputs/inst_inputs_static_modified.abc", "foo",
78 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) { TransformIrStatic(graph); },
79 [](AbckitGraph *graph) {
80 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
81 {{{},
82 {1},
83 {
84 {4, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}},
85 {5, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}},
86 {0, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
87 {1, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
88 }},
89 {{0},
90 {2},
91 {
92 {2, ABCKIT_ISA_API_STATIC_OPCODE_CALL_STATIC, {0, 1}},
93 {3, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
94 }},
95 {{1}, {}, {}}});
96 helpers::VerifyGraph(graph, bbSchemas);
97 });
98
99 output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_inputs/inst_inputs_static_modified.abc",
100 "inst_inputs_static/ETSGLOBAL", "main");
101 EXPECT_TRUE(helpers::Match(output, "10 20\n"));
102 }
103
104 // Test: test-kind=api, api=GraphApiImpl::iSetInputs, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstInputsTest,StaticSetInputs)105 TEST_F(LibAbcKitIrInstInputsTest, StaticSetInputs)
106 {
107 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_inputs/inst_inputs_static.abc",
108 "inst_inputs_static/ETSGLOBAL", "main");
109 EXPECT_TRUE(helpers::Match(output, "1 2\n"));
110
111 helpers::TransformMethod(
112 ABCKIT_ABC_DIR "ut/ir_core/inst_inputs/inst_inputs_static.abc",
113 ABCKIT_ABC_DIR "ut/ir_core/inst_inputs/inst_inputs_static_modified.abc", "foo",
114 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) { TransformIrStatic2(graph); },
115 [](AbckitGraph *graph) {
116 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
117 {{{},
118 {1},
119 {
120 {4, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}},
121 {5, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}},
122 {0, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
123 {1, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}},
124 }},
125 {{0},
126 {2},
127 {
128 {2, ABCKIT_ISA_API_STATIC_OPCODE_CALL_STATIC, {0, 1}},
129 {3, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
130 }},
131 {{1}, {}, {}}});
132 helpers::VerifyGraph(graph, bbSchemas);
133 });
134
135 output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_inputs/inst_inputs_static_modified.abc",
136 "inst_inputs_static/ETSGLOBAL", "main");
137 EXPECT_TRUE(helpers::Match(output, "10 20\n"));
138 }
139
140 namespace {
TransformIrDynamic(AbckitGraph * graph)141 void TransformIrDynamic(AbckitGraph *graph)
142 {
143 auto constant1 = g_implG->gFindOrCreateConstantI64(graph, 10);
144 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
145 auto constant2 = g_implG->gFindOrCreateConstantI64(graph, 20);
146 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
147
148 AbckitInst *callInst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLTHIS1);
149 ASSERT_NE(callInst, nullptr);
150
151 g_implG->iSetInput(callInst, constant1, 0);
152 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
153 g_implG->iSetInput(callInst, constant2, 1);
154 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
155 }
156
TransformIrDynamic2(AbckitGraph * graph)157 void TransformIrDynamic2(AbckitGraph *graph)
158 {
159 auto constant1 = g_implG->gFindOrCreateConstantI64(graph, 10);
160 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
161 auto constant2 = g_implG->gFindOrCreateConstantI64(graph, 20);
162 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
163
164 AbckitInst *callInst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLTHIS1);
165 const size_t secondInput = 2;
166 g_implG->iSetInputs(callInst, secondInput, constant1, constant2);
167 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
168 }
169 } // namespace
170
171 // Test: test-kind=api, api=GraphApiImpl::iSetInput, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstInputsTest,DynamicSetInput)172 TEST_F(LibAbcKitIrInstInputsTest, DynamicSetInput)
173 {
174 helpers::TransformMethod(
175 ABCKIT_ABC_DIR "ut/ir_core/inst_inputs/inst_inputs_dynamic.abc",
176 ABCKIT_ABC_DIR "ut/ir_core/inst_inputs/inst_inputs_dynamic_modified.abc", "foo",
177 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) { TransformIrDynamic(graph); },
178 [](AbckitGraph *graph) {
179 std::vector<helpers::InstSchema<AbckitIsaApiDynamicOpcode>> insts1(
180 {{0, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
181 {1, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
182 {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
183 {3, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
184 {4, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
185 {10, ABCKIT_ISA_API_DYNAMIC_OPCODE_CONSTANT, {}},
186 {11, ABCKIT_ISA_API_DYNAMIC_OPCODE_CONSTANT, {}}});
187 helpers::BBSchema<AbckitIsaApiDynamicOpcode> bb1({{}, {1}, insts1});
188 std::vector<helpers::InstSchema<AbckitIsaApiDynamicOpcode>> insts2({
189 {5, ABCKIT_ISA_API_DYNAMIC_OPCODE_TRYLDGLOBALBYNAME, {}},
190 {6, ABCKIT_ISA_API_DYNAMIC_OPCODE_LDOBJBYNAME, {5}},
191 {7, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLTHIS1, {10, 11, 3}},
192 {8, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED, {}},
193 });
194 helpers::BBSchema<AbckitIsaApiDynamicOpcode> bb2({{0}, {2}, insts2});
195 helpers::BBSchema<AbckitIsaApiDynamicOpcode> bb3({{1}, {}, {}});
196 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchemas({bb1, bb2, bb3});
197 helpers::VerifyGraph(graph, bbSchemas);
198 });
199 }
200
201 // Test: test-kind=api, api=GraphApiImpl::iSetInputs, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstInputsTest,DynamicSetInputs)202 TEST_F(LibAbcKitIrInstInputsTest, DynamicSetInputs)
203 {
204 auto cb = [](AbckitGraph *graph) {
205 // clang-format off
206 std::vector<helpers::InstSchema<AbckitIsaApiDynamicOpcode>> insts1({
207 {0, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
208 {1, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
209 {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
210 {3, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
211 {4, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
212 {10, ABCKIT_ISA_API_DYNAMIC_OPCODE_CONSTANT, {}},
213 {11, ABCKIT_ISA_API_DYNAMIC_OPCODE_CONSTANT, {}}
214 });
215 // clang-format on
216 helpers::BBSchema<AbckitIsaApiDynamicOpcode> bb1({{}, {1}, insts1});
217 std::vector<helpers::InstSchema<AbckitIsaApiDynamicOpcode>> insts2({
218 {5, ABCKIT_ISA_API_DYNAMIC_OPCODE_TRYLDGLOBALBYNAME, {}},
219 {6, ABCKIT_ISA_API_DYNAMIC_OPCODE_LDOBJBYNAME, {5}},
220 {7, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLTHIS1, {10, 11, 3}},
221 {8, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED, {}},
222 });
223 helpers::BBSchema<AbckitIsaApiDynamicOpcode> bb2({{0}, {2}, insts2});
224 helpers::BBSchema<AbckitIsaApiDynamicOpcode> bb3({{1}, {}, {}});
225 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchemas({bb1, bb2, bb3});
226 helpers::VerifyGraph(graph, bbSchemas);
227 };
228 helpers::TransformMethod(
229 ABCKIT_ABC_DIR "ut/ir_core/inst_inputs/inst_inputs_dynamic.abc",
230 ABCKIT_ABC_DIR "ut/ir_core/inst_inputs/inst_inputs_dynamic_modified.abc", "foo",
231 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) { TransformIrDynamic2(graph); },
232 cb);
233 }
234
235 } // namespace libabckit::test
236
237 // NOLINTEND(readability-magic-numbers)
238