Skip to content

Commit a16c616

Browse files
authored
handle old cmake with newer llvm (rust-lang#719)
* handle old cmake * fix off-by-one
1 parent 42419dc commit a16c616

File tree

1 file changed

+132
-1
lines changed

1 file changed

+132
-1
lines changed

enzyme/Enzyme/CMakeLists.txt

+132-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,142 @@
55
set(Enzyme_TABLEGEN_EXE enzyme-tblgen)
66

77
function(enzyme_tablegen ofn)
8-
tablegen(Enzyme ${ARGV})
8+
if (${LLVM_VERSION_MAJOR} GREATER 11)
9+
if(${CMAKE_VERSION} VERSION_LESS "3.13.0")
10+
message("Using fallback code. Please switch to a newer cmake")
11+
tablegenSubstitute(Enzyme ${ARGV})
12+
else()
13+
tablegen(Enzyme ${ARGV})
14+
endif()
15+
else()
16+
tablegen(Enzyme ${ARGV})
17+
endif()
18+
19+
920
set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
1021
PARENT_SCOPE)
1122
endfunction()
1223

24+
function(tablegenSubstitute project ofn)
25+
# Validate calling context.
26+
if(NOT ${project}_TABLEGEN_EXE)
27+
message(FATAL_ERROR "${project}_TABLEGEN_EXE not set")
28+
endif()
29+
30+
# Use depfile instead of globbing arbitrary *.td(s) for Ninja.
31+
if(CMAKE_GENERATOR STREQUAL "Ninja")
32+
# Make output path relative to build.ninja, assuming located on
33+
# ${CMAKE_BINARY_DIR}.
34+
# CMake emits build targets as relative paths but Ninja doesn't identify
35+
# absolute path (in *.d) as relative path (in build.ninja)
36+
# Note that tblgen is executed on ${CMAKE_BINARY_DIR} as working directory.
37+
file(RELATIVE_PATH ofn_rel
38+
${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${ofn})
39+
set(additional_cmdline
40+
-o ${ofn_rel}
41+
-d ${ofn_rel}.d
42+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
43+
DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${ofn}.d
44+
)
45+
set(local_tds)
46+
set(global_tds)
47+
else()
48+
file(GLOB local_tds "*.td")
49+
file(GLOB_RECURSE global_tds "${LLVM_MAIN_INCLUDE_DIR}/llvm/*.td")
50+
set(additional_cmdline
51+
-o ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
52+
)
53+
endif()
54+
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()
61+
if (LLVM_ENABLE_DAGISEL_COV)
62+
list(FIND ARGN "-gen-dag-isel" idx)
63+
if( NOT idx EQUAL -1 )
64+
list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-coverage")
65+
endif()
66+
endif()
67+
if (LLVM_ENABLE_GISEL_COV)
68+
list(FIND ARGN "-gen-global-isel" idx)
69+
if( NOT idx EQUAL -1 )
70+
list(APPEND LLVM_TABLEGEN_FLAGS "-instrument-gisel-coverage")
71+
list(APPEND LLVM_TABLEGEN_FLAGS "-gisel-coverage-file=${LLVM_GISEL_COV_PREFIX}all")
72+
endif()
73+
endif()
74+
# Comments are only useful for Debug builds. Omit them if the backend
75+
# supports it.
76+
if (NOT (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" OR
77+
uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO"))
78+
list(FIND ARGN "-gen-dag-isel" idx)
79+
if (NOT idx EQUAL -1)
80+
list(APPEND LLVM_TABLEGEN_FLAGS "-omit-comments")
81+
endif()
82+
endif()
83+
84+
# MSVC can't support long string literals ("long" > 65534 bytes)[1], so if there's
85+
# a possibility of generated tables being consumed by MSVC, generate arrays of
86+
# char literals, instead. If we're cross-compiling, then conservatively assume
87+
# that the source might be consumed by MSVC.
88+
# [1] https://docs.microsoft.com/en-us/cpp/cpp/compiler-limits?view=vs-2017
89+
if (MSVC AND project STREQUAL LLVM)
90+
list(APPEND LLVM_TABLEGEN_FLAGS "--long-string-literals=0")
91+
endif()
92+
if (CMAKE_GENERATOR MATCHES "Visual Studio")
93+
# Visual Studio has problems with llvm-tblgen's native --write-if-changed
94+
# behavior. Since it doesn't do restat optimizations anyway, just don't
95+
# pass --write-if-changed there.
96+
set(tblgen_change_flag)
97+
else()
98+
set(tblgen_change_flag "--write-if-changed")
99+
endif()
100+
101+
# We need both _TABLEGEN_TARGET and _TABLEGEN_EXE in the DEPENDS list
102+
# (both the target and the file) to have .inc files rebuilt on
103+
# a tablegen change, as cmake does not propagate file-level dependencies
104+
# of custom targets. See the following ticket for more information:
105+
# https://cmake.org/Bug/view.php?id=15858
106+
# The dependency on both, the target and the file, produces the same
107+
# dependency twice in the result file when
108+
# ("${${project}_TABLEGEN_TARGET}" STREQUAL "${${project}_TABLEGEN_EXE}")
109+
# but lets us having smaller and cleaner code here.
110+
get_directory_property(tblgen_includes INCLUDE_DIRECTORIES)
111+
112+
# we are here since the used cmake doesn't support the TRANSFORM command
113+
# so we manually implement it
114+
#list(TRANSFORM tblgen_includes PREPEND -I)
115+
foreach(incl ${tblgen_includes})
116+
list(APPEND tblgen_includes_prefixed "-I ${incl}")
117+
endforeach()
118+
119+
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
120+
COMMAND ${${project}_TABLEGEN_EXE} ${ARGN} -I ${CMAKE_CURRENT_SOURCE_DIR}
121+
${tblgen_includes_prefixed}
122+
${LLVM_TABLEGEN_FLAGS}
123+
${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
124+
${tblgen_change_flag}
125+
${additional_cmdline}
126+
# The file in LLVM_TARGET_DEFINITIONS may be not in the current
127+
# directory and local_tds may not contain it, so we must
128+
# explicitly list it here:
129+
DEPENDS ${${project}_TABLEGEN_TARGET} ${${project}_TABLEGEN_EXE}
130+
${local_tds} ${global_tds}
131+
${LLVM_TARGET_DEFINITIONS_ABSOLUTE}
132+
COMMENT "Building ${ofn}..."
133+
)
134+
135+
# `make clean' must remove all those generated files:
136+
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${ofn})
137+
138+
set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn} PARENT_SCOPE)
139+
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${ofn} PROPERTIES
140+
GENERATED 1)
141+
endfunction()
142+
143+
13144
set(LLVM_TARGET_DEFINITIONS InstructionDerivatives.td)
14145
enzyme_tablegen(InstructionDerivatives.inc -gen-derivatives)
15146
add_public_tablegen_target(InstructionDerivativesIncGen)

0 commit comments

Comments
 (0)