Skip to content

Commit c65dc6d

Browse files
authored
Using our own tablegen with depfile support. (#22554)
Experimenting with this. Currently we are incorrect at head: changes to .td included by a target .td will not trigger rebuilds and build with old cached copies. The bazel side handles this by explicitly specifying deps but that doesn't quite work when lowered to cmake as LLVM's rules do a bunch of silly stuff to prevent it. The proper solution is available anyway, though: depfiles. Tablegen can produce them and they give us perfect deps. So here's an attempt at using them.
1 parent f2e5f56 commit c65dc6d

File tree

4 files changed

+84
-20
lines changed

4 files changed

+84
-20
lines changed

CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@
66

77
cmake_minimum_required(VERSION 3.21...3.24)
88

9-
# LLVM requires CMP0116 for tblgen: https://reviews.llvm.org/D101083
10-
# CMP0116: Ninja generators transform `DEPFILE`s from `add_custom_command()`
11-
# New in CMake 3.20. https://cmake.org/cmake/help/latest/policy/CMP0116.html
12-
set(CMAKE_POLICY_DEFAULT_CMP0116 OLD)
13-
if(POLICY CMP0116)
14-
cmake_policy(SET CMP0116 OLD)
15-
endif()
16-
179
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1810

1911
# NOTE: ASM is at the end of the list so CMake can test whether the C/CXX

build_tools/bazel_to_cmake/bazel_to_cmake_converter.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ def exports_files(self, *args, **kwargs):
281281
pass
282282

283283
def iree_td_library(self, *args, **kwargs):
284+
"""Ignores iree_td_library - no CMake equivalent needed.
285+
286+
Transitive .td dependencies are automatically tracked via depfiles
287+
generated by tablegen, so we don't need explicit td_library targets.
288+
"""
284289
pass
285290

286291
# Technically we could do something with a CMake equivalent but we have no use

build_tools/cmake/iree_tablegen_doc.cmake

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ function(iree_tablegen_doc)
3737
iree_package_name(_PACKAGE_NAME)
3838
set(_NAME "${_PACKAGE_NAME}_${_RULE_NAME}")
3939

40+
# Get tablegen executable and target based on TBLGEN parameter.
4041
if(${_RULE_TBLGEN} MATCHES "IREE")
41-
set(_TBLGEN "IREE")
42+
set(_TBLGEN_EXE ${IREE_TABLEGEN_EXE})
43+
set(_TBLGEN_TARGET ${IREE_TABLEGEN_TARGET})
4244
else()
43-
set(_TBLGEN "MLIR")
45+
set(_TBLGEN_EXE ${MLIR_TABLEGEN_EXE})
46+
set(_TBLGEN_TARGET ${MLIR_TABLEGEN_TARGET})
4447
endif()
4548

4649
set(_INCLUDE_DIRS
@@ -51,8 +54,12 @@ function(iree_tablegen_doc)
5154
list(APPEND _INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
5255
list(TRANSFORM _INCLUDE_DIRS PREPEND "-I")
5356

54-
set(_INPUTS ${_RULE_TD_FILE})
55-
set(LLVM_TARGET_DEFINITIONS ${_INPUTS})
57+
# Build absolute path for the main .td file.
58+
if(IS_ABSOLUTE ${_RULE_TD_FILE})
59+
set(_TD_FILE_ABS ${_RULE_TD_FILE})
60+
else()
61+
set(_TD_FILE_ABS ${CMAKE_CURRENT_SOURCE_DIR}/${_RULE_TD_FILE})
62+
endif()
5663

5764
set(_FLAGS
5865
"--strip-prefix=::mlir::iree_compiler::IREE::"
@@ -70,9 +77,30 @@ function(iree_tablegen_doc)
7077
list(GET _RULE_OUTS 0 _OUTPUT)
7178
list(REMOVE_AT _RULE_OUTS 0)
7279

73-
# TableGen this output with the given command.
74-
tablegen(${_TBLGEN} ${_OUTPUT} ${_COMMAND} ${_DIALECT} ${_INCLUDE_DIRS} ${_FLAGS})
75-
list(APPEND _OUTPUTS ${CMAKE_CURRENT_BINARY_DIR}/${_OUTPUT})
80+
set(_OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/${_OUTPUT})
81+
82+
# Create add_custom_command with depfile support for Ninja.
83+
if(CMAKE_GENERATOR MATCHES "Ninja")
84+
add_custom_command(
85+
OUTPUT ${_OUTPUT_FILE}
86+
COMMAND ${_TBLGEN_EXE} ${_COMMAND} ${_DIALECT} ${_INCLUDE_DIRS} ${_FLAGS} ${_TD_FILE_ABS}
87+
--write-if-changed -o ${_OUTPUT_FILE} -d ${_OUTPUT_FILE}.d
88+
DEPENDS ${_TBLGEN_TARGET} ${_TBLGEN_EXE} ${_TD_FILE_ABS}
89+
DEPFILE ${_OUTPUT_FILE}.d
90+
COMMENT "Building ${_OUTPUT}..."
91+
)
92+
else()
93+
add_custom_command(
94+
OUTPUT ${_OUTPUT_FILE}
95+
COMMAND ${_TBLGEN_EXE} ${_COMMAND} ${_DIALECT} ${_INCLUDE_DIRS} ${_FLAGS} ${_TD_FILE_ABS}
96+
--write-if-changed -o ${_OUTPUT_FILE}
97+
DEPENDS ${_TBLGEN_TARGET} ${_TBLGEN_EXE} ${_TD_FILE_ABS}
98+
COMMENT "Building ${_OUTPUT}..."
99+
)
100+
endif()
101+
102+
list(APPEND _OUTPUTS ${_OUTPUT_FILE})
103+
set_source_files_properties(${_OUTPUT_FILE} PROPERTIES GENERATED 1)
76104
endwhile()
77105

78106
# Put all dialect docs at one place.

build_tools/cmake/iree_tablegen_library.cmake

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
# iree_tablegen_library()
88
#
99
# Runs iree-tablegen to produce some artifacts.
10+
#
11+
# Parameters:
12+
# NAME: Name of the tablegen library target.
13+
# TBLGEN: Tablegen executable to use (IREE or MLIR).
14+
# TD_FILE: Main .td file to process.
15+
# OUTS: List of output files and their generation flags.
1016
function(iree_tablegen_library)
1117
cmake_parse_arguments(
1218
_RULE
@@ -24,13 +30,15 @@ function(iree_tablegen_library)
2430
iree_package_name(_PACKAGE_NAME)
2531
set(_NAME "${_PACKAGE_NAME}_${_RULE_NAME}")
2632

33+
# Get tablegen executable and target based on TBLGEN parameter.
2734
if(${_RULE_TBLGEN} MATCHES "IREE")
28-
set(_TBLGEN "IREE")
35+
set(_TBLGEN_EXE ${IREE_TABLEGEN_EXE})
36+
set(_TBLGEN_TARGET ${IREE_TABLEGEN_TARGET})
2937
else()
30-
set(_TBLGEN "MLIR")
38+
set(_TBLGEN_EXE ${MLIR_TABLEGEN_EXE})
39+
set(_TBLGEN_TARGET ${MLIR_TABLEGEN_TARGET})
3140
endif()
3241

33-
set(LLVM_TARGET_DEFINITIONS ${_RULE_TD_FILE})
3442
set(_INCLUDE_DIRS
3543
"${MLIR_INCLUDE_DIRS}"
3644
"${IREE_SOURCE_DIR}/compiler/src"
@@ -41,6 +49,14 @@ function(iree_tablegen_library)
4149
endif()
4250
list(APPEND _INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
4351
list(TRANSFORM _INCLUDE_DIRS PREPEND "-I")
52+
53+
# Build absolute path for the main .td file.
54+
if(IS_ABSOLUTE ${_RULE_TD_FILE})
55+
set(_TD_FILE_ABS ${_RULE_TD_FILE})
56+
else()
57+
set(_TD_FILE_ABS ${CMAKE_CURRENT_SOURCE_DIR}/${_RULE_TD_FILE})
58+
endif()
59+
4460
set(_OUTPUTS)
4561
while(_RULE_OUTS)
4662
# Eat any number of flags (--a --b ...) and a single file.
@@ -58,8 +74,31 @@ function(iree_tablegen_library)
5874
set(_FILE ${_PART})
5975
endif()
6076
endwhile()
61-
tablegen(${_TBLGEN} ${_FILE} ${_COMMAND} ${_INCLUDE_DIRS})
62-
list(APPEND _OUTPUTS ${CMAKE_CURRENT_BINARY_DIR}/${_FILE})
77+
78+
set(_OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/${_FILE})
79+
80+
# Create add_custom_command with depfile support for Ninja.
81+
if(CMAKE_GENERATOR MATCHES "Ninja")
82+
add_custom_command(
83+
OUTPUT ${_OUTPUT_FILE}
84+
COMMAND ${_TBLGEN_EXE} ${_COMMAND} ${_INCLUDE_DIRS} ${_TD_FILE_ABS}
85+
--write-if-changed -o ${_OUTPUT_FILE} -d ${_OUTPUT_FILE}.d
86+
DEPENDS ${_TBLGEN_TARGET} ${_TBLGEN_EXE} ${_TD_FILE_ABS}
87+
DEPFILE ${_OUTPUT_FILE}.d
88+
COMMENT "Building ${_FILE}..."
89+
)
90+
else()
91+
add_custom_command(
92+
OUTPUT ${_OUTPUT_FILE}
93+
COMMAND ${_TBLGEN_EXE} ${_COMMAND} ${_INCLUDE_DIRS} ${_TD_FILE_ABS}
94+
--write-if-changed -o ${_OUTPUT_FILE}
95+
DEPENDS ${_TBLGEN_TARGET} ${_TBLGEN_EXE} ${_TD_FILE_ABS}
96+
COMMENT "Building ${_FILE}..."
97+
)
98+
endif()
99+
100+
list(APPEND _OUTPUTS ${_OUTPUT_FILE})
101+
set_source_files_properties(${_OUTPUT_FILE} PROPERTIES GENERATED 1)
63102
endwhile()
64103
add_custom_target(${_NAME}_target DEPENDS ${_OUTPUTS})
65104
set_target_properties(${_NAME}_target PROPERTIES FOLDER "Tablegenning")

0 commit comments

Comments
 (0)