• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2023-2024 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14project(llvmbackend)
15
16if (NOT PANDA_BUILD_LLVM_BACKEND)
17    message(FATAL_ERROR "PANDA_BUILD_LLVM_BACKEND must be true")
18endif()
19
20include(${PANDA_ROOT}/compiler/cmake/target.cmake)
21
22if(ENABLE_COMPILER_COVERAGE)
23    # Set coverage options
24    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
25endif()
26
27set(SOURCES
28        llvm_ark_interface.cpp
29        llvm_compiler.cpp
30        llvm_irtoc_compiler.cpp
31        llvm_logger.cpp
32        llvm_options.cpp
33        lowering/debug_data_builder.cpp
34        lowering/gc_barriers.cpp
35        lowering/irtoc_function_utils.cpp
36        lowering/llvm_ir_constructor.cpp
37        lowering/wrapped_module.cpp
38        mir_compiler.cpp
39        object_code/ark_aot_linker.cpp
40        object_code/code_info_producer.cpp
41        object_code/created_object_file.cpp
42        object_code/dump.cpp
43        target_machine_builder.cpp
44        transforms/builtins.cpp
45        transforms/gc_utils.cpp
46        transforms/llvm_optimizer.cpp
47        transforms/passes/aarch64_fixup_sdiv.cpp
48        transforms/passes/ark_frame_lowering/frame_builder.cpp
49        transforms/passes/ark_frame_lowering/frame_lowering.cpp
50        transforms/passes/ark_gvn.cpp
51        transforms/passes/ark_inlining.cpp
52        transforms/passes/check_external.cpp
53        transforms/passes/check_tail_calls.cpp
54        transforms/passes/devirt.cpp
55        transforms/passes/expand_atomics.cpp
56        transforms/passes/fixup_poisons.cpp
57        transforms/passes/gc_intrusion.cpp
58        transforms/passes/gc_intrusion_check.cpp
59        transforms/passes/gep_propagation.cpp
60        transforms/passes/infer_flags.cpp
61        transforms/passes/inline_devirt.cpp
62        transforms/passes/inline_ir/cleanup_inline_module.cpp
63        transforms/passes/inline_ir/discard_inline_module.cpp
64        transforms/passes/inline_ir/inline_ir_utils.cpp
65        transforms/passes/inline_ir/mark_always_inline.cpp
66        transforms/passes/inline_ir/mark_inline_module.cpp
67        transforms/passes/inline_ir/patch_return_handler_stack_adjustment.cpp
68        transforms/passes/inline_ir/remove_unused_functions.cpp
69        transforms/passes/insert_safepoints.cpp
70        transforms/passes/intrinsics_lowering.cpp
71        transforms/passes/loop_peeling.cpp
72        transforms/passes/mem_barriers.cpp
73        transforms/passes/panda_runtime_lowering.cpp
74        transforms/passes/propagate_lenarray.cpp
75        transforms/passes/prune_deopt.cpp
76        transforms/runtime_calls.cpp
77        utils.cpp
78)
79
80if (PANDA_LLVM_AOT)
81    list(APPEND SOURCES
82        llvm_aot_compiler.cpp
83    )
84endif()
85
86set(GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated)
87file(MAKE_DIRECTORY ${GENERATED_DIR})
88
89set(PIPELINE_CFG ${CMAKE_CURRENT_LIST_DIR}/transforms/pipeline.cfg)
90set(PIPELINE_GEN_INC ${GENERATED_DIR}/pipeline_gen.inc)
91add_custom_command(
92    OUTPUT ${PIPELINE_GEN_INC}
93    COMMAND ruby ${CMAKE_CURRENT_LIST_DIR}/templates/pipeline.rb
94    -d ${PIPELINE_CFG}
95    -o ${PIPELINE_GEN_INC}
96    DEPENDS ${PIPELINE_CFG}
97)
98add_custom_target(llvmbackend_pipeline_gen DEPENDS ${PIPELINE_CFG} ${PIPELINE_GEN_INC})
99
100set(PIPELINE_IRTOC_CFG ${CMAKE_CURRENT_LIST_DIR}/transforms/pipeline_irtoc.cfg)
101set(PIPELINE_IRTOC_GEN_INC ${GENERATED_DIR}/pipeline_irtoc_gen.inc)
102add_custom_command(
103    OUTPUT ${PIPELINE_IRTOC_GEN_INC}
104    COMMAND ruby ${CMAKE_CURRENT_LIST_DIR}/templates/pipeline.rb
105    -d ${PIPELINE_IRTOC_CFG}
106    -o ${PIPELINE_IRTOC_GEN_INC}
107    -s "_IRTOC"
108    DEPENDS ${PIPELINE_IRTOC_CFG}
109)
110add_custom_target(llvmbackend_pipeline_irtoc_gen DEPENDS ${PIPELINE_IRTOC_CFG} ${PIPELINE_IRTOC_GEN_INC})
111
112panda_gen(DATA ${CMAKE_CURRENT_LIST_DIR}/transforms/passes/passes.yaml
113    TEMPLATES llvm_passes.inl.erb
114    SOURCE ${CMAKE_CURRENT_LIST_DIR}/templates
115    DESTINATION ${GENERATED_DIR}
116    API ${CMAKE_CURRENT_LIST_DIR}/templates/llvm_pass.rb
117    TARGET_NAME llvmbackend_passes_gen
118)
119
120set(OPTIONS_GEN_H ${GENERATED_DIR}/llvm_options_gen.h)
121panda_gen_file(
122        DATA ${CMAKE_CURRENT_LIST_DIR}/llvmbackend.yaml
123        TEMPLATE ${PANDA_ROOT}/templates/options/options.h.erb
124        OUTPUTFILE ${OPTIONS_GEN_H}
125        API ${PANDA_ROOT}/templates/common.rb
126)
127add_custom_target(llvmbackend_options_gen DEPENDS ${OPTIONS_GEN_H})
128
129panda_gen(DATA ${PANDA_BINARY_ROOT}/runtime/intrinsics.yaml
130    TEMPLATES intrinsics_gen.inl.erb
131    SOURCE ${CMAKE_CURRENT_LIST_DIR}/templates
132    DESTINATION ${GENERATED_DIR}
133    API ${CMAKE_CURRENT_LIST_DIR}/templates/intrinsics.rb
134    REQUIRES ${PANDA_ROOT}/libpandabase/utils.rb
135    EXTRA_DEPENDENCIES arkruntime_gen_intrinsics_yaml
136)
137
138panda_gen(DATA ${PANDA_BINARY_ROOT}/runtime/intrinsics.yaml
139    TEMPLATES can_compile_intrinsics_gen.inl.erb
140    SOURCE ${CMAKE_CURRENT_LIST_DIR}/templates
141    DESTINATION ${GENERATED_DIR}
142    API ${CMAKE_CURRENT_LIST_DIR}/templates/intrinsics.rb
143    REQUIRES ${PANDA_ROOT}/libpandabase/utils.rb
144    EXTRA_DEPENDENCIES arkruntime_gen_intrinsics_yaml
145    TARGET_NAME can_compile_intrinsics_gen_llvmbackend
146)
147
148panda_gen(DATA ${PANDA_BINARY_ROOT}/runtime/intrinsics.yaml
149    TEMPLATES intrinsic_names_gen.inl.erb
150    SOURCE ${CMAKE_CURRENT_LIST_DIR}/templates
151    DESTINATION ${GENERATED_DIR}
152    API ${CMAKE_CURRENT_LIST_DIR}/templates/intrinsics.rb
153    REQUIRES ${PANDA_ROOT}/libpandabase/utils.rb
154    EXTRA_DEPENDENCIES arkruntime_gen_intrinsics_yaml
155    TARGET_NAME intrinsic_names_gen_llvmbackend
156)
157
158panda_gen(DATA ${PANDA_BINARY_ROOT}/runtime/intrinsics.yaml
159    TEMPLATES intrinsics_llvm_codegen.inl.erb
160    SOURCE ${CMAKE_CURRENT_LIST_DIR}/templates
161    DESTINATION ${GENERATED_DIR}
162    API ${CMAKE_CURRENT_LIST_DIR}/templates/intrinsics.rb
163    REQUIRES ${PANDA_ROOT}/libpandabase/utils.rb
164    EXTRA_DEPENDENCIES arkruntime_gen_intrinsics_yaml
165    TARGET_NAME intrinsic_llvm_codegen_llvmbackend
166)
167
168panda_gen(DATA ${PANDA_BINARY_ROOT}/runtime/entrypoints.yaml
169    TEMPLATES entrypoints_gen.inl.erb
170    SOURCE ${CMAKE_CURRENT_LIST_DIR}/templates
171    DESTINATION ${GENERATED_DIR}
172    API ${CMAKE_CURRENT_LIST_DIR}/templates/entrypoints.rb
173    EXTRA_DEPENDENCIES entrypoints_yaml_merge
174)
175
176panda_gen(DATA ${PANDA_BINARY_ROOT}/runtime/entrypoints.yaml
177    TEMPLATES entrypoints_llvm_ark_interface_gen.inl.erb
178    SOURCE ${CMAKE_CURRENT_LIST_DIR}/templates
179    DESTINATION ${GENERATED_DIR}
180    API ${CMAKE_CURRENT_LIST_DIR}/templates/entrypoints.rb
181    EXTRA_DEPENDENCIES entrypoints_yaml_merge
182    TARGET_NAME entrypoints_llvm_ark_interface_gen_llvmbackend
183)
184
185set(LOGGER_COMPONENTS_GEN_H ${GENERATED_DIR}/llvm_logger_components.inc)
186panda_gen_file(
187    DATA ${CMAKE_CURRENT_LIST_DIR}/llvmbackend.yaml
188    TEMPLATE ${PANDA_ROOT}/templates/logger_components/logger_components.inc.erb
189    OUTPUTFILE ${LOGGER_COMPONENTS_GEN_H}
190    API ${PANDA_ROOT}/templates/common.rb
191)
192add_custom_target(llvmbackend_logger_components_gen DEPENDS ${LOGGER_COMPONENTS_GEN_H})
193
194panda_add_library(llvmbackend SHARED ${SOURCES})
195
196add_custom_target(llvmbackend_gen_files COMMENT "Umbrella target for llvmbackend generated files")
197add_dependencies(llvmbackend_gen_files
198    llvmbackend_pipeline_gen
199    llvmbackend_pipeline_irtoc_gen
200    llvmbackend_options_gen
201    llvmbackend_passes_gen
202    entrypoints_gen_llvmbackend
203    intrinsics_gen_llvmbackend
204    can_compile_intrinsics_gen_llvmbackend
205    intrinsic_names_gen_llvmbackend
206    intrinsic_llvm_codegen_llvmbackend
207    entrypoints_llvm_ark_interface_gen_llvmbackend
208    llvmbackend_logger_components_gen
209)
210
211add_dependencies(llvmbackend llvmbackend_gen_files copy-libLLVM.so)
212add_dependencies(panda_gen_files llvmbackend_gen_files)
213
214panda_target_include_directories(llvmbackend SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS})
215panda_target_include_directories(llvmbackend PRIVATE .)
216
217panda_target_include_directories(llvmbackend
218        PRIVATE ${PANDA_ROOT}/compiler
219        PUBLIC ${GENERATED_DIR}
220)
221
222panda_target_link_libraries(llvmbackend arkcompiler arkbase arkfile aot_builder ${LIB_LLVM})
223
224if (NOT (PANDA_TARGET_MOBILE))
225    panda_target_link_libraries(llvmbackend rt)
226endif()
227
228panda_target_compile_options(llvmbackend PUBLIC "-Wno-unused-parameter")
229
230panda_target_compile_definitions(llvmbackend PUBLIC REQUIRED_LLVM_VERSION=15.0.4-ark18)
231
232if (PANDA_LLVM_AOT AND PANDA_TARGET_AMD64 AND PANDA_COMPILER_TARGET_AARCH64 AND PANDA_WITH_TESTS)
233    set(PANDA_LLVM_AOT_TESTS_SOURCES
234        ${PANDA_ROOT}/compiler/tests/unit_test.cpp
235        ${PANDA_ROOT}/compiler/tests/inst_generator.cpp
236        tests/inst_generator_test.cpp
237    )
238    set(PANDA_LLVM_AOT_TESTS_LIBRARIES arkcompiler arkbase arkassembler arkruntime llvmbackend)
239
240    panda_add_gtest(
241        CONTAINS_MAIN
242        NAME llvm_aot_unit_tests
243        SOURCES
244        ${PANDA_LLVM_AOT_TESTS_SOURCES}
245        LIBRARIES
246        ${PANDA_LLVM_AOT_TESTS_LIBRARIES}
247        SANITIZERS
248            ${PANDA_SANITIZERS_LIST}
249    )
250
251    panda_target_include_directories(llvm_aot_unit_tests SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS})
252    panda_target_include_directories(llvm_aot_unit_tests PRIVATE .)
253    panda_target_include_directories(llvm_aot_unit_tests PRIVATE ${PANDA_ROOT}/compiler
254        PUBLIC "$<TARGET_PROPERTY:arkruntime,INTERFACE_INCLUDE_DIRECTORIES>")
255    panda_target_compile_options(llvm_aot_unit_tests PUBLIC "-Wno-unused-parameter")
256endif()
257