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