1 /*
2 * Copyright (c) 2021-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 "unit_test.h"
17 #include "inst_generator.h"
18 #include "optimizer/code_generator/codegen.h"
19 #include "optimizer/optimizations/regalloc/reg_alloc.h"
20 #include "target/amd64/target.h"
21 #include "target/aarch64/target.h"
22 #include "target/aarch32/target.h"
23
24 namespace ark::compiler {
25
26 class IntrinsicCodegenTest : public GraphTest {
27 public:
IntrinsicCodegenTest()28 IntrinsicCodegenTest()
29 : alloc_ {SpaceType::SPACE_TYPE_COMPILER},
30 local_alloc_ {SpaceType::SPACE_TYPE_COMPILER},
31 graph_creator_ {alloc_, local_alloc_},
32 aarch64_encoder_ {&alloc_},
33 aarch32_encoder_ {&alloc_},
34 amd64_encoder_ {&alloc_}
35 {
36 }
37
TryEncode(DataType::Type type,RuntimeInterface::IntrinsicId intrinsic_id,Arch arch,bool prepare_graph)38 bool TryEncode(DataType::Type type, RuntimeInterface::IntrinsicId intrinsic_id, Arch arch, bool prepare_graph)
39 {
40 graph_creator_.SetRuntimeTargetArch(arch);
41 auto inst = GenerateIntrinsic(&alloc_, type, intrinsic_id);
42 auto graph = graph_creator_.GenerateGraph(inst);
43 Codegen codegen(graph);
44 codegen.GetCodeBuilder()->BeginMethod(0U, 0U);
45 if (prepare_graph) {
46 graph->GetAnalysis<LoopAnalyzer>().Run();
47 GraphChecker(graph).Check();
48 RegAlloc(graph);
49 codegen.Initialize();
50 }
51 codegen.CreateBuiltinIntrinsic(inst);
52 return codegen.GetEncoder()->GetResult();
53 }
54
55 private:
56 ArenaAllocator alloc_;
57 ArenaAllocator local_alloc_;
58 GraphCreator graph_creator_;
59 aarch64::Aarch64Encoder aarch64_encoder_;
60 aarch32::Aarch32Encoder aarch32_encoder_;
61 amd64::Amd64Encoder amd64_encoder_;
62
GenerateIntrinsic(ArenaAllocator * allocator,DataType::Type type,RuntimeInterface::IntrinsicId intrinsic_id)63 IntrinsicInst *GenerateIntrinsic(ArenaAllocator *allocator, DataType::Type type,
64 RuntimeInterface::IntrinsicId intrinsic_id)
65 {
66 auto inst = Inst::New<IntrinsicInst>(allocator, Opcode::Intrinsic, intrinsic_id);
67 inst->SetType(type);
68 return inst;
69 }
70 };
71
72 #ifdef INTRINSIC_CODEGEN_TEST_ARM64
TEST_F(IntrinsicCodegenTest,EncodingARM64)73 TEST_F(IntrinsicCodegenTest, EncodingARM64)
74 {
75 std::pair<Arch, std::string> arch = std::pair {Arch::AARCH64, "arm64"};
76 #include "intrinsic_codegen_test.inl"
77 }
78 #endif
79
80 #ifdef INTRINSIC_CODEGEN_TEST_AMD64
TEST_F(IntrinsicCodegenTest,EncodingAMD64)81 TEST_F(IntrinsicCodegenTest, EncodingAMD64)
82 {
83 std::pair<Arch, std::string> arch = std::pair {Arch::X86_64, "amd64"};
84 #include "intrinsic_codegen_test.inl"
85 }
86 #endif
87
88 #ifdef INTRINSIC_CODEGEN_TEST_ARM32
TEST_F(IntrinsicCodegenTest,EncodingARM32)89 TEST_F(IntrinsicCodegenTest, EncodingARM32)
90 {
91 std::pair<Arch, std::string> arch = std::pair {Arch::AARCH32, "arm32"};
92 #include "intrinsic_codegen_test.inl"
93 }
94 #endif
95 } // namespace ark::compiler
96