<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/121499>121499</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[cmake] Improve cross-compilation support in `TableGen.cmake` for third-party projects
</td>
</tr>
<tr>
<th>Labels</th>
<td>
cmake
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mgorny
</td>
</tr>
</table>
<pre>
For example, [triton uses `mlir_tablegen` in a few places](https://github.com/search?q=repo%3Atriton-lang%2Ftriton%20mlir_tablegen&type=code). This functions is defined to call LLVM's `tablegen()` function:
https://github.com/llvm/llvm-project/blob/4075ddad7183e6f0b66e2c8cc7a03b461a8038e6/mlir/cmake/modules/AddMLIR.cmake#L4-L5
This function, in turn, ends up using `MLIR_TABLEGEN_EXE`:
https://github.com/llvm/llvm-project/blob/4075ddad7183e6f0b66e2c8cc7a03b461a8038e6/llvm/cmake/modules/TableGen.cmake#L125
`MLIR_TABLEGEN_EXE` is provided by `MLIRConfig.cmake` as a hardcoded value:
https://github.com/llvm/llvm-project/blob/4075ddad7183e6f0b66e2c8cc7a03b461a8038e6/mlir/cmake/modules/MLIRConfig.cmake.in#L12
When cross-compiling Triton, `find_package(MLIR)` needs to use the package from sysroot which is correct. However, this implicitly causes `MLIR_TABLEGEN_EXE` to also refer to sysroot, i.e. the build system ends up trying to call the tablegen executable for the `CHOST` system rather than one from `CBUILD`. So we need a way to override this path.
FWICS `add_tablegen` macro has support for cross-compiling — but that's only used when a particular tablegen is built as part of the project. When it is used externally, one needs to overwrite `*_TABLEGEN_EXE` variable after doing `find_package()`, e.g. Clang does this in standalone builds:
https://github.com/llvm/llvm-project/blob/4075ddad7183e6f0b66e2c8cc7a03b461a8038e6/clang/CMakeLists.txt#L50-L51
I think it would be beneficial to provide a standard way of overriding different TableGen tools directly in `TableGen.cmake`, so that different projects wouldn't have to provide custom methods of doing this. My initial thought would be to make `tablegen()` create a `CACHE` variable `${project}_TABLEGEN_EXE` but I'm not sure whether it would play right with all the existing uses (I'm thinking existing `set(...)` calls would simply set the value of the cache variable but I may be wrong).
CC @thesamesam @MaskRay
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMVkFv2zgT_TX0ZRCBpizZPviQOHEbwPk-oM1u9xbQ5MjihhK1JGVH_34xlJw2abvAXooFEtg0pZn33rwZUoZgji3ihhU3rLidyT7Wzm-ao_PtMDs4PWx2zgO-yKazyMQWWHETvYmuhT5gAFbyxhr_FOXB4hFbVnIwLUio8AydlQoDK26ZWNUxdoHl10zsmNgdTaz7Q6Zcw8QuoPSqZvnuL5bfeuwcE0V-PWa5srI9MlGI3bimr-8yijIOHbL8VjmNTKwzeKxNgKpvVTSuDWACaKxMixqiAyWthf3-9wcmlgn_10ArJtZE4PIqweX09w_grT1dPq467_5EFZnYHaw7MLFb8GWhtdTL-SrHsuKHskShVkotJc8Pi3IuVzxfYcnEjjgxsVONfEZaOt1bDEzsrrV-2N9_yqadfL-42hcjrDc8qTimhdj79BVbHaDvoA-mPRJNCvL0eH2zv_tw97-nuz_uWMl_JcEpzvcEH0n_D9h-ZTgXE8GfwKaKdt6djEYNh-HCbuvayhynMCUHGUBCLb0mY2g4Sdvjf6Kk78Fmph1pj9C-1NiC8i6EK-Wazliq4ONk_y2xrUyrnzqpnuURmVhRvMm6LaIOZPM-IMQaYXoKKu8aCEPwzkU410bVpKJy3qOKGXx0Zzyhp_iRXGWazhploh1AyUun_7AW0YG0wYHHCj2tpiTJjxlmCcWhN1bTTsTm1ZrRD8Ts0pP03KUXAV9Q9WkFlfNpj5V8-_H_nx8p6RTJy1hT0lq24NqJJD1389v9_paVPIPPDs6YZAEJZzlQOndC743GkWonY52Nyu--3G8_UwCp9ZuR1kjlHdQyQOi7zvmYUL2vEbsTbMXZegGHPhKqmGaMa-1A9dBwpspK6KSPRvVW-q-ETUgiRXIt7YOrxvqNBswgucJEejDFwpeIvpXWDqQ0sX-tPfE7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