Skip to content

Commit 791c5d0

Browse files
authored
[libc] Improve get_object_files_for_test to reduce CMake configure time for tests. (#75552)
Profiling cmake shows that a significant time configuring `libc` folder is spent on running `get_object_files_for_test` in the `test` folder (13 sec in `libc/test` folder / 16 sec in `libc` folder). By caching all needed objects for each target instead of resolving every time, the time cmake spends on configuring `libc/test` folder is reduced to ~1s.
1 parent b26ee97 commit 791c5d0

File tree

1 file changed

+49
-30
lines changed

1 file changed

+49
-30
lines changed

libc/cmake/modules/LLVMLibCTestRules.cmake

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,62 +18,81 @@ function(get_object_files_for_test result skipped_entrypoints_list)
1818
set(checked_list "")
1919
set(unchecked_list "${ARGN}")
2020
list(REMOVE_DUPLICATES unchecked_list)
21-
list(LENGTH unchecked_list length)
2221

23-
while(length)
24-
set(indirect_list "")
22+
foreach(dep IN LISTS unchecked_list)
23+
if (NOT TARGET ${dep})
24+
# Skip tests with undefined dependencies.
25+
list(APPEND skipped_list ${dep})
26+
continue()
27+
endif()
28+
get_target_property(aliased_target ${dep} "ALIASED_TARGET")
29+
if(aliased_target)
30+
# If the target is just an alias, switch to the real target.
31+
set(dep ${aliased_target})
32+
endif()
2533

26-
foreach(dep IN LISTS unchecked_list)
27-
if (NOT TARGET ${dep})
28-
# Skip tests with undefined dependencies.
29-
list(APPEND skipped_list ${dep})
30-
continue()
31-
endif()
32-
get_target_property(dep_type ${dep} "TARGET_TYPE")
33-
if(NOT dep_type)
34-
# Skip tests with no object dependencies.
35-
continue()
36-
endif()
34+
get_target_property(dep_type ${dep} "TARGET_TYPE")
35+
if(NOT dep_type)
36+
# Skip tests with no object dependencies.
37+
continue()
38+
endif()
39+
40+
get_target_property(dep_checked ${dep} "CHECK_OBJ_FOR_TESTS")
41+
42+
if(dep_checked)
43+
# Target full dependency has already been checked. Just use the results.
44+
get_target_property(dep_obj ${dep} "OBJECT_FILES_FOR_TESTS")
45+
get_target_property(dep_skip ${dep} "SKIPPED_LIST_FOR_TESTS")
46+
else()
47+
# Target full dependency hasn't been checked. Recursively check its DEPS.
48+
set(dep_obj "${dep}")
49+
set(dep_skip "")
50+
51+
get_target_property(indirect_deps ${dep} "DEPS")
52+
get_object_files_for_test(dep_obj dep_skip ${indirect_deps})
3753

3854
if(${dep_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})
3955
get_target_property(dep_object_files ${dep} "OBJECT_FILES")
4056
if(dep_object_files)
41-
list(APPEND object_files ${dep_object_files})
57+
list(APPEND dep_obj ${dep_object_files})
4258
endif()
4359
elseif(${dep_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE})
4460
get_target_property(is_skipped ${dep} "SKIPPED")
4561
if(is_skipped)
46-
list(APPEND skipped_list ${dep})
47-
continue()
62+
list(APPEND dep_skip ${dep})
63+
list(REMOVE_ITEM dep_obj ${dep})
4864
endif()
4965
get_target_property(object_file_raw ${dep} "OBJECT_FILE_RAW")
5066
if(object_file_raw)
51-
list(APPEND object_files ${object_file_raw})
67+
list(APPEND dep_obj ${object_file_raw})
5268
endif()
5369
elseif(${dep_type} STREQUAL ${ENTRYPOINT_OBJ_VENDOR_TARGET_TYPE})
5470
# Skip tests for externally implemented entrypoints.
55-
list(APPEND skipped_list ${dep})
56-
continue()
71+
list(APPEND dep_skip ${dep})
72+
list(REMOVE_ITEM dep_obj ${dep})
5773
endif()
5874

59-
get_target_property(indirect_deps ${dep} "DEPS")
60-
list(APPEND indirect_list "${indirect_deps}")
61-
endforeach(dep)
75+
set_target_properties(${dep} PROPERTIES
76+
OBJECT_FILES_FOR_TESTS "${dep_obj}"
77+
SKIPPED_LIST_FOR_TESTS "${dep_skip}"
78+
CHECK_OBJ_FOR_TESTS "YES"
79+
)
80+
81+
endif()
82+
83+
list(APPEND object_files ${dep_obj})
84+
list(APPEND skipped_list ${dep_skip})
6285

63-
# Only add new indirect dependencies to check.
64-
list(APPEND checked_list "${unchecked_list}")
65-
list(REMOVE_DUPLICATES indirect_list)
66-
list(REMOVE_ITEM indirect_list checked_list)
67-
set(unchecked_list "${indirect_list}")
68-
list(LENGTH unchecked_list length)
69-
endwhile()
86+
endforeach(dep)
7087

7188
list(REMOVE_DUPLICATES object_files)
7289
set(${result} ${object_files} PARENT_SCOPE)
7390
list(REMOVE_DUPLICATES skipped_list)
7491
set(${skipped_entrypoints_list} ${skipped_list} PARENT_SCOPE)
92+
7593
endfunction(get_object_files_for_test)
7694

95+
7796
# Rule to add a libc unittest.
7897
# Usage
7998
# add_libc_unittest(

0 commit comments

Comments
 (0)