Skip to content

Commit ab8b8c1

Browse files
authored
[TargetParser][cmake] Be Smarter about TableGen Deps (#144848)
This tries to be a bit smarter for the OLD behaviour of CMP0116, to glob more relevant directories looking for possible dependencies. The changes are: - Remove some duplication of lines in the `tablegen` function. - Put CURRENT_SOURCE_DIR into `tblgen_includes` (at the front) - Glob all directories in `tblgen_includes` - Give up on `local_tds` which was wrong when using tablegen to compile a file in a different directory (as TargetParser does) - Use `EXTRA_INCLUDES` in TargetParser `tablegen` calls. This is still an under-approximation of what might be included, at least comparing the RISCVTargetParserDef.inc.d (after building `target_parser_gen`), and the list of deps in the ninja file when explicitly setting CMP0116 to OLD. Fixes #144639
1 parent 04e2e58 commit ab8b8c1

File tree

2 files changed

+28
-37
lines changed

2 files changed

+28
-37
lines changed

llvm/cmake/modules/TableGen.cmake

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ function(tablegen project ofn)
2121
message(FATAL_ERROR "${project}_TABLEGEN_EXE not set")
2222
endif()
2323

24+
# Set the include directories
25+
get_directory_property(tblgen_includes INCLUDE_DIRECTORIES)
26+
list(PREPEND tblgen_includes ${ARG_EXTRA_INCLUDES})
27+
list(PREPEND tblgen_includes ${CMAKE_CURRENT_SOURCE_DIR})
28+
# Filter out any empty include items.
29+
list(REMOVE_ITEM tblgen_includes "")
30+
2431
# Use depfile instead of globbing arbitrary *.td(s) for Ninja. We force
2532
# CMake versions older than v3.30 on Windows to use the fallback behavior
2633
# due to a depfile parsing bug on Windows paths in versions prior to 3.30.
@@ -42,22 +49,16 @@ function(tablegen project ofn)
4249
-d ${ofn}.d
4350
DEPFILE ${ofn}.d
4451
)
45-
set(local_tds)
4652
set(global_tds)
4753
else()
48-
file(GLOB local_tds "*.td")
49-
file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td")
54+
set(include_td_dirs "${tblgen_includes}")
55+
list(TRANSFORM include_td_dirs APPEND "/*.td")
56+
file(GLOB global_tds ${include_td_dirs})
5057
set(additional_cmdline
5158
-o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
5259
)
5360
endif()
5461

55-
if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
56-
set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
57-
else()
58-
set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
59-
${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
60-
endif()
6162
if (LLVM_ENABLE_DAGISEL_COV AND "-gen-dag-isel" IN_LIST ARGN)
6263
list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
6364
endif()
@@ -92,44 +93,34 @@ function(tablegen project ofn)
9293
list(APPEND LLVM_TABLEGEN_FLAGS "-no-warn-on-unused-template-args")
9394
endif()
9495

95-
# We need both _TABLEGEN_TARGET and _TABLEGEN_EXE in the DEPENDS list
96-
# (both the target and the file) to have .inc files rebuilt on
97-
# a tablegen change, as cmake does not propagate file-level dependencies
98-
# of custom targets. See the following ticket for more information:
99-
# https://cmake.org/Bug/view.php?id=15858
100-
# The dependency on both, the target and the file, produces the same
101-
# dependency twice in the result file when
102-
# ("${${project}_TABLEGEN_TARGET}" STREQUAL "${${project}_TABLEGEN_EXE}")
103-
# but lets us having smaller and cleaner code here.
104-
get_directory_property(tblgen_includes INCLUDE_DIRECTORIES)
105-
list(APPEND tblgen_includes ${ARG_EXTRA_INCLUDES})
106-
107-
# Get the current set of include paths for this td file.
108-
cmake_parse_arguments(ARG "" "" "DEPENDS;EXTRA_INCLUDES" ${ARGN})
109-
get_directory_property(tblgen_includes INCLUDE_DIRECTORIES)
110-
list(APPEND tblgen_includes ${ARG_EXTRA_INCLUDES})
111-
# Filter out any empty include items.
112-
list(REMOVE_ITEM tblgen_includes "")
113-
11496
# Build the absolute path for the current input file.
11597
if (IS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
11698
set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${LLVM_TARGET_DEFINITIONS})
11799
else()
118-
set(LLVM_TARGET_DEFINITIONS_ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
100+
set(LLVM_TARGET_DEFINITIONS_ABSOLUTE
101+
${CMAKE_CURRENT_SOURCE_DIR}/${LLVM_TARGET_DEFINITIONS})
119102
endif()
120103

121104
# Append this file and its includes to the compile commands file.
122105
# This file is used by the TableGen LSP Language Server (tblgen-lsp-server).
123106
file(APPEND ${CMAKE_BINARY_DIR}/tablegen_compile_commands.yml
124107
"--- !FileInfo:\n"
125108
" filepath: \"${LLVM_TARGET_DEFINITIONS_ABSOLUTE}\"\n"
126-
" includes: \"${CMAKE_CURRENT_SOURCE_DIR};${tblgen_includes}\"\n"
109+
" includes: \"${tblgen_includes}\"\n"
127110
)
128111

129-
# Filter out empty items before prepending each entry with -I
130-
list(REMOVE_ITEM tblgen_includes "")
112+
# Prepend each include entry with -I for arguments.
131113
list(TRANSFORM tblgen_includes PREPEND -I)
132114

115+
# We need both _TABLEGEN_TARGET and _TABLEGEN_EXE in the DEPENDS list
116+
# (both the target and the file) to have .inc files rebuilt on
117+
# a tablegen change, as cmake does not propagate file-level dependencies
118+
# of custom targets. See the following ticket for more information:
119+
# https://cmake.org/Bug/view.php?id=15858
120+
# The dependency on both, the target and the file, produces the same
121+
# dependency twice in the result file when
122+
# ("${${project}_TABLEGEN_TARGET}" STREQUAL "${${project}_TABLEGEN_EXE}")
123+
# but lets us having smaller and cleaner code here.
133124
set(tablegen_exe ${${project}_TABLEGEN_EXE})
134125
set(tablegen_depends ${${project}_TABLEGEN_TARGET} ${tablegen_exe})
135126

@@ -140,7 +131,7 @@ function(tablegen project ofn)
140131
endif()
141132

142133
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
143-
COMMAND ${tablegen_exe} ${ARG_UNPARSED_ARGUMENTS} -I ${CMAKE_CURRENT_SOURCE_DIR}
134+
COMMAND ${tablegen_exe} ${ARG_UNPARSED_ARGUMENTS}
144135
${tblgen_includes}
145136
${LLVM_TABLEGEN_FLAGS}
146137
${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
@@ -150,7 +141,7 @@ function(tablegen project ofn)
150141
# directory and local_tds may not contain it, so we must
151142
# explicitly list it here:
152143
DEPENDS ${ARG_DEPENDS} ${tablegen_depends}
153-
${local_tds} ${global_tds}
144+
${global_tds}
154145
${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
155146
${LLVM_TARGET_DEPENDS}
156147
${LLVM_TABLEGEN_JOB_POOL}

llvm/include/llvm/TargetParser/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
set(LLVM_TARGET_DEFINITIONS ${PROJECT_SOURCE_DIR}/lib/Target/ARM/ARM.td)
2-
tablegen(LLVM ARMTargetParserDef.inc -gen-arm-target-def -I ${PROJECT_SOURCE_DIR}/lib/Target/ARM/)
2+
tablegen(LLVM ARMTargetParserDef.inc -gen-arm-target-def EXTRA_INCLUDES ${PROJECT_SOURCE_DIR}/lib/Target/ARM)
33

44
set(LLVM_TARGET_DEFINITIONS ${PROJECT_SOURCE_DIR}/lib/Target/AArch64/AArch64.td)
5-
tablegen(LLVM AArch64TargetParserDef.inc -gen-arm-target-def -I ${PROJECT_SOURCE_DIR}/lib/Target/AArch64/)
5+
tablegen(LLVM AArch64TargetParserDef.inc -gen-arm-target-def EXTRA_INCLUDES ${PROJECT_SOURCE_DIR}/lib/Target/AArch64)
66

77
set(LLVM_TARGET_DEFINITIONS ${PROJECT_SOURCE_DIR}/lib/Target/RISCV/RISCV.td)
8-
tablegen(LLVM RISCVTargetParserDef.inc -gen-riscv-target-def -I ${PROJECT_SOURCE_DIR}/lib/Target/RISCV/)
8+
tablegen(LLVM RISCVTargetParserDef.inc -gen-riscv-target-def EXTRA_INCLUDES ${PROJECT_SOURCE_DIR}/lib/Target/RISCV)
99

1010
set(LLVM_TARGET_DEFINITIONS ${PROJECT_SOURCE_DIR}/lib/Target/PowerPC/PPC.td)
1111
tablegen(LLVM PPCGenTargetFeatures.inc -gen-target-features -I${PROJECT_SOURCE_DIR}/lib/Target/PowerPC)

0 commit comments

Comments
 (0)